LanguageServers/Cpp: Add SemanticType::PreprocessorMacro

This adds a new semantic token type, PreprocessorMacro.

Calls to preprocessor macros will now be highlighted when semantic
highlighting is enabled in Hack Studio.
This commit is contained in:
Itamar 2022-03-31 19:09:29 +03:00 committed by Andreas Kling
parent 597ca68e2d
commit 9cd27d1e15
4 changed files with 18 additions and 5 deletions

View file

@ -149,8 +149,8 @@ void ConnectionFromClient::get_tokens_info(String const& filename)
return;
}
auto token_info = m_autocomplete_engine->get_tokens_info(filename);
async_tokens_info_result(move(token_info));
auto tokens_info = m_autocomplete_engine->get_tokens_info(filename);
async_tokens_info_result(move(tokens_info));
}
}

View file

@ -429,15 +429,21 @@ RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(const DocumentDa
Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_preprocessor_definition(const DocumentData& document, const GUI::TextPosition& text_position)
{
Position cpp_position { text_position.line(), text_position.column() };
auto substitution = find_preprocessor_substitution(document, cpp_position);
if (!substitution.has_value())
return {};
return GUI::AutocompleteProvider::ProjectLocation { substitution->defined_value.filename, substitution->defined_value.line, substitution->defined_value.column };
}
Optional<Cpp::Preprocessor::Substitution> CppComprehensionEngine::find_preprocessor_substitution(DocumentData const& document, Cpp::Position const& cpp_position)
{
// Search for a replaced preprocessor token that intersects with text_position
for (auto& substitution : document.preprocessor().substitutions()) {
if (substitution.original_tokens.first().start() > cpp_position)
continue;
if (substitution.original_tokens.first().end() < cpp_position)
continue;
return GUI::AutocompleteProvider::ProjectLocation { substitution.defined_value.filename, substitution.defined_value.line, substitution.defined_value.column };
return substitution;
}
return {};
}
@ -978,6 +984,9 @@ GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_t
GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_semantic_type_for_identifier(DocumentData const& document, Position position)
{
if (find_preprocessor_substitution(document, position).has_value())
return GUI::AutocompleteProvider::TokenInfo::SemanticType::PreprocessorMacro;
auto decl = find_declaration_of(document, GUI::TextPosition { position.line, position.column });
if (!decl)
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Identifier;

View file

@ -127,6 +127,7 @@ private:
Vector<StringView> scope_of_reference_to_symbol(const ASTNode&) const;
Optional<GUI::AutocompleteProvider::ProjectLocation> find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&);
Optional<Cpp::Preprocessor::Substitution> find_preprocessor_substitution(DocumentData const&, Cpp::Position const&);
OwnPtr<DocumentData> create_document_data(String&& text, const String& filename);
Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_property(const DocumentData&, const ASTNode&, Optional<Token> containing_token) const;

View file

@ -100,6 +100,8 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, GUI::
return { palette.syntax_member(), false };
case GUI::AutocompleteProvider::TokenInfo::SemanticType::Parameter:
return { palette.syntax_parameter(), false };
case GUI::AutocompleteProvider::TokenInfo::SemanticType::PreprocessorMacro:
return { palette.syntax_preprocessor_value(), false };
default:
VERIFY_NOT_REACHED();
return { palette.base_text(), false };
@ -152,7 +154,8 @@ bool SemanticSyntaxHighlighter::is_identifier(u64 token_type) const
|| type == AutocompleteProvider::TokenInfo::SemanticType::CustomType
|| type == AutocompleteProvider::TokenInfo::SemanticType::Namespace
|| type == AutocompleteProvider::TokenInfo::SemanticType::Member
|| type == AutocompleteProvider::TokenInfo::SemanticType::Parameter;
|| type == AutocompleteProvider::TokenInfo::SemanticType::Parameter
|| type == AutocompleteProvider::TokenInfo::SemanticType::PreprocessorMacro;
}
bool SemanticSyntaxHighlighter::is_navigatable(u64 token_type) const