Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ CodeAction toCodeAction(const Fix &F, const URIForFile &File,
Edit.textDocument = VersionedTextDocumentIdentifier{{File}, Version};
for (const auto &E : F.Edits)
Edit.edits.push_back(
{E.range, E.newText,
SupportChangeAnnotation ? E.annotationId : ""});
{E.range, E.newText, SupportChangeAnnotation ? E.annotationId : ""});
if (SupportChangeAnnotation) {
for (const auto &[AID, Annotation]: F.Annotations)
for (const auto &[AID, Annotation] : F.Annotations)
Action.edit->changeAnnotations[AID] = Annotation;
}
}
Expand Down Expand Up @@ -909,24 +908,24 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
if (!Server->getDraft(File))
return Reply(llvm::make_error<LSPError>(
"onRename called for non-added file", ErrorCode::InvalidParams));
Server->rename(File, Params.position, Params.newName, Opts.Rename,
[File, Params, Reply = std::move(Reply),
this](llvm::Expected<RenameResult> R) mutable {
if (!R)
return Reply(R.takeError());
if (auto Err = validateEdits(*Server, R->GlobalChanges))
return Reply(std::move(Err));
WorkspaceEdit Result;
// FIXME: use documentChanges if SupportDocumentChanges is
// true.
Result.changes.emplace();
for (const auto &Rep : R->GlobalChanges) {
(*Result
.changes)[URI::createFile(Rep.first()).toString()] =
Rep.second.asTextEdits();
}
Reply(Result);
});
Server->rename(
File, Params.position, Params.newName, Opts.Rename,
[File, Params, Reply = std::move(Reply),
this](llvm::Expected<RenameResult> R) mutable {
if (!R)
return Reply(R.takeError());
if (auto Err = validateEdits(*Server, R->GlobalChanges))
return Reply(std::move(Err));
WorkspaceEdit Result;
// FIXME: use documentChanges if SupportDocumentChanges is
// true.
Result.changes.emplace();
for (const auto &Rep : R->GlobalChanges) {
(*Result.changes)[URI::createFile(Rep.first()).toString()] =
Rep.second.asTextEdits();
}
Reply(Result);
});
}

