Index: clangd/index/FileIndex.h =================================================================== --- clangd/index/FileIndex.h +++ clangd/index/FileIndex.h @@ -59,7 +59,6 @@ }; /// This manages symbols from files and an in-memory index on all symbols. -/// FIXME: Expose an interface to remove files that are closed. class FileIndex : public MergedIndex { public: /// If URISchemes is empty, the default schemes in SymbolCollector will be @@ -75,6 +74,9 @@ /// `indexMainDecls`. void updateMain(PathRef Path, ParsedAST &AST); + /// Remove all index data associated with the file \p Path. + void removeFile(PathRef Path); + private: std::vector URISchemes; Index: clangd/index/FileIndex.cpp =================================================================== --- clangd/index/FileIndex.cpp +++ clangd/index/FileIndex.cpp @@ -174,5 +174,12 @@ MainFileIndex.reset(MainFileSymbols.buildMemIndex()); } +void FileIndex::removeFile(PathRef Path) { + PreambleSymbols.update(Path, nullptr, nullptr); + PreambleIndex.reset(PreambleSymbols.buildMemIndex()); + MainFileSymbols.update(Path, nullptr, nullptr); + MainFileIndex.reset(MainFileSymbols.buildMemIndex()); +} + } // namespace clangd } // namespace clang Index: unittests/clangd/FileIndexTests.cpp =================================================================== --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -21,6 +21,7 @@ #include "gtest/gtest.h" using testing::_; +using testing::IsEmpty; using testing::AllOf; using testing::ElementsAre; using testing::Pair; @@ -164,6 +165,17 @@ EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X")); } +TEST(FileIndexTest, RemoveFile) { + FileIndex M; + update(M, "f1", "class Foo {};"); + + FuzzyFindRequest Req; + EXPECT_THAT(match(M, Req), UnorderedElementsAre("Foo")); + + M.removeFile("f1.cpp"); + EXPECT_THAT(match(M, Req), IsEmpty()); +} + TEST(FileIndexTest, NoLocal) { FileIndex M; update(M, "f1", "namespace ns { void f() { int local = 0; } class X {}; }");