clang-tools  7.0.0
Classes | Public Member Functions | Static Public Member Functions | List of all members
clang::clangd::ClangdServer Class Reference

Provides API to manage ASTs for a collection of C++ files and request various language features. More...

#include <ClangdServer.h>

Classes

struct  Options
 

Public Member Functions

 ClangdServer (GlobalCompilationDatabase &CDB, FileSystemProvider &FSProvider, DiagnosticsConsumer &DiagConsumer, const Options &Opts)
 Creates a new ClangdServer instance. More...
 
void setRootPath (PathRef RootPath)
 Set the root path of the workspace. More...
 
void addDocument (PathRef File, StringRef Contents, WantDiagnostics WD=WantDiagnostics::Auto)
 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 findDefinitions (PathRef File, Position Pos, Callback< std::vector< Location >> CB)
 Get definition of symbol at a specified Line and Column in File. More...
 
llvm::Optional< PathswitchSourceHeader (PathRef Path)
 Helper function that returns a path to the 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< Hover >> CB)
 Get code hover for a given position. 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< SymbolInformation >> CB)
 Retrieve the symbols within the specified file. More...
 
llvm::Expected< tooling::Replacements > formatRange (StringRef Code, PathRef File, Range Rng)
 Run formatting for Rng inside File with content Code. More...
 
llvm::Expected< tooling::Replacements > formatFile (StringRef Code, PathRef File)
 Run formatting for the whole File with content Code. More...
 
llvm::Expected< tooling::Replacements > formatOnType (StringRef Code, PathRef File, Position Pos)
 Run formatting after a character was typed at Pos in File with content Code. More...
 
void rename (PathRef File, Position Pos, llvm::StringRef NewName, Callback< std::vector< tooling::Replacement >> CB)
 Rename all occurrences of the symbol at the Pos in File to NewName. 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...
 
std::vector< std::pair< Path, std::size_t > > getUsedBytesPerFile () const
 Returns estimated memory usage for each of the currently open files. More...
 
LLVM_NODISCARD bool blockUntilIdleForTest (llvm::Optional< double > TimeoutSeconds=10)
 

Static Public Member Functions

static Options optsForTest ()
 

Detailed Description

Provides API to manage ASTs for a collection of C++ files and request various language features.

Currently supports async diagnostics, code completion, formatting and goto definition.

Definition at line 50 of file ClangdServer.h.

Constructor & Destructor Documentation

◆ ClangdServer()

ClangdServer::ClangdServer ( GlobalCompilationDatabase CDB,
FileSystemProvider FSProvider,
DiagnosticsConsumer DiagConsumer,
const Options Opts 
)

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().

After each parsing request finishes, ClangdServer reports diagnostics to DiagConsumer. Note that a callback to DiagConsumer happens on a worker thread. Therefore, instances of DiagConsumer must properly synchronize access to shared state.

Definition at line 79 of file ClangdServer.cpp.

Member Function Documentation

◆ addDocument()

void ClangdServer::addDocument ( PathRef  File,
StringRef  Contents,
WantDiagnostics  WD = WantDiagnostics::Auto 
)

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.

Definition at line 125 of file ClangdServer.cpp.

References clang::clangd::File, clang::clangd::FileSystemProvider::getFileSystem(), and clang::clangd::TUScheduler::update().

Referenced by clang::clangd::ClangdLSPServer::run().

◆ blockUntilIdleForTest()

LLVM_NODISCARD bool ClangdServer::blockUntilIdleForTest ( llvm::Optional< double >  TimeoutSeconds = 10)

◆ codeComplete()

void 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 143 of file ClangdServer.cpp.

◆ documentSymbols()

void ClangdServer::documentSymbols ( StringRef  File,
Callback< std::vector< SymbolInformation >>  CB 
)

Retrieve the symbols within the specified file.

Definition at line 455 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::getDocumentSymbols(), and clang::clangd::TUScheduler::runWithAST().

◆ dumpAST()

void 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 278 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::dumpAST(), and clang::clangd::TUScheduler::runWithAST().

◆ findDefinitions()

void ClangdServer::findDefinitions ( PathRef  File,
Position  Pos,
Callback< std::vector< Location >>  CB 
)

Get definition of symbol at a specified Line and Column in File.

Definition at line 298 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::findDefinitions(), Pos, and clang::clangd::TUScheduler::runWithAST().

◆ findDocumentHighlights()

