Skip to content

Commit f99cf50

Browse files
committed
textDocument/didOpen: index related files when a header is opened
Fix #180 index.initialBlacklist: ["."] can inhibit initial indexing (useful for larger code bases). Opened files are still indexed, though. This heuristic allows related files (a/foo.c a/b/foo.cc) to be indexed when a header (foo.h) is opened.
1 parent 8835a55 commit f99cf50

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/messages/textDocument_did.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ void MessageHandler::textDocument_didOpen(DidOpenTextDocumentParam &param) {
4141

4242
// Submit new index request if it is not a header file or there is no
4343
// pending index request.
44-
std::pair<LanguageId, bool> lang = lookupExtension(path);
45-
if ((lang.first != LanguageId::Unknown && !lang.second) ||
44+
auto [lang, header] = lookupExtension(path);
45+
if ((lang != LanguageId::Unknown && !header) ||
4646
!pipeline::pending_index_requests)
4747
pipeline::Index(path, {}, IndexMode::Normal, false);
48+
if (header)
49+
project->IndexRelated(path);
4850

4951
manager->OnView(path);
5052
}

src/project.cc

+18
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,22 @@ void Project::Index(WorkingFiles *wfiles, RequestId id) {
535535
// trigger refreshing semantic highlight for all working files.
536536
pipeline::Index("", {}, IndexMode::NonInteractive, false);
537537
}
538+
539+
void Project::IndexRelated(const std::string &path) {
540+
auto &gi = g_config->index;
541+
GroupMatch match(gi.whitelist, gi.blacklist);
542+
std::string stem = sys::path::stem(path);
543+
std::lock_guard lock(mtx);
544+
for (auto &[root, folder] : root2folder)
545+
if (StringRef(path).startswith(root)) {
546+
for (const Project::Entry &entry : folder.entries) {
547+
std::string reason;
548+
if (sys::path::stem(entry.filename) == stem && entry.filename != path &&
549+
match.Matches(entry.filename, &reason))
550+
pipeline::Index(entry.filename, entry.args, IndexMode::NonInteractive,
551+
true);
552+
}
553+
break;
554+
}
555+
}
538556
} // namespace ccls

src/project.hh

+1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ struct Project {
6565
const std::string &path);
6666

6767
void Index(WorkingFiles *wfiles, RequestId id);
68+
void IndexRelated(const std::string &path);
6869
};
6970
} // namespace ccls

0 commit comments

Comments
 (0)