diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp index bc46722c31..27d936b028 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp @@ -135,7 +135,7 @@ NonnullRefPtrVector ParserAutoComplete::get_available_declarations( return available_declarations; } -Vector ParserAutoComplete::autocomplete_name(const DocumentData& document, const ASTNode& node, const StringView& partial_text) const +Vector ParserAutoComplete::autocomplete_name(const DocumentData& document, const ASTNode& node, const String& partial_text) const { auto available_declarations = get_available_declarations(document, node); Vector available_names; @@ -166,7 +166,7 @@ Vector ParserAutoComplete::autocomplete_name(c return suggestions; } -Vector ParserAutoComplete::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const StringView partial_text) const +Vector ParserAutoComplete::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const String partial_text) const { auto type = type_of(document, *parent.m_object); if (type.is_null()) { diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.h b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.h index d06a416d9a..9c10b0b7c7 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.h +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.h @@ -81,8 +81,8 @@ private: OwnPtr m_parser; }; - Vector autocomplete_property(const DocumentData&, const MemberExpression&, const StringView partial_text) const; - Vector autocomplete_name(const DocumentData&, const ASTNode&, const StringView& partial_text) const; + Vector autocomplete_property(const DocumentData&, const MemberExpression&, const String partial_text) const; + Vector autocomplete_name(const DocumentData&, const ASTNode&, const String& partial_text) const; String type_of(const DocumentData&, const Expression&) const; String type_of_property(const DocumentData&, const Identifier&) const; String type_of_variable(const Identifier&) const; diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h index 7eef26643e..1dce0550a3 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -443,7 +443,7 @@ public: virtual const char* class_name() const override { return "StringLiteral"; } virtual void dump(size_t indent) const override; - StringView m_value; + String m_value; }; class ReturnStatement : public Statement { diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 56b1a421db..a477517813 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -40,7 +40,6 @@ namespace Cpp { Parser::Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& definitions) : m_program(program) , m_definitions(move(definitions)) - , m_lines(m_program.split_view("\n", true)) , m_filename(filename) { initialize_program_tokens(); @@ -702,30 +701,23 @@ StringView Parser::text_of_token(const Cpp::Token& token) const return token.text(); } -StringView Parser::text_of_node(const ASTNode& node) const +String Parser::text_of_node(const ASTNode& node) const { - return text_of_range(node.start(), node.end()); + return text_in_range(node.start(), node.end()); } -StringView Parser::text_of_range(Position start, Position end) const +String Parser::text_in_range(Position start, Position end) const { - if (start.line == end.line) { - VERIFY(start.column <= end.column); - return m_lines[start.line].substring_view(start.column, end.column - start.column + 1); + auto start_token_index = index_of_token_at(start); + auto end_node_index = index_of_token_at(end); + VERIFY(start_token_index.has_value()); + VERIFY(end_node_index.has_value()); + StringBuilder text; + for(size_t i = start_token_index.value(); i <= end_node_index.value(); ++i) + { + text.append(m_tokens[i].text()); } - - auto index_of_position([this](auto position) { - size_t start_index = 0; - for (size_t line = 0; line < position.line; ++line) { - start_index += m_lines[line].length() + 1; - } - start_index += position.column; - return start_index; - }); - auto start_index = index_of_position(start); - auto end_index = index_of_position(end); - VERIFY(end_index >= start_index); - return m_program.substring_view(start_index, end_index - start_index); + return text.build(); } void Parser::error(StringView message) @@ -807,10 +799,19 @@ Optional Parser::index_of_node_at(Position pos) const Optional Parser::token_at(Position pos) const { - for (auto& token : m_tokens) { + auto index = index_of_token_at(pos); + if (!index.has_value()) + return {}; + return m_tokens[index.value()]; +} + +Optional Parser::index_of_token_at(Position pos) const +{ + for (size_t token_index = 0; token_index < m_tokens.size(); ++token_index) { + auto token = m_tokens[token_index]; if (token.start() > pos || token.end() < pos) continue; - return token; + return token_index; } return {}; } @@ -880,7 +881,7 @@ NonnullRefPtr Parser::parse_string_literal(ASTNode& parent) Token start_token = m_tokens[start_token_index.value()]; Token end_token = m_tokens[end_token_index.value()]; - auto text = text_of_range(start_token.start(), end_token.end()); + auto text = text_in_range(start_token.start(), end_token.end()); auto string_literal = create_ast_node(parent, start_token.start(), end_token.end()); string_literal->m_value = text; return string_literal; diff --git a/Userland/Libraries/LibCpp/Parser.h b/Userland/Libraries/LibCpp/Parser.h index c78a901320..818818508c 100644 --- a/Userland/Libraries/LibCpp/Parser.h +++ b/Userland/Libraries/LibCpp/Parser.h @@ -27,15 +27,16 @@ #pragma once #include "AK/NonnullRefPtr.h" -#include #include "AST.h" #include "Preprocessor.h" +#include #include namespace Cpp { class Parser final { AK_MAKE_NONCOPYABLE(Parser); + public: explicit Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& = {}); ~Parser() = default; @@ -47,12 +48,13 @@ public: RefPtr node_at(Position) const; Optional index_of_node_at(Position) const; Optional token_at(Position) const; + Optional index_of_token_at(Position) const; RefPtr root_node() const { return m_root_node; } - StringView text_of_node(const ASTNode&) const; + String text_of_node(const ASTNode&) const; StringView text_of_token(const Cpp::Token& token) const; void print_tokens() const; Vector errors() const { return m_errors; } - const Preprocessor::Definitions& definitions() const {return m_definitions;} + const Preprocessor::Definitions& definitions() const { return m_definitions; } private: enum class DeclarationType { @@ -119,7 +121,7 @@ private: Token peek(size_t offset = 0) const; Optional peek(Token::Type) const; Position position() const; - StringView text_of_range(Position start, Position end) const; + String text_in_range(Position start, Position end) const; void save_state(); void load_state();