Skip to content

Commit aea355d

Browse files
committed
Dramatically simplified ide2ClangTextDocumentPositionParams
Also added new very useful helper method ide2ClangPosition
1 parent fd61ed4 commit aea355d

File tree

3 files changed

+55
-62
lines changed

3 files changed

+55
-62
lines changed

Diff for: ls/ls.go

+21-45
Original file line numberDiff line numberDiff line change
@@ -432,48 +432,49 @@ func (ls *INOLanguageServer) TextDocumentSignatureHelpReqFromIDE(ctx context.Con
432432
return cppSignatureHelp, nil
433433
}
434434

435-
func (ls *INOLanguageServer) TextDocumentDefinitionReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, p *lsp.DefinitionParams) ([]lsp.Location, []lsp.LocationLink, *jsonrpc.ResponseError) {
435+
func (ls *INOLanguageServer) TextDocumentDefinitionReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, ideParams *lsp.DefinitionParams) ([]lsp.Location, []lsp.LocationLink, *jsonrpc.ResponseError) {
436436
ls.readLock(logger, true)
437437
defer ls.readUnlock(logger)
438438

439-
inoTextDocPosition := p.TextDocumentPositionParams
440-
441-
logger.Logf("%s", inoTextDocPosition)
442-
cppTextDocPosition, err := ls.ide2ClangTextDocumentPositionParams(logger, inoTextDocPosition)
439+
ideTextDocPosition := ideParams.TextDocumentPositionParams
440+
clangTextDocPosition, err := ls.ide2ClangTextDocumentPositionParams(logger, ideTextDocPosition)
443441
if err != nil {
444442
logger.Logf("Error: %s", err)
445443
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
446444
}
447445

448-
logger.Logf("-> %s", cppTextDocPosition)
449-
cppParams := *p
450-
cppParams.TextDocumentPositionParams = cppTextDocPosition
451-
cppLocations, cppLocationLinks, cppErr, err := ls.Clangd.conn.TextDocumentDefinition(ctx, &cppParams)
446+
clangParams := &lsp.DefinitionParams{
447+
TextDocumentPositionParams: clangTextDocPosition,
448+
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
449+
PartialResultParams: ideParams.PartialResultParams,
450+
}
451+
clangLocations, clangLocationLinks, clangErr, err := ls.Clangd.conn.TextDocumentDefinition(ctx, clangParams)
452452
if err != nil {
453453
logger.Logf("clangd communication error: %v", err)
454454
ls.Close()
455455
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
456456
}
457-
if cppErr != nil {
458-
logger.Logf("clangd response error: %v", cppErr.AsError())
459-
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: cppErr.AsError().Error()}
457+
if clangErr != nil {
458+
logger.Logf("clangd response error: %v", clangErr.AsError())
459+
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: clangErr.AsError().Error()}
460460
}
461461

