Skip to content

Commit 0489a1c

Browse files
authored
Merge pull request #56 from bcmi-labs/include-fix
Check for includes changes at the right place
2 parents fa579af + 88647ab commit 0489a1c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

Diff for: handler/handler.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type InoHandler struct {
5858
buildSketchCpp *paths.Path
5959
buildSketchCppVersion int
6060
buildSketchSymbols []lsp.DocumentSymbol
61+
buildSketchIncludesCanary string
6162
buildSketchSymbolsCanary string
6263
buildSketchSymbolsLoad bool
6364
buildSketchSymbolsCheck bool
@@ -604,28 +605,23 @@ func (handler *InoHandler) refreshCppDocumentSymbols(prefix string) error {
604605
i++
605606
}
606607
symbols = symbols[:i]
608+
handler.buildSketchSymbols = symbols
607609

608-
canary := ""
609-
for _, line := range strings.Split(handler.sketchMapper.CppText.Text, "\n") {
610-
if strings.Contains(line, "#include <") {
611-
canary += line
612-
}
613-
}
610+
symbolsCanary := ""
614611
for _, symbol := range symbols {
615612
log.Printf(prefix+" symbol: %s %s %s", symbol.Kind, symbol.Name, symbol.Range)
616613
if symbolText, err := textutils.ExtractRange(handler.sketchMapper.CppText.Text, symbol.Range); err != nil {
617614
log.Printf(prefix+" > invalid range: %s", err)
618-
canary += "/"
615+
symbolsCanary += "/"
619616
} else if end := strings.Index(symbolText, "{"); end != -1 {
620617
log.Printf(prefix+" TRIMMED> %s", symbolText[:end])
621-
canary += symbolText[:end]
618+
symbolsCanary += symbolText[:end]
622619
} else {
623620
log.Printf(prefix+" > %s", symbolText)
624-
canary += symbolText
621+
symbolsCanary += symbolText
625622
}
626623
}
627-
handler.buildSketchSymbols = symbols
628-
handler.buildSketchSymbolsCanary = canary
624+
handler.buildSketchSymbolsCanary = symbolsCanary
629625
return nil
630626
}
631627

@@ -655,6 +651,23 @@ func (handler *InoHandler) CheckCppDocumentSymbols() error {
655651
return nil
656652
}
657653

654+
func (handler *InoHandler) CheckCppIncludesChanges() {
655+
prefix := "INCK--- "
656+
657+
includesCanary := ""
658+
for _, line := range strings.Split(handler.sketchMapper.CppText.Text, "\n") {
659+
if strings.Contains(line, "#include <") {
660+
includesCanary += line
661+
}
662+
}
663+
664+
if includesCanary != handler.buildSketchIncludesCanary {
665+
handler.buildSketchIncludesCanary = includesCanary
666+
log.Println(prefix + "#include change detected, triggering sketch rebuild!")
667+
handler.scheduleRebuildEnvironment()
668+
}
669+
}
670+
658671
func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
659672
// Open compile_commands.json and find the main cross-compiler executable
660673
compileCommandsJSONPath := compileCommandsDir.Join("compile_commands.json")
@@ -833,6 +846,8 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
833846
cppChanges = append(cppChanges, cppChange)
834847
}
835848

849+
handler.CheckCppIncludesChanges()
850+
836851
// build a cpp equivalent didChange request
837852
cppReq := &lsp.DidChangeTextDocumentParams{
838853
ContentChanges: cppChanges,
@@ -843,6 +858,7 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
843858
Version: handler.sketchMapper.CppText.Version,
844859
},
845860
}
861+
846862
return cppReq, nil
847863
}
848864

0 commit comments

Comments
 (0)