This patch adds support for watching for changes to
compile_commands.json, and reparsing files if needed.
The watching is done using the "workspace/didChangeWatchedFiles"
notification, so the actual file watching is the frontend's problem.
To keep things simple, we react to any change involving a file called
"compile_commands.json".
The chosen strategy tries to avoid useless reparses. We don't want to
reparse a file if its compile commands don't change. So when we get a
change notification, we:
- Save the compile commands of all open files on the side.
- Clear everything we know about compilation databases and compilation commands.
- Query the compile commands again for all open files (which will go read the possibly updated compile commands). If the commands are different than the saved ones, we reparse the file.
I updated the vscode-clangd extension. If you don't feel inspired, you
can use this small .cpp to test it:
#ifdef MACRO #warning "MACRO is defined" #else #warning "MACRO is not defined" #endif int main() {}
and this compile_commands.json:
[{ "directory": ".", "file": "test.cpp", "arguments": ["g++", "-c", "test.cpp", "-DMACRO=2"] }]
Adding and removing the "-DMACRO=2" argument, you should see a different
Maybe keep the old logic of reparsing all open files? This would make the change way simpler and I don't think we need this extra complexity in the long run, when we have better integration with the build system.
ClangdServer will reuse the preamble if compile command didn't change anyway, so reparse will be very fast and shouldn't be affected.
If the compile command does change, we'll retrigger the full rebuild.