Detect buffer newline style and honor it when saving

This commit is contained in:
Antonio Scandurra 2022-07-04 17:25:19 +02:00
parent f9bad2d81d
commit 3480b50920
8 changed files with 136 additions and 33 deletions

View file

@ -22,7 +22,7 @@ use gpui::{
}; };
use language::{ use language::{
range_to_lsp, tree_sitter_rust, Diagnostic, DiagnosticEntry, FakeLspAdapter, Language, range_to_lsp, tree_sitter_rust, Diagnostic, DiagnosticEntry, FakeLspAdapter, Language,
LanguageConfig, LanguageRegistry, OffsetRangeExt, Point, Rope, LanguageConfig, LanguageRegistry, NewlineStyle, OffsetRangeExt, Point, Rope,
}; };
use lsp::{self, FakeLanguageServer}; use lsp::{self, FakeLanguageServer};
use parking_lot::Mutex; use parking_lot::Mutex;
@ -1263,7 +1263,11 @@ async fn test_buffer_reloading(cx_a: &mut TestAppContext, cx_b: &mut TestAppCont
client_a client_a
.fs .fs
.save("/dir/a.txt".as_ref(), &"new contents".into()) .save(
"/dir/a.txt".as_ref(),
&"new contents".into(),
NewlineStyle::Unix,
)
.await .await
.unwrap(); .unwrap();
buffer_b buffer_b
@ -1857,7 +1861,11 @@ async fn test_reloading_buffer_manually(cx_a: &mut TestAppContext, cx_b: &mut Te
client_a client_a
.fs .fs
.save("/a/a.rs".as_ref(), &Rope::from("let seven = 7;")) .save(
"/a/a.rs".as_ref(),
&Rope::from("let seven = 7;"),
NewlineStyle::Unix,
)
.await .await
.unwrap(); .unwrap();
buffer_a buffer_a

View file

@ -53,6 +53,7 @@ pub struct Buffer {
saved_version: clock::Global, saved_version: clock::Global,
saved_version_fingerprint: String, saved_version_fingerprint: String,
saved_mtime: SystemTime, saved_mtime: SystemTime,
newline_style: NewlineStyle,
transaction_depth: usize, transaction_depth: usize,
was_dirty_before_starting_transaction: Option<bool>, was_dirty_before_starting_transaction: Option<bool>,
language: Option<Arc<Language>>, language: Option<Arc<Language>>,
@ -97,6 +98,12 @@ pub enum IndentKind {
Tab, Tab,
} }
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum NewlineStyle {
Unix,
Windows,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct SelectionSet { struct SelectionSet {
line_mode: bool, line_mode: bool,
@ -194,6 +201,7 @@ pub trait File: Send + Sync {
buffer_id: u64, buffer_id: u64,
text: Rope, text: Rope,
version: clock::Global, version: clock::Global,
newline_style: NewlineStyle,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> Task<Result<(clock::Global, String, SystemTime)>>; ) -> Task<Result<(clock::Global, String, SystemTime)>>;
@ -309,13 +317,12 @@ impl Buffer {
base_text: T, base_text: T,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Self { ) -> Self {
let history = History::new(base_text.into());
let newline_style = NewlineStyle::detect(&history.base_text);
Self::build( Self::build(
TextBuffer::new( TextBuffer::new(replica_id, cx.model_id() as u64, history),
replica_id,
cx.model_id() as u64,
History::new(base_text.into()),
),
None, None,
newline_style,
) )
} }
@ -325,13 +332,12 @@ impl Buffer {
file: Arc<dyn File>, file: Arc<dyn File>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Self { ) -> Self {
let history = History::new(base_text.into());
let newline_style = NewlineStyle::detect(&history.base_text);
Self::build( Self::build(
TextBuffer::new( TextBuffer::new(replica_id, cx.model_id() as u64, history),
replica_id,
cx.model_id() as u64,
History::new(base_text.into()),
),
Some(file), Some(file),
newline_style,
) )
} }
@ -346,7 +352,9 @@ impl Buffer {
message.id, message.id,
History::new(Arc::from(message.base_text)), History::new(Arc::from(message.base_text)),
); );
let mut this = Self::build(buffer, file); let newline_style = proto::NewlineStyle::from_i32(message.newline_style)
.ok_or_else(|| anyhow!("missing newline_style"))?;
let mut this = Self::build(buffer, file, NewlineStyle::from_proto(newline_style));
let ops = message let ops = message
.operations .operations
.into_iter() .into_iter()
@ -411,6 +419,7 @@ impl Buffer {
diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()), diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
diagnostics_timestamp: self.diagnostics_timestamp.value, diagnostics_timestamp: self.diagnostics_timestamp.value,
completion_triggers: self.completion_triggers.clone(), completion_triggers: self.completion_triggers.clone(),
newline_style: self.newline_style.to_proto() as i32,
} }
} }
@ -419,7 +428,7 @@ impl Buffer {
self self
} }
fn build(buffer: TextBuffer, file: Option<Arc<dyn File>>) -> Self { fn build(buffer: TextBuffer, file: Option<Arc<dyn File>>, newline_style: NewlineStyle) -> Self {
let saved_mtime; let saved_mtime;
if let Some(file) = file.as_ref() { if let Some(file) = file.as_ref() {
saved_mtime = file.mtime(); saved_mtime = file.mtime();
@ -435,6 +444,7 @@ impl Buffer {
was_dirty_before_starting_transaction: None, was_dirty_before_starting_transaction: None,
text: buffer, text: buffer,
file, file,
newline_style,
syntax_tree: Mutex::new(None), syntax_tree: Mutex::new(None),
parsing_in_background: false, parsing_in_background: false,
parse_count: 0, parse_count: 0,
@ -491,7 +501,13 @@ impl Buffer {
}; };
let text = self.as_rope().clone(); let text = self.as_rope().clone();
let version = self.version(); let version = self.version();
let save = file.save(self.remote_id(), text, version, cx.as_mut()); let save = file.save(
self.remote_id(),
text,
version,
self.newline_style,
cx.as_mut(),
);
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
let (version, fingerprint, mtime) = save.await?; let (version, fingerprint, mtime) = save.await?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
@ -1492,6 +1508,10 @@ impl Buffer {
pub fn completion_triggers(&self) -> &[String] { pub fn completion_triggers(&self) -> &[String] {
&self.completion_triggers &self.completion_triggers
} }
pub fn newline_style(&self) -> NewlineStyle {
self.newline_style
}
} }
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
@ -2512,6 +2532,52 @@ impl std::ops::SubAssign for IndentSize {
} }
} }
impl NewlineStyle {
fn from_proto(style: proto::NewlineStyle) -> Self {
match style {
proto::NewlineStyle::Unix => Self::Unix,
proto::NewlineStyle::Windows => Self::Windows,
}
}
fn detect(text: &str) -> Self {
let text = &text[..cmp::min(text.len(), 1000)];
if let Some(ix) = text.find('\n') {
if ix == 0 || text.as_bytes()[ix - 1] != b'\r' {
Self::Unix
} else {
Self::Windows
}
} else {
Default::default()
}
}
pub fn as_str(self) -> &'static str {
match self {
NewlineStyle::Unix => "\n",
NewlineStyle::Windows => "\r\n",
}
}
fn to_proto(self) -> proto::NewlineStyle {
match self {
NewlineStyle::Unix => proto::NewlineStyle::Unix,
NewlineStyle::Windows => proto::NewlineStyle::Windows,
}
}
}
impl Default for NewlineStyle {
fn default() -> Self {
#[cfg(unix)]
return Self::Unix;
#[cfg(not(unix))]
return Self::Windows;
}
}
impl Completion { impl Completion {
pub fn sort_key(&self) -> (usize, &str) { pub fn sort_key(&self) -> (usize, &str) {
let kind_key = match self.lsp_completion.kind { let kind_key = match self.lsp_completion.kind {

View file

@ -9,7 +9,7 @@ use rpc::proto;
use std::{ops::Range, sync::Arc}; use std::{ops::Range, sync::Arc};
use text::*; use text::*;
pub use proto::{Buffer, BufferState, SelectionSet}; pub use proto::{Buffer, BufferState, NewlineStyle, SelectionSet};
pub fn serialize_operation(operation: &Operation) -> proto::Operation { pub fn serialize_operation(operation: &Operation) -> proto::Operation {
proto::Operation { proto::Operation {

View file

@ -1,6 +1,7 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use fsevent::EventStream; use fsevent::EventStream;
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use language::NewlineStyle;
use smol::io::{AsyncReadExt, AsyncWriteExt}; use smol::io::{AsyncReadExt, AsyncWriteExt};
use std::{ use std::{
io, io,
@ -21,7 +22,7 @@ pub trait Fs: Send + Sync {
async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>; async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>;
async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>>; async fn open_sync(&self, path: &Path) -> Result<Box<dyn io::Read>>;
async fn load(&self, path: &Path) -> Result<String>; async fn load(&self, path: &Path) -> Result<String>;
async fn save(&self, path: &Path, text: &Rope) -> Result<()>; async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()>;
async fn canonicalize(&self, path: &Path) -> Result<PathBuf>; async fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
async fn is_file(&self, path: &Path) -> bool; async fn is_file(&self, path: &Path) -> bool;
async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>; async fn metadata(&self, path: &Path) -> Result<Option<Metadata>>;
@ -169,12 +170,19 @@ impl Fs for RealFs {
Ok(text) Ok(text)
} }
async fn save(&self, path: &Path, text: &Rope) -> Result<()> { async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()> {
let buffer_size = text.summary().bytes.min(10 * 1024); let buffer_size = text.summary().bytes.min(10 * 1024);
let file = smol::fs::File::create(path).await?; let file = smol::fs::File::create(path).await?;
let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file); let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
let mut newline = false;
for chunk in text.chunks() { for chunk in text.chunks() {
writer.write_all(chunk.as_bytes()).await?; for line in chunk.split('\n') {
if newline {
writer.write_all(newline_style.as_str().as_bytes()).await?;
}
writer.write_all(line.as_bytes()).await?;
newline = true;
}
} }
writer.flush().await?; writer.flush().await?;
Ok(()) Ok(())
@ -646,7 +654,7 @@ impl Fs for FakeFs {
Ok(text.clone()) Ok(text.clone())
} }
async fn save(&self, path: &Path, text: &Rope) -> Result<()> { async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()> {
self.simulate_random_delay().await; self.simulate_random_delay().await;
let mut state = self.state.lock().await; let mut state = self.state.lock().await;
let path = normalize_path(path); let path = normalize_path(path);
@ -655,7 +663,11 @@ impl Fs for FakeFs {
if entry.metadata.is_dir { if entry.metadata.is_dir {
Err(anyhow!("cannot overwrite a directory with a file")) Err(anyhow!("cannot overwrite a directory with a file"))
} else { } else {
entry.content = Some(text.chunks().collect()); entry.content = Some(
text.chunks()
.map(|chunk| chunk.replace('\n', newline_style.as_str()))
.collect(),
);
entry.metadata.mtime = SystemTime::now(); entry.metadata.mtime = SystemTime::now();
state.emit_event(&[path]).await; state.emit_event(&[path]).await;
Ok(()) Ok(())

View file

@ -6054,7 +6054,7 @@ mod tests {
use gpui::{executor::Deterministic, test::subscribe}; use gpui::{executor::Deterministic, test::subscribe};
use language::{ use language::{
tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig, tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig,
OffsetRangeExt, Point, ToPoint, NewlineStyle, OffsetRangeExt, Point, ToPoint,
}; };
use lsp::Url; use lsp::Url;
use serde_json::json; use serde_json::json;
@ -8547,9 +8547,13 @@ mod tests {
assert!(!buffer.has_conflict()); assert!(!buffer.has_conflict());
}); });
let new_contents = "AAAA\naaa\nBB\nbbbbb\n"; let new_contents = "AAAA\naaa\nBB\nbbbbb\n";
fs.save("/dir/the-file".as_ref(), &new_contents.into()) fs.save(
.await "/dir/the-file".as_ref(),
.unwrap(); &new_contents.into(),
NewlineStyle::Unix,
)
.await
.unwrap();
// Because the buffer was not modified, it is reloaded from disk. Its // Because the buffer was not modified, it is reloaded from disk. Its
// contents are edited according to the diff between the old and new // contents are edited according to the diff between the old and new
@ -8584,6 +8588,7 @@ mod tests {
fs.save( fs.save(
"/dir/the-file".as_ref(), "/dir/the-file".as_ref(),
&"\n\n\nAAAA\naaa\nBB\nbbbbb\n".into(), &"\n\n\nAAAA\naaa\nBB\nbbbbb\n".into(),
NewlineStyle::Unix,
) )
.await .await
.unwrap(); .unwrap();

View file

@ -24,7 +24,7 @@ use gpui::{
}; };
use language::{ use language::{
proto::{deserialize_version, serialize_version}, proto::{deserialize_version, serialize_version},
Buffer, DiagnosticEntry, PointUtf16, Rope, Buffer, DiagnosticEntry, NewlineStyle, PointUtf16, Rope,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
use parking_lot::Mutex; use parking_lot::Mutex;
@ -595,7 +595,7 @@ impl LocalWorktree {
let text = buffer.as_rope().clone(); let text = buffer.as_rope().clone();
let fingerprint = text.fingerprint(); let fingerprint = text.fingerprint();
let version = buffer.version(); let version = buffer.version();
let save = self.write_file(path, text, cx); let save = self.write_file(path, text, buffer.newline_style(), cx);
let handle = cx.handle(); let handle = cx.handle();
cx.as_mut().spawn(|mut cx| async move { cx.as_mut().spawn(|mut cx| async move {
let entry = save.await?; let entry = save.await?;
@ -636,9 +636,10 @@ impl LocalWorktree {
&self, &self,
path: impl Into<Arc<Path>>, path: impl Into<Arc<Path>>,
text: Rope, text: Rope,
newline_style: NewlineStyle,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Task<Result<Entry>> { ) -> Task<Result<Entry>> {
self.write_entry_internal(path, Some(text), cx) self.write_entry_internal(path, Some((text, newline_style)), cx)
} }
pub fn delete_entry( pub fn delete_entry(
@ -754,7 +755,7 @@ impl LocalWorktree {
fn write_entry_internal( fn write_entry_internal(
&self, &self,
path: impl Into<Arc<Path>>, path: impl Into<Arc<Path>>,
text_if_file: Option<Rope>, text_if_file: Option<(Rope, NewlineStyle)>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Task<Result<Entry>> { ) -> Task<Result<Entry>> {
let path = path.into(); let path = path.into();
@ -763,8 +764,8 @@ impl LocalWorktree {
let fs = self.fs.clone(); let fs = self.fs.clone();
let abs_path = abs_path.clone(); let abs_path = abs_path.clone();
async move { async move {
if let Some(text) = text_if_file { if let Some((text, newline_style)) = text_if_file {
fs.save(&abs_path, &text).await fs.save(&abs_path, &text, newline_style).await
} else { } else {
fs.create_dir(&abs_path).await fs.create_dir(&abs_path).await
} }
@ -1653,6 +1654,7 @@ impl language::File for File {
buffer_id: u64, buffer_id: u64,
text: Rope, text: Rope,
version: clock::Global, version: clock::Global,
newline_style: NewlineStyle,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
) -> Task<Result<(clock::Global, String, SystemTime)>> { ) -> Task<Result<(clock::Global, String, SystemTime)>> {
self.worktree.update(cx, |worktree, cx| match worktree { self.worktree.update(cx, |worktree, cx| match worktree {
@ -1660,7 +1662,7 @@ impl language::File for File {
let rpc = worktree.client.clone(); let rpc = worktree.client.clone();
let project_id = worktree.share.as_ref().map(|share| share.project_id); let project_id = worktree.share.as_ref().map(|share| share.project_id);
let fingerprint = text.fingerprint(); let fingerprint = text.fingerprint();
let save = worktree.write_file(self.path.clone(), text, cx); let save = worktree.write_file(self.path.clone(), text, newline_style, cx);
cx.background().spawn(async move { cx.background().spawn(async move {
let entry = save.await?; let entry = save.await?;
if let Some(project_id) = project_id { if let Some(project_id) = project_id {
@ -2841,6 +2843,7 @@ mod tests {
tree.as_local().unwrap().write_file( tree.as_local().unwrap().write_file(
Path::new("tracked-dir/file.txt"), Path::new("tracked-dir/file.txt"),
"hello".into(), "hello".into(),
Default::default(),
cx, cx,
) )
}) })
@ -2850,6 +2853,7 @@ mod tests {
tree.as_local().unwrap().write_file( tree.as_local().unwrap().write_file(
Path::new("ignored-dir/file.txt"), Path::new("ignored-dir/file.txt"),
"world".into(), "world".into(),
Default::default(),
cx, cx,
) )
}) })

View file

@ -810,6 +810,12 @@ message BufferState {
repeated Diagnostic diagnostics = 6; repeated Diagnostic diagnostics = 6;
uint32 diagnostics_timestamp = 7; uint32 diagnostics_timestamp = 7;
repeated string completion_triggers = 8; repeated string completion_triggers = 8;
NewlineStyle newline_style = 9;
}
enum NewlineStyle {
Unix = 0;
Windows = 1;
} }
message SelectionSet { message SelectionSet {

View file

@ -117,6 +117,7 @@ mod tests {
} }
"# "#
.into(), .into(),
Default::default(),
) )
.await .await
.unwrap(); .unwrap();
@ -174,6 +175,7 @@ mod tests {
} }
"# "#
.into(), .into(),
Default::default(),
) )
.await .await
.unwrap(); .unwrap();