mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:57:35 +00:00
HackStudio: Add tokens_info_result() and tokens_info_result() IPC calls
These IPC calls are used in the communication with the language server to fetch semantic information about the tokens in a code document.
This commit is contained in:
parent
76000e9137
commit
33043f269d
9 changed files with 130 additions and 2 deletions
|
@ -894,4 +894,79 @@ Optional<CppComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get
|
|||
return hint;
|
||||
}
|
||||
|
||||
Vector<GUI::AutocompleteProvider::TokenInfo> CppComprehensionEngine::get_tokens_info(const String& filename)
|
||||
{
|
||||
dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "CppComprehensionEngine::get_tokens_info: {}", filename);
|
||||
|
||||
const auto* document_ptr = get_or_create_document_data(filename);
|
||||
if (!document_ptr)
|
||||
return {};
|
||||
|
||||
const auto& document = *document_ptr;
|
||||
|
||||
Vector<GUI::AutocompleteProvider::TokenInfo> tokens_info;
|
||||
size_t i = 0;
|
||||
for (auto const& token : document.preprocessor().unprocessed_tokens()) {
|
||||
|
||||
tokens_info.append({ get_token_semantic_type(document, token),
|
||||
token.start().line, token.start().column, token.end().line, token.end().column });
|
||||
++i;
|
||||
}
|
||||
return tokens_info;
|
||||
}
|
||||
|
||||
GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_token_semantic_type(DocumentData const& document, Token const& token)
|
||||
{
|
||||
using GUI::AutocompleteProvider;
|
||||
switch (token.type()) {
|
||||
case Cpp::Token::Type::Identifier:
|
||||
return get_semantic_type_for_identifier(document, token.start());
|
||||
case Cpp::Token::Type::Keyword:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Keyword;
|
||||
case Cpp::Token::Type::KnownType:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Type;
|
||||
case Cpp::Token::Type::DoubleQuotedString:
|
||||
case Cpp::Token::Type::SingleQuotedString:
|
||||
case Cpp::Token::Type::RawString:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::String;
|
||||
case Cpp::Token::Type::Integer:
|
||||
case Cpp::Token::Type::Float:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Number;
|
||||
case Cpp::Token::Type::IncludePath:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::IncludePath;
|
||||
case Cpp::Token::Type::EscapeSequence:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Keyword;
|
||||
case Cpp::Token::Type::PreprocessorStatement:
|
||||
case Cpp::Token::Type::IncludeStatement:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::PreprocessorStatement;
|
||||
case Cpp::Token::Type::Comment:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Comment;
|
||||
default:
|
||||
return AutocompleteProvider::TokenInfo::SemanticType::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_semantic_type_for_identifier(DocumentData const& document, Position position)
|
||||
{
|
||||
auto decl = find_declaration_of(document, GUI::TextPosition { position.line, position.column });
|
||||
if (!decl)
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Identifier;
|
||||
|
||||
if (decl->is_function())
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Function;
|
||||
if (decl->is_parameter())
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Parameter;
|
||||
if (decl->is_variable_declaration()) {
|
||||
if (decl->is_member())
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Member;
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Variable;
|
||||
}
|
||||
if (decl->is_struct_or_class())
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::CustomType;
|
||||
if (decl->is_namespace())
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Namespace;
|
||||
|
||||
return GUI::AutocompleteProvider::TokenInfo::SemanticType::Identifier;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ public:
|
|||
virtual void file_opened([[maybe_unused]] const String& file) override;
|
||||
virtual Optional<GUI::AutocompleteProvider::ProjectLocation> find_declaration_of(const String& filename, const GUI::TextPosition& identifier_position) override;
|
||||
virtual Optional<FunctionParamsHint> get_function_params_hint(const String&, const GUI::TextPosition&) override;
|
||||
virtual Vector<GUI::AutocompleteProvider::TokenInfo> get_tokens_info(const String& filename) override;
|
||||
|
||||
private:
|
||||
struct SymbolName {
|
||||
|
@ -140,6 +141,9 @@ private:
|
|||
template<typename Func>
|
||||
void for_each_included_document_recursive(const DocumentData&, Func) const;
|
||||
|
||||
GUI::AutocompleteProvider::TokenInfo::SemanticType get_token_semantic_type(DocumentData const&, Token const&);
|
||||
GUI::AutocompleteProvider::TokenInfo::SemanticType get_semantic_type_for_identifier(DocumentData const&, Position);
|
||||
|
||||
HashMap<String, OwnPtr<DocumentData>> m_documents;
|
||||
|
||||
// A document's path will be in this set if we're currently processing it.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue