Skip to content

Commit

Permalink
Moved textutils in lsp package, slightly improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 23, 2021
1 parent 98bbd42 commit d0c0168
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 315 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.old
build.sh
arduino-language-server*
arduino-language-server*
6 changes: 5 additions & 1 deletion ls/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ func (ls *INOLanguageServer) generateBuildEnvironment(logger jsonrpc.FunctionLog
}
data.Overrides[rel.String()] = trackedFile.Text
}

for filename, override := range data.Overrides {
logger.Logf("Dumped %s override:\n%s", filename, override)
}

var overridesJSON *paths.Path
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
return errors.WithMessage(err, "dumping tracked files")
} else if tmp, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
return errors.WithMessage(err, "dumping tracked files")
} else {
logger.Logf("Dumped overrides: %s", string(jsonBytes))
overridesJSON = tmp
defer tmp.Remove()
}
Expand Down
37 changes: 18 additions & 19 deletions ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/arduino-language-server/sourcemapper"
"github.com/arduino/arduino-language-server/streams"
"github.com/arduino/arduino-language-server/textutils"
"github.com/arduino/go-paths-helper"
"github.com/fatih/color"
"github.com/pkg/errors"
"go.bug.st/json"
"go.bug.st/lsp"
"go.bug.st/lsp/jsonrpc"
"go.bug.st/lsp/textedits"
)

