From 9cd27d1e1578ce948694121479d9cbabe6795910 Mon Sep 17 00:00:00 2001 From: Itamar Date: Thu, 31 Mar 2022 19:09:29 +0300 Subject: [PATCH] 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. --- .../LanguageServers/ConnectionFromClient.cpp | 4 ++-- .../LanguageServers/Cpp/CppComprehensionEngine.cpp | 13 +++++++++++-- .../LanguageServers/Cpp/CppComprehensionEngine.h | 1 + .../Libraries/LibCpp/SemanticSyntaxHighlighter.cpp | 5 ++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Userland/DevTools/HackStudio/LanguageServers/ConnectionFromClient.cpp b/Userland/DevTools/HackStudio/LanguageServers/ConnectionFromClient.cpp index 741774ae69..d1f29a4199 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/ConnectionFromClient.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/ConnectionFromClient.cpp @@ -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)); } } diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp index c6560ec011..4485f2b3c1 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp @@ -429,15 +429,21 @@ RefPtr CppComprehensionEngine::find_declaration_of(const DocumentDa Optional 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 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; diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.h b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.h index 370c5c9939..91eb1f1b64 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.h +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.h @@ -127,6 +127,7 @@ private: Vector scope_of_reference_to_symbol(const ASTNode&) const; Optional find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&); + Optional find_preprocessor_substitution(DocumentData const&, Cpp::Position const&); OwnPtr create_document_data(String&& text, const String& filename); Optional> try_autocomplete_property(const DocumentData&, const ASTNode&, Optional containing_token) const; diff --git a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp index 30200227ea..6b39a4746c 100644 --- a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp +++ b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp @@ -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