Skip to content

Commit 101de48

Browse files
committed
Fix performance issue
1 parent dfeada2 commit 101de48

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

crates/ark/src/lsp/folding_range.rs

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
//
77

8+
use std::borrow::Cow;
89
use std::cmp::Ordering;
910
use std::sync::LazyLock;
1011

@@ -81,15 +82,18 @@ fn parse_ts_node(
8182
}
8283

8384
// Nested comment section handling
84-
let comment_line = get_line_text(document, start.row, None, None);
85-
86-
if let Err(err) =
87-
nested_processor(comment_stack, folding_ranges, start.row, &comment_line)
88-
{
89-
lsp::log_error!("Can't process comment: {err:?}");
85+
if let Some(comment_line) = document.contents.get_line(start.row) {
86+
// O(n) if comment overlaps rope chunks, O(1) otherwise
87+
let comment_line: Cow<'_, str> = comment_line.into();
88+
89+
if let Err(err) =
90+
nested_processor(comment_stack, folding_ranges, start.row, &comment_line)
91+
{
92+
lsp::log_error!("Can't process comment: {err:?}");
93+
};
94+
region_processor(folding_ranges, region_marker, start.row, &comment_line);
95+
cell_processor(folding_ranges, cell_marker, start.row, &comment_line);
9096
};
91-
region_processor(folding_ranges, region_marker, start.row, &comment_line);
92-
cell_processor(folding_ranges, cell_marker, start.row, &comment_line);
9397
},
9498
_ => (),
9599
}
@@ -168,42 +172,13 @@ fn comment_range(start_line: usize, end_line: usize) -> FoldingRange {
168172
}
169173
}
170174

171-
fn get_line_text(
172-
document: &Document,
173-
line_num: usize,
174-
start_char: Option<usize>,
175-
end_char: Option<usize>,
176-
) -> String {
177-
let text = &document.contents;
178-
// Split the text into lines
179-
let lines: Vec<&str> = text.lines().filter_map(|line| line.as_str()).collect();
180-
181-
// Ensure the start_line is within bounds
182-
if line_num >= lines.len() {
183-
return String::new(); // Return an empty string if out of bounds
184-
}
185-
186-
// Get the line corresponding to start_line
187-
let line = lines[line_num];
188-
189-
// Determine the start and end character indices
190-
let start_idx = start_char.unwrap_or(0); // Default to 0 if None
191-
let end_idx = end_char.unwrap_or(line.len()); // Default to the line's length if None
192-
193-
// Ensure indices are within bounds for the line
194-
let start_idx = start_idx.min(line.len());
195-
let end_idx = end_idx.min(line.len());
196-
197-
// Extract the substring and return it
198-
line[start_idx..end_idx].to_string()
199-
}
200-
201175
fn count_leading_whitespaces(document: &Document, line_num: usize) -> usize {
202-
let line_text = get_line_text(document, line_num, None, None);
203-
line_text
204-
.as_bytes()
205-
.iter()
206-
.take_while(|&&b| b == b' ' || b == b'\t')
176+
let Some(line) = document.contents.get_line(line_num) else {
177+
return 0;
178+
};
179+
180+
line.bytes()
181+
.take_while(|&b| b == b' ' || b == b'\t')
207182
.count()
208183
}
209184

0 commit comments

Comments
 (0)