var globalCliPath string
Expand Down Expand Up @@ -944,17 +944,17 @@ func (ls *INOLanguageServer) TextDocumentDidChangeNotifFromIDE(logger jsonrpc.Fu

logger.Logf("didChange(%s)", inoParams.TextDocument)
for _, change := range inoParams.ContentChanges {
logger.Logf("> %s", change)
logger.Logf(" > %s", change)
}

if cppParams, err := ls.didChange(logger, inoParams); err != nil {
logger.Logf("--E Error: %s", err)
logger.Logf("Error: %s", err)
} else if cppParams == nil {
logger.Logf("--X Notification is not propagated to clangd")
logger.Logf("Notification is not propagated to clangd")
} else {
logger.Logf("LS --> CL NOTIF didChange(%s@%d)", cppParams.TextDocument)
logger.Logf("to Clang: didChange(%s@%d)", cppParams.TextDocument)
for _, change := range cppParams.ContentChanges {
logger.Logf(" > %s", change)
logger.Logf(" > %s", change)
}
if err := ls.Clangd.conn.TextDocumentDidChange(cppParams); err != nil {
// Exit the process and trigger a restart by the client in case of a severe error
Expand Down Expand Up @@ -1015,27 +1015,28 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
ls.readLock(logger, false)
defer ls.readUnlock(logger)

logger.Logf("from clang %s (%d diagnostics):", cppParams.URI, cppParams.Diagnostics)
logger.Logf("%s (%d diagnostics):", cppParams.URI, len(cppParams.Diagnostics))
for _, diag := range cppParams.Diagnostics {
logger.Logf("> %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
logger.Logf(" > %s - %s: %s", diag.Range.Start, diag.Severity, string(diag.Code))
}

// the diagnostics on sketch.cpp.ino once mapped into their
// .ino counter parts may span over multiple .ino files...
allInoParams, err := ls.cpp2inoDiagnostics(logger, cppParams)
if err != nil {
logger.Logf(" Error converting diagnostics to .ino: %s", err)
logger.Logf("Error converting diagnostics to .ino: %s", err)
return
}

// Push back to IDE the converted diagnostics
logger.Logf("diagnostics to IDE:")
for _, inoParams := range allInoParams {
logger.Logf("to IDE: %s (%d diagnostics):", inoParams.URI, len(inoParams.Diagnostics))
logger.Logf(" - %s (%d diagnostics):", inoParams.URI, len(inoParams.Diagnostics))
for _, diag := range inoParams.Diagnostics {
logger.Logf(" > %d:%d - %v: %s", diag.Range.Start.Line, diag.Range.Start.Character, diag.Severity, diag.Code)
logger.Logf(" > %s - %s: %s", diag.Range.Start, diag.Severity, diag.Code)
}
if err := ls.IDE.conn.TextDocumentPublishDiagnostics(inoParams); err != nil {
logger.Logf(" Error sending diagnostics to IDE: %s", err)
logger.Logf("Error sending diagnostics to IDE: %s", err)
return
}
}
Expand Down Expand Up @@ -1216,7 +1217,7 @@ func (ls *INOLanguageServer) refreshCppDocumentSymbols(logger jsonrpc.FunctionLo
symbolsCanary := ""
for _, symbol := range cppDocumentSymbols {
logger.Logf(" symbol: %s %s %s", symbol.Kind, symbol.Name, symbol.Range)
if symbolText, err := textutils.ExtractRange(ls.sketchMapper.CppText.Text, symbol.Range); err != nil {
if symbolText, err := textedits.ExtractRange(ls.sketchMapper.CppText.Text, symbol.Range); err != nil {
logger.Logf(" > invalid range: %s", err)
symbolsCanary += "/"
} else if end := strings.Index(symbolText, "{"); end != -1 {
Expand Down Expand Up @@ -1298,7 +1299,7 @@ func (ls *INOLanguageServer) ino2cppTextDocumentItem(logger jsonrpc.FunctionLogg
func (ls *INOLanguageServer) didChange(logger jsonrpc.FunctionLogger, inoDidChangeParams *lsp.DidChangeTextDocumentParams) (*lsp.DidChangeTextDocumentParams, error) {
// Clear all RangeLengths: it's a deprecated field and if the byte count is wrong the
// source text file will be unloaded from clangd without notice, leading to a "non-added
// docuemtn" error for all subsequent requests.
// document" error for all subsequent requests.
// https://github.com/clangd/clangd/issues/717#issuecomment-793220007
for i := range inoDidChangeParams.ContentChanges {
inoDidChangeParams.ContentChanges[i].RangeLength = nil
Expand All @@ -1308,14 +1309,12 @@ func (ls *INOLanguageServer) didChange(logger jsonrpc.FunctionLogger, inoDidChan

// Apply the change to the tracked sketch file.
trackedInoID := inoDoc.URI.AsPath().String()
trackedInoDoc, ok := ls.trackedInoDocs[trackedInoID]
if !ok {
if doc, ok := ls.trackedInoDocs[trackedInoID]; !ok {
return nil, unknownURI(inoDoc.URI)
}
if updatedTrackedInoDoc, err := textutils.ApplyLSPTextDocumentContentChangeEvent(trackedInoDoc, inoDidChangeParams.ContentChanges, inoDoc.Version); err != nil {
} else if updatedDoc, err := textedits.ApplyLSPTextDocumentContentChangeEvent(doc, inoDidChangeParams); err != nil {
return nil, err
} else {
ls.trackedInoDocs[trackedInoID] = updatedTrackedInoDoc
ls.trackedInoDocs[trackedInoID] = updatedDoc
}

logger.Logf("Tracked SKETCH file:----------+\n" + ls.trackedInoDocs[trackedInoID].Text + "\n----------------------")
Expand Down
4 changes: 2 additions & 2 deletions sourcemapper/ino.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"strconv"
"strings"

"github.com/arduino/arduino-language-server/textutils"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
"go.bug.st/lsp"
"go.bug.st/lsp/textedits"
)

// SketchMapper is a mapping between the .ino sketch and the preprocessed .cpp file
Expand Down Expand Up @@ -219,7 +219,7 @@ func (s *SketchMapper) ApplyTextChange(inoURI lsp.DocumentURI, inoChange lsp.Tex
deletedLines := inoRange.End.Line - inoRange.Start.Line

// Apply text changes
newText, err := textutils.ApplyTextChange(s.CppText.Text, cppRange, inoChange.Text)
newText, err := textedits.ApplyTextChange(s.CppText.Text, cppRange, inoChange.Text)
if err != nil {
panic("error replacing text: " + err.Error())
}
Expand Down
123 changes: 0 additions & 123 deletions textutils/textutils.go

This file was deleted.

Loading

0 comments on commit d0c0168

Please sign in to comment.