clang-tools
11.0.0
|
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware features such as code completion. More...
#include <ClangdServer.h>
Classes | |
class | Callbacks |
Interface with hooks for users of ClangdServer to be notified of events. More... | |
struct | Options |
struct | TweakRef |
Public Member Functions | |
ClangdServer (const GlobalCompilationDatabase &CDB, const ThreadsafeFS &TFS, const Options &Opts, Callbacks *Callbacks=nullptr) | |
Creates a new ClangdServer instance. More... | |
void | addDocument (PathRef File, StringRef Contents, llvm::StringRef Version="null", WantDiagnostics WD=WantDiagnostics::Auto, bool ForceRebuild=false) |
Add a File to the list of tracked C++ files or update the contents if File is already tracked. More... | |
void | removeDocument (PathRef File) |
Remove File from list of tracked files, schedule a request to free resources associated with it. More... | |
void | codeComplete (PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, Callback< CodeCompleteResult > CB) |
Run code completion for File at Pos . More... | |
void | signatureHelp (PathRef File, Position Pos, Callback< SignatureHelp > CB) |
Provide signature help for File at Pos . More... | |
void | locateSymbolAt (PathRef File, Position Pos, Callback< std::vector< LocatedSymbol >> CB) |
Find declaration/definition locations of symbol at a specified position. More... | |
void | switchSourceHeader (PathRef Path, Callback< llvm::Optional< clangd::Path >> CB) |
Switch to a corresponding source file when given a header file, and vice versa. More... | |
void | findDocumentHighlights (PathRef File, Position Pos, Callback< std::vector< DocumentHighlight >> CB) |
Get document highlights for a given position. More... | |
void | findHover (PathRef File, Position Pos, Callback< llvm::Optional< HoverInfo >> CB) |
Get code hover for a given position. More... | |
void | typeHierarchy (PathRef File, Position Pos, int Resolve, TypeHierarchyDirection Direction, Callback< llvm::Optional< TypeHierarchyItem >> CB) |
Get information about type hierarchy for a given position. More... | |
void | resolveTypeHierarchy (TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction, Callback< llvm::Optional< TypeHierarchyItem >> CB) |
Resolve type hierarchy item in the given direction. More... | |
void | workspaceSymbols (StringRef Query, int Limit, Callback< std::vector< SymbolInformation >> CB) |
Retrieve the top symbols from the workspace matching a query. More... | |
void | documentSymbols (StringRef File, Callback< std::vector< DocumentSymbol >> CB) |
Retrieve the symbols within the specified file. More... | |
void | foldingRanges (StringRef File, Callback< std::vector< FoldingRange >> CB) |
Retrieve ranges that can be used to fold code within the specified file. More... | |
void | findReferences (PathRef File, Position Pos, uint32_t Limit, Callback< ReferencesResult > CB) |
Retrieve locations for symbol references. More... | |
void | formatRange (PathRef File, StringRef Code, Range Rng, Callback< tooling::Replacements > CB) |
Run formatting for Rng inside File with content Code . More... | |
void | formatFile (PathRef File, StringRef Code, Callback< tooling::Replacements > CB) |
Run formatting for the whole File with content Code . More... | |
void | formatOnType (PathRef File, StringRef Code, Position Pos, StringRef TriggerText, Callback< std::vector< TextEdit >> CB) |
Run formatting after TriggerText was typed at Pos in File with content Code . More... | |
void | prepareRename (PathRef File, Position Pos, const RenameOptions &RenameOpts, Callback< llvm::Optional< Range >> CB) |
Test the validity of a rename operation. More... | |
void | rename (PathRef File, Position Pos, llvm::StringRef NewName, const RenameOptions &Opts, Callback< FileEdits > CB) |
Rename all occurrences of the symbol at the Pos in File to NewName . More... | |
void | enumerateTweaks (PathRef File, Range Sel, Callback< std::vector< TweakRef >> CB) |
Enumerate the code tweaks available to the user at a specified point. More... | |
void | applyTweak (PathRef File, Range Sel, StringRef ID, Callback< Tweak::Effect > CB) |
Apply the code tweak with a specified ID . More... | |
void | dumpAST (PathRef File, llvm::unique_function< void(std::string)> Callback) |
Only for testing purposes. More... | |
void | onFileEvent (const DidChangeWatchedFilesParams &Params) |
Called when an event occurs for a watched file in the workspace. More... | |
void | symbolInfo (PathRef File, Position Pos, Callback< std::vector< SymbolDetails >> CB) |
Get symbol info for given position. More... | |
void | semanticRanges (PathRef File, const std::vector< Position > &Pos, Callback< std::vector< SelectionRange >> CB) |
Get semantic ranges around a specified position in a file. More... | |
void | documentLinks (PathRef File, Callback< std::vector< DocumentLink >> CB) |
Get all document links in a file. More... | |
void | semanticHighlights (PathRef File, Callback< std::vector< HighlightingToken >>) |
llvm::StringMap< TUScheduler::FileStats > | fileStats () const |
Returns estimated memory usage and other statistics for each of the currently open files. More... | |
LLVM_NODISCARD bool | blockUntilIdleForTest (llvm::Optional< double > TimeoutSeconds=10) |
Static Public Member Functions | |
static Options | optsForTest () |
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware features such as code completion.
The primary client is ClangdLSPServer which exposes these features via the Language Server protocol. ClangdServer may also be embedded directly, though its API is not stable over time.
ClangdServer should be used from a single thread. Many potentially-slow operations have asynchronous APIs and deliver their results on another thread. Such operations support cancellation: if the caller sets up a cancelable context, many operations will notice cancellation and fail early. (ClangdLSPServer uses this to implement $/cancelRequest).
Definition at line 63 of file ClangdServer.h.
clang::clangd::ClangdServer::ClangdServer | ( | const GlobalCompilationDatabase & | CDB, |
const ThreadsafeFS & | TFS, | ||
const Options & | Opts, | ||
Callbacks * | Callbacks = nullptr |
||
) |
Creates a new ClangdServer instance.
ClangdServer uses CDB
to obtain compilation arguments for parsing. Note that ClangdServer only obtains compilation arguments once for each newly added file (i.e., when processing a first call to addDocument) and reuses those arguments for subsequent reparses. However, ClangdServer will check if compilation arguments changed on calls to forceReparse().
Definition at line 134 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::addDocument | ( | PathRef | File, |
StringRef | Contents, | ||
llvm::StringRef | Version = "null" , |
||
WantDiagnostics | WD = WantDiagnostics::Auto , |
||
bool | ForceRebuild = false |
||
) |
Add a File
to the list of tracked C++ files or update the contents if File
is already tracked.
Also schedules parsing of the AST for it on a separate thread. When the parsing is complete, DiagConsumer passed in constructor will receive onDiagnosticsReady callback. Version identifies this snapshot and is propagated to ASTs, preambles, diagnostics etc built from it.
Definition at line 190 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::applyTweak | ( | PathRef | File, |
Range | Sel, | ||
StringRef | ID, | ||
Callback< Tweak::Effect > | CB | ||
) |
Apply the code tweak with a specified ID
.
Definition at line 487 of file ClangdServer.cpp.
References Action, clang::clangd::trace::Metric::Counter, E, clang::clangd::File, FormatStyle(), clang::clangd::prepareTweak(), and clang::clangd::tweakSelection().
LLVM_NODISCARD bool clang::clangd::ClangdServer::blockUntilIdleForTest | ( | llvm::Optional< double > | TimeoutSeconds = 10 | ) |
Definition at line 790 of file ClangdServer.cpp.
References clang::clangd::TUScheduler::blockUntilIdle(), and clang::clangd::timeoutSeconds().
void clang::clangd::ClangdServer::codeComplete | ( | PathRef | File, |
Position | Pos, | ||
const clangd::CodeCompleteOptions & | Opts, | ||
Callback< CodeCompleteResult > | CB | ||
) |
Run code completion for File
at Pos
.
Request is processed asynchronously.
This method should only be called for currently tracked files. However, it is safe to call removeDocument for File
after this method returns, even while returned future is not yet ready. A version of codeComplete
that runs Callback
on the processing thread when codeComplete results become available.
Definition at line 219 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::documentLinks | ( | PathRef | File, |
Callback< std::vector< DocumentLink >> | CB | ||
) |
Get all document links in a file.
Definition at line 733 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getDocumentLinks(), clang::clangd::TUScheduler::InvalidateOnUpdate, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::documentSymbols | ( | StringRef | File, |
Callback< std::vector< DocumentSymbol >> | CB | ||
) |
Retrieve the symbols within the specified file.
Definition at line 666 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getDocumentSymbols(), clang::clangd::TUScheduler::InvalidateOnUpdate, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::dumpAST | ( | PathRef | File, |
llvm::unique_function< void(std::string)> | Callback | ||
) |
Only for testing purposes.
Waits until all requests to worker thread are finished and dumps AST for File
. File
must be in the list of added documents.
Definition at line 528 of file ClangdServer.cpp.
References Action.
void clang::clangd::ClangdServer::enumerateTweaks | ( | PathRef | File, |
Range | Sel, | ||
Callback< std::vector< TweakRef >> | CB | ||
) |
Enumerate the code tweaks available to the user at a specified point.
Definition at line 454 of file ClangdServer.cpp.
References Action, clang::clangd::trace::Metric::Counter, clang::clangd::File, and clang::clangd::tweakSelection().
llvm::StringMap< TUScheduler::FileStats > clang::clangd::ClangdServer::fileStats | ( | ) | const |
Returns estimated memory usage and other statistics for each of the currently open files.
Overall memory usage of clangd may be significantly more than reported here, as this metric does not account (at least) for:
Definition at line 757 of file ClangdServer.cpp.
References clang::clangd::TUScheduler::fileStats().
void clang::clangd::ClangdServer::findDocumentHighlights | ( | PathRef | File, |
Position | Pos, | ||
Callback< std::vector< DocumentHighlight >> | CB | ||
) |
Get document highlights for a given position.
Definition at line 601 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::findDocumentHighlights(), clang::clangd::TUScheduler::InvalidateOnUpdate, Pos, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::findHover | ( | PathRef | File, |
Position | Pos, | ||
Callback< llvm::Optional< HoverInfo >> | CB | ||
) |
Get code hover for a given position.
Definition at line 614 of file ClangdServer.cpp.
References Action, clang::clangd::File, FormatStyle(), and Pos.
void clang::clangd::ClangdServer::findReferences | ( | PathRef | File, |
Position | Pos, | ||
uint32_t | Limit, | ||
Callback< ReferencesResult > | CB | ||
) |
Retrieve locations for symbol references.
Definition at line 690 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::foldingRanges | ( | StringRef | File, |
Callback< std::vector< FoldingRange >> | CB | ||
) |
Retrieve ranges that can be used to fold code within the specified file.
Definition at line 678 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getFoldingRanges(), clang::clangd::TUScheduler::InvalidateOnUpdate, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::formatFile | ( | PathRef | File, |
StringRef | Code, | ||
Callback< tooling::Replacements > | CB | ||
) |
Run formatting for the whole File
with content Code
.
Definition at line 321 of file ClangdServer.cpp.
References Code, clang::clangd::File, and Range.
void clang::clangd::ClangdServer::formatOnType | ( | PathRef | File, |
StringRef | Code, | ||
Position | Pos, | ||
StringRef | TriggerText, | ||
Callback< std::vector< TextEdit >> | CB | ||
) |
Run formatting after TriggerText
was typed at Pos
in File
with content Code
.
Definition at line 327 of file ClangdServer.cpp.
References Action, Code, clang::clangd::File, Pos, and clang::clangd::positionToOffset().
void clang::clangd::ClangdServer::formatRange | ( | PathRef | File, |
StringRef | Code, | ||
Range | Rng, | ||
Callback< tooling::Replacements > | CB | ||
) |
Run formatting for Rng
inside File
with content Code
.
Definition at line 309 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::locateSymbolAt | ( | PathRef | File, |
Position | Pos, | ||
Callback< std::vector< LocatedSymbol >> | CB | ||
) |
Find declaration/definition locations of symbol at a specified position.
Definition at line 548 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::locateSymbolAt(), Pos, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::onFileEvent | ( | const DidChangeWatchedFilesParams & | Params | ) |
Called when an event occurs for a watched file in the workspace.
Definition at line 650 of file ClangdServer.cpp.
|
static |
Definition at line 114 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::prepareRename | ( | PathRef | File, |
Position | Pos, | ||
const RenameOptions & | RenameOpts, | ||
Callback< llvm::Optional< Range >> | CB | ||
) |
Test the validity of a rename operation.
Definition at line 351 of file ClangdServer.cpp.
References Action, clang::clangd::AST, clang::clangd::File, Pos, and RenameOpts.
void clang::clangd::ClangdServer::removeDocument | ( | PathRef | File | ) |
Remove File
from list of tracked files, schedule a request to free resources associated with it.
Pending diagnostics for closed files may not be delivered, even if requested with WantDiags::Auto or WantDiags::Yes. An empty set of diagnostics will be delivered, with Version = "".
Definition at line 217 of file ClangdServer.cpp.
References clang::clangd::File, and clang::clangd::TUScheduler::remove().
void clang::clangd::ClangdServer::rename | ( | PathRef | File, |
Position | Pos, | ||
llvm::StringRef | NewName, | ||
const RenameOptions & | Opts, | ||
Callback< FileEdits > | CB | ||
) |
Rename all occurrences of the symbol at the Pos
in File
to NewName
.
If WantFormat is false, the final TextEdit will be not formatted, embedders could use this method to get all occurrences of the symbol (e.g. highlighting them in prepare stage).
Definition at line 391 of file ClangdServer.cpp.
void clang::clangd::ClangdServer::resolveTypeHierarchy | ( | TypeHierarchyItem | Item, |
int | Resolve, | ||
TypeHierarchyDirection | Direction, | ||
Callback< llvm::Optional< TypeHierarchyItem >> | CB | ||
) |
Resolve type hierarchy item in the given direction.
Definition at line 643 of file ClangdServer.cpp.
References clang::clangd::resolveTypeHierarchy().
void clang::clangd::ClangdServer::semanticHighlights | ( | PathRef | File, |
Callback< std::vector< HighlightingToken >> | CB | ||
) |
Definition at line 745 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getSemanticHighlightings(), clang::clangd::TUScheduler::InvalidateOnUpdate, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::semanticRanges | ( | PathRef | File, |
const std::vector< Position > & | Pos, | ||
Callback< std::vector< SelectionRange >> | CB | ||
) |
Get semantic ranges around a specified position in a file.
Definition at line 714 of file ClangdServer.cpp.
References Action.
void clang::clangd::ClangdServer::signatureHelp | ( | PathRef | File, |
Position | Pos, | ||
Callback< SignatureHelp > | CB | ||
) |
Provide signature help for File
at Pos
.
This method should only be called for tracked files.
Definition at line 284 of file ClangdServer.cpp.
References Action, clang::clangd::File, ParseInput, Pos, clang::clangd::PreambleData::Preamble, clang::clangd::TUScheduler::runWithPreamble(), clang::clangd::signatureHelp(), and clang::clangd::TUScheduler::Stale.
void clang::clangd::ClangdServer::switchSourceHeader | ( | PathRef | Path, |
Callback< llvm::Optional< clangd::Path >> | CB | ||
) |
Switch to a corresponding source file when given a header file, and vice versa.
Definition at line 560 of file ClangdServer.cpp.
References Action, clang::clangd::getCorrespondingHeaderOrSource(), clang::clangd::None, clang::clangd::TUScheduler::runWithAST(), and clang::clangd::ThreadsafeFS::view().
void clang::clangd::ClangdServer::symbolInfo | ( | PathRef | File, |
Position | Pos, | ||
Callback< std::vector< SymbolDetails >> | CB | ||
) |
Get symbol info for given position.
Clangd extension - not part of official LSP.
Definition at line 702 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getSymbolInfo(), Pos, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::typeHierarchy | ( | PathRef | File, |
Position | Pos, | ||
int | Resolve, | ||
TypeHierarchyDirection | Direction, | ||
Callback< llvm::Optional< TypeHierarchyItem >> | CB | ||
) |
Get information about type hierarchy for a given position.
Definition at line 629 of file ClangdServer.cpp.
References Action, clang::clangd::File, clang::clangd::getTypeHierarchy(), Pos, and clang::clangd::TUScheduler::runWithAST().
void clang::clangd::ClangdServer::workspaceSymbols | ( | StringRef | Query, |
int | Limit, | ||
Callback< std::vector< SymbolInformation >> | CB | ||
) |
Retrieve the top symbols from the workspace matching a query.
Definition at line 655 of file ClangdServer.cpp.