2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdLSPServer.h - LSP server --------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDLSPSERVER_H
|
|
|
|
|
|
|
|
#include "ClangdServer.h"
|
2017-06-13 23:59:43 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "Path.h"
|
|
|
|
#include "Protocol.h"
|
2017-09-30 18:08:52 +08:00
|
|
|
#include "ProtocolHandlers.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
2017-07-19 23:43:35 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
class JSONOutput;
|
2018-01-10 22:44:34 +08:00
|
|
|
class SymbolIndex;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// This class provides implementation of an LSP server, glueing the JSON
|
|
|
|
/// dispatch and ClangdServer together.
|
2017-09-30 18:08:52 +08:00
|
|
|
class ClangdLSPServer : private DiagnosticsConsumer, private ProtocolCallbacks {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2017-10-02 23:13:20 +08:00
|
|
|
/// If \p CompileCommandsDir has a value, compile_commands.json will be
|
|
|
|
/// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
|
|
|
|
/// for compile_commands.json in all parent directories of each file.
|
2017-08-14 16:45:47 +08:00
|
|
|
ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
|
2017-11-24 00:58:22 +08:00
|
|
|
bool StorePreamblesInMemory,
|
|
|
|
const clangd::CodeCompleteOptions &CCOpts,
|
2017-10-02 23:13:20 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDir,
|
2017-12-20 02:00:37 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir,
|
2018-01-10 22:44:34 +08:00
|
|
|
bool BuildDynamicSymbolIndex,
|
|
|
|
SymbolIndex *StaticIdx = nullptr);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Run LSP server loop, receiving input for it from \p In. \p In must be
|
|
|
|
/// opened in binary mode. Output will be written using Out variable passed to
|
|
|
|
/// class constructor. This method must not be executed more than once for
|
|
|
|
/// each instance of ClangdLSPServer.
|
2017-10-25 16:45:41 +08:00
|
|
|
///
|
|
|
|
/// \return Wether we received a 'shutdown' request before an 'exit' request
|
|
|
|
bool run(std::istream &In);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
2017-09-30 18:08:52 +08:00
|
|
|
// Implement DiagnosticsConsumer.
|
|
|
|
virtual void
|
2018-01-11 01:59:27 +08:00
|
|
|
onDiagnosticsReady(const Context &Ctx, PathRef File,
|
2017-09-30 18:08:52 +08:00
|
|
|
Tagged<std::vector<DiagWithFixIts>> Diagnostics) override;
|
|
|
|
|
|
|
|
// Implement ProtocolCallbacks.
|
2017-10-12 21:29:58 +08:00
|
|
|
void onInitialize(Ctx C, InitializeParams &Params) override;
|
|
|
|
void onShutdown(Ctx C, ShutdownParams &Params) override;
|
2017-10-25 16:45:41 +08:00
|
|
|
void onExit(Ctx C, ExitParams &Params) override;
|
2017-10-12 21:29:58 +08:00
|
|
|
void onDocumentDidOpen(Ctx C, DidOpenTextDocumentParams &Params) override;
|
|
|
|
void onDocumentDidChange(Ctx C, DidChangeTextDocumentParams &Params) override;
|
|
|
|
void onDocumentDidClose(Ctx C, DidCloseTextDocumentParams &Params) override;
|
|
|
|
void
|
|
|
|
onDocumentOnTypeFormatting(Ctx C,
|
|
|
|
DocumentOnTypeFormattingParams &Params) override;
|
|
|
|
void
|
|
|
|
onDocumentRangeFormatting(Ctx C,
|
|
|
|
DocumentRangeFormattingParams &Params) override;
|
|
|
|
void onDocumentFormatting(Ctx C, DocumentFormattingParams &Params) override;
|
|
|
|
void onCodeAction(Ctx C, CodeActionParams &Params) override;
|
|
|
|
void onCompletion(Ctx C, TextDocumentPositionParams &Params) override;
|
|
|
|
void onSignatureHelp(Ctx C, TextDocumentPositionParams &Params) override;
|
|
|
|
void onGoToDefinition(Ctx C, TextDocumentPositionParams &Params) override;
|
|
|
|
void onSwitchSourceHeader(Ctx C, TextDocumentIdentifier &Params) override;
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
void onDocumentHighlight(Ctx C, TextDocumentPositionParams &Params) override;
|
2017-10-12 21:29:58 +08:00
|
|
|
void onFileEvent(Ctx C, DidChangeWatchedFilesParams &Params) override;
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
void onCommand(Ctx C, ExecuteCommandParams &Params) override;
|
2017-11-09 19:30:04 +08:00
|
|
|
void onRename(Ctx C, RenameParams &Parames) override;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
[clangd] Emit ranges for clangd diagnostics, and fix off-by-one positions
Summary:
- when the diagnostic has an explicit range, we prefer that
- if the diagnostic has a fixit, its RemoveRange is our next choice
- otherwise we try to expand the diagnostic location into a whole token.
(inspired by VSCode, which does this client-side when given an empty range)
- if all else fails, we return the zero-width range as now.
(clients react in different ways to this, highlighting a token or a char)
- this includes the off-by-one fix from D40860, and borrows heavily from it
Reviewers: rwols, hokein
Subscribers: klimek, ilya-biryukov, cfe-commits
Differential Revision: https://reviews.llvm.org/D41118
llvm-svn: 320555
2017-12-13 16:48:42 +08:00
|
|
|
std::vector<TextEdit> getFixIts(StringRef File, const clangd::Diagnostic &D);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
JSONOutput &Out;
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Used to indicate that the 'shutdown' request was received from the
|
|
|
|
/// Language Server client.
|
2017-10-25 16:45:41 +08:00
|
|
|
bool ShutdownRequestReceived = false;
|
|
|
|
|
|
|
|
/// Used to indicate that the 'exit' notification was received from the
|
|
|
|
/// Language Server client.
|
2017-05-16 22:40:30 +08:00
|
|
|
/// It's used to break out of the LSP parsing loop.
|
|
|
|
bool IsDone = false;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
std::mutex FixItsMutex;
|
2017-12-20 04:52:56 +08:00
|
|
|
typedef std::map<clangd::Diagnostic, std::vector<TextEdit>,
|
|
|
|
LSPDiagnosticCompare>
|
2017-05-16 17:38:59 +08:00
|
|
|
DiagnosticToReplacementMap;
|
|
|
|
/// Caches FixIts per file and diagnostics
|
|
|
|
llvm::StringMap<DiagnosticToReplacementMap> FixItsMap;
|
2017-06-13 23:59:43 +08:00
|
|
|
|
|
|
|
// Various ClangdServer parameters go here. It's important they're created
|
|
|
|
// before ClangdServer.
|
|
|
|
DirectoryBasedGlobalCompilationDatabase CDB;
|
|
|
|
RealFileSystemProvider FSProvider;
|
2017-12-05 18:42:57 +08:00
|
|
|
/// Options used for code completion
|
|
|
|
clangd::CodeCompleteOptions CCOpts;
|
2017-05-16 17:38:59 +08:00
|
|
|
// Server must be the last member of the class to allow its destructor to exit
|
|
|
|
// the worker thread that may otherwise run an async callback on partially
|
|
|
|
// destructed instance of ClangdLSPServer.
|
|
|
|
ClangdServer Server;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|