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.
|
|
|
|
//
|
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,
|
2021-02-28 05:08:07 +08:00
|
|
|
Interface,
|
2019-07-10 16:41:25 +08:00
|
|
|
Enum,
|
2019-07-15 15:41:12 +08:00
|
|
|
EnumConstant,
|
2019-09-09 22:33:10 +08:00
|
|
|
Typedef,
|
2021-01-30 08:12:36 +08:00
|
|
|
Type,
|
|
|
|
Unknown,
|
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 08:12:36 +08:00
|
|
|
DependentName,
|
2021-01-27 16:47:17 +08:00
|
|
|
|
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);
|
|
|
|
|
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-06-26 21:08:36 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|