ccls/src/clang_complete.cc

720 lines
26 KiB
C++
Raw Normal View History

#include "clang_complete.h"
#include "clang_utils.h"
2018-05-27 19:24:56 +00:00
#include "filesystem.hh"
#include "log.hh"
2017-05-10 04:00:05 +00:00
#include "platform.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
2018-07-14 17:00:04 +00:00
#include <clang/Sema/CodeCompleteConsumer.h>
2018-05-27 19:24:56 +00:00
#include <llvm/ADT/Twine.h>
#include <llvm/Support/Threading.h>
2018-07-14 17:00:04 +00:00
using namespace clang;
using namespace llvm;
#include <algorithm>
#include <thread>
namespace {
2018-04-08 00:10:54 +00:00
std::string StripFileType(const std::string& path) {
SmallString<128> Ret;
sys::path::append(Ret, sys::path::parent_path(path), sys::path::stem(path));
return Ret.str();
2018-04-08 00:10:54 +00:00
}
unsigned GetCompletionPriority(const CodeCompletionString &CCS,
2017-12-07 19:53:48 +00:00
CXCursorKind result_kind,
const std::optional<std::string> &typedText) {
2018-07-14 17:00:04 +00:00
unsigned priority = CCS.getPriority();
if (CCS.getAvailability() != CXAvailability_Available ||
result_kind == CXCursor_Destructor ||
result_kind == CXCursor_ConversionFunction ||
(result_kind == CXCursor_CXXMethod && typedText &&
2018-07-14 17:00:04 +00:00
StartsWith(*typedText, "operator")))
priority *= 100;
return priority;
}
lsCompletionItemKind GetCompletionKind(CXCursorKind cursor_kind) {
switch (cursor_kind) {
case CXCursor_UnexposedDecl:
return lsCompletionItemKind::Text;
2018-03-01 05:49:44 +00:00
case CXCursor_StructDecl:
case CXCursor_UnionDecl:
return lsCompletionItemKind::Struct;
case CXCursor_ClassDecl:
return lsCompletionItemKind::Class;
case CXCursor_EnumDecl:
return lsCompletionItemKind::Enum;
case CXCursor_FieldDecl:
return lsCompletionItemKind::Field;
case CXCursor_EnumConstantDecl:
return lsCompletionItemKind::EnumMember;
case CXCursor_FunctionDecl:
return lsCompletionItemKind::Function;
case CXCursor_VarDecl:
case CXCursor_ParmDecl:
return lsCompletionItemKind::Variable;
case CXCursor_ObjCInterfaceDecl:
return lsCompletionItemKind::Interface;
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_CXXMethod:
2017-12-15 05:18:43 +00:00
case CXCursor_ObjCClassMethodDecl:
return lsCompletionItemKind::Method;
case CXCursor_FunctionTemplate:
2018-03-20 02:51:42 +00:00
return lsCompletionItemKind::Function;
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_ConversionFunction:
return lsCompletionItemKind::Constructor;
2017-12-15 05:18:43 +00:00
case CXCursor_ObjCIvarDecl:
return lsCompletionItemKind::Variable;
case CXCursor_ClassTemplate:
case CXCursor_ClassTemplatePartialSpecialization:
case CXCursor_UsingDeclaration:
case CXCursor_TypedefDecl:
case CXCursor_TypeAliasDecl:
case CXCursor_TypeAliasTemplateDecl:
2017-12-15 05:18:43 +00:00
case CXCursor_ObjCCategoryDecl:
case CXCursor_ObjCProtocolDecl:
case CXCursor_ObjCImplementationDecl:
case CXCursor_ObjCCategoryImplDecl:
return lsCompletionItemKind::Class;
2018-03-01 05:49:44 +00:00
case CXCursor_ObjCPropertyDecl:
return lsCompletionItemKind::Property;
case CXCursor_MacroInstantiation:
case CXCursor_MacroDefinition:
return lsCompletionItemKind::Interface;
case CXCursor_Namespace:
case CXCursor_NamespaceAlias:
case CXCursor_NamespaceRef:
return lsCompletionItemKind::Module;
case CXCursor_MemberRef:
case CXCursor_TypeRef:
2017-12-15 05:18:43 +00:00
case CXCursor_ObjCSuperClassRef:
case CXCursor_ObjCProtocolRef:
case CXCursor_ObjCClassRef:
return lsCompletionItemKind::Reference;
2018-01-11 02:43:01 +00:00
// return lsCompletionItemKind::Unit;
// return lsCompletionItemKind::Value;
// return lsCompletionItemKind::Keyword;
// return lsCompletionItemKind::Snippet;
// return lsCompletionItemKind::Color;
// return lsCompletionItemKind::File;
case CXCursor_NotImplemented:
case CXCursor_OverloadCandidate:
return lsCompletionItemKind::Text;
2018-03-01 05:49:44 +00:00
case CXCursor_TemplateTypeParameter:
case CXCursor_TemplateTemplateParameter:
return lsCompletionItemKind::TypeParameter;
2017-12-30 00:32:43 +00:00
default:
LOG_S(WARNING) << "Unhandled completion kind " << cursor_kind;
return lsCompletionItemKind::Text;
}
}
2018-07-14 17:00:04 +00:00
void BuildCompletionItemTexts(std::vector<lsCompletionItem> &out,
CodeCompletionString &CCS,
bool include_snippets) {
assert(!out.empty());
auto out_first = out.size() - 1;
std::string result_type;
2018-07-14 17:00:04 +00:00
for (unsigned i = 0, num_chunks = CCS.size(); i < num_chunks; ++i) {
const CodeCompletionString::Chunk &Chunk = CCS[i];
CodeCompletionString::ChunkKind Kind = Chunk.Kind;
std::string text;
2018-07-14 17:00:04 +00:00
switch (Kind) {
case CodeCompletionString::CK_TypedText:
case CodeCompletionString::CK_Text:
case CodeCompletionString::CK_Placeholder:
case CodeCompletionString::CK_Informative:
if (Chunk.Text)
text = Chunk.Text;
for (auto i = out_first; i < out.size(); i++) {
// first TypedText is used for filtering
if (Kind == CodeCompletionString::CK_TypedText && !out[i].filterText)
2018-02-22 07:34:32 +00:00
out[i].filterText = text;
2018-07-14 17:00:04 +00:00
if (Kind == CodeCompletionString::CK_Placeholder)
2018-02-22 07:34:32 +00:00
out[i].parameters_.push_back(text);
}
break;
2018-07-14 17:00:04 +00:00
case CodeCompletionString::CK_ResultType:
if (Chunk.Text)
result_type = Chunk.Text;
continue;
case CodeCompletionString::CK_CurrentParameter:
2018-02-22 07:34:32 +00:00
// We have our own parsing logic for active parameter. This doesn't seem
// to be very reliable.
continue;
2018-07-14 17:00:04 +00:00
case CodeCompletionString::CK_Optional: {
2018-02-22 07:34:32 +00:00
// duplicate last element, the recursive call will complete it
out.push_back(out.back());
2018-07-14 17:00:04 +00:00
BuildCompletionItemTexts(out, *Chunk.Optional, include_snippets);
2018-02-22 07:34:32 +00:00
continue;
}
2018-07-14 17:00:04 +00:00
// clang-format off
case CodeCompletionString::CK_LeftParen: text = '('; break;
case CodeCompletionString::CK_RightParen: text = ')'; break;
case CodeCompletionString::CK_LeftBracket: text = '['; break;
case CodeCompletionString::CK_RightBracket: text = ']'; break;
case CodeCompletionString::CK_LeftBrace: text = '{'; break;
case CodeCompletionString::CK_RightBrace: text = '}'; break;
case CodeCompletionString::CK_LeftAngle: text = '<'; break;
case CodeCompletionString::CK_RightAngle: text = '>'; break;
case CodeCompletionString::CK_Comma: text = ", "; break;
case CodeCompletionString::CK_Colon: text = ':'; break;
case CodeCompletionString::CK_SemiColon: text = ';'; break;
case CodeCompletionString::CK_Equal: text = '='; break;
case CodeCompletionString::CK_HorizontalSpace: text = ' '; break;
case CodeCompletionString::CK_VerticalSpace: text = ' '; break;
// clang-format on
}
if (Kind != CodeCompletionString::CK_Informative)
for (auto i = out_first; i < out.size(); ++i) {
out[i].label += text;
if (!include_snippets && !out[i].parameters_.empty())
continue;
if (Kind == CodeCompletionString::CK_Placeholder) {
out[i].insertText +=
"${" + std::to_string(out[i].parameters_.size()) + ":" + text + "}";
out[i].insertTextFormat = lsInsertTextFormat::Snippet;
} else {
out[i].insertText += text;
}
}
}
if (result_type.size())
for (auto i = out_first; i < out.size(); ++i) {
// ' : ' for variables,
// ' -> ' (trailing return type-like) for functions
out[i].label += (out[i].label == out[i].filterText ? " : " : " -> ");
out[i].label += result_type;
}
}
// |do_insert|: if |!do_insert|, do not append strings to |insert| after
// a placeholder.
2018-07-14 17:00:04 +00:00
void BuildDetailString(const CodeCompletionString &CCS, lsCompletionItem &item,
bool &do_insert, std::vector<std::string> *parameters,
bool include_snippets, int &angle_stack) {
for (unsigned i = 0, num_chunks = CCS.size(); i < num_chunks; ++i) {
const CodeCompletionString::Chunk &Chunk = CCS[i];
CodeCompletionString::ChunkKind Kind = Chunk.Kind;
const char* text = nullptr;
switch (Kind) {
case CodeCompletionString::CK_TypedText:
item.label = Chunk.Text;
[[fallthrough]];
case CodeCompletionString::CK_Text:
item.detail += Chunk.Text;
if (do_insert)
item.insertText += Chunk.Text;
break;
case CodeCompletionString::CK_Placeholder: {
parameters->push_back(Chunk.Text);
item.detail += Chunk.Text;
// Add parameter declarations as snippets if enabled
if (include_snippets) {
item.insertText += "${" + std::to_string(parameters->size()) + ":" + Chunk.Text + "}";
item.insertTextFormat = lsInsertTextFormat::Snippet;
} else
do_insert = false;
break;
}
case CodeCompletionString::CK_Informative:
item.detail += Chunk.Text;
break;
case CodeCompletionString::CK_Optional: {
// Do not add text to insert string if we're in angle brackets.
bool should_insert = do_insert && angle_stack == 0;
BuildDetailString(*Chunk.Optional, item, should_insert,
parameters, include_snippets, angle_stack);
break;
}
case CodeCompletionString::CK_ResultType:
item.detail = Chunk.Text + item.detail + " ";
break;
case CodeCompletionString::CK_CurrentParameter:
// We have our own parsing logic for active parameter. This doesn't seem
// to be very reliable.
break;
// clang-format off
case CodeCompletionString::CK_LeftParen: text = "("; break;
case CodeCompletionString::CK_RightParen: text = ")"; break;
case CodeCompletionString::CK_LeftBracket: text = "["; break;
case CodeCompletionString::CK_RightBracket: text = "]"; break;
case CodeCompletionString::CK_LeftBrace: text = "{"; break;
case CodeCompletionString::CK_RightBrace: text = "}"; break;
case CodeCompletionString::CK_LeftAngle: text = "<"; angle_stack++; break;
case CodeCompletionString::CK_RightAngle: text = ">"; angle_stack--; break;
case CodeCompletionString::CK_Comma: text = ", "; break;
case CodeCompletionString::CK_Colon: text = ":"; break;
case CodeCompletionString::CK_SemiColon: text = ";"; break;
case CodeCompletionString::CK_Equal: text = "="; break;
case CodeCompletionString::CK_HorizontalSpace:
case CodeCompletionString::CK_VerticalSpace: text = " "; break;
// clang-format on
}
if (text) {
item.detail += text;
if (do_insert && include_snippets)
item.insertText += text;
}
}
}
class CaptureCompletionResults : public CodeCompleteConsumer {
std::shared_ptr<clang::GlobalCodeCompletionAllocator> Alloc;
CodeCompletionTUInfo CCTUInfo;
public:
std::vector<lsCompletionItem> ls_items;
CaptureCompletionResults(const CodeCompleteOptions &Opts)
: CodeCompleteConsumer(Opts, false),
Alloc(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
CCTUInfo(Alloc) {}
void ProcessCodeCompleteResults(Sema &S,
CodeCompletionContext Context,
CodeCompletionResult *Results,
unsigned NumResults) override {
ls_items.reserve(NumResults);
for (unsigned i = 0; i != NumResults; i++) {
CodeCompletionString *CCS = Results[i].CreateCodeCompletionString(
S, Context, getAllocator(), getCodeCompletionTUInfo(),
includeBriefComments());
if (CCS->getAvailability() == CXAvailability_NotAvailable)
continue;
lsCompletionItem ls_item;
ls_item.kind = GetCompletionKind(Results[i].CursorKind);
if (const char* brief = CCS->getBriefComment())
ls_item.documentation = brief;
// label/detail/filterText/insertText/priority
if (g_config->completion.detailedLabel) {
ls_item.detail = CCS->getParentContextName().str();
size_t first_idx = ls_items.size();
ls_items.push_back(ls_item);
BuildCompletionItemTexts(ls_items, *CCS,
g_config->client.snippetSupport);
for (size_t j = first_idx; j < ls_items.size(); j++) {
if (g_config->client.snippetSupport &&
ls_items[j].insertTextFormat == lsInsertTextFormat::Snippet)
ls_items[j].insertText += "$0";
ls_items[j].priority_ = GetCompletionPriority(
*CCS, Results[i].CursorKind, ls_items[i].filterText);
}
} else {
bool do_insert = true;
int angle_stack = 0;
BuildDetailString(*CCS, ls_item, do_insert,
&ls_item.parameters_,
g_config->client.snippetSupport, angle_stack);
if (g_config->client.snippetSupport &&
ls_item.insertTextFormat == lsInsertTextFormat::Snippet)
ls_item.insertText += "$0";
ls_item.priority_ =
GetCompletionPriority(*CCS, Results[i].CursorKind, ls_item.label);
ls_items.push_back(ls_item);
}
}
}
CodeCompletionAllocator &getAllocator() override { return *Alloc; }
CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo;}
};
void TryEnsureDocumentParsed(ClangCompleteManager* manager,
std::shared_ptr<CompletionSession> session,
std::unique_ptr<ClangTranslationUnit>* tu,
2018-04-14 16:52:17 +00:00
bool emit_diag) {
// Nothing to do. We already have a translation unit.
if (*tu)
return;
const auto &args = session->file.args;
2018-01-30 00:27:43 +00:00
WorkingFiles::Snapshot snapshot = session->working_files->AsSnapshot(
{StripFileType(session->file.filename)});
LOG_S(INFO) << "Creating completion session with arguments "
2018-02-21 07:52:49 +00:00
<< StringJoin(args, " ");
*tu = ClangTranslationUnit::Create(session->file.filename, args, snapshot);
}
2018-04-14 16:52:17 +00:00
void CompletionPreloadMain(ClangCompleteManager* completion_manager) {
while (true) {
// Fetching the completion request blocks until we have a request.
2018-04-14 16:52:17 +00:00
auto request = completion_manager->preload_requests_.Dequeue();
2017-06-10 01:02:48 +00:00
// If we don't get a session then that means we don't care about the file
// anymore - abandon the request.
2017-09-22 01:14:57 +00:00
std::shared_ptr<CompletionSession> session =
completion_manager->TryGetSession(request.path,
false /*mark_as_completion*/,
false /*create_if_needed*/);
if (!session)
continue;
2018-04-14 16:52:17 +00:00
// Note: we only preload completion. We emit diagnostics for the
// completion preload though.
CompletionSession::Tu* tu = &session->completion;
// If we've parsed it more recently than the request time, don't bother
// reparsing.
2018-04-14 16:52:17 +00:00
if (tu->last_parsed_at && *tu->last_parsed_at > request.request_time)
continue;
std::unique_ptr<ClangTranslationUnit> parsing;
TryEnsureDocumentParsed(completion_manager, session, &parsing, true);
// Activate new translation unit.
2018-04-14 16:52:17 +00:00
std::lock_guard<std::mutex> lock(tu->lock);
tu->last_parsed_at = std::chrono::high_resolution_clock::now();
tu->tu = std::move(parsing);
}
}
void CompletionQueryMain(ClangCompleteManager* completion_manager) {
while (true) {
// Fetching the completion request blocks until we have a request.
2017-09-22 01:14:57 +00:00
std::unique_ptr<ClangCompleteManager::CompletionRequest> request =
completion_manager->completion_request_.Dequeue();
// Drop older requests if we're not buffering.
2018-04-04 06:05:41 +00:00
while (g_config->completion.dropOldRequests &&
!completion_manager->completion_request_.IsEmpty()) {
completion_manager->on_dropped_(request->id);
request = completion_manager->completion_request_.Dequeue();
}
std::string path = request->document.uri.GetPath();
2017-09-22 01:14:57 +00:00
std::shared_ptr<CompletionSession> session =
completion_manager->TryGetSession(path, true /*mark_as_completion*/,
true /*create_if_needed*/);
2017-06-10 01:02:48 +00:00
2018-04-14 16:52:17 +00:00
std::lock_guard<std::mutex> lock(session->completion.lock);
TryEnsureDocumentParsed(completion_manager, session, &session->completion.tu, false);
// It is possible we failed to create the document despite
// |TryEnsureDocumentParsed|.
if (ClangTranslationUnit* tu = session->completion.tu.get()) {
WorkingFiles::Snapshot snapshot =
completion_manager->working_files_->AsSnapshot({StripFileType(path)});
IntrusiveRefCntPtr<FileManager> FileMgr(&tu->Unit->getFileManager());
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
IntrusiveRefCntPtr<DiagnosticsEngine> Diag(new DiagnosticsEngine(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts));
// StoreDiags Diags;
// IntrusiveRefCntPtr<DiagnosticsEngine> DiagE =
// CompilerInstance::createDiagnostics(DiagOpts.get(), &Diags, false);
IntrusiveRefCntPtr<SourceManager> SrcMgr(
new SourceManager(*Diag, *FileMgr));
std::vector<ASTUnit::RemappedFile> Remapped = GetRemapped(snapshot);
SmallVector<StoredDiagnostic, 8> Diagnostics;
SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
CodeCompleteOptions Opts;
LangOptions LangOpts;
Opts.IncludeBriefComments = true;
Opts.LoadExternal = false;
Opts.IncludeFixIts = true;
CaptureCompletionResults capture(Opts);
tu->Unit->CodeComplete(session->file.filename, request->position.line + 1,
request->position.character + 1, Remapped,
/*IncludeMacros=*/true,
/*IncludeCodePatterns=*/false,
/*IncludeBriefComments=*/true, capture, tu->PCHCO,
*Diag, LangOpts, *SrcMgr, *FileMgr, Diagnostics,
TemporaryBuffers);
request->on_complete(capture.ls_items, false /*is_cached_result*/);
// completion_manager->on_diagnostic_(session->file.filename, Diags.take());
}
}
}
2018-04-14 16:52:17 +00:00
void DiagnosticQueryMain(ClangCompleteManager* completion_manager) {
while (true) {
// Fetching the completion request blocks until we have a request.
ClangCompleteManager::DiagnosticRequest request =
completion_manager->diagnostic_request_.Dequeue();
2018-05-09 05:01:58 +00:00
if (!g_config->diagnostics.onType)
continue;
2018-04-14 16:52:17 +00:00
std::string path = request.document.uri.GetPath();
2018-04-14 16:52:17 +00:00
std::shared_ptr<CompletionSession> session =
completion_manager->TryGetSession(path, true /*mark_as_completion*/,
true /*create_if_needed*/);
2018-04-14 16:52:17 +00:00
// At this point, we must have a translation unit. Block until we have one.
std::lock_guard<std::mutex> lock(session->diagnostics.lock);
TryEnsureDocumentParsed(completion_manager, session,
&session->diagnostics.tu,
false /*emit_diagnostics*/);
2018-04-14 16:52:17 +00:00
// It is possible we failed to create the document despite
// |TryEnsureDocumentParsed|.
ClangTranslationUnit* tu = session->diagnostics.tu.get();
if (!tu)
2018-04-14 16:52:17 +00:00
continue;
2017-09-22 01:14:57 +00:00
2018-04-14 16:52:17 +00:00
WorkingFiles::Snapshot snapshot =
completion_manager->working_files_->AsSnapshot({StripFileType(path)});
llvm::CrashRecoveryContext CRC;
if (tu->Reparse(CRC, snapshot)) {
2018-04-14 16:52:17 +00:00
LOG_S(ERROR) << "Reparsing translation unit for diagnostics failed for "
<< path;
continue;
}
auto &SM = tu->Unit->getSourceManager();
auto &LangOpts = tu->Unit->getLangOpts();
std::vector<lsDiagnostic> ls_diags;
for (ASTUnit::stored_diag_iterator I = tu->Unit->stored_diag_begin(),
E = tu->Unit->stored_diag_end();
I != E; ++I) {
FullSourceLoc FLoc = I->getLocation();
SourceRange R;
for (const auto &CR : I->getRanges()) {
auto RT = Lexer::makeFileCharRange(CR, SM, LangOpts);
if (SM.isPointWithin(FLoc, RT.getBegin(), RT.getEnd())) {
R = CR.getAsRange();
break;
}
}
Range r = R.isValid() ? FromCharRange(SM, LangOpts, R)
: FromTokenRange(SM, LangOpts, {FLoc, FLoc});
lsDiagnostic ls_diag;
ls_diag.range =
lsRange{{r.start.line, r.start.column}, {r.end.line, r.end.column}};
switch (I->getLevel()) {
case DiagnosticsEngine::Ignored:
// llvm_unreachable
break;
case DiagnosticsEngine::Note:
case DiagnosticsEngine::Remark:
ls_diag.severity = lsDiagnosticSeverity::Information;
break;
case DiagnosticsEngine::Warning:
ls_diag.severity = lsDiagnosticSeverity::Warning;
break;
case DiagnosticsEngine::Error:
case DiagnosticsEngine::Fatal:
ls_diag.severity = lsDiagnosticSeverity::Error;
}
ls_diag.message = I->getMessage().str();
for (const FixItHint &FixIt : I->getFixIts()) {
lsTextEdit edit;
edit.newText = FixIt.CodeToInsert;
r = FromCharRange(SM, LangOpts, FixIt.RemoveRange.getAsRange());
edit.range =
lsRange{{r.start.line, r.start.column}, {r.end.line, r.end.column}};
ls_diag.fixits_.push_back(edit);
}
ls_diags.push_back(ls_diag);
2018-04-14 16:52:17 +00:00
}
completion_manager->on_diagnostic_(path, ls_diags);
2018-04-14 16:52:17 +00:00
}
}
} // namespace
2018-04-04 06:05:41 +00:00
ClangCompleteManager::ClangCompleteManager(Project* project,
2017-09-22 01:14:57 +00:00
WorkingFiles* working_files,
2017-09-27 06:03:43 +00:00
OnDiagnostic on_diagnostic,
OnDropped on_dropped)
2018-04-04 06:05:41 +00:00
: project_(project),
2017-09-22 01:14:57 +00:00
working_files_(working_files),
on_diagnostic_(on_diagnostic),
on_dropped_(on_dropped),
preloaded_sessions_(kMaxPreloadedSessions),
completion_sessions_(kMaxCompletionSessions) {
2018-04-16 19:36:02 +00:00
std::thread([&]() {
2018-05-27 19:24:56 +00:00
set_thread_name("comp-query");
CompletionQueryMain(this);
2018-04-16 19:36:02 +00:00
}).detach();
std::thread([&]() {
2018-05-27 19:24:56 +00:00
set_thread_name("comp-preload");
2018-04-14 16:52:17 +00:00
CompletionPreloadMain(this);
2018-04-16 19:36:02 +00:00
}).detach();
std::thread([&]() {
2018-05-27 19:24:56 +00:00
set_thread_name("diag-query");
2018-04-14 16:52:17 +00:00
DiagnosticQueryMain(this);
2018-04-16 19:36:02 +00:00
}).detach();
}
2017-09-22 01:14:57 +00:00
void ClangCompleteManager::CodeComplete(
const lsRequestId& id,
2017-09-22 01:14:57 +00:00
const lsTextDocumentPositionParams& completion_location,
const OnComplete& on_complete) {
completion_request_.PushBack(std::make_unique<CompletionRequest>(
id, completion_location.textDocument, completion_location.position,
2018-04-14 16:52:17 +00:00
on_complete));
}
void ClangCompleteManager::DiagnosticsUpdate(
const lsTextDocumentIdentifier& document) {
2018-04-14 16:52:17 +00:00
bool has = false;
diagnostic_request_.Iterate([&](const DiagnosticRequest& request) {
if (request.document.uri == document.uri)
has = true;
});
if (!has)
diagnostic_request_.PushBack(DiagnosticRequest{document},
true /*priority*/);
}
void ClangCompleteManager::NotifyView(const std::string& filename) {
//
// On view, we reparse only if the file has not been parsed. The existence of
// a CompletionSession instance implies the file is already parsed or will be
// parsed soon.
//
// Only reparse the file if we create a new CompletionSession.
if (EnsureCompletionOrCreatePreloadSession(filename))
2018-04-14 16:52:17 +00:00
preload_requests_.PushBack(PreloadRequest(filename), true);
2017-03-28 01:47:12 +00:00
}
void ClangCompleteManager::NotifyEdit(const std::string& filename) {
//
// We treat an edit like a view, because the completion logic will handle
// moving the CompletionSession instance from preloaded to completion
// storage.
//
NotifyView(filename);
}
void ClangCompleteManager::NotifySave(const std::string& filename) {
//
// On save, always reparse.
//
EnsureCompletionOrCreatePreloadSession(filename);
2018-04-14 16:52:17 +00:00
preload_requests_.PushBack(PreloadRequest(filename), true);
}
void ClangCompleteManager::NotifyClose(const std::string& filename) {
//
// On close, we clear any existing CompletionSession instance.
//
std::lock_guard<std::mutex> lock(sessions_lock_);
// Take and drop. It's okay if we don't actually drop the file, it'll
// eventually get pushed out of the caches as the user opens other files.
auto preloaded_ptr = preloaded_sessions_.TryTake(filename);
LOG_IF_S(INFO, !!preloaded_ptr)
<< "Dropped preloaded-based code completion session for " << filename;
auto completion_ptr = completion_sessions_.TryTake(filename);
LOG_IF_S(INFO, !!completion_ptr)
<< "Dropped completion-based code completion session for " << filename;
// We should never have both a preloaded and completion session.
assert((preloaded_ptr && completion_ptr) == false);
}
bool ClangCompleteManager::EnsureCompletionOrCreatePreloadSession(
const std::string& filename) {
std::lock_guard<std::mutex> lock(sessions_lock_);
// Check for an existing CompletionSession.
if (preloaded_sessions_.TryGet(filename) ||
completion_sessions_.TryGet(filename)) {
return false;
2017-10-23 15:39:33 +00:00
}
// No CompletionSession, create new one.
auto session = std::make_shared<CompletionSession>(
project_->FindCompilationEntryForFile(filename), working_files_);
preloaded_sessions_.Insert(session->file.filename, session);
return true;
}
std::shared_ptr<CompletionSession> ClangCompleteManager::TryGetSession(
2017-09-22 01:14:57 +00:00
const std::string& filename,
bool mark_as_completion,
2017-09-22 01:14:57 +00:00
bool create_if_needed) {
std::lock_guard<std::mutex> lock(sessions_lock_);
// Try to find a preloaded session.
std::shared_ptr<CompletionSession> preloaded_session =
preloaded_sessions_.TryGet(filename);
if (preloaded_session) {
// If this request is for a completion, we should move it to
// |completion_sessions|.
if (mark_as_completion) {
preloaded_sessions_.TryTake(filename);
completion_sessions_.Insert(filename, preloaded_session);
}
return preloaded_session;
}
2017-06-10 01:02:48 +00:00
// Try to find a completion session. If none create one.
std::shared_ptr<CompletionSession> completion_session =
completion_sessions_.TryGet(filename);
if (!completion_session && create_if_needed) {
completion_session = std::make_shared<CompletionSession>(
project_->FindCompilationEntryForFile(filename), working_files_);
completion_sessions_.Insert(filename, completion_session);
}
2017-06-10 01:02:48 +00:00
return completion_session;
2017-06-10 01:02:48 +00:00
}
void ClangCompleteManager::FlushSession(const std::string& filename) {
std::lock_guard<std::mutex> lock(sessions_lock_);
preloaded_sessions_.TryTake(filename);
completion_sessions_.TryTake(filename);
}
void ClangCompleteManager::FlushAllSessions() {
2018-05-27 19:24:56 +00:00
LOG_S(INFO) << "flush all clang complete sessions";
std::lock_guard<std::mutex> lock(sessions_lock_);
2018-05-27 19:24:56 +00:00
preloaded_sessions_.Clear();
completion_sessions_.Clear();
}
2018-03-31 08:01:32 +00:00
void CodeCompleteCache::WithLock(std::function<void()> action) {
std::lock_guard<std::mutex> lock(mutex_);
action();
}
bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) {
std::lock_guard<std::mutex> lock(mutex_);
return cached_path_ == position.textDocument.uri.GetPath() &&
cached_completion_position_ == position.position;
}