2019-06-26 21:08:36 +08:00
|
|
|
//==-- SemanticHighlighting.h - Generating highlights from the AST-- C++ -*-==//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-04 15:53:12 +08:00
|
|
|
//
|
2020-03-24 09:24:47 +08:00
|
|
|
// This file supports semantic highlighting: categorizing tokens in the file so
|
|
|
|
// that the editor can color/style them differently.
|
|
|
|
//
|
|
|
|
// This is particularly valuable for C++: its complex and context-dependent
|
|
|
|
// grammar is a challenge for simple syntax-highlighting techniques.
|
|
|
|
//
|
|
|
|
// We support two protocols for providing highlights to the client:
|
|
|
|
// - the `textDocument/semanticTokens` request from LSP 3.16
|
|
|
|
// https://github.com/microsoft/vscode-languageserver-node/blob/release/protocol/3.16.0-next.1/protocol/src/protocol.semanticTokens.proposed.ts
|
|
|
|
// - the earlier proposed `textDocument/semanticHighlighting` notification
|
|
|
|
// https://github.com/microsoft/vscode-languageserver-node/pull/367
|
|
|
|
// This is referred to as "Theia" semantic highlighting in the code.
|
|
|
|
// It was supported from clangd 9 but should be considered deprecated as of
|
|
|
|
// clangd 11 and eventually removed.
|
|
|
|
//
|
2019-07-04 15:53:12 +08:00
|
|
|
// Semantic highlightings are calculated for an AST by visiting every AST node
|
|
|
|
// and classifying nodes that are interesting to highlight (variables/function
|
|
|
|
// calls etc.).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-26 21:08:36 +08:00
|
|
|
|
2019-06-27 20:22:18 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
|
2019-06-26 21:08:36 +08:00
|
|
|
|
2019-07-04 15:53:12 +08:00
|
|
|
#include "Protocol.h"
|
2019-09-09 16:47:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2019-09-04 17:46:06 +08:00
|
|
|
class ParsedAST;
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
enum class HighlightingKind {
|
2019-07-04 15:53:12 +08:00
|
|
|
Variable = 0,
|
2019-08-28 03:39:11 +08:00
|
|
|
LocalVariable,
|
2019-08-19 15:51:39 +08:00
|
|
|
Parameter,
|
2019-07-04 20:27:21 +08:00
|
|
|
Function,
|
2019-07-15 16:12:21 +08:00
|
|
|
Method,
|
2019-08-30 11:37:24 +08:00
|
|
|
StaticMethod,
|
2019-07-15 16:12:21 +08:00
|
|
|
Field,
|
2019-08-30 11:37:24 +08:00
|
|
|
StaticField,
|
2019-07-10 16:41:25 +08:00
|
|
|
Class,
|
|
|
|
Enum,
|
2019-07-15 15:41:12 +08:00
|
|
|
EnumConstant,
|
2019-09-09 22:33:10 +08:00
|
|
|
Typedef,
|
2019-10-15 02:26:13 +08:00
|
|
|
DependentType,
|
|
|
|
DependentName,
|
2019-07-11 17:29:16 +08:00
|
|
|
Namespace,
|
2019-07-18 17:56:38 +08:00
|
|
|
TemplateParameter,
|
2020-01-22 02:21:08 +08:00
|
|
|
Concept,
|
2019-08-08 21:10:30 +08:00
|
|
|
Primitive,
|
2019-08-30 23:47:27 +08:00
|
|
|
Macro,
|
2019-07-04 20:27:21 +08:00
|
|
|
|
2019-09-25 06:17:55 +08:00
|
|
|
// This one is different from the other kinds as it's a line style
|
|
|
|
// rather than a token style.
|
|
|
|
InactiveCode,
|
|
|
|
|
|
|
|
LastKind = InactiveCode
|
2019-06-26 21:08:36 +08:00
|
|
|
};
|
2021-01-27 16:47:17 +08:00
|
|
|
|
2019-09-09 16:47:05 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
|
2019-06-26 21:08:36 +08:00
|
|
|
|
2021-01-27 16:47:17 +08:00
|
|
|
enum class HighlightingModifier {
|
|
|
|
Declaration,
|
|
|
|
// FIXME: Definition (needs findExplicitReferences support)
|
|
|
|
Deprecated,
|
|
|
|
Deduced,
|
|
|
|
Readonly,
|
|
|
|
Static,
|
|
|
|
Abstract,
|
|
|
|
|
2021-01-30 06:18:34 +08:00
|
|
|
FunctionScope,
|
|
|
|
ClassScope,
|
|
|
|
FileScope,
|
|
|
|
GlobalScope,
|
|
|
|
|
|
|
|
LastModifier = GlobalScope
|
2021-01-27 16:47:17 +08:00
|
|
|
};
|
|
|
|
static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32,
|
|
|
|
"Increase width of modifiers bitfield!");
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K);
|
|
|
|
|
2019-06-26 21:08:36 +08:00
|
|
|
// Contains all information needed for the highlighting a token.
|
|
|
|
struct HighlightingToken {
|
|
|
|
HighlightingKind Kind;
|
2021-01-27 16:47:17 +08:00
|
|
|
uint32_t Modifiers = 0;
|
2019-06-26 21:08:36 +08:00
|
|
|
Range R;
|
2021-01-27 16:47:17 +08:00
|
|
|
|
|
|
|
HighlightingToken &addModifier(HighlightingModifier M) {
|
|
|
|
Modifiers |= 1 << static_cast<unsigned>(M);
|
|
|
|
return *this;
|
|
|
|
}
|
2019-06-26 21:08:36 +08:00
|
|
|
};
|
|
|
|
|
2019-08-01 16:08:44 +08:00
|
|
|
bool operator==(const HighlightingToken &L, const HighlightingToken &R);
|
|
|
|
bool operator<(const HighlightingToken &L, const HighlightingToken &R);
|
|
|
|
|
|
|
|
/// Contains all information about highlightings on a single line.
|
|
|
|
struct LineHighlightings {
|
|
|
|
int Line;
|
|
|
|
std::vector<HighlightingToken> Tokens;
|
2019-09-25 06:17:55 +08:00
|
|
|
bool IsInactive;
|
2019-08-01 16:08:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const LineHighlightings &L, const LineHighlightings &R);
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
// Returns all HighlightingTokens from an AST. Only generates highlights for the
|
|
|
|
// main AST.
|
|
|
|
std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
|
|
|
|
|
2020-03-24 09:24:47 +08:00
|
|
|
std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>);
|
|
|
|
llvm::StringRef toSemanticTokenType(HighlightingKind Kind);
|
2021-01-27 16:47:17 +08:00
|
|
|
llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier);
|
[clangd] Support textDocument/semanticTokens/edits
Summary:
This returns incremental highlights as a set of edits against the
previous highlights.
Server-side, we compute the full set of highlights, this just saves
wire-format size.
For now, the diff used is trivial: everything from the first change to
the last change is sent as a single edit.
The wire format is grungy - the replacement offset/length refer to
positions in the encoded array instead of the logical list of tokens.
We use token-oriented structs and translating to LSP forms when serializing.
This departs from LSP (but is consistent with semanticTokens today).
Tested in VSCode insiders (with a patched client to enable experimental
features).
Reviewers: hokein
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D77225
2020-04-01 22:21:44 +08:00
|
|
|
std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before,
|
|
|
|
llvm::ArrayRef<SemanticToken> After);
|
2020-03-24 09:24:47 +08:00
|
|
|
|
2019-07-04 20:27:21 +08:00
|
|
|
/// Converts a HighlightingKind to a corresponding TextMate scope
|
|
|
|
/// (https://manual.macromates.com/en/language_grammars).
|
|
|
|
llvm::StringRef toTextMateScope(HighlightingKind Kind);
|
2019-07-04 15:53:12 +08:00
|
|
|
|
2019-08-01 16:08:44 +08:00
|
|
|
/// Convert to LSP's semantic highlighting information.
|
2020-03-24 07:31:14 +08:00
|
|
|
std::vector<TheiaSemanticHighlightingInformation>
|
|
|
|
toTheiaSemanticHighlightingInformation(
|
|
|
|
llvm::ArrayRef<LineHighlightings> Tokens);
|
2019-08-01 16:08:44 +08:00
|
|
|
|
|
|
|
/// Return a line-by-line diff between two highlightings.
|
2020-01-04 23:28:41 +08:00
|
|
|
/// - if the tokens on a line are the same in both highlightings, this line is
|
2019-08-01 16:08:44 +08:00
|
|
|
/// omitted.
|
2019-08-26 16:38:45 +08:00
|
|
|
/// - if a line exists in New but not in Old, the tokens on this line are
|
2019-08-01 16:08:44 +08:00
|
|
|
/// emitted.
|
2019-08-26 16:38:45 +08:00
|
|
|
/// - if a line does not exist in New but exists in Old, an empty line is
|
2019-08-01 16:08:44 +08:00
|
|
|
/// emitted (to tell client to clear the previous highlightings on this line).
|
2019-08-26 16:38:45 +08:00
|
|
|
///
|
2019-08-01 16:08:44 +08:00
|
|
|
/// REQUIRED: Old and New are sorted.
|
|
|
|
std::vector<LineHighlightings>
|
|
|
|
diffHighlightings(ArrayRef<HighlightingToken> New,
|
2019-08-26 16:38:45 +08:00
|
|
|
ArrayRef<HighlightingToken> Old);
|
2019-07-04 15:53:12 +08:00
|
|
|
|
2019-06-26 21:08:36 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|