Skip to content

Enforce rebuild when files are added to the sketch #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ls/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (

type SketchRebuilder struct {
ls *INOLanguageServer
trigger chan bool
trigger chan chan<- bool
cancel func()
mutex sync.Mutex
}

func NewSketchBuilder(ls *INOLanguageServer) *SketchRebuilder {
res := &SketchRebuilder{
trigger: make(chan bool, 1),
trigger: make(chan chan<- bool, 1),
cancel: func() {},
ls: ls,
}
Expand All @@ -45,25 +45,33 @@ func NewSketchBuilder(ls *INOLanguageServer) *SketchRebuilder {
return res
}

func (ls *INOLanguageServer) triggerRebuildAndWait(logger jsonrpc.FunctionLogger) {
completed := make(chan bool)
ls.sketchRebuilder.TriggerRebuild(completed)
ls.writeUnlock(logger)
<-completed
ls.writeLock(logger, true)
}

func (ls *INOLanguageServer) triggerRebuild() {
ls.sketchRebuilder.TriggerRebuild()
ls.sketchRebuilder.TriggerRebuild(nil)
}

func (r *SketchRebuilder) TriggerRebuild() {
func (r *SketchRebuilder) TriggerRebuild(completed chan<- bool) {
r.mutex.Lock()
defer r.mutex.Unlock()

r.cancel() // Stop possibly already running builds
select {
case r.trigger <- true:
case r.trigger <- completed:
default:
}
}

func (r *SketchRebuilder) rebuilderLoop() {
logger := NewLSPFunctionLogger(color.HiMagentaString, "SKETCH REBUILD: ")
for {
<-r.trigger
completed := <-r.trigger

for {
// Concede a 200ms delay to accumulate bursts of changes
Expand All @@ -90,6 +98,9 @@ func (r *SketchRebuilder) rebuilderLoop() {

cancel()
r.ls.progressHandler.End("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressEnd{Message: "done"})
if completed != nil {
close(completed)
}
}
}

Expand Down
43 changes: 36 additions & 7 deletions ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,10 +961,20 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
ls.writeLock(logger, true)
defer ls.writeUnlock(logger)

ls.triggerRebuild()
ideTextDocItem := ideParam.TextDocument
clangURI, _, err := ls.ide2ClangDocumentURI(logger, ideTextDocItem.URI)
if err != nil {
logger.Logf("Error: %s", err)
return
}

if ls.ideURIIsPartOfTheSketch(ideTextDocItem.URI) {
if !clangURI.AsPath().Exist() {
ls.triggerRebuildAndWait(logger)
}
}

// Add the TextDocumentItem in the tracked files list
ideTextDocItem := ideParam.TextDocument
ls.trackedIdeDocs[ideTextDocItem.URI.AsPath().String()] = ideTextDocItem

// If we are tracking a .ino...
Expand All @@ -979,11 +989,6 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
}
}

clangURI, _, err := ls.ide2ClangDocumentURI(logger, ideTextDocItem.URI)
if err != nil {
logger.Logf("Error: %s", err)
return
}
clangTextDocItem := lsp.TextDocumentItem{
URI: clangURI,
}
Expand Down Expand Up @@ -1198,6 +1203,30 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
}
}

// Try to filter as much bogus errors as possible (due to wrong clang "driver" or missing
// support for specific embedded CPU architecture).
for _, ideParams := range allIdeParams {
n := 0
for _, ideDiag := range ideParams.Diagnostics {
var code string
_ = json.Unmarshal(ideDiag.Code, &code)
switch code {
case "":
// Filter unkown non-string codes
case "drv_unknown_argument_with_suggestion":
// Skip errors like: "Unknown argument '-mlongcalls'; did you mean '-mlong-calls'?"
case "drv_unknown_argument":
// Skip errors like: "Unknown argument: '-mtext-section-literals'"
default:
ideParams.Diagnostics[n] = ideDiag
n++
continue
}
logger.Logf("filtered out diagnostic with error-code: %s", ideDiag.Code)
}
ideParams.Diagnostics = ideParams.Diagnostics[:n]
}

// Push back to IDE the converted diagnostics
logger.Logf("diagnostics to IDE:")
for _, ideParams := range allIdeParams {
Expand Down