mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-03 01:42:14 +00:00
feat: support more ast nodes to folding (#11)
This commit is contained in:
parent
47a9b88c90
commit
3fe61971b5
7 changed files with 78 additions and 14 deletions
|
@ -33,7 +33,21 @@ impl TryFrom<LexicalKind> for SymbolKind {
|
||||||
pub(crate) enum LexicalScopeKind {
|
pub(crate) enum LexicalScopeKind {
|
||||||
#[default]
|
#[default]
|
||||||
Symbol,
|
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)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
|
@ -75,13 +89,6 @@ pub(crate) fn get_lexical_hierarchy(
|
||||||
let (symbol, children) = self.stack.pop().unwrap();
|
let (symbol, children) = self.stack.pop().unwrap();
|
||||||
let current = &mut self.stack.last_mut().unwrap().1;
|
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));
|
current.push(symbreak(symbol, children));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +142,7 @@ pub(crate) fn get_lexical_hierarchy(
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
fn get_ident(node: &LinkedNode, g: LexicalScopeKind) -> anyhow::Result<Option<LexicalInfo>> {
|
fn get_ident(node: &LinkedNode, g: LexicalScopeKind) -> anyhow::Result<Option<LexicalInfo>> {
|
||||||
let (name, kind) = match node.kind() {
|
let (name, kind) = match node.kind() {
|
||||||
SyntaxKind::Label if LexicalScopeKind::Block != g => {
|
SyntaxKind::Label if g.affect_symbol() => {
|
||||||
let ast_node = node
|
let ast_node = node
|
||||||
.cast::<ast::Label>()
|
.cast::<ast::Label>()
|
||||||
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
|
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
|
||||||
|
@ -143,10 +150,7 @@ pub(crate) fn get_lexical_hierarchy(
|
||||||
|
|
||||||
(name, LexicalKind::Constant)
|
(name, LexicalKind::Constant)
|
||||||
}
|
}
|
||||||
SyntaxKind::CodeBlock | SyntaxKind::ContentBlock if LexicalScopeKind::Symbol != g => {
|
SyntaxKind::Ident if g.affect_symbol() => {
|
||||||
(String::new(), LexicalKind::Block)
|
|
||||||
}
|
|
||||||
SyntaxKind::Ident if LexicalScopeKind::Block != g => {
|
|
||||||
let ast_node = node
|
let ast_node = node
|
||||||
.cast::<ast::Ident>()
|
.cast::<ast::Ident>()
|
||||||
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
|
.ok_or_else(|| anyhow!("cast to ast node failed: {:?}", node))?;
|
||||||
|
@ -173,6 +177,24 @@ pub(crate) fn get_lexical_hierarchy(
|
||||||
|
|
||||||
(name, kind)
|
(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 => {
|
SyntaxKind::Markup => {
|
||||||
let name = node.get().to_owned().into_text().to_string();
|
let name = node.get().to_owned().into_text().to_string();
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#(
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
)
|
|
@ -0,0 +1,3 @@
|
||||||
|
#(
|
||||||
|
1
|
||||||
|
)
|
|
@ -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
|
||||||
|
}
|
||||||
|
]
|
|
@ -4,6 +4,20 @@ expression: "JsonRepr::new_pure(result.unwrap())"
|
||||||
input_file: crates/tinymist-query/src/fixtures/folding_range/headings-in-blocks.typ
|
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",
|
"collapsedText": "Heading 2",
|
||||||
"endCharacter": 16,
|
"endCharacter": 16,
|
||||||
|
|
|
@ -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
|
||||||
|
---
|
||||||
|
[]
|
|
@ -17,7 +17,7 @@ impl FoldingRangeRequest {
|
||||||
) -> Option<Vec<FoldingRange>> {
|
) -> Option<Vec<FoldingRange>> {
|
||||||
let line_folding_only = self.line_folding_only;
|
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 mut results = vec![];
|
||||||
let LspPosition { line, character } =
|
let LspPosition { line, character } =
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue