Skip to content

Commit fb4b242

Browse files
committed
improved logging and some refactorings
1 parent a82b525 commit fb4b242

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

Diff for: ls/ls.go

+58-43
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,12 @@ func (ls *INOLanguageServer) TextDocumentDidSaveNotifFromIDE(logger jsonrpc.Func
892892
ls.writeLock(logger, true)
893893
defer ls.writeUnlock(logger)
894894

895-
logger.Logf("didSave(%s)", inoParams.TextDocument)
895+
logger.Logf("didSave(%s) hasText=%v", inoParams.TextDocument, inoParams.Text != "")
896896
if cppTextDocument, err := ls.ino2cppTextDocumentIdentifier(logger, inoParams.TextDocument); err != nil {
897897
logger.Logf("--E Error: %s", err)
898898
} else if cppTextDocument.URI.AsPath().EquivalentTo(ls.buildSketchCpp) {
899899
logger.Logf(" didSave(%s) equals %s", cppTextDocument, ls.buildSketchCpp)
900-
logger.Logf("--| didSave not forwarded to clangd")
900+
logger.Logf(" the notification will be not forwarded to clangd")
901901
} else {
902902
logger.Logf("LS --> CL NOTIF didSave(%s)", cppTextDocument)
903903
if err := ls.Clangd.conn.TextDocumentDidSave(&lsp.DidSaveTextDocumentParams{
@@ -938,7 +938,7 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
938938
ls.readLock(logger, false)
939939
defer ls.readUnlock(logger)
940940

941-
logger.Logf("publishDiagnostics(%s):", cppParams.URI)
941+
logger.Logf("from clang %s (%d diagnostics):", cppParams.URI, cppParams.Diagnostics)
942942
for _, diag := range cppParams.Diagnostics {
943943
logger.Logf("> %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
944944
}
@@ -953,12 +953,10 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
953953

954954
// Push back to IDE the converted diagnostics
955955
for _, inoParams := range allInoParams {
956-
logger.Logf("to IDE: publishDiagnostics(%s):", inoParams.URI)
956+
logger.Logf("to IDE: %s (%d diagnostics):", inoParams.URI, len(inoParams.Diagnostics))
957957
for _, diag := range inoParams.Diagnostics {
958-
logger.Logf("> %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
958+
logger.Logf(" > %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
959959
}
960-
logger.Logf("IDE <-- LS NOTIF textDocument/publishDiagnostics:")
961-
962960
if err := ls.IDE.conn.TextDocumentPublishDiagnostics(inoParams); err != nil {
963961
logger.Logf(" Error sending diagnostics to IDE: %s", err)
964962
return
@@ -1265,7 +1263,7 @@ func (ls *INOLanguageServer) didClose(logger jsonrpc.FunctionLogger, inoDidClose
12651263
ls.sketchTrackedFilesCount--
12661264
logger.Logf(" decreasing .ino tracked files count: %d", ls.sketchTrackedFilesCount)
12671265

1268-
// notify clang that sketchCpp has been close only once all .ino are closed
1266+
// notify clang that sketch.cpp.ino has been closed only once all .ino are closed
12691267
if ls.sketchTrackedFilesCount != 0 {
12701268
return nil, nil
12711269
}
@@ -1753,31 +1751,50 @@ func (ls *INOLanguageServer) cpp2inoSymbolInformation(syms []lsp.SymbolInformati
17531751
panic("not implemented")
17541752
}
17551753

1756-
func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, cppDiags *lsp.PublishDiagnosticsParams) ([]*lsp.PublishDiagnosticsParams, error) {
1757-
inoDiagsParam := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
1754+
func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, cppDiagsParams *lsp.PublishDiagnosticsParams) ([]*lsp.PublishDiagnosticsParams, error) {
17581755

1759-
cppURI := cppDiags.URI
1756+
cppURI := cppDiagsParams.URI
17601757
isSketch := cppURI.AsPath().EquivalentTo(ls.buildSketchCpp)
1761-
if isSketch {
1762-
for inoURI := range ls.inoDocsWithDiagnostics {
1763-
inoDiagsParam[inoURI] = &lsp.PublishDiagnosticsParams{
1764-
URI: inoURI,
1765-
Diagnostics: []lsp.Diagnostic{},
1766-
}
1767-
}
1768-
ls.inoDocsWithDiagnostics = map[lsp.DocumentURI]bool{}
1769-
} else {
1758+
1759+
if !isSketch {
17701760
inoURI, _, err := ls.cpp2inoDocumentURI(logger, cppURI, lsp.NilRange)
17711761
if err != nil {
17721762
return nil, err
17731763
}
1774-
inoDiagsParam[inoURI] = &lsp.PublishDiagnosticsParams{
1764+
inoDiags := []lsp.Diagnostic{}
1765+
for _, cppDiag := range cppDiagsParams.Diagnostics {
1766+
inoURIofConvertedRange, inoRange, err := ls.cpp2inoDocumentURI(logger, cppURI, cppDiag.Range)
1767+
if err != nil {
1768+
return nil, err
1769+
}
1770+
if inoURIofConvertedRange.String() == sourcemapper.NotInoURI.String() {
1771+
continue
1772+
}
1773+
if inoURIofConvertedRange.String() != inoURI.String() {
1774+
return nil, fmt.Errorf("unexpected inoURI %s: it should be %s", inoURIofConvertedRange, inoURI)
1775+
}
1776+
inoDiag := cppDiag
1777+
inoDiag.Range = inoRange
1778+
inoDiags = append(inoDiags, inoDiag)
1779+
}
1780+
return []*lsp.PublishDiagnosticsParams{
1781+
{
1782+
URI: inoURI,
1783+
Diagnostics: inoDiags,
1784+
},
1785+
}, nil
1786+
}
1787+
1788+
allInoDiagsParams := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
1789+
for inoURI := range ls.inoDocsWithDiagnostics {
1790+
allInoDiagsParams[inoURI] = &lsp.PublishDiagnosticsParams{
17751791
URI: inoURI,
17761792
Diagnostics: []lsp.Diagnostic{},
17771793
}
17781794
}
1795+
ls.inoDocsWithDiagnostics = map[lsp.DocumentURI]bool{}
17791796

1780-
for _, cppDiag := range cppDiags.Diagnostics {
1797+
for _, cppDiag := range cppDiagsParams.Diagnostics {
17811798
inoURI, inoRange, err := ls.cpp2inoDocumentURI(logger, cppURI, cppDiag.Range)
17821799
if err != nil {
17831800
return nil, err
@@ -1786,39 +1803,37 @@ func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, c
17861803
continue
17871804
}
17881805

1789-
inoDiagParam, created := inoDiagsParam[inoURI]
1790-
if !created {
1791-
inoDiagParam = &lsp.PublishDiagnosticsParams{
1806+
inoDiagsParams, ok := allInoDiagsParams[inoURI]
1807+
if !ok {
1808+
inoDiagsParams = &lsp.PublishDiagnosticsParams{
17921809
URI: inoURI,
17931810
Diagnostics: []lsp.Diagnostic{},
17941811
}
1795-
inoDiagsParam[inoURI] = inoDiagParam
1812+
allInoDiagsParams[inoURI] = inoDiagsParams
17961813
}
17971814

17981815
inoDiag := cppDiag
17991816
inoDiag.Range = inoRange
1800-
inoDiagParam.Diagnostics = append(inoDiagParam.Diagnostics, inoDiag)
1801-
1802-
if isSketch {
1803-
ls.inoDocsWithDiagnostics[inoURI] = true
1804-
1805-
// If we have an "undefined reference" in the .ino code trigger a
1806-
// check for newly created symbols (that in turn may trigger a
1807-
// new arduino-preprocessing of the sketch).
1808-
var inoDiagCode string
1809-
if err := json.Unmarshal(inoDiag.Code, &inoDiagCode); err != nil {
1810-
if inoDiagCode == "undeclared_var_use_suggest" ||
1811-
inoDiagCode == "undeclared_var_use" ||
1812-
inoDiagCode == "ovl_no_viable_function_in_call" ||
1813-
inoDiagCode == "pp_file_not_found" {
1814-
ls.queueCheckCppDocumentSymbols()
1815-
}
1817+
inoDiagsParams.Diagnostics = append(inoDiagsParams.Diagnostics, inoDiag)
1818+
1819+
ls.inoDocsWithDiagnostics[inoURI] = true
1820+
1821+
// If we have an "undefined reference" in the .ino code trigger a
1822+
// check for newly created symbols (that in turn may trigger a
1823+
// new arduino-preprocessing of the sketch).
1824+
var inoDiagCode string
1825+
if err := json.Unmarshal(inoDiag.Code, &inoDiagCode); err != nil {
1826+
if inoDiagCode == "undeclared_var_use_suggest" ||
1827+
inoDiagCode == "undeclared_var_use" ||
1828+
inoDiagCode == "ovl_no_viable_function_in_call" ||
1829+
inoDiagCode == "pp_file_not_found" {
1830+
ls.queueCheckCppDocumentSymbols()
18161831
}
18171832
}
18181833
}
18191834

18201835
inoDiagParams := []*lsp.PublishDiagnosticsParams{}
1821-
for _, v := range inoDiagsParam {
1836+
for _, v := range allInoDiagsParams {
18221837
inoDiagParams = append(inoDiagParams, v)
18231838
}
18241839
return inoDiagParams, nil

Diff for: textutils/textutils.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ import (
77
)
88

99
// ApplyLSPTextDocumentContentChangeEvent applies the LSP change in the given text
10-
func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, changes []lsp.TextDocumentContentChangeEvent, version int) error {
11-
newText := textDoc.Text
10+
func ApplyLSPTextDocumentContentChangeEvent(textDoc lsp.TextDocumentItem, changes []lsp.TextDocumentContentChangeEvent, version int) (lsp.TextDocumentItem, error) {
1211
for _, change := range changes {
13-
if t, err := ApplyTextChange(newText, change.Range, change.Text); err == nil {
14-
newText = t
12+
if t, err := ApplyTextChange(textDoc.Text, change.Range, change.Text); err == nil {
13+
textDoc.Text = t
1514
} else {
16-
return err
15+
return lsp.TextDocumentItem{}, err
1716
}
1817
}
19-
textDoc.Text = newText
2018
textDoc.Version = version
21-
return nil
19+
return textDoc, nil
2220
}
2321

2422
// ApplyTextChange replaces startingText substring specified by replaceRange with insertText

0 commit comments

Comments
 (0)