Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 84ac4ad

Browse files
committed
Fix TextEdit calculation.
Rather than calculating a column, use line+1, column 0 as the end of the Range and include the final newline in the output text.
1 parent fc9b527 commit 84ac4ad

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

rls/src/actions/format.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ impl Rustfmt {
7878
NewlineStyle::Native => native,
7979
};
8080

81-
let lsp_line_length = |line: &str| line.chars().map(char::len_utf16).sum();
82-
let line_cols: Vec<usize> = input.lines().map(lsp_line_length).collect();
83-
8481
let output = self.format(input, cfg)?;
8582
let ModifiedLines { chunks } = output.parse().map_err(|_| Error::Failed)?;
8683

@@ -89,23 +86,18 @@ impl Rustfmt {
8986
.map(|item| {
9087
// Rustfmt's line indices are 1-based
9188
let start_line = u64::from(item.line_number_orig) - 1;
92-
let end_line = {
93-
// Could underflow if we don't remove lines and there's only one
94-
let removed = u64::from(item.lines_removed).saturating_sub(1);
95-
start_line + removed
96-
};
97-
let end_col: Option<usize> = line_cols.get(end_line as usize).copied();
98-
let end_col: u64 = end_col.map(|col| col as u64).unwrap_or_else(u64::max_value);
89+
let end_line = start_line + u64::from(item.lines_removed);
9990

10091
TextEdit {
10192
range: Range {
10293
start: Position::new(start_line, 0),
103-
// We don't extend the range past the last line because
104-
// sometimes it may not exist, skewing the diff and
105-
// making us add an invalid additional trailing newline.
106-
end: Position::new(end_line, end_col),
94+
end: Position::new(end_line, 0),
10795
},
108-
new_text: item.lines.join(newline),
96+
new_text: item.lines.iter().fold(String::new(), |mut acc, s| {
97+
acc.push_str(s);
98+
acc.push_str(newline);
99+
acc
100+
}),
109101
}
110102
})
111103
.collect())

0 commit comments

Comments
 (0)