Skip to content

Commit 71dd136

Browse files
committed
Partial refactoring of CodeAction conversions subroutines
1 parent 6847aa5 commit 71dd136

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

Diff for: ls/ls.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,6 @@ func (ls *INOLanguageServer) TextDocumentHoverReqFromIDE(ctx context.Context, lo
512512
return &ideResp, nil
513513
}
514514

515-
func (ls *INOLanguageServer) clangURIRefersToIno(clangURI lsp.DocumentURI) bool {
516-
return clangURI.AsPath().EquivalentTo(ls.buildSketchCpp)
517-
}
518-
519515
func (ls *INOLanguageServer) TextDocumentSignatureHelpReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, ideParams *lsp.SignatureHelpParams) (*lsp.SignatureHelp, *jsonrpc.ResponseError) {
520516
ls.readLock(logger, true)
521517
defer ls.readUnlock(logger)
@@ -782,24 +778,23 @@ func (ls *INOLanguageServer) TextDocumentCodeActionReqFromIDE(ctx context.Contex
782778
ideURI := ideTextDocument.URI
783779
logger.Logf("--> codeAction(%s:%s)", ideTextDocument, ideParams.Range.Start)
784780

785-
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
781+
clangURI, clangRange, err := ls.ide2ClangRange(logger, ideURI, ideParams.Range)
786782
if err != nil {
787783
logger.Logf("Error: %s", err)
788784
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
789785
}
790786

787+
clangContext, err := ls.ide2ClangCodeActionContext(logger, ideURI, ideParams.Context)
788+
if err != nil {
789+
logger.Logf("Error: %s", err)
790+
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
791+
}
791792
clangParams := &lsp.CodeActionParams{
792-
TextDocument: cppTextDocument,
793793
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
794794
PartialResultParams: ideParams.PartialResultParams,
795-
Range: ideParams.Range,
796-
Context: ideParams.Context,
797-
}
798-
if cppTextDocument.URI.AsPath().EquivalentTo(ls.buildSketchCpp) {
799-
clangParams.Range = ls.sketchMapper.InoToCppLSPRange(ideURI, ideParams.Range)
800-
for i, inoDiag := range ideParams.Context.Diagnostics {
801-
clangParams.Context.Diagnostics[i].Range = ls.sketchMapper.InoToCppLSPRange(ideURI, inoDiag.Range)
802-
}
795+
TextDocument: lsp.TextDocumentIdentifier{URI: clangURI},
796+
Range: clangRange,
797+
Context: clangContext,
803798
}
804799
logger.Logf(" --> codeAction(%s:%s)", clangParams.TextDocument, ideParams.Range.Start)
805800

Diff for: ls/ls_clang_to_ide.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"go.bug.st/lsp/jsonrpc"
99
)
1010

11+
func (ls *INOLanguageServer) clangURIRefersToIno(clangURI lsp.DocumentURI) bool {
12+
return clangURI.AsPath().EquivalentTo(ls.buildSketchCpp)
13+
}
14+
1115
func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangRange lsp.Range) (lsp.DocumentURI, lsp.Range, bool, error) {
1216
// Sketchbook/Sketch/Sketch.ino <-> build-path/sketch/Sketch.ino.cpp
1317
// Sketchbook/Sketch/AnotherTab.ino <-> build-path/sketch/Sketch.ino.cpp (different section from above)

Diff for: ls/ls_ide_to_clang.go

+60
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,63 @@ func (ls *INOLanguageServer) ide2ClangVersionedTextDocumentIdentifier(logger jso
117117
Version: ideVersionedDoc.Version,
118118
}, err
119119
}
120+
121+
func (ls *INOLanguageServer) ide2ClangDiagnosticRelatedInformationArray(logger jsonrpc.FunctionLogger, ideInfos []lsp.DiagnosticRelatedInformation) ([]lsp.DiagnosticRelatedInformation, error) {
122+
clangInfos := []lsp.DiagnosticRelatedInformation{}
123+
for _, ideInfo := range ideInfos {
124+
clangLocation, err := ls.ide2ClangLocation(logger, ideInfo.Location)
125+
if err != nil {
126+
return nil, err
127+
}
128+
clangInfos = append(clangInfos, lsp.DiagnosticRelatedInformation{
129+
Message: ideInfo.Message,
130+
Location: clangLocation,
131+
})
132+
}
133+
return clangInfos, nil
134+
}
135+
136+
func (ls *INOLanguageServer) ide2ClangLocation(logger jsonrpc.FunctionLogger, ideLocation lsp.Location) (lsp.Location, error) {
137+
clangURI, clangRange, err := ls.ide2ClangRange(logger, ideLocation.URI, ideLocation.Range)
138+
return lsp.Location{
139+
URI: clangURI,
140+
Range: clangRange,
141+
}, err
142+
}
143+
144+
func (ls *INOLanguageServer) ide2ClangDiagnostic(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideDiag lsp.Diagnostic) (lsp.DocumentURI, lsp.Diagnostic, error) {
145+
clangURI, clangRange, err := ls.ide2ClangRange(logger, ideURI, ideDiag.Range)
146+
if err != nil {
147+
return lsp.DocumentURI{}, lsp.Diagnostic{}, err
148+
}
149+
clangDiagRelatedInfo, err := ls.ide2ClangDiagnosticRelatedInformationArray(logger, ideDiag.RelatedInformation)
150+
if err != nil {
151+
return lsp.DocumentURI{}, lsp.Diagnostic{}, err
152+
}
153+
return clangURI, lsp.Diagnostic{
154+
Range: clangRange,
155+
RelatedInformation: clangDiagRelatedInfo,
156+
Severity: ideDiag.Severity,
157+
Code: ideDiag.Code,
158+
CodeDescription: ideDiag.CodeDescription,
159+
Source: ideDiag.Source,
160+
Message: ideDiag.Message,
161+
Tags: ideDiag.Tags,
162+
Data: ideDiag.Data,
163+
}, nil
164+
}
165+
166+
func (ls *INOLanguageServer) ide2ClangCodeActionContext(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideContext lsp.CodeActionContext) (lsp.CodeActionContext, error) {
167+
clangDiagnostics := []lsp.Diagnostic{}
168+
for _, ideDiag := range ideContext.Diagnostics {
169+
_, clangDiag, err := ls.ide2ClangDiagnostic(logger, ideURI, ideDiag)
170+
if err != nil {
171+
return lsp.CodeActionContext{}, err
172+
}
173+
clangDiagnostics = append(clangDiagnostics, clangDiag)
174+
}
175+
return lsp.CodeActionContext{
176+
Diagnostics: clangDiagnostics,
177+
Only: ideContext.Only,
178+
}, nil
179+
}

0 commit comments

Comments
 (0)