feat: support more ast nodes to folding (#11)

This commit is contained in:
Myriad-Dreamin 2024-03-11 23:47:13 +08:00 committed by GitHub
parent 47a9b88c90
commit 3fe61971b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 78 additions and 14 deletions

View file

@ -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)]
@ -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));
}
@ -135,7 +142,7 @@ 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))?;
@ -143,10 +150,7 @@ pub(crate) fn get_lexical_hierarchy(
(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))?;
@ -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() {

View file

@ -0,0 +1,5 @@
#(
1,
2,
3,
)

View file

@ -0,0 +1,3 @@
#(
1
)

View file

@ -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
}
]

View file

@ -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,

View file

@ -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
---
[]

View file

@ -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 } =