From 150ba485ef3eb1c3d406ef7d5b7666d3265e172c Mon Sep 17 00:00:00 2001 From: jansul Date: Tue, 22 Aug 2023 12:27:13 +0100 Subject: [PATCH] Fix keywords and operators being kept when parsing a parent node fails (#630) Co-authored-by: Sijawusz Pur Rahnama --- .../language_server/semantic_tokens/component | 106 ++++++++++++++++++ src/parser.cr | 19 +++- 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 spec/language_server/semantic_tokens/component diff --git a/spec/language_server/semantic_tokens/component b/spec/language_server/semantic_tokens/component new file mode 100644 index 000000000..0357c4a26 --- /dev/null +++ b/spec/language_server/semantic_tokens/component @@ -0,0 +1,106 @@ +component Test { + property nextTitle : String = "World" + + /* + Comment that spans + multiple lines + */ + fun title : String { + nextTitle + } + + fun render { +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "semanticTokens": { + "dynamicRegistration": false, + "tokenTypes": ["property"] + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + } + }, + "method": "textDocument/semanticTokens/full" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "data": [ + 0, + 0, + 9, + 4, + 0, + 0, + 10, + 4, + 1, + 0, + 1, + 2, + 8, + 4, + 0, + 0, + 21, + 6, + 1, + 0, + 0, + 9, + 7, + 8, + 0, + 2, + 2, + 48, + 5, + 0, + 4, + 2, + 3, + 4, + 0, + 0, + 12, + 6, + 1, + 0, + 1, + 4, + 9, + 6, + 0, + 3, + 2, + 3, + 4, + 0, + 1, + 5, + 3, + 2, + 0 + ] + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/src/parser.cr b/src/parser.cr index c2d9083a8..30368009f 100644 --- a/src/parser.cr +++ b/src/parser.cr @@ -22,16 +22,29 @@ module Mint def start(&) start_position = position - node_size = ast.nodes.size + + nodes_size = ast.nodes.size + keywords_size = ast.keywords.size + operators_size = ast.operators.size begin node = yield position @position = start_position unless node - ast.nodes.delete_at(node_size...) unless node + + unless node + ast.nodes.delete_at(nodes_size...) + ast.keywords.delete_at(keywords_size...) + ast.operators.delete_at(operators_size...) + end + node rescue error : Error @position = start_position - ast.nodes.delete_at(node_size...) + + ast.nodes.delete_at(nodes_size...) + ast.keywords.delete_at(keywords_size...) + ast.operators.delete_at(operators_size...) + raise error end end