Fix ESLint linebreak-style errors by preserving line endings in LSP communication (#38773)
Closes https://github.com/zed-industries/zed/issues/38453 Current `Buffer` API only allows getting buffer text with `\n` line breaks — even if the `\r\n` was used in the original file's text. This it not correct in certain cases like LSP formatting, where language servers need to have original document context for e.g. formatting purposes. Added new `Buffer` API, replaced all buffer LSP registration places with the new one and added more tests. Release Notes: - Fixed ESLint linebreak-style errors by preserving line endings in LSP communication --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
co-authored by
Claude
Kirill Bulatov
parent
55bc679c19
commit
435eab6896
+156
-1
@@ -5,11 +5,14 @@ mod point_utf16;
|
||||
mod unclipped;
|
||||
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator as _};
|
||||
use regex::Regex;
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
cmp, fmt, io, mem,
|
||||
ops::{self, AddAssign, Range},
|
||||
str,
|
||||
sync::{Arc, LazyLock},
|
||||
};
|
||||
use sum_tree::{Bias, Dimension, Dimensions, SumTree};
|
||||
|
||||
@@ -21,6 +24,95 @@ pub use unclipped::Unclipped;
|
||||
|
||||
use crate::chunk::Bitmap;
|
||||
|
||||
static LINE_SEPARATORS_REGEX: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"\r\n|\r").expect("Failed to create LINE_SEPARATORS_REGEX"));
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum LineEnding {
|
||||
Unix,
|
||||
Windows,
|
||||
}
|
||||
|
||||
impl Default for LineEnding {
|
||||
fn default() -> Self {
|
||||
#[cfg(unix)]
|
||||
return Self::Unix;
|
||||
|
||||
#[cfg(not(unix))]
|
||||
return Self::Windows;
|
||||
}
|
||||
}
|
||||
|
||||
impl LineEnding {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
LineEnding::Unix => "\n",
|
||||
LineEnding::Windows => "\r\n",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(&self) -> &'static str {
|
||||
match self {
|
||||
LineEnding::Unix => "LF",
|
||||
LineEnding::Windows => "CRLF",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn detect(text: &str) -> Self {
|
||||
let mut max_ix = cmp::min(text.len(), 1000);
|
||||
while !text.is_char_boundary(max_ix) {
|
||||
max_ix -= 1;
|
||||
}
|
||||
|
||||
if let Some(ix) = text[..max_ix].find(['\n']) {
|
||||
if ix > 0 && text.as_bytes()[ix - 1] == b'\r' {
|
||||
Self::Windows
|
||||
} else {
|
||||
Self::Unix
|
||||
}
|
||||
} else {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize(text: &mut String) {
|
||||
if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(text, "\n") {
|
||||
*text = replaced;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_arc(text: Arc<str>) -> Arc<str> {
|
||||
if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") {
|
||||
replaced.into()
|
||||
} else {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_cow(text: Cow<str>) -> Cow<str> {
|
||||
if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") {
|
||||
replaced.into()
|
||||
} else {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts text chunks into a [`String`] using the current line ending.
|
||||
pub fn into_string(&self, chunks: Chunks<'_>) -> String {
|
||||
match self {
|
||||
LineEnding::Unix => chunks.collect(),
|
||||
LineEnding::Windows => {
|
||||
let line_ending = self.as_str();
|
||||
let mut result = String::new();
|
||||
for chunk in chunks {
|
||||
result.push_str(&chunk.replace('\n', line_ending));
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Rope {
|
||||
chunks: SumTree<Chunk>,
|
||||
@@ -370,6 +462,16 @@ impl Rope {
|
||||
Chunks::new(self, range, true)
|
||||
}
|
||||
|
||||
/// Formats the rope's text with the specified line ending string.
|
||||
/// This replaces all `\n` characters with the provided line ending.
|
||||
///
|
||||
/// The rope internally stores all line breaks as `\n` (see `Display` impl).
|
||||
/// Use this method to convert to different line endings for file operations,
|
||||
/// LSP communication, or other scenarios requiring specific line ending formats.
|
||||
pub fn to_string_with_line_ending(&self, line_ending: LineEnding) -> String {
|
||||
line_ending.into_string(self.chunks())
|
||||
}
|
||||
|
||||
pub fn offset_to_offset_utf16(&self, offset: usize) -> OffsetUtf16 {
|
||||
if offset >= self.summary().len {
|
||||
return self.summary().len_utf16;
|
||||
@@ -611,10 +713,16 @@ impl From<&String> for Rope {
|
||||
}
|
||||
}
|
||||
|
||||
/// Display implementation for Rope.
|
||||
///
|
||||
/// Note: This always uses `\n` as the line separator, regardless of the original
|
||||
/// file's line endings. The rope internally normalizes all line breaks to `\n`.
|
||||
/// If you need to preserve original line endings (e.g., for LSP communication),
|
||||
/// use `to_string_with_line_ending` instead.
|
||||
impl fmt::Display for Rope {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for chunk in self.chunks() {
|
||||
write!(f, "{}", chunk)?;
|
||||
write!(f, "{chunk}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -2264,6 +2372,53 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_string_with_line_ending() {
|
||||
// Test Unix line endings (no conversion)
|
||||
let rope = Rope::from("line1\nline2\nline3");
|
||||
assert_eq!(
|
||||
rope.to_string_with_line_ending(LineEnding::Unix),
|
||||
"line1\nline2\nline3"
|
||||
);
|
||||
|
||||
// Test Windows line endings
|
||||
assert_eq!(
|
||||
rope.to_string_with_line_ending(LineEnding::Windows),
|
||||
"line1\r\nline2\r\nline3"
|
||||
);
|
||||
|
||||
// Test empty rope
|
||||
let empty_rope = Rope::from("");
|
||||
assert_eq!(
|
||||
empty_rope.to_string_with_line_ending(LineEnding::Windows),
|
||||
""
|
||||
);
|
||||
|
||||
// Test single line (no newlines)
|
||||
let single_line = Rope::from("single line");
|
||||
assert_eq!(
|
||||
single_line.to_string_with_line_ending(LineEnding::Windows),
|
||||
"single line"
|
||||
);
|
||||
|
||||
// Test rope ending with newline
|
||||
let ending_newline = Rope::from("line1\nline2\n");
|
||||
assert_eq!(
|
||||
ending_newline.to_string_with_line_ending(LineEnding::Windows),
|
||||
"line1\r\nline2\r\n"
|
||||
);
|
||||
|
||||
// Test large rope with multiple chunks
|
||||
let mut large_rope = Rope::new();
|
||||
for i in 0..100 {
|
||||
large_rope.push(&format!("line{}\n", i));
|
||||
}
|
||||
let result = large_rope.to_string_with_line_ending(LineEnding::Windows);
|
||||
assert!(result.contains("\r\n"));
|
||||
assert!(!result.contains("\n\n"));
|
||||
assert_eq!(result.matches("\r\n").count(), 100);
|
||||
}
|
||||
|
||||
fn clip_offset(text: &str, mut offset: usize, bias: Bias) -> usize {
|
||||
while !text.is_char_boundary(offset) {
|
||||
match bias {
|
||||
|
||||
Reference in New Issue
Block a user