void ClangdServer::findDocumentHighlights ( PathRef  File,
Position  Pos,
Callback< std::vector< DocumentHighlight >>  CB 
)

Get document highlights for a given position.

Definition at line 391 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::findDocumentHighlights(), Pos, and clang::clangd::TUScheduler::runWithAST().

◆ findHover()

void ClangdServer::findHover ( PathRef  File,
Position  Pos,
Callback< llvm::Optional< Hover >>  CB 
)

Get code hover for a given position.

Definition at line 403 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::getHover(), Pos, and clang::clangd::TUScheduler::runWithAST().

◆ formatFile()

llvm::Expected< tooling::Replacements > ClangdServer::formatFile ( StringRef  Code,
PathRef  File 
)

Run formatting for the whole File with content Code.

Definition at line 206 of file ClangdServer.cpp.

References Range.

◆ formatOnType()

llvm::Expected< tooling::Replacements > ClangdServer::formatOnType ( StringRef  Code,
PathRef  File,
Position  Pos 
)

Run formatting after a character was typed at Pos in File with content Code.

Definition at line 213 of file ClangdServer.cpp.

References clang::clangd::positionToOffset(), and Range.

◆ formatRange()

llvm::Expected< tooling::Replacements > ClangdServer::formatRange ( StringRef  Code,
PathRef  File,
Range  Rng 
)

Run formatting for Rng inside File with content Code.

Definition at line 196 of file ClangdServer.cpp.

References clang::clangd::Range::end, clang::clangd::positionToOffset(), Range, and clang::clangd::Range::start.

◆ getUsedBytesPerFile()

std::vector< std::pair< Path, std::size_t > > ClangdServer::getUsedBytesPerFile ( ) const

Returns estimated memory usage for each of the currently open files.

The order of results is unspecified. Overall memory usage of clangd may be significantly more than reported here, as this metric does not account (at least) for:

  • memory occupied by static and dynamic index,
  • memory required for in-flight requests, FIXME: those metrics might be useful too, we should add them.

Definition at line 468 of file ClangdServer.cpp.

References clang::clangd::TUScheduler::getUsedBytesPerFile().

◆ onFileEvent()

void ClangdServer::onFileEvent ( const DidChangeWatchedFilesParams Params)

Called when an event occurs for a watched file in the workspace.

Definition at line 444 of file ClangdServer.cpp.

◆ optsForTest()

ClangdServer::Options ClangdServer::optsForTest ( )
static

Definition at line 71 of file ClangdServer.cpp.

◆ removeDocument()

void ClangdServer::removeDocument ( PathRef  File)

Remove File from list of tracked files, schedule a request to free resources associated with it.

Definition at line 138 of file ClangdServer.cpp.

References clang::clangd::File, and clang::clangd::TUScheduler::remove().

◆ rename()

void ClangdServer::rename ( PathRef  File,
Position  Pos,
llvm::StringRef  NewName,
Callback< std::vector< tooling::Replacement >>  CB 
)

Rename all occurrences of the symbol at the Pos in File to NewName.

Definition at line 227 of file ClangdServer.cpp.

References Action, clang::clangd::AST, clang::clangd::File, and Pos.

◆ setRootPath()

void ClangdServer::setRootPath ( PathRef  RootPath)

Set the root path of the workspace.

Definition at line 113 of file ClangdServer.cpp.

References clang::clangd::elog(), and clang::clangd::FileSystemProvider::getFileSystem().

◆ signatureHelp()

void 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 175 of file ClangdServer.cpp.

References Action, clang::clangd::Bind(), clang::clangd::File, clang::clangd::FileSystemProvider::getFileSystem(), Pos, clang::clangd::PreambleData::Preamble, clang::clangd::TUScheduler::runWithPreamble(), and clang::clangd::signatureHelp().

◆ switchSourceHeader()

llvm::Optional< Path > ClangdServer::switchSourceHeader ( PathRef  Path)

Helper function that returns a path to the corresponding source file when given a header file and vice versa.

Definition at line 310 of file ClangdServer.cpp.

References clang::clangd::FileSystemProvider::getFileSystem(), and clang::clangd::None.

◆ workspaceSymbols()

void ClangdServer::workspaceSymbols ( StringRef  Query,
int  Limit,
Callback< std::vector< SymbolInformation >>  CB 
)

Retrieve the top symbols from the workspace matching a query.

Definition at line 449 of file ClangdServer.cpp.

References clang::clangd::getWorkspaceSymbols().


The documentation for this class was generated from the following files: