summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-05-09 21:40:27 +0300
committerAndreas Kling <kling@serenityos.org>2021-05-15 23:28:50 +0200
commit84e41c456542409fa4d51d856f03d908806c5e41 (patch)
treef3d8d75dbca46cc37d7d1e365b187854be587b23 /Userland/Libraries/LibCpp
parentf9b8e9c01c939f3680af5d5fb7821b57c0dafc95 (diff)
downloadserenity-84e41c456542409fa4d51d856f03d908806c5e41.zip
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.
Diffstat (limited to 'Userland/Libraries/LibCpp')
-rw-r--r--Userland/Libraries/LibCpp/Parser.cpp2
-rw-r--r--Userland/Libraries/LibCpp/Token.cpp5
-rw-r--r--Userland/Libraries/LibCpp/Token.h1
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<size_t> 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;
};