Skip to content

Commit

Permalink
Add foldingRange support for do/while loops
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Nov 5, 2023
1 parent add5985 commit 0548d53
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -13,7 +13,7 @@ These are the currently supported language server endpoints.
* `textDocument/didChange`
* `textDocument/documentSymbol`
* `textDocument/foldingRange`
* Currently only for function declarations and `if/else if/else`` statements
* Currently only for function declarations, `If/ElseIf/Else` statements, and `Do/While` loops
* `textDocument/hover`
* `textDocument/references`
* Currently only for functions
Expand Down
30 changes: 30 additions & 0 deletions src/server/state.rs
Expand Up @@ -363,6 +363,36 @@ impl ServerState {
}
}

// Find do while loops
let query = Query::new(get_quickbms_language(), r#"(do_statement) @do_statement"#).unwrap();

let mut query_cursor = QueryCursor::new();
let text_callback = |node: tree_sitter::Node| format!("{:?}", node); // TODO: placeholder

let matches = query_cursor.captures(&query, tree.root_node(), text_callback);
for (m, _) in matches {
let do_statement = m.captures[0].node;
let location = do_statement.range().to_location(url);

eprintln!("Do statement: {:?}", do_statement.to_sexp());

let bottom_location = do_statement
.children_by_field_name("body", &mut do_statement.walk())
.map(|c| c.range().to_location(url))
.max_by_key(|l| l.range.end.line);
eprintln!("bottom: {:?}", bottom_location);
if let Some(bottom_location) = bottom_location {
// For statement body
folding_ranges.push(FoldingRange {
start_line: location.range.start.line,
start_character: None,
end_line: bottom_location.range.end.line,
end_character: None,
kind: Some(FoldingRangeKind::Region),
});
}
}

Some(folding_ranges)
}
}
29 changes: 29 additions & 0 deletions tree-sitter-quickbms/test/corpus/do.bms
Expand Up @@ -6,6 +6,14 @@ Do
print "Hello"
While OFFSET < MAX_OFFSET

Do
print "Hello"
print "Hello"
print "Hello"
print "Hello"
print "Hello"
While OFFSET < MAX_OFFSET

--------------------------------------------------------------------------------

(source_file
Expand All @@ -17,4 +25,25 @@ While OFFSET < MAX_OFFSET
(while)
(identifier)
(comparison)
(identifier))
(do_statement
(do)
(print_statement
(print)
(string_literal))
(print_statement
(print)
(string_literal))
(print_statement
(print)
(string_literal))
(print_statement
(print)
(string_literal))
(print_statement
(print)
(string_literal))
(while)
(identifier)
(comparison)
(identifier)))

0 comments on commit 0548d53

Please sign in to comment.