diff --git a/README.md b/README.md index 42b14da..85f7d92 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/server/state.rs b/src/server/state.rs index 26c0c06..7e7773f 100644 --- a/src/server/state.rs +++ b/src/server/state.rs @@ -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) } } diff --git a/tree-sitter-quickbms/test/corpus/do.bms b/tree-sitter-quickbms/test/corpus/do.bms index 01fe73f..8a1786a 100644 --- a/tree-sitter-quickbms/test/corpus/do.bms +++ b/tree-sitter-quickbms/test/corpus/do.bms @@ -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 @@ -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)))