From 84e41c456542409fa4d51d856f03d908806c5e41 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sun, 9 May 2021 21:40:27 +0300 Subject: [PATCH] LibCpp: Modify logic of Parser::index_of_node_at After this commit, Parser::index_of_node_at will prefer to return nodes with greater indices. Since the parsing logic ensures that child nodes come after parent nodes, this change makes this function return child nodes when possible. --- Userland/Libraries/LibCpp/Parser.cpp | 2 +- Userland/Libraries/LibCpp/Token.cpp | 5 ++++- Userland/Libraries/LibCpp/Token.h | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 12224b6e5d..9619abab04 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -899,7 +899,7 @@ Optional Parser::index_of_node_at(Position pos) const if (node.start() > pos || node.end() < pos) continue; - if (!match_node_index.has_value() || (node_span(node) < node_span(m_state.nodes[match_node_index.value()]))) + if (!match_node_index.has_value() || (node_span(node) <= node_span(m_state.nodes[match_node_index.value()]))) match_node_index = node_index; } return match_node_index; diff --git a/Userland/Libraries/LibCpp/Token.cpp b/Userland/Libraries/LibCpp/Token.cpp index f069bf454a..06c21fcffe 100644 --- a/Userland/Libraries/LibCpp/Token.cpp +++ b/Userland/Libraries/LibCpp/Token.cpp @@ -20,5 +20,8 @@ bool Position::operator==(const Position& other) const { return line == other.line && column == other.column; } - +bool Position::operator<=(const Position& other) const +{ + return !(*this > other); +} } diff --git a/Userland/Libraries/LibCpp/Token.h b/Userland/Libraries/LibCpp/Token.h index 9ead283011..3f9253b06a 100644 --- a/Userland/Libraries/LibCpp/Token.h +++ b/Userland/Libraries/LibCpp/Token.h @@ -84,6 +84,7 @@ struct Position { size_t column { 0 }; bool operator<(const Position&) const; + bool operator<=(const Position&) const; bool operator>(const Position&) const; bool operator==(const Position&) const; };