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
|
|
|
//
|
|
|
|
// An implementation of semantic highlighting based on this proposal:
|
|
|
|
// https://github.com/microsoft/vscode-languageserver-node/pull/367 in clangd.
|
|
|
|
// 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
|
|
|
};
|
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
|
|
|
|
|
|
|
// Contains all information needed for the highlighting a token.
|
|
|
|
struct HighlightingToken {
|
|
|
|
HighlightingKind Kind;
|
|
|
|
Range R;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
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.
|
2019-07-04 15:53:12 +08:00
|
|
|
std::vector<SemanticHighlightingInformation>
|
2019-08-01 16:08:44 +08:00
|
|
|
toSemanticHighlightingInformation(llvm::ArrayRef<LineHighlightings> Tokens);
|
|
|
|
|
|
|
|
/// 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
|