Skip to content

Commit fa579af

Browse files
authored
Merge pull request #55 from bcmi-labs/stablity-fix
Regenerate compile_commands.json if included files are changed
2 parents 51c75e6 + eeabe2c commit fa579af

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: handler/handler.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
514514
return errors.WithMessage(err, "reading generated cpp file from sketch")
515515
}
516516

517+
compilers := examineCompileCommandsJSON(handler.buildPath)
518+
517519
if params == nil {
518520
// If we are restarting re-synchronize clangd
519521
cppURI := lsp.NewDocumentURIFromPath(handler.buildSketchCpp)
@@ -535,7 +537,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
535537
}
536538
} else {
537539
// Otherwise start clangd!
538-
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp)
540+
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp, compilers)
539541
clangdStdio := streams.NewReadWriteCloser(clangdStdin, clangdStdout)
540542
if enableLogging {
541543
clangdStdio = streams.LogReadWriteCloserAs(clangdStdio, "inols-clangd.log")
@@ -604,6 +606,11 @@ func (handler *InoHandler) refreshCppDocumentSymbols(prefix string) error {
604606
symbols = symbols[:i]
605607

606608
canary := ""
609+
for _, line := range strings.Split(handler.sketchMapper.CppText.Text, "\n") {
610+
if strings.Contains(line, "#include <") {
611+
canary += line
612+
}
613+
}
607614
for _, symbol := range symbols {
608615
log.Printf(prefix+" symbol: %s %s %s", symbol.Kind, symbol.Name, symbol.Range)
609616
if symbolText, err := textutils.ExtractRange(handler.sketchMapper.CppText.Text, symbol.Range); err != nil {
@@ -648,9 +655,10 @@ func (handler *InoHandler) CheckCppDocumentSymbols() error {
648655
return nil
649656
}
650657

651-
func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
658+
func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
652659
// Open compile_commands.json and find the main cross-compiler executable
653-
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsDir.Join("compile_commands.json"))
660+
compileCommandsJSONPath := compileCommandsDir.Join("compile_commands.json")
661+
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsJSONPath)
654662
if err != nil {
655663
panic("could not find compile_commands.json")
656664
}
@@ -676,6 +684,10 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
676684
// Save back compile_commands.json with OS native file separator and extension
677685
compileCommands.SaveToFile()
678686

687+
return compilers
688+
}
689+
690+
func startClangd(compileCommandsDir, sketchCpp *paths.Path, compilers map[string]bool) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
679691
// Start clangd
680692
args := []string{
681693
globalClangdPath,
@@ -1596,7 +1608,8 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
15961608
for _, diag := range inoDiag.Diagnostics {
15971609
if diag.Code == "undeclared_var_use_suggest" ||
15981610
diag.Code == "undeclared_var_use" ||
1599-
diag.Code == "ovl_no_viable_function_in_call" {
1611+
diag.Code == "ovl_no_viable_function_in_call" ||
1612+
diag.Code == "pp_file_not_found" {
16001613
handler.buildSketchSymbolsCheck = true
16011614
}
16021615
}

0 commit comments

Comments
 (0)