void ClangdLSPServer::onDocumentDidClose(
Expand Down Expand Up @@ -1070,7 +1069,7 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
std::map<ClangdServer::DiagRef, clangd::Diagnostic> ToLSPDiags;
ClangdServer::CodeActionInputs Inputs;

for (const auto& LSPDiag : Params.context.diagnostics) {
for (const auto &LSPDiag : Params.context.diagnostics) {
if (auto DiagRef = getDiagRef(File.file(), LSPDiag)) {
ToLSPDiags[*DiagRef] = LSPDiag;
Inputs.Diagnostics.push_back(*DiagRef);
Expand All @@ -1079,13 +1078,9 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
Inputs.File = File.file();
Inputs.Selection = Params.range;
Inputs.RequestedActionKinds = Params.context.only;
Inputs.TweakFilter = [this](const Tweak &T) {
return Opts.TweakFilter(T);
};
auto CB = [this,
Reply = std::move(Reply),
ToLSPDiags = std::move(ToLSPDiags), File,
Selection = Params.range](
Inputs.TweakFilter = [this](const Tweak &T) { return Opts.TweakFilter(T); };
auto CB = [this, Reply = std::move(Reply), ToLSPDiags = std::move(ToLSPDiags),
File, Selection = Params.range](
llvm::Expected<ClangdServer::CodeActionResult> Fixits) mutable {
if (!Fixits)
return Reply(Fixits.takeError());
Expand All @@ -1094,8 +1089,7 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
for (const auto &QF : Fixits->QuickFixes) {
CAs.push_back(toCodeAction(QF.F, File, Version, SupportsDocumentChanges,
SupportsChangeAnnotation));
if (auto It = ToLSPDiags.find(QF.Diag);
It != ToLSPDiags.end()) {
if (auto It = ToLSPDiags.find(QF.Diag); It != ToLSPDiags.end()) {
CAs.back().diagnostics = {It->second};
}
}
Expand Down Expand Up @@ -1356,7 +1350,8 @@ void ClangdLSPServer::onResolveTypeHierarchy(
}
Reply(serializeTHIForExtension(std::move(**Resp)));
};
Server->resolveTypeHierarchy(Params.item, Params.resolve, Params.direction,
Server->resolveTypeHierarchy(Params.item.uri.file(), Params.item,
Params.resolve, Params.direction,
std::move(Serialize));
}

Expand All @@ -1370,13 +1365,13 @@ void ClangdLSPServer::onPrepareTypeHierarchy(
void ClangdLSPServer::onSuperTypes(
const ResolveTypeHierarchyItemParams &Params,
Callback<std::optional<std::vector<TypeHierarchyItem>>> Reply) {
Server->superTypes(Params.item, std::move(Reply));
Server->superTypes(Params.item.uri.file(), Params.item, std::move(Reply));
}

void ClangdLSPServer::onSubTypes(
const ResolveTypeHierarchyItemParams &Params,
Callback<std::vector<TypeHierarchyItem>> Reply) {
Server->subTypes(Params.item, std::move(Reply));
Server->subTypes(Params.item.uri.file(), Params.item, std::move(Reply));
}

void ClangdLSPServer::onPrepareCallHierarchy(
Expand All @@ -1389,7 +1384,7 @@ void ClangdLSPServer::onPrepareCallHierarchy(
void ClangdLSPServer::onCallHierarchyIncomingCalls(
const CallHierarchyIncomingCallsParams &Params,
Callback<std::vector<CallHierarchyIncomingCall>> Reply) {
Server->incomingCalls(Params.item, std::move(Reply));
Server->incomingCalls(Params.item.uri.file(), Params.item, std::move(Reply));
}

void ClangdLSPServer::onClangdInlayHints(const InlayHintsParams &Params,
Expand Down Expand Up @@ -1434,7 +1429,7 @@ void ClangdLSPServer::onInlayHint(const InlayHintsParams &Params,
void ClangdLSPServer::onCallHierarchyOutgoingCalls(
const CallHierarchyOutgoingCallsParams &Params,
Callback<std::vector<CallHierarchyOutgoingCall>> Reply) {
Server->outgoingCalls(Params.item, std::move(Reply));
Server->outgoingCalls(Params.item.uri.file(), Params.item, std::move(Reply));
}

void ClangdLSPServer::applyConfiguration(
Expand Down
11 changes: 5 additions & 6 deletions clang-tools-extra/clangd/ClangdLSPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
Callback<std::vector<Location>>);
void onGoToImplementation(const TextDocumentPositionParams &,
Callback<std::vector<Location>>);
void onReference(const ReferenceParams &, Callback<std::vector<ReferenceLocation>>);
void onReference(const ReferenceParams &,
Callback<std::vector<ReferenceLocation>>);
void onSwitchSourceHeader(const TextDocumentIdentifier &,
Callback<std::optional<URIForFile>>);
void onDocumentHighlight(const TextDocumentPositionParams &,
Expand Down Expand Up @@ -243,7 +244,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
/// Used to indicate the ClangdLSPServer is being destroyed.
std::atomic<bool> IsBeingDestroyed = {false};

// FIXME: The caching is a temporary solution to get corresponding clangd
// FIXME: The caching is a temporary solution to get corresponding clangd
// diagnostic from a LSP diagnostic.
// Ideally, ClangdServer can generate an identifier for each diagnostic,
// emit them via the LSP's data field (which was newly added in LSP 3.16).
Expand All @@ -259,11 +260,9 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
return {LSPDiag.range, LSPDiag.message};
}
/// A map from LSP diagnostic to clangd-naive diagnostic.
typedef std::map<DiagKey, ClangdServer::DiagRef>
DiagnosticToDiagRefMap;
typedef std::map<DiagKey, ClangdServer::DiagRef> DiagnosticToDiagRefMap;
/// Caches the mapping LSP and clangd-naive diagnostics per file.
llvm::StringMap<DiagnosticToDiagRefMap>
DiagRefMap;
llvm::StringMap<DiagnosticToDiagRefMap> DiagRefMap;

// Last semantic-tokens response, for incremental requests.
std::mutex SemanticTokensMutex;
Expand Down
73 changes: 46 additions & 27 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ namespace clangd {
namespace {

// Tracks number of times a tweak has been offered.
static constexpr trace::Metric TweakAvailable(
"tweak_available", trace::Metric::Counter, "tweak_id");
static constexpr trace::Metric
TweakAvailable("tweak_available", trace::Metric::Counter, "tweak_id");

// Update the FileIndex with new ASTs and plumb the diagnostics responses.
struct UpdateIndexCallbacks : public ParsingCallbacks {
Expand Down Expand Up @@ -872,29 +872,42 @@ void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
}

void ClangdServer::superTypes(
const TypeHierarchyItem &Item,
PathRef File, const TypeHierarchyItem &Item,
Callback<std::optional<std::vector<TypeHierarchyItem>>> CB) {
WorkScheduler->run("typeHierarchy/superTypes", /*Path=*/"",
[=, CB = std::move(CB)]() mutable {
CB(clangd::superTypes(Item, Index));
});
auto Action = [File = File.str(), Item, CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::superTypes(Item, Index, InpAST->AST));
};
WorkScheduler->runWithAST("superTypes Calls", File, std::move(Action));
}

void ClangdServer::subTypes(const TypeHierarchyItem &Item,
void ClangdServer::subTypes(PathRef File, const TypeHierarchyItem &Item,
Callback<std::vector<TypeHierarchyItem>> CB) {
WorkScheduler->run(
"typeHierarchy/subTypes", /*Path=*/"",
[=, CB = std::move(CB)]() mutable { CB(clangd::subTypes(Item, Index)); });

auto Action = [File = File.str(), Item, CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::subTypes(Item, Index, InpAST->AST));
};
WorkScheduler->runWithAST("subTypes Calls", File, std::move(Action));
}

void ClangdServer::resolveTypeHierarchy(
TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
PathRef File, TypeHierarchyItem Item, int Resolve,
TypeHierarchyDirection Direction,
Callback<std::optional<TypeHierarchyItem>> CB) {
WorkScheduler->run(
"Resolve Type Hierarchy", "", [=, CB = std::move(CB)]() mutable {
clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
CB(Item);
});
auto Action = [Item = std::move(Item), Resolve, Direction, CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
if (!InpAST)
return CB(InpAST.takeError());
clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index, InpAST->AST);
CB(Item);
};
WorkScheduler->runWithAST("resolveTypeHierarchy Calls", File,
std::move(Action));
}

void ClangdServer::prepareCallHierarchy(
Expand All @@ -909,12 +922,15 @@ void ClangdServer::prepareCallHierarchy(
}

void ClangdServer::incomingCalls(
const CallHierarchyItem &Item,
PathRef File, const CallHierarchyItem &Item,
Callback<std::vector<CallHierarchyIncomingCall>> CB) {
WorkScheduler->run("Incoming Calls", "",
[CB = std::move(CB), Item, this]() mutable {
CB(clangd::incomingCalls(Item, Index));
});
auto Action = [Item, CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::incomingCalls(Item, Index, InpAST->AST));
};
WorkScheduler->runWithAST("Incoming Calls", File, std::move(Action));
}

void ClangdServer::inlayHints(PathRef File, std::optional<Range> RestrictRange,
Expand All @@ -929,12 +945,15 @@ void ClangdServer::inlayHints(PathRef File, std::optional<Range> RestrictRange,
}

void ClangdServer::outgoingCalls(
const CallHierarchyItem &Item,
PathRef File, const CallHierarchyItem &Item,
Callback<std::vector<CallHierarchyOutgoingCall>> CB) {
WorkScheduler->run("Outgoing Calls", "",
[CB = std::move(CB), Item, this]() mutable {
CB(clangd::outgoingCalls(Item, Index));
});
auto Action = [Item, CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::outgoingCalls(Item, Index, InpAST->AST));
};
WorkScheduler->runWithAST("Outgoing Calls", File, std::move(Action));
}

void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
Expand Down
12 changes: 6 additions & 6 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ class ClangdServer {
TypeHierarchyDirection Direction,
Callback<std::vector<TypeHierarchyItem>> CB);
/// Get direct parents of a type hierarchy item.
void superTypes(const TypeHierarchyItem &Item,
void superTypes(PathRef File, const TypeHierarchyItem &Item,
Callback<std::optional<std::vector<TypeHierarchyItem>>> CB);
/// Get direct children of a type hierarchy item.
void subTypes(const TypeHierarchyItem &Item,
void subTypes(PathRef File, const TypeHierarchyItem &Item,
Callback<std::vector<TypeHierarchyItem>> CB);

/// Resolve type hierarchy item in the given direction.
void resolveTypeHierarchy(TypeHierarchyItem Item, int Resolve,
void resolveTypeHierarchy(PathRef File, TypeHierarchyItem Item, int Resolve,
TypeHierarchyDirection Direction,
Callback<std::optional<TypeHierarchyItem>> CB);

Expand All @@ -299,11 +299,11 @@ class ClangdServer {
Callback<std::vector<CallHierarchyItem>> CB);

/// Resolve incoming calls for a given call hierarchy item.
void incomingCalls(const CallHierarchyItem &Item,
Callback<std::vector<CallHierarchyIncomingCall>>);
void incomingCalls(PathRef File, const CallHierarchyItem &Item,
Callback<std::vector<CallHierarchyIncomingCall>> CB);

/// Resolve outgoing calls for a given call hierarchy item.
void outgoingCalls(const CallHierarchyItem &Item,
void outgoingCalls(PathRef File, const CallHierarchyItem &Item,
Callback<std::vector<CallHierarchyOutgoingCall>>);

/// Resolve inlay hints for a given document.
Expand Down
Loading