Skip to content

Commit

Permalink
feat: support more ast nodes to folding (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Mar 11, 2024
1 parent 47a9b88 commit 3fe6197
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
48 changes: 35 additions & 13 deletions crates/tinymist-query/src/analysis/lexical_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ impl TryFrom<LexicalKind> for SymbolKind {
pub(crate) enum LexicalScopeKind {
#[default]
Symbol,
Block,
Braced,
}

impl LexicalScopeKind {
fn affect_symbol(&self) -> bool {
matches!(self, LexicalScopeKind::Symbol)
}

fn affect_block(&self) -> bool {
matches!(self, LexicalScopeKind::Braced)
}

fn affect_expr(&self) -> bool {
matches!(self, LexicalScopeKind::Braced)
}
}

#[derive(Debug, Clone, Hash)]
Expand Down Expand Up @@ -75,13 +89,6 @@ pub(crate) fn get_lexical_hierarchy(
let (symbol, children) = self.stack.pop().unwrap();
let current = &mut self.stack.last_mut().unwrap().1;

// symbol.wide_range = children
// .iter()
// .map(|c| c.info.wide_range.clone())
// .fold(symbol.range.clone(), |acc, r| {
// acc.start.min(r.start)..acc.end.max(r.end)
// });

current.push(symbreak(symbol, children));
}

Expand Down Expand Up @@ -135,18 +142,15 @@ pub(crate) fn get_lexical_hierarchy(
#[allow(deprecated)]
fn get_ident(node: &LinkedNode, g: LexicalScopeKind) -> anyhow::Result<Option<LexicalInfo>> {
let (name, kind) = match node.kind() {
SyntaxKind::Label if LexicalScopeKind::Block != g => {
SyntaxKind::Label if g.affect_symbol() => {
let ast_node = node
.cast::<ast::Label>()
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
let name = ast_node.get().to_string();

(name, LexicalKind::Constant)
}
SyntaxKind::CodeBlock | SyntaxKind::ContentBlock if LexicalScopeKind::Symbol != g => {
(String::new(), LexicalKind::Block)
}
SyntaxKind::Ident if LexicalScopeKind::Block != g => {
SyntaxKind::Ident if g.affect_symbol() => {
let ast_node = node
.cast::<ast::Ident>()
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
Expand All @@ -173,6 +177,24 @@ pub(crate) fn get_lexical_hierarchy(

(name, kind)
}
SyntaxKind::Equation
| SyntaxKind::Raw
| SyntaxKind::CodeBlock
| SyntaxKind::ContentBlock
| SyntaxKind::BlockComment
if g.affect_block() =>
{
(String::new(), LexicalKind::Block)
}
SyntaxKind::Parenthesized
| SyntaxKind::Destructuring
| SyntaxKind::Args
| SyntaxKind::Array
| SyntaxKind::Dict
if g.affect_expr() =>
{
(String::new(), LexicalKind::Block)
}
SyntaxKind::Markup => {
let name = node.get().to_owned().into_text().to_string();
if name.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#(
1,
2,
3,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#(
1
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/tinymist-query/src/folding_range.rs
expression: "JsonRepr::new_pure(result.unwrap())"
input_file: crates/tinymist-query/src/fixtures/folding_range/array_folding.typ
---
[
{
"collapsedText": "",
"endCharacter": 1,
"endLine": 4,
"startCharacter": 1,
"startLine": 0
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ expression: "JsonRepr::new_pure(result.unwrap())"
input_file: crates/tinymist-query/src/fixtures/folding_range/headings-in-blocks.typ
---
[
{
"collapsedText": "",
"endCharacter": 11,
"endLine": 2,
"startCharacter": 8,
"startLine": 2
},
{
"collapsedText": "",
"endCharacter": 13,
"endLine": 6,
"startCharacter": 10,
"startLine": 6
},
{
"collapsedText": "Heading 2",
"endCharacter": 16,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/tinymist-query/src/folding_range.rs
expression: "JsonRepr::new_pure(result.unwrap())"
input_file: crates/tinymist-query/src/fixtures/folding_range/paren_folding.typ
---
[]
2 changes: 1 addition & 1 deletion crates/tinymist-query/src/folding_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl FoldingRangeRequest {
) -> Option<Vec<FoldingRange>> {
let line_folding_only = self.line_folding_only;

let symbols = get_lexical_hierarchy(source.clone(), LexicalScopeKind::Block)?;
let symbols = get_lexical_hierarchy(source.clone(), LexicalScopeKind::Braced)?;

let mut results = vec![];
let LspPosition { line, character } =
Expand Down

0 comments on commit 3fe6197

Please sign in to comment.