Skip to content

Commit 27aeede

Browse files
cleaning
1 parent 0780a92 commit 27aeede

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

crates/djls-workspace/src/document.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,16 @@ impl TextDocument {
101101
}
102102

103103
// Incremental path: apply changes to rebuild the document
104-
// Clone current content and apply each change
105104
let mut new_content = self.content.clone();
106105

107106
for change in changes {
108107
if let Some(range) = change.range {
109108
// Convert LSP range to byte offsets using the negotiated encoding
110109
let start_offset =
111-
self.line_index
112-
.offset(range.start, &new_content, encoding)
113-
as usize;
114-
let end_offset =
115-
self.line_index
116-
.offset(range.end, &new_content, encoding)
117-
as usize;
118-
119-
// Apply the change by replacing the range
110+
self.line_index.offset(range.start, &new_content, encoding) as usize;
111+
let end_offset = self.line_index.offset(range.end, &new_content, encoding) as usize;
112+
113+
// Apply change
120114
new_content.replace_range(start_offset..end_offset, &change.text);
121115

122116
// Rebuild line index after each change since positions shift
@@ -129,7 +123,6 @@ impl TextDocument {
129123
}
130124
}
131125

132-
// Store the rebuilt document
133126
self.content = new_content;
134127
self.version = version;
135128
}
@@ -164,16 +157,15 @@ pub struct LineIndex {
164157
impl LineIndex {
165158
#[must_use]
166159
pub fn new(text: &str) -> Self {
167-
let mut line_starts = vec![0];
168-
let mut pos_utf8 = 0;
169-
170-
// Check if text is pure ASCII for optimization
171160
let kind = if text.is_ascii() {
172161
IndexKind::Ascii
173162
} else {
174163
IndexKind::Utf8
175164
};
176165

166+
let mut line_starts = vec![0];
167+
let mut pos_utf8 = 0;
168+
177169
for c in text.chars() {
178170
pos_utf8 += u32::try_from(c.len_utf8()).unwrap_or(0);
179171
if c == '\n' {
@@ -198,12 +190,10 @@ impl LineIndex {
198190
None => return self.length, // Past end of document
199191
};
200192

201-
// If position is at start of line, return line start
202193
if position.character == 0 {
203194
return line_start_utf8;
204195
}
205196

206-
// Find the line text
207197
let next_line_start = self
208198
.line_starts
209199
.get(position.line as usize + 1)
@@ -214,16 +204,14 @@ impl LineIndex {
214204
return line_start_utf8;
215205
};
216206

217-
// ASCII fast path optimization
207+
// Fast path optimization for ASCII text, all encodings are equivalent to byte offsets
218208
if matches!(self.kind, IndexKind::Ascii) {
219-
// For ASCII text, all encodings are equivalent to byte offsets
220209
let char_offset = position
221210
.character
222211
.min(u32::try_from(line_text.len()).unwrap_or(u32::MAX));
223212
return line_start_utf8 + char_offset;
224213
}
225214

226-
// Handle different encodings for non-ASCII text
227215
match encoding {
228216
PositionEncoding::Utf8 => {
229217
// UTF-8: character positions are already byte offsets

0 commit comments

Comments
 (0)