Skip to content

Commit a82b525

Browse files
committed
Renamed fields and improved didChange handling
1 parent 83860ca commit a82b525

File tree

2 files changed

+73
-73
lines changed

2 files changed

+73
-73
lines changed

Diff for: ls/builder.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (handler *INOLanguageServer) generateBuildEnvironment(logger jsonrpc.Functi
9494
Overrides map[string]string `json:"overrides"`
9595
}
9696
data := overridesFile{Overrides: map[string]string{}}
97-
for uri, trackedFile := range handler.docs {
97+
for uri, trackedFile := range handler.trackedInoDocs {
9898
rel, err := paths.New(uri).RelFrom(handler.sketchRoot)
9999
if err != nil {
100100
return errors.WithMessage(err, "dumping tracked files")
@@ -107,6 +107,7 @@ func (handler *INOLanguageServer) generateBuildEnvironment(logger jsonrpc.Functi
107107
} else if tmpFile, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
108108
return errors.WithMessage(err, "dumping tracked files")
109109
} else {
110+
// logger.Logf("Dumped overrides: %s", string(jsonBytes))
110111
overridesJSON = tmpFile
111112
defer tmpFile.Remove()
112113
}

Diff for: ls/ls.go

+71-72
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ type INOLanguageServer struct {
5656
buildPath *paths.Path
5757
buildSketchRoot *paths.Path
5858
buildSketchCpp *paths.Path
59-
buildSketchCppVersion int
6059
buildSketchSymbols []lsp.DocumentSymbol
6160
buildSketchIncludesCanary string
6261
buildSketchSymbolsCanary string
@@ -68,7 +67,7 @@ type INOLanguageServer struct {
6867
sketchName string
6968
sketchMapper *sourcemapper.InoMapper
7069
sketchTrackedFilesCount int
71-
docs map[string]lsp.TextDocumentItem
70+
trackedInoDocs map[string]lsp.TextDocumentItem
7271
inoDocsWithDiagnostics map[lsp.DocumentURI]bool
7372

7473
config BoardConfig
@@ -147,7 +146,7 @@ func (ls *INOLanguageServer) waitClangdStart(logger jsonrpc.FunctionLogger) erro
147146
func NewINOLanguageServer(stdin io.Reader, stdout io.Writer, board Board) *INOLanguageServer {
148147
logger := NewLSPFunctionLogger(color.HiWhiteString, "LS: ")
149148
ls := &INOLanguageServer{
150-
docs: map[string]lsp.TextDocumentItem{},
149+
trackedInoDocs: map[string]lsp.TextDocumentItem{},
151150
inoDocsWithDiagnostics: map[lsp.DocumentURI]bool{},
152151
closing: make(chan bool),
153152
buildSketchSymbolsLoad: make(chan bool, 1),
@@ -833,7 +832,7 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
833832

834833
// Add the TextDocumentItem in the tracked files list
835834
inoTextDocItem := inoParam.TextDocument
836-
ls.docs[inoTextDocItem.URI.AsPath().String()] = inoTextDocItem
835+
ls.trackedInoDocs[inoTextDocItem.URI.AsPath().String()] = inoTextDocItem
837836

838837
// If we are tracking a .ino...
839838
if inoTextDocItem.URI.Ext() == ".ino" {
@@ -1044,7 +1043,6 @@ func (ls *INOLanguageServer) initializeWorkbench(logger jsonrpc.FunctionLogger,
10441043
return err
10451044
}
10461045
ls.buildSketchCpp = ls.buildSketchRoot.Join(ls.sketchName + ".ino.cpp")
1047-
ls.buildSketchCppVersion = 1
10481046
ls.lspInitializeParams.RootPath = ls.buildSketchRoot.String()
10491047
ls.lspInitializeParams.RootURI = lsp.NewDocumentURIFromPath(ls.buildSketchRoot)
10501048

@@ -1255,8 +1253,8 @@ func canonicalizeCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]
12551253

12561254
func (ls *INOLanguageServer) didClose(logger jsonrpc.FunctionLogger, inoDidClose *lsp.DidCloseTextDocumentParams) (*lsp.DidCloseTextDocumentParams, error) {
12571255
inoIdentifier := inoDidClose.TextDocument
1258-
if _, exist := ls.docs[inoIdentifier.URI.AsPath().String()]; exist {
1259-
delete(ls.docs, inoIdentifier.URI.AsPath().String())
1256+
if _, exist := ls.trackedInoDocs[inoIdentifier.URI.AsPath().String()]; exist {
1257+
delete(ls.trackedInoDocs, inoIdentifier.URI.AsPath().String())
12601258
} else {
12611259
logger.Logf(" didClose of untracked document: %s", inoIdentifier.URI)
12621260
return nil, unknownURI(inoIdentifier.URI)
@@ -1293,89 +1291,90 @@ func (ls *INOLanguageServer) ino2cppTextDocumentItem(logger jsonrpc.FunctionLogg
12931291
} else {
12941292
cppItem.LanguageID = inoItem.LanguageID
12951293
inoPath := inoItem.URI.AsPath().String()
1296-
cppItem.Text = ls.docs[inoPath].Text
1297-
cppItem.Version = ls.docs[inoPath].Version
1294+
cppItem.Text = ls.trackedInoDocs[inoPath].Text
1295+
cppItem.Version = ls.trackedInoDocs[inoPath].Version
12981296
}
12991297

13001298
return cppItem, nil
13011299
}
13021300

1303-
func (ls *INOLanguageServer) didChange(logger jsonrpc.FunctionLogger, req *lsp.DidChangeTextDocumentParams) (*lsp.DidChangeTextDocumentParams, error) {
1304-
doc := req.TextDocument
1301+
func (ls *INOLanguageServer) didChange(logger jsonrpc.FunctionLogger, inoDidChangeParams *lsp.DidChangeTextDocumentParams) (*lsp.DidChangeTextDocumentParams, error) {
1302+
inoDoc := inoDidChangeParams.TextDocument
13051303

1306-
trackedDoc, ok := ls.docs[doc.URI.AsPath().String()]
1304+
// Apply the change to the tracked sketch file.
1305+
trackedInoID := inoDoc.URI.AsPath().String()
1306+
trackedInoDoc, ok := ls.trackedInoDocs[trackedInoID]
13071307
if !ok {
1308-
return nil, unknownURI(doc.URI)
1308+
return nil, unknownURI(inoDoc.URI)
1309+
}
1310+
if updatedTrackedInoDoc, err := textutils.ApplyLSPTextDocumentContentChangeEvent(trackedInoDoc, inoDidChangeParams.ContentChanges, inoDoc.Version); err != nil {
1311+
return nil, err
1312+
} else {
1313+
ls.trackedInoDocs[trackedInoID] = updatedTrackedInoDoc
1314+
}
1315+
1316+
logger.Logf("Tracked SKETCH file:----------+\n" + ls.trackedInoDocs[trackedInoID].Text + "\n----------------------")
1317+
1318+
// If the file is not part of a .ino flie forward the change as-is to clangd
1319+
if inoDoc.URI.Ext() != ".ino" {
1320+
if cppDoc, err := ls.ino2cppVersionedTextDocumentIdentifier(logger, inoDidChangeParams.TextDocument); err != nil {
1321+
return nil, err
1322+
} else {
1323+
cppDidChangeParams := *inoDidChangeParams
1324+
cppDidChangeParams.TextDocument = cppDoc
1325+
return &cppDidChangeParams, nil
1326+
}
13091327
}
1310-
textutils.ApplyLSPTextDocumentContentChangeEvent(&trackedDoc, req.ContentChanges, doc.Version)
13111328

13121329
// If changes are applied to a .ino file we increment the global .ino.cpp versioning
13131330
// for each increment of the single .ino file.
1314-
if doc.URI.Ext() == ".ino" {
13151331

1316-
cppChanges := []lsp.TextDocumentContentChangeEvent{}
1317-
for _, inoChange := range req.ContentChanges {
1318-
cppRange, ok := ls.sketchMapper.InoToCppLSPRangeOk(doc.URI, inoChange.Range)
1319-
if !ok {
1320-
return nil, errors.Errorf("invalid change range %s:%s", doc.URI, inoChange.Range)
1321-
}
1332+
cppChanges := []lsp.TextDocumentContentChangeEvent{}
1333+
for _, inoChange := range inoDidChangeParams.ContentChanges {
1334+
cppChangeRange, ok := ls.sketchMapper.InoToCppLSPRangeOk(inoDoc.URI, inoChange.Range)
1335+
if !ok {
1336+
return nil, errors.Errorf("invalid change range %s:%s", inoDoc.URI, inoChange.Range)
1337+
}
13221338

1323-
// Detect changes in critical lines (for example function definitions)
1324-
// and trigger arduino-preprocessing + clangd restart.
1325-
dirty := false
1326-
for _, sym := range ls.buildSketchSymbols {
1327-
if sym.SelectionRange.Overlaps(cppRange) {
1328-
dirty = true
1329-
logger.Logf("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!")
1330-
break
1331-
}
1332-
}
1333-
if ls.sketchMapper.ApplyTextChange(doc.URI, inoChange) {
1339+
// Detect changes in critical lines (for example function definitions)
1340+
// and trigger arduino-preprocessing + clangd restart.
1341+
dirty := false
1342+
for _, sym := range ls.buildSketchSymbols {
1343+
if sym.SelectionRange.Overlaps(cppChangeRange) {
13341344
dirty = true
1335-
logger.Logf("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!")
1336-
}
1337-
if dirty {
1338-
ls.scheduleRebuildEnvironment()
1339-
}
1340-
1341-
// logger.Logf("New version:----------")
1342-
// logger.Logf(ls.sketchMapper.CppText.Text)
1343-
// logger.Logf("----------------------")
1344-
1345-
cppChange := lsp.TextDocumentContentChangeEvent{
1346-
Range: cppRange,
1347-
RangeLength: inoChange.RangeLength,
1348-
Text: inoChange.Text,
1345+
logger.Logf("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!")
1346+
break
13491347
}
1350-
cppChanges = append(cppChanges, cppChange)
13511348
}
1352-
1353-
ls.CheckCppIncludesChanges()
1354-
1355-
// build a cpp equivalent didChange request
1356-
cppReq := &lsp.DidChangeTextDocumentParams{
1357-
ContentChanges: cppChanges,
1358-
TextDocument: lsp.VersionedTextDocumentIdentifier{
1359-
TextDocumentIdentifier: lsp.TextDocumentIdentifier{
1360-
URI: lsp.NewDocumentURIFromPath(ls.buildSketchCpp),
1361-
},
1362-
Version: ls.sketchMapper.CppText.Version,
1363-
},
1349+
if ls.sketchMapper.ApplyTextChange(inoDoc.URI, inoChange) {
1350+
dirty = true
1351+
logger.Logf("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!")
1352+
}
1353+
if dirty {
1354+
ls.scheduleRebuildEnvironment()
13641355
}
13651356

1366-
return cppReq, nil
1367-
}
1357+
logger.Logf("New version:----------+\n" + ls.sketchMapper.CppText.Text + "\n----------------------")
13681358

1369-
// If changes are applied to other files pass them by converting just the URI
1370-
cppDoc, err := ls.ino2cppVersionedTextDocumentIdentifier(logger, req.TextDocument)
1371-
if err != nil {
1372-
return nil, err
1373-
}
1374-
cppReq := &lsp.DidChangeTextDocumentParams{
1375-
TextDocument: cppDoc,
1376-
ContentChanges: req.ContentChanges,
1359+
cppChanges = append(cppChanges, lsp.TextDocumentContentChangeEvent{
1360+
Range: cppChangeRange,
1361+
RangeLength: inoChange.RangeLength,
1362+
Text: inoChange.Text,
1363+
})
13771364
}
1378-
return cppReq, err
1365+
1366+
ls.CheckCppIncludesChanges()
1367+
1368+
// build a cpp equivalent didChange request
1369+
return &lsp.DidChangeTextDocumentParams{
1370+
ContentChanges: cppChanges,
1371+
TextDocument: lsp.VersionedTextDocumentIdentifier{
1372+
TextDocumentIdentifier: lsp.TextDocumentIdentifier{
1373+
URI: lsp.NewDocumentURIFromPath(ls.buildSketchCpp),
1374+
},
1375+
Version: ls.sketchMapper.CppText.Version,
1376+
},
1377+
}, nil
13791378
}
13801379

13811380
func (ls *INOLanguageServer) ino2cppVersionedTextDocumentIdentifier(logger jsonrpc.FunctionLogger, doc lsp.VersionedTextDocumentIdentifier) (lsp.VersionedTextDocumentIdentifier, error) {
@@ -1429,11 +1428,11 @@ func (ls *INOLanguageServer) inoDocumentURIFromInoPath(logger jsonrpc.FunctionLo
14291428
if inoPath == sourcemapper.NotIno.File {
14301429
return sourcemapper.NotInoURI, nil
14311430
}
1432-
doc, ok := ls.docs[inoPath]
1431+
doc, ok := ls.trackedInoDocs[inoPath]
14331432
if !ok {
14341433
logger.Logf(" !!! Unresolved .ino path: %s", inoPath)
14351434
logger.Logf(" !!! Known doc paths are:")
1436-
for p := range ls.docs {
1435+
for p := range ls.trackedInoDocs {
14371436
logger.Logf(" !!! > %s", p)
14381437
}
14391438
uri := lsp.NewDocumentURI(inoPath)

0 commit comments

Comments
 (0)