462-
var inoLocations []lsp.Location
463-
if cppLocations != nil {
464-
inoLocations, err = ls.cpp2inoLocationArray(logger, cppLocations)
462+
var ideLocations []lsp.Location
463+
if clangLocations != nil {
464+
ideLocations, err = ls.clang2IdeLocationsArray(logger, clangLocations)
465465
if err != nil {
466+
logger.Logf("Error: %v", err)
466467
ls.Close()
467468
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
468469
}
469470
}
470471

471-
var inoLocationLinks []lsp.LocationLink
472-
if cppLocationLinks != nil {
472+
var ideLocationLinks []lsp.LocationLink
473+
if clangLocationLinks != nil {
473474
panic("unimplemented")
474475
}
475476

476-
return inoLocations, inoLocationLinks, nil
477+
return ideLocations, ideLocationLinks, nil
477478
}
478479

479480
func (ls *INOLanguageServer) TextDocumentTypeDefinitionReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, inoParams *lsp.TypeDefinitionParams) ([]lsp.Location, []lsp.LocationLink, *jsonrpc.ResponseError) {
@@ -508,7 +509,7 @@ func (ls *INOLanguageServer) TextDocumentTypeDefinitionReqFromIDE(ctx context.Co
508509

509510
var inoLocations []lsp.Location
510511
if cppLocations != nil {
511-
inoLocations, err = ls.cpp2inoLocationArray(logger, cppLocations)
512+
inoLocations, err = ls.clang2IdeLocationsArray(logger, cppLocations)
512513
if err != nil {
513514
ls.Close()
514515
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
@@ -552,7 +553,7 @@ func (ls *INOLanguageServer) TextDocumentImplementationReqFromIDE(ctx context.Co
552553

553554
var inoLocations []lsp.Location
554555
if cppLocations != nil {
555-
inoLocations, err = ls.cpp2inoLocationArray(logger, cppLocations)
556+
inoLocations, err = ls.clang2IdeLocationsArray(logger, cppLocations)
556557
if err != nil {
557558
ls.Close()
558559
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
@@ -1224,23 +1225,6 @@ func (ls *INOLanguageServer) ino2cppVersionedTextDocumentIdentifier(logger jsonr
12241225
return res, err
12251226
}
12261227

1227-
func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger, cppLocations []lsp.Location) ([]lsp.Location, error) {
1228-
inoLocations := []lsp.Location{}
1229-
for _, cppLocation := range cppLocations {
1230-
inoLocation, inPreprocessed, err := ls.cpp2inoLocation(logger, cppLocation)
1231-
if err != nil {
1232-
logger.Logf("ERROR converting location %s: %s", cppLocation, err)
1233-
return nil, err
1234-
}
1235-
if inPreprocessed {
1236-
logger.Logf("ignored in-preprocessed-section location")
1237-
continue
1238-
}
1239-
inoLocations = append(inoLocations, inoLocation)
1240-
}
1241-
return inoLocations, nil
1242-
}
1243-
12441228
func (ls *INOLanguageServer) cpp2inoCodeAction(logger jsonrpc.FunctionLogger, codeAction lsp.CodeAction, uri lsp.DocumentURI) lsp.CodeAction {
12451229
inoCodeAction := lsp.CodeAction{
12461230
Title: codeAction.Title,
@@ -1336,14 +1320,6 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
13361320
return inoWorkspaceEdit
13371321
}
13381322

1339-
func (ls *INOLanguageServer) cpp2inoLocation(logger jsonrpc.FunctionLogger, cppLocation lsp.Location) (lsp.Location, bool, error) {
1340-
inoURI, inoRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, cppLocation.URI, cppLocation.Range)
1341-
return lsp.Location{
1342-
URI: inoURI,
1343-
Range: inoRange,
1344-
}, inPreprocessed, err
1345-
}
1346-
13471323
func (ls *INOLanguageServer) cpp2inoTextEdit(logger jsonrpc.FunctionLogger, cppURI lsp.DocumentURI, cppEdit lsp.TextEdit) (lsp.DocumentURI, lsp.TextEdit, bool, error) {
13481324
inoURI, inoRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, cppEdit.Range)
13491325
inoEdit := cppEdit

Diff for: ls/ls_clang_to_ide.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (ls *INOLanguageServer) clang2IdeDiagnostic(logger jsonrpc.FunctionLogger,
116116
func (ls *INOLanguageServer) clang2IdeDiagnosticRelatedInformationArray(logger jsonrpc.FunctionLogger, clangInfos []lsp.DiagnosticRelatedInformation) ([]lsp.DiagnosticRelatedInformation, error) {
117117
ideInfos := []lsp.DiagnosticRelatedInformation{}
118118
for _, clangInfo := range clangInfos {
119-
ideLocation, inPreprocessed, err := ls.cpp2inoLocation(logger, clangInfo.Location)
119+
ideLocation, inPreprocessed, err := ls.clang2IdeLocation(logger, clangInfo.Location)
120120
if err != nil {
121121
return nil, err
122122
}
@@ -205,6 +205,31 @@ func (ls *INOLanguageServer) cland2IdeTextEdits(logger jsonrpc.FunctionLogger, c
205205
return allIdeTextEdits, nil
206206
}
207207

208+
func (ls *INOLanguageServer) clang2IdeLocationsArray(logger jsonrpc.FunctionLogger, clangLocations []lsp.Location) ([]lsp.Location, error) {
209+
ideLocations := []lsp.Location{}
210+
for _, clangLocation := range clangLocations {
211+
ideLocation, inPreprocessed, err := ls.clang2IdeLocation(logger, clangLocation)
212+
if err != nil {
213+
logger.Logf("ERROR converting location %s: %s", clangLocation, err)
214+
return nil, err
215+
}
216+
if inPreprocessed {
217+
logger.Logf("ignored in-preprocessed-section location")
218+
continue
219+
}
220+
ideLocations = append(ideLocations, ideLocation)
221+
}
222+
return ideLocations, nil
223+
}
224+
225+
func (ls *INOLanguageServer) clang2IdeLocation(logger jsonrpc.FunctionLogger, clangLocation lsp.Location) (lsp.Location, bool, error) {
226+
ideURI, ideRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangLocation.URI, clangLocation.Range)
227+
return lsp.Location{
228+
URI: ideURI,
229+
Range: ideRange,
230+
}, inPreprocessed, err
231+
}
232+
208233
func (ls *INOLanguageServer) clang2IdeSymbolTags(logger jsonrpc.FunctionLogger, clangSymbolTags []lsp.SymbolTag) []lsp.SymbolTag {
209234
if len(clangSymbolTags) == 0 || clangSymbolTags == nil {
210235
return clangSymbolTags

Diff for: ls/ls_ide_to_clang.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,24 @@ func (ls *INOLanguageServer) ide2ClangDocumentURI(logger jsonrpc.FunctionLogger,
6666
}
6767

6868
func (ls *INOLanguageServer) ide2ClangTextDocumentPositionParams(logger jsonrpc.FunctionLogger, ideParams lsp.TextDocumentPositionParams) (lsp.TextDocumentPositionParams, error) {
69-
ideTextDocument := ideParams.TextDocument
70-
idePosition := ideParams.Position
71-
ideURI := ideTextDocument.URI
72-
73-
clangTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
69+
clangURI, clangPosition, err := ls.ide2ClangPosition(logger, ideParams.TextDocument.URI, ideParams.Position)
7470
if err != nil {
75-
logger.Logf("%s -> invalid text document: %s", ideParams, err)
71+
logger.Logf("Error converting position %s: %s", ideParams, err)
7672
return lsp.TextDocumentPositionParams{}, err
7773
}
78-
clangPosition := idePosition
79-
if ls.clangURIRefersToIno(clangTextDocument.URI) {
80-
if cppLine, ok := ls.sketchMapper.InoToCppLineOk(ideURI, idePosition.Line); ok {
81-
clangPosition.Line = cppLine
82-
} else {
83-
logger.Logf("%s -> invalid line requested: %s:%d", ideParams, ideURI, idePosition.Line)
84-
return lsp.TextDocumentPositionParams{}, &UnknownURI{ideURI}
85-
}
86-
}
8774
clangParams := lsp.TextDocumentPositionParams{
88-
TextDocument: clangTextDocument,
75+
TextDocument: lsp.TextDocumentIdentifier{URI: clangURI},
8976
Position: clangPosition,
9077
}
9178
logger.Logf("%s -> %s", ideParams, clangParams)
9279
return clangParams, nil
9380
}
9481

82+
func (ls *INOLanguageServer) ide2ClangPosition(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, idePosition lsp.Position) (lsp.DocumentURI, lsp.Position, error) {
83+
clangURI, clangRange, err := ls.ide2ClangRange(logger, ideURI, lsp.Range{Start: idePosition, End: idePosition})
84+
return clangURI, clangRange.Start, err
85+
}
86+
9587
func (ls *INOLanguageServer) ide2ClangRange(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
9688
clangURI, err := ls.ide2ClangDocumentURI(logger, ideURI)
9789
if err != nil {

0 commit comments

Comments
 (0)