Skip to content

Commit

Permalink
folding but not right
Browse files Browse the repository at this point in the history
  • Loading branch information
kv9898 committed Oct 30, 2024
1 parent 688b46b commit e3e933b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
48 changes: 48 additions & 0 deletions crates/ark/src/lsp/folding_range-node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::result::Result::Ok;

use tower_lsp::lsp_types::FoldingRange;
use tower_lsp::lsp_types::FoldingRangeKind;
use tree_sitter::Node;

use crate::lsp::documents::Document;
use crate::lsp::log_info;
use crate::lsp::symbols::parse_comment_as_section;

/// Detects and returns folding ranges for comment sections and curly-bracketed blocks
pub fn folding_range(document: &Document) -> anyhow::Result<Vec<FoldingRange>> {
let ast = &document.ast;
let contents = &document.contents;
let node = ast.root_node();

match comment_node(&node, Vec::new(), contents) {
Ok(folding_ranges) => log_info!("folding_ranges: {:#?}", folding_ranges), //Ok(folding_ranges),
Err(err) => {
log::error!("Error processing node: {err:?}");
// return Ok(Vec::new());
},
}
let sample_folding_range = FoldingRange {
start_line: 0,
start_character: None,
end_line: 1,
end_character: None,
kind: Some(FoldingRangeKind::Comment),
collapsed_text: None,
};
Ok(vec![sample_folding_range])
}

fn comment_node(
node: &Node,
store: Vec<FoldingRange>,
contents: &Rope,
) -> anyhow::Result<Vec<FoldingRange>> {
Ok(match node.node_type() {
// Handle comment sections in expression lists
NodeType::Program | NodeType::BracedExpression => {
let cursor = node.walk();
let mut comment_stack: Vec<(usize, usize)> = Vec::new(); // a stack of (level, start_line) tuples
},
_ => store,
})
}
4 changes: 2 additions & 2 deletions crates/ark/src/lsp/folding_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::lsp::log_info;
use crate::lsp::symbols::parse_comment_as_section;

/// Detects and returns folding ranges for comment sections and curly-bracketed blocks
pub fn folding_range(document: &Document) -> Option<Vec<FoldingRange>> {
pub fn folding_range(document: &Document) -> anyhow::Result<Vec<FoldingRange>> {
let mut folding_ranges: Vec<FoldingRange> = Vec::new();
let text = &document.contents; // Assuming `contents()` gives the text of the document
let mut line_iter = text.lines().enumerate().peekable();
Expand All @@ -22,7 +22,7 @@ pub fn folding_range(document: &Document) -> Option<Vec<FoldingRange>> {

// TODO: End line handling

Some(folding_ranges)
Ok(folding_ranges)
}

fn comment_processor(
Expand Down
18 changes: 13 additions & 5 deletions crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,20 @@ pub(crate) fn handle_folding_range(
) -> anyhow::Result<Option<Vec<FoldingRange>>> {
let uri = params.text_document.uri;
let document = state.get_document(&uri)?;
let Some(foldings) = folding_range(document) else {
return Ok(None);
};

Ok(Some(foldings))
match folding_range(document) {
Ok(foldings) => Ok(Some(foldings)),
Err(err) => {
lsp::log_error!("{err:?}");
Ok(None)
},
}
}
// let Ok(foldings) = folding_range(document) else {
// return Ok(None);
// };

// Ok(Some(foldings))
// }

#[tracing::instrument(level = "info", skip_all)]
pub(crate) fn handle_references(
Expand Down

0 comments on commit e3e933b

Please sign in to comment.