2019-06-27 20:22:18 +08:00
|
|
|
//===--- SemanticHighlighting.cpp - ------------------------- ---*- C++ -*-===//
|
2019-06-26 21:08:36 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SemanticHighlighting.h"
|
|
|
|
#include "Logger.h"
|
2019-07-04 15:53:12 +08:00
|
|
|
#include "Protocol.h"
|
2019-06-26 21:08:36 +08:00
|
|
|
#include "SourceCode.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2019-08-19 15:51:39 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2019-08-09 20:19:10 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2019-06-26 21:08:36 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2019-08-01 16:08:44 +08:00
|
|
|
#include <algorithm>
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Collects all semantic tokens in an ASTContext.
|
|
|
|
class HighlightingTokenCollector
|
|
|
|
: public RecursiveASTVisitor<HighlightingTokenCollector> {
|
|
|
|
std::vector<HighlightingToken> Tokens;
|
2019-08-30 23:47:27 +08:00
|
|
|
ParsedAST &AST;
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
public:
|
2019-08-30 23:47:27 +08:00
|
|
|
HighlightingTokenCollector(ParsedAST &AST) : AST(AST) {}
|
2019-06-26 21:08:36 +08:00
|
|
|
|
|
|
|
std::vector<HighlightingToken> collectTokens() {
|
|
|
|
Tokens.clear();
|
2019-08-30 23:47:27 +08:00
|
|
|
TraverseAST(AST.getASTContext());
|
|
|
|
// Add highlightings for macro expansions as they are not traversed by the
|
|
|
|
// visitor.
|
|
|
|
// FIXME: Should add highlighting to the macro definitions as well. But this
|
|
|
|
// information is not collected in ParsedAST right now.
|
|
|
|
for (const SourceLocation &L : AST.getMainFileExpansions())
|
|
|
|
addToken(L, HighlightingKind::Macro);
|
2019-07-15 23:08:27 +08:00
|
|
|
// Initializer lists can give duplicates of tokens, therefore all tokens
|
|
|
|
// must be deduplicated.
|
2019-08-01 16:08:44 +08:00
|
|
|
llvm::sort(Tokens);
|
2019-07-15 23:08:27 +08:00
|
|
|
auto Last = std::unique(Tokens.begin(), Tokens.end());
|
|
|
|
Tokens.erase(Last, Tokens.end());
|
2019-08-20 00:27:49 +08:00
|
|
|
// Macros can give tokens that have the same source range but conflicting
|
|
|
|
// kinds. In this case all tokens sharing this source range should be
|
|
|
|
// removed.
|
|
|
|
std::vector<HighlightingToken> NonConflicting;
|
|
|
|
NonConflicting.reserve(Tokens.size());
|
|
|
|
for (ArrayRef<HighlightingToken> TokRef = Tokens; !TokRef.empty();) {
|
|
|
|
ArrayRef<HighlightingToken> Conflicting =
|
|
|
|
TokRef.take_while([&](const HighlightingToken &T) {
|
|
|
|
// TokRef is guaranteed at least one element here because otherwise
|
|
|
|
// this predicate would never fire.
|
|
|
|
return T.R == TokRef.front().R;
|
|
|
|
});
|
|
|
|
// If there is exactly one token with this range it's non conflicting and
|
|
|
|
// should be in the highlightings.
|
|
|
|
if (Conflicting.size() == 1)
|
|
|
|
NonConflicting.push_back(TokRef.front());
|
|
|
|
// TokRef[Conflicting.size()] is the next token with a different range (or
|
|
|
|
// the end of the Tokens).
|
|
|
|
TokRef = TokRef.drop_front(Conflicting.size());
|
|
|
|
}
|
|
|
|
return NonConflicting;
|
2019-06-26 21:08:36 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:29:16 +08:00
|
|
|
bool VisitNamespaceAliasDecl(NamespaceAliasDecl *NAD) {
|
|
|
|
// The target namespace of an alias can not be found in any other way.
|
|
|
|
addToken(NAD->getTargetNameLoc(), HighlightingKind::Namespace);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-15 16:12:21 +08:00
|
|
|
bool VisitMemberExpr(MemberExpr *ME) {
|
|
|
|
const auto *MD = ME->getMemberDecl();
|
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
// When calling the destructor manually like: AAA::~A(); The ~ is a
|
|
|
|
// MemberExpr. Other methods should still be highlighted though.
|
|
|
|
return true;
|
2019-08-08 20:43:55 +08:00
|
|
|
if (isa<CXXConversionDecl>(MD))
|
|
|
|
// The MemberLoc is invalid for C++ conversion operators. We do not
|
|
|
|
// attempt to add tokens with invalid locations.
|
|
|
|
return true;
|
2019-07-15 16:12:21 +08:00
|
|
|
addToken(ME->getMemberLoc(), MD);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-05 21:06:03 +08:00
|
|
|
bool VisitNamedDecl(NamedDecl *ND) {
|
2019-07-11 17:29:16 +08:00
|
|
|
// UsingDirectiveDecl's namespaces do not show up anywhere else in the
|
|
|
|
// Visit/Traverse mehods. But they should also be highlighted as a
|
|
|
|
// namespace.
|
|
|
|
if (const auto *UD = dyn_cast<UsingDirectiveDecl>(ND)) {
|
|
|
|
addToken(UD->getIdentLocation(), HighlightingKind::Namespace);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:41:25 +08:00
|
|
|
// Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has
|
|
|
|
// no tag decl and therefore constructors must be gotten as NamedDecls
|
|
|
|
// instead.
|
|
|
|
if (ND->getDeclName().getNameKind() ==
|
|
|
|
DeclarationName::CXXConstructorName) {
|
|
|
|
addToken(ND->getLocation(), ND);
|
2019-07-05 21:06:03 +08:00
|
|
|
return true;
|
2019-07-10 16:41:25 +08:00
|
|
|
}
|
2019-07-05 21:06:03 +08:00
|
|
|
|
2019-07-10 16:41:25 +08:00
|
|
|
if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
|
2019-07-05 21:06:03 +08:00
|
|
|
return true;
|
2019-07-10 16:41:25 +08:00
|
|
|
|
2019-07-05 21:06:03 +08:00
|
|
|
addToken(ND->getLocation(), ND);
|
2019-06-26 21:08:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-05 21:06:03 +08:00
|
|
|
|
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *Ref) {
|
|
|
|
if (Ref->getNameInfo().getName().getNameKind() !=
|
|
|
|
DeclarationName::Identifier)
|
|
|
|
// Only want to highlight identifiers.
|
|
|
|
return true;
|
|
|
|
|
|
|
|
addToken(Ref->getLocation(), Ref->getDecl());
|
2019-06-26 21:08:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:23:12 +08:00
|
|
|
bool VisitTypedefNameDecl(TypedefNameDecl *TD) {
|
2019-08-12 15:45:12 +08:00
|
|
|
if (const auto *TSI = TD->getTypeSourceInfo())
|
|
|
|
addType(TD->getLocation(), TSI->getTypeLoc().getTypePtr());
|
2019-07-16 21:23:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:56:38 +08:00
|
|
|
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) {
|
|
|
|
// TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr.
|
|
|
|
addToken(TL.getBeginLoc(), TL.getDecl());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) {
|
|
|
|
if (const TemplateDecl *TD =
|
|
|
|
TL.getTypePtr()->getTemplateName().getAsTemplateDecl())
|
|
|
|
addToken(TL.getBeginLoc(), TD);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:41:25 +08:00
|
|
|
bool VisitTypeLoc(TypeLoc &TL) {
|
|
|
|
// This check is for not getting two entries when there are anonymous
|
2019-07-11 17:29:16 +08:00
|
|
|
// structs. It also makes us not highlight certain namespace qualifiers
|
|
|
|
// twice. For elaborated types the actual type is highlighted as an inner
|
|
|
|
// TypeLoc.
|
2019-08-12 15:45:12 +08:00
|
|
|
if (TL.getTypeLocClass() != TypeLoc::TypeLocClass::Elaborated)
|
|
|
|
addType(TL.getBeginLoc(), TL.getTypePtr());
|
2019-07-10 16:41:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:29:16 +08:00
|
|
|
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
|
|
|
|
if (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier())
|
|
|
|
if (NNS->getKind() == NestedNameSpecifier::Namespace ||
|
|
|
|
NNS->getKind() == NestedNameSpecifier::NamespaceAlias)
|
|
|
|
addToken(NNSLoc.getLocalBeginLoc(), HighlightingKind::Namespace);
|
|
|
|
|
|
|
|
return RecursiveASTVisitor<
|
|
|
|
HighlightingTokenCollector>::TraverseNestedNameSpecifierLoc(NNSLoc);
|
|
|
|
}
|
|
|
|
|
2019-08-09 20:19:10 +08:00
|
|
|
bool TraverseConstructorInitializer(CXXCtorInitializer *CI) {
|
|
|
|
if (const FieldDecl *FD = CI->getMember())
|
|
|
|
addToken(CI->getSourceLocation(), FD);
|
|
|
|
return RecursiveASTVisitor<
|
|
|
|
HighlightingTokenCollector>::TraverseConstructorInitializer(CI);
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:45:12 +08:00
|
|
|
bool VisitDeclaratorDecl(DeclaratorDecl *D) {
|
|
|
|
if ((!D->getTypeSourceInfo()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (auto *AT = D->getType()->getContainedAutoType()) {
|
|
|
|
const auto Deduced = AT->getDeducedType();
|
|
|
|
if (!Deduced.isNull())
|
|
|
|
addType(D->getTypeSpecStartLoc(), Deduced.getTypePtr());
|
2019-08-08 21:10:30 +08:00
|
|
|
}
|
2019-08-12 15:45:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void addType(SourceLocation Loc, const Type *TP) {
|
|
|
|
if (!TP)
|
|
|
|
return;
|
|
|
|
if (TP->isBuiltinType())
|
|
|
|
// Builtins must be special cased as they do not have a TagDecl.
|
|
|
|
addToken(Loc, HighlightingKind::Primitive);
|
|
|
|
if (const TagDecl *TD = TP->getAsTagDecl())
|
|
|
|
addToken(Loc, TD);
|
2019-07-16 21:23:12 +08:00
|
|
|
}
|
|
|
|
|
2019-07-10 16:41:25 +08:00
|
|
|
void addToken(SourceLocation Loc, const NamedDecl *D) {
|
|
|
|
if (D->getDeclName().isIdentifier() && D->getName().empty())
|
|
|
|
// Don't add symbols that don't have any length.
|
|
|
|
return;
|
|
|
|
// We highlight class decls, constructor decls and destructor decls as
|
|
|
|
// `Class` type. The destructor decls are handled in `VisitTypeLoc` (we will
|
|
|
|
// visit a TypeLoc where the underlying Type is a CXXRecordDecl).
|
2019-07-18 17:56:38 +08:00
|
|
|
if (isa<ClassTemplateDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Class);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-10 16:41:25 +08:00
|
|
|
if (isa<RecordDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Class);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isa<CXXConstructorDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Class);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-30 11:37:24 +08:00
|
|
|
if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
|
|
|
|
addToken(Loc, MD->isStatic() ? HighlightingKind::StaticMethod
|
|
|
|
: HighlightingKind::Method);
|
2019-07-15 16:12:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isa<FieldDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Field);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-10 16:41:25 +08:00
|
|
|
if (isa<EnumDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Enum);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-15 15:41:12 +08:00
|
|
|
if (isa<EnumConstantDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::EnumConstant);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-19 15:51:39 +08:00
|
|
|
if (isa<ParmVarDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Parameter);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-28 03:39:11 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2019-08-30 11:37:24 +08:00
|
|
|
addToken(Loc, VD->isStaticDataMember()
|
|
|
|
? HighlightingKind::StaticField
|
|
|
|
: VD->isLocalVarDecl() ? HighlightingKind::LocalVariable
|
|
|
|
: HighlightingKind::Variable);
|
2019-06-26 21:08:36 +08:00
|
|
|
return;
|
2019-07-05 21:06:03 +08:00
|
|
|
}
|
2019-08-30 22:07:05 +08:00
|
|
|
if (isa<BindingDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Variable);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-05 21:06:03 +08:00
|
|
|
if (isa<FunctionDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Function);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-11 17:29:16 +08:00
|
|
|
if (isa<NamespaceDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Namespace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isa<NamespaceAliasDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::Namespace);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-18 17:56:38 +08:00
|
|
|
if (isa<TemplateTemplateParmDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::TemplateParameter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isa<TemplateTypeParmDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::TemplateParameter);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-16 17:30:21 +08:00
|
|
|
if (isa<NonTypeTemplateParmDecl>(D)) {
|
|
|
|
addToken(Loc, HighlightingKind::TemplateParameter);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-05 21:06:03 +08:00
|
|
|
}
|
2019-06-26 21:08:36 +08:00
|
|
|
|
2019-07-05 21:06:03 +08:00
|
|
|
void addToken(SourceLocation Loc, HighlightingKind Kind) {
|
2019-08-30 23:47:27 +08:00
|
|
|
const auto &SM = AST.getSourceManager();
|
2019-08-28 03:39:11 +08:00
|
|
|
if (Loc.isMacroID()) {
|
2019-08-20 00:27:49 +08:00
|
|
|
// Only intereseted in highlighting arguments in macros (DEF_X(arg)).
|
|
|
|
if (!SM.isMacroArgExpansion(Loc))
|
|
|
|
return;
|
|
|
|
Loc = SM.getSpellingLoc(Loc);
|
|
|
|
}
|
2019-06-26 21:08:36 +08:00
|
|
|
|
2019-08-12 21:01:11 +08:00
|
|
|
// Non top level decls that are included from a header are not filtered by
|
|
|
|
// topLevelDecls. (example: method declarations being included from another
|
|
|
|
// file for a class from another file)
|
2019-08-28 03:39:11 +08:00
|
|
|
// There are also cases with macros where the spelling loc will not be in
|
|
|
|
// the main file and the highlighting would be incorrect.
|
2019-08-12 21:01:11 +08:00
|
|
|
if (!isInsideMainFile(Loc, SM))
|
|
|
|
return;
|
|
|
|
|
2019-08-30 23:47:27 +08:00
|
|
|
auto R = getTokenRange(SM, AST.getASTContext().getLangOpts(), Loc);
|
2019-06-26 21:08:36 +08:00
|
|
|
if (!R) {
|
|
|
|
// R should always have a value, if it doesn't something is very wrong.
|
|
|
|
elog("Tried to add semantic token with an invalid range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tokens.push_back({Kind, R.getValue()});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-04 15:53:12 +08:00
|
|
|
// Encode binary data into base64.
|
|
|
|
// This was copied from compiler-rt/lib/fuzzer/FuzzerUtil.cpp.
|
|
|
|
// FIXME: Factor this out into llvm/Support?
|
|
|
|
std::string encodeBase64(const llvm::SmallVectorImpl<char> &Bytes) {
|
|
|
|
static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789+/";
|
|
|
|
std::string Res;
|
|
|
|
size_t I;
|
|
|
|
for (I = 0; I + 2 < Bytes.size(); I += 3) {
|
|
|
|
uint32_t X = (Bytes[I] << 16) + (Bytes[I + 1] << 8) + Bytes[I + 2];
|
|
|
|
Res += Table[(X >> 18) & 63];
|
|
|
|
Res += Table[(X >> 12) & 63];
|
|
|
|
Res += Table[(X >> 6) & 63];
|
|
|
|
Res += Table[X & 63];
|
|
|
|
}
|
|
|
|
if (I + 1 == Bytes.size()) {
|
|
|
|
uint32_t X = (Bytes[I] << 16);
|
|
|
|
Res += Table[(X >> 18) & 63];
|
|
|
|
Res += Table[(X >> 12) & 63];
|
|
|
|
Res += "==";
|
|
|
|
} else if (I + 2 == Bytes.size()) {
|
|
|
|
uint32_t X = (Bytes[I] << 16) + (Bytes[I + 1] << 8);
|
|
|
|
Res += Table[(X >> 18) & 63];
|
|
|
|
Res += Table[(X >> 12) & 63];
|
|
|
|
Res += Table[(X >> 6) & 63];
|
|
|
|
Res += "=";
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write32be(uint32_t I, llvm::raw_ostream &OS) {
|
|
|
|
std::array<char, 4> Buf;
|
|
|
|
llvm::support::endian::write32be(Buf.data(), I);
|
|
|
|
OS.write(Buf.data(), Buf.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void write16be(uint16_t I, llvm::raw_ostream &OS) {
|
|
|
|
std::array<char, 2> Buf;
|
|
|
|
llvm::support::endian::write16be(Buf.data(), I);
|
|
|
|
OS.write(Buf.data(), Buf.size());
|
|
|
|
}
|
2019-08-01 16:08:44 +08:00
|
|
|
|
|
|
|
// Get the highlightings on \c Line where the first entry of line is at \c
|
|
|
|
// StartLineIt. If it is not at \c StartLineIt an empty vector is returned.
|
|
|
|
ArrayRef<HighlightingToken>
|
|
|
|
takeLine(ArrayRef<HighlightingToken> AllTokens,
|
|
|
|
ArrayRef<HighlightingToken>::iterator StartLineIt, int Line) {
|
|
|
|
return ArrayRef<HighlightingToken>(StartLineIt, AllTokens.end())
|
|
|
|
.take_while([Line](const HighlightingToken &Token) {
|
|
|
|
return Token.R.start.line == Line;
|
|
|
|
});
|
|
|
|
}
|
2019-06-26 21:08:36 +08:00
|
|
|
} // namespace
|
|
|
|
|
2019-08-01 16:08:44 +08:00
|
|
|
std::vector<LineHighlightings>
|
|
|
|
diffHighlightings(ArrayRef<HighlightingToken> New,
|
2019-08-26 16:38:45 +08:00
|
|
|
ArrayRef<HighlightingToken> Old) {
|
|
|
|
assert(std::is_sorted(New.begin(), New.end()) &&
|
|
|
|
"New must be a sorted vector");
|
|
|
|
assert(std::is_sorted(Old.begin(), Old.end()) &&
|
|
|
|
"Old must be a sorted vector");
|
2019-08-01 16:08:44 +08:00
|
|
|
|
|
|
|
// FIXME: There's an edge case when tokens span multiple lines. If the first
|
|
|
|
// token on the line started on a line above the current one and the rest of
|
|
|
|
// the line is the equal to the previous one than we will remove all
|
|
|
|
// highlights but the ones for the token spanning multiple lines. This means
|
|
|
|
// that when we get into the LSP layer the only highlights that will be
|
|
|
|
// visible are the ones for the token spanning multiple lines.
|
|
|
|
// Example:
|
|
|
|
// EndOfMultilineToken Token Token Token
|
|
|
|
// If "Token Token Token" don't differ from previously the line is
|
|
|
|
// incorrectly removed. Suggestion to fix is to separate any multiline tokens
|
|
|
|
// into one token for every line it covers. This requires reading from the
|
|
|
|
// file buffer to figure out the length of each line though.
|
|
|
|
std::vector<LineHighlightings> DiffedLines;
|
|
|
|
// ArrayRefs to the current line in the highlightings.
|
|
|
|
ArrayRef<HighlightingToken> NewLine(New.begin(),
|
2019-08-01 17:08:41 +08:00
|
|
|
/*length*/ static_cast<size_t>(0));
|
2019-08-01 16:08:44 +08:00
|
|
|
ArrayRef<HighlightingToken> OldLine(Old.begin(),
|
2019-08-01 17:08:41 +08:00
|
|
|
/*length*/ static_cast<size_t>(0));
|
2019-08-01 16:08:44 +08:00
|
|
|
auto NewEnd = New.end();
|
|
|
|
auto OldEnd = Old.end();
|
|
|
|
auto NextLineNumber = [&]() {
|
|
|
|
int NextNew = NewLine.end() != NewEnd ? NewLine.end()->R.start.line
|
2019-08-28 03:39:11 +08:00
|
|
|
: std::numeric_limits<int>::max();
|
2019-08-01 16:08:44 +08:00
|
|
|
int NextOld = OldLine.end() != OldEnd ? OldLine.end()->R.start.line
|
2019-08-28 03:39:11 +08:00
|
|
|
: std::numeric_limits<int>::max();
|
2019-08-01 16:08:44 +08:00
|
|
|
return std::min(NextNew, NextOld);
|
|
|
|
};
|
|
|
|
|
2019-08-26 16:38:45 +08:00
|
|
|
for (int LineNumber = 0; NewLine.end() < NewEnd || OldLine.end() < OldEnd;
|
2019-08-01 16:08:44 +08:00
|
|
|
LineNumber = NextLineNumber()) {
|
|
|
|
NewLine = takeLine(New, NewLine.end(), LineNumber);
|
|
|
|
OldLine = takeLine(Old, OldLine.end(), LineNumber);
|
|
|
|
if (NewLine != OldLine)
|
|
|
|
DiffedLines.push_back({LineNumber, NewLine});
|
|
|
|
}
|
|
|
|
|
|
|
|
return DiffedLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const HighlightingToken &L, const HighlightingToken &R) {
|
|
|
|
return std::tie(L.R, L.Kind) == std::tie(R.R, R.Kind);
|
|
|
|
}
|
|
|
|
bool operator<(const HighlightingToken &L, const HighlightingToken &R) {
|
|
|
|
return std::tie(L.R, L.Kind) < std::tie(R.R, R.Kind);
|
|
|
|
}
|
|
|
|
bool operator==(const LineHighlightings &L, const LineHighlightings &R) {
|
|
|
|
return std::tie(L.Line, L.Tokens) == std::tie(R.Line, R.Tokens);
|
2019-06-26 21:08:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST) {
|
|
|
|
return HighlightingTokenCollector(AST).collectTokens();
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:53:12 +08:00
|
|
|
std::vector<SemanticHighlightingInformation>
|
2019-08-01 16:08:44 +08:00
|
|
|
toSemanticHighlightingInformation(llvm::ArrayRef<LineHighlightings> Tokens) {
|
2019-07-04 15:53:12 +08:00
|
|
|
if (Tokens.size() == 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// FIXME: Tokens might be multiple lines long (block comments) in this case
|
|
|
|
// this needs to add multiple lines for those tokens.
|
|
|
|
std::vector<SemanticHighlightingInformation> Lines;
|
2019-08-01 16:08:44 +08:00
|
|
|
Lines.reserve(Tokens.size());
|
|
|
|
for (const auto &Line : Tokens) {
|
2019-07-04 15:53:12 +08:00
|
|
|
llvm::SmallVector<char, 128> LineByteTokens;
|
|
|
|
llvm::raw_svector_ostream OS(LineByteTokens);
|
2019-08-01 16:08:44 +08:00
|
|
|
for (const auto &Token : Line.Tokens) {
|
2019-07-04 15:53:12 +08:00
|
|
|
// Writes the token to LineByteTokens in the byte format specified by the
|
|
|
|
// LSP proposal. Described below.
|
|
|
|
// |<---- 4 bytes ---->|<-- 2 bytes -->|<--- 2 bytes -->|
|
|
|
|
// | character | length | index |
|
|
|
|
|
|
|
|
write32be(Token.R.start.character, OS);
|
|
|
|
write16be(Token.R.end.character - Token.R.start.character, OS);
|
|
|
|
write16be(static_cast<int>(Token.Kind), OS);
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:08:44 +08:00
|
|
|
Lines.push_back({Line.Line, encodeBase64(LineByteTokens)});
|
2019-07-04 15:53:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Lines;
|
|
|
|
}
|
|
|
|
|
2019-07-04 20:27:21 +08:00
|
|
|
llvm::StringRef toTextMateScope(HighlightingKind Kind) {
|
2019-07-04 15:53:12 +08:00
|
|
|
// FIXME: Add scopes for C and Objective C.
|
2019-07-04 20:27:21 +08:00
|
|
|
switch (Kind) {
|
|
|
|
case HighlightingKind::Function:
|
|
|
|
return "entity.name.function.cpp";
|
2019-07-15 16:12:21 +08:00
|
|
|
case HighlightingKind::Method:
|
|
|
|
return "entity.name.function.method.cpp";
|
2019-08-30 11:37:24 +08:00
|
|
|
case HighlightingKind::StaticMethod:
|
|
|
|
return "entity.name.function.method.static.cpp";
|
2019-07-04 20:27:21 +08:00
|
|
|
case HighlightingKind::Variable:
|
2019-07-15 16:12:21 +08:00
|
|
|
return "variable.other.cpp";
|
2019-08-28 03:39:11 +08:00
|
|
|
case HighlightingKind::LocalVariable:
|
|
|
|
return "variable.other.local.cpp";
|
2019-08-19 15:51:39 +08:00
|
|
|
case HighlightingKind::Parameter:
|
|
|
|
return "variable.parameter.cpp";
|
2019-07-15 16:12:21 +08:00
|
|
|
case HighlightingKind::Field:
|
|
|
|
return "variable.other.field.cpp";
|
2019-08-30 11:37:24 +08:00
|
|
|
case HighlightingKind::StaticField:
|
|
|
|
return "variable.other.field.static.cpp";
|
2019-07-10 16:41:25 +08:00
|
|
|
case HighlightingKind::Class:
|
|
|
|
return "entity.name.type.class.cpp";
|
|
|
|
case HighlightingKind::Enum:
|
|
|
|
return "entity.name.type.enum.cpp";
|
2019-07-15 15:41:12 +08:00
|
|
|
case HighlightingKind::EnumConstant:
|
|
|
|
return "variable.other.enummember.cpp";
|
2019-07-11 17:29:16 +08:00
|
|
|
case HighlightingKind::Namespace:
|
|
|
|
return "entity.name.namespace.cpp";
|
2019-07-18 17:56:38 +08:00
|
|
|
case HighlightingKind::TemplateParameter:
|
|
|
|
return "entity.name.type.template.cpp";
|
2019-08-08 21:10:30 +08:00
|
|
|
case HighlightingKind::Primitive:
|
|
|
|
return "storage.type.primitive.cpp";
|
2019-08-30 23:47:27 +08:00
|
|
|
case HighlightingKind::Macro:
|
|
|
|
return "entity.name.function.preprocessor.cpp";
|
2019-07-04 20:27:21 +08:00
|
|
|
case HighlightingKind::NumKinds:
|
|
|
|
llvm_unreachable("must not pass NumKinds to the function");
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled HighlightingKind");
|
2019-07-04 15:53:12 +08:00
|
|
|
}
|
|
|
|
|
2019-06-26 21:08:36 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|