From 9804c683c0118634a9b59a388d7f3da2b151ebe6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 4 Jul 2022 12:47:40 -0700 Subject: [PATCH] Rename NewlineStyle -> LineEnding --- crates/collab/src/integration_tests.rs | 6 +-- crates/language/src/buffer.rs | 60 +++++++++++++------------- crates/language/src/proto.rs | 2 +- crates/project/src/fs.rs | 12 +++--- crates/project/src/project_tests.rs | 14 +++--- crates/project/src/worktree.rs | 18 ++++---- crates/rpc/proto/zed.proto | 4 +- 7 files changed, 58 insertions(+), 58 deletions(-) diff --git a/crates/collab/src/integration_tests.rs b/crates/collab/src/integration_tests.rs index 9cefeac664..63225e1159 100644 --- a/crates/collab/src/integration_tests.rs +++ b/crates/collab/src/integration_tests.rs @@ -22,7 +22,7 @@ use gpui::{ }; use language::{ range_to_lsp, tree_sitter_rust, Diagnostic, DiagnosticEntry, FakeLspAdapter, Language, - LanguageConfig, LanguageRegistry, NewlineStyle, OffsetRangeExt, Point, Rope, + LanguageConfig, LanguageRegistry, LineEnding, OffsetRangeExt, Point, Rope, }; use lsp::{self, FakeLanguageServer}; use parking_lot::Mutex; @@ -1266,7 +1266,7 @@ async fn test_buffer_reloading(cx_a: &mut TestAppContext, cx_b: &mut TestAppCont .save( "/dir/a.txt".as_ref(), &"new contents".into(), - NewlineStyle::Unix, + LineEnding::Unix, ) .await .unwrap(); @@ -1864,7 +1864,7 @@ async fn test_reloading_buffer_manually(cx_a: &mut TestAppContext, cx_b: &mut Te .save( "/a/a.rs".as_ref(), &Rope::from("let seven = 7;"), - NewlineStyle::Unix, + LineEnding::Unix, ) .await .unwrap(); diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 18c38678a6..5f0cc1234a 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -53,7 +53,7 @@ pub struct Buffer { saved_version: clock::Global, saved_version_fingerprint: String, saved_mtime: SystemTime, - newline_style: NewlineStyle, + line_ending: LineEnding, transaction_depth: usize, was_dirty_before_starting_transaction: Option, language: Option>, @@ -99,7 +99,7 @@ pub enum IndentKind { } #[derive(Copy, Debug, Clone, PartialEq, Eq)] -pub enum NewlineStyle { +pub enum LineEnding { Unix, Windows, } @@ -201,7 +201,7 @@ pub trait File: Send + Sync { buffer_id: u64, text: Rope, version: clock::Global, - newline_style: NewlineStyle, + line_ending: LineEnding, cx: &mut MutableAppContext, ) -> Task>; @@ -283,7 +283,7 @@ pub(crate) struct Diff { base_version: clock::Global, new_text: Arc, changes: Vec<(ChangeTag, usize)>, - newline_style: NewlineStyle, + line_ending: LineEnding, start_offset: usize, } @@ -319,11 +319,11 @@ impl Buffer { cx: &mut ModelContext, ) -> Self { let history = History::new(base_text.into()); - let newline_style = NewlineStyle::detect(&history.base_text); + let line_ending = LineEnding::detect(&history.base_text); Self::build( TextBuffer::new(replica_id, cx.model_id() as u64, history), None, - newline_style, + line_ending, ) } @@ -334,11 +334,11 @@ impl Buffer { cx: &mut ModelContext, ) -> Self { let history = History::new(base_text.into()); - let newline_style = NewlineStyle::detect(&history.base_text); + let line_ending = LineEnding::detect(&history.base_text); Self::build( TextBuffer::new(replica_id, cx.model_id() as u64, history), Some(file), - newline_style, + line_ending, ) } @@ -353,9 +353,9 @@ impl Buffer { message.id, History::new(Arc::from(message.base_text)), ); - 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 line_ending = proto::LineEnding::from_i32(message.line_ending) + .ok_or_else(|| anyhow!("missing line_ending"))?; + let mut this = Self::build(buffer, file, LineEnding::from_proto(line_ending)); let ops = message .operations .into_iter() @@ -420,7 +420,7 @@ impl Buffer { diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()), diagnostics_timestamp: self.diagnostics_timestamp.value, completion_triggers: self.completion_triggers.clone(), - newline_style: self.newline_style.to_proto() as i32, + line_ending: self.line_ending.to_proto() as i32, } } @@ -429,7 +429,7 @@ impl Buffer { self } - fn build(buffer: TextBuffer, file: Option>, newline_style: NewlineStyle) -> Self { + fn build(buffer: TextBuffer, file: Option>, line_ending: LineEnding) -> Self { let saved_mtime; if let Some(file) = file.as_ref() { saved_mtime = file.mtime(); @@ -445,7 +445,7 @@ impl Buffer { was_dirty_before_starting_transaction: None, text: buffer, file, - newline_style, + line_ending, syntax_tree: Mutex::new(None), parsing_in_background: false, parse_count: 0, @@ -506,7 +506,7 @@ impl Buffer { self.remote_id(), text, version, - self.newline_style, + self.line_ending, cx.as_mut(), ); cx.spawn(|this, mut cx| async move { @@ -974,7 +974,7 @@ impl Buffer { let base_version = self.version(); cx.background().spawn(async move { let old_text = old_text.to_string(); - let newline_style = NewlineStyle::detect(&new_text); + let line_ending = LineEnding::detect(&new_text); let new_text = new_text.replace("\r\n", "\n").replace('\r', "\n"); let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_str()) .iter_all_changes() @@ -984,7 +984,7 @@ impl Buffer { base_version, new_text: new_text.into(), changes, - newline_style, + line_ending, start_offset: 0, } }) @@ -998,7 +998,7 @@ impl Buffer { if self.version == diff.base_version { self.finalize_last_transaction(); self.start_transaction(); - self.newline_style = diff.newline_style; + self.line_ending = diff.line_ending; let mut offset = diff.start_offset; for (tag, len) in diff.changes { let range = offset..(offset + len); @@ -1514,8 +1514,8 @@ impl Buffer { &self.completion_triggers } - pub fn newline_style(&self) -> NewlineStyle { - self.newline_style + pub fn line_ending(&self) -> LineEnding { + self.line_ending } } @@ -2537,11 +2537,11 @@ impl std::ops::SubAssign for IndentSize { } } -impl NewlineStyle { - fn from_proto(style: proto::NewlineStyle) -> Self { +impl LineEnding { + fn from_proto(style: proto::LineEnding) -> Self { match style { - proto::NewlineStyle::Unix => Self::Unix, - proto::NewlineStyle::Windows => Self::Windows, + proto::LineEnding::Unix => Self::Unix, + proto::LineEnding::Windows => Self::Windows, } } @@ -2560,20 +2560,20 @@ impl NewlineStyle { pub fn as_str(self) -> &'static str { match self { - NewlineStyle::Unix => "\n", - NewlineStyle::Windows => "\r\n", + LineEnding::Unix => "\n", + LineEnding::Windows => "\r\n", } } - fn to_proto(self) -> proto::NewlineStyle { + fn to_proto(self) -> proto::LineEnding { match self { - NewlineStyle::Unix => proto::NewlineStyle::Unix, - NewlineStyle::Windows => proto::NewlineStyle::Windows, + LineEnding::Unix => proto::LineEnding::Unix, + LineEnding::Windows => proto::LineEnding::Windows, } } } -impl Default for NewlineStyle { +impl Default for LineEnding { fn default() -> Self { #[cfg(unix)] return Self::Unix; diff --git a/crates/language/src/proto.rs b/crates/language/src/proto.rs index 698c34853a..0e876d14df 100644 --- a/crates/language/src/proto.rs +++ b/crates/language/src/proto.rs @@ -9,7 +9,7 @@ use rpc::proto; use std::{ops::Range, sync::Arc}; use text::*; -pub use proto::{Buffer, BufferState, NewlineStyle, SelectionSet}; +pub use proto::{Buffer, BufferState, LineEnding, SelectionSet}; pub fn serialize_operation(operation: &Operation) -> proto::Operation { proto::Operation { diff --git a/crates/project/src/fs.rs b/crates/project/src/fs.rs index 5ae1ab3339..69b0b7ff3d 100644 --- a/crates/project/src/fs.rs +++ b/crates/project/src/fs.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use fsevent::EventStream; use futures::{Stream, StreamExt}; -use language::NewlineStyle; +use language::LineEnding; use smol::io::{AsyncReadExt, AsyncWriteExt}; use std::{ io, @@ -22,7 +22,7 @@ pub trait Fs: Send + Sync { async fn remove_file(&self, path: &Path, options: RemoveOptions) -> Result<()>; async fn open_sync(&self, path: &Path) -> Result>; async fn load(&self, path: &Path) -> Result; - async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()>; + async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()>; async fn canonicalize(&self, path: &Path) -> Result; async fn is_file(&self, path: &Path) -> bool; async fn metadata(&self, path: &Path) -> Result>; @@ -170,7 +170,7 @@ impl Fs for RealFs { Ok(text) } - async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()> { + async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> { let buffer_size = text.summary().bytes.min(10 * 1024); let file = smol::fs::File::create(path).await?; let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file); @@ -178,7 +178,7 @@ impl Fs for RealFs { for chunk in text.chunks() { for line in chunk.split('\n') { if newline { - writer.write_all(newline_style.as_str().as_bytes()).await?; + writer.write_all(line_ending.as_str().as_bytes()).await?; } writer.write_all(line.as_bytes()).await?; newline = true; @@ -654,7 +654,7 @@ impl Fs for FakeFs { Ok(text.clone()) } - async fn save(&self, path: &Path, text: &Rope, newline_style: NewlineStyle) -> Result<()> { + async fn save(&self, path: &Path, text: &Rope, line_ending: LineEnding) -> Result<()> { self.simulate_random_delay().await; let mut state = self.state.lock().await; let path = normalize_path(path); @@ -665,7 +665,7 @@ impl Fs for FakeFs { } else { entry.content = Some( text.chunks() - .map(|chunk| chunk.replace('\n', newline_style.as_str())) + .map(|chunk| chunk.replace('\n', line_ending.as_str())) .collect(), ); entry.metadata.mtime = SystemTime::now(); diff --git a/crates/project/src/project_tests.rs b/crates/project/src/project_tests.rs index 0a0d9c195d..9d80dadb84 100644 --- a/crates/project/src/project_tests.rs +++ b/crates/project/src/project_tests.rs @@ -4,7 +4,7 @@ use futures::{future, StreamExt}; use gpui::{executor::Deterministic, test::subscribe}; use language::{ tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig, - NewlineStyle, OffsetRangeExt, Point, ToPoint, + LineEnding, OffsetRangeExt, Point, ToPoint, }; use lsp::Url; use serde_json::json; @@ -2429,7 +2429,7 @@ async fn test_buffer_file_changes_on_disk(cx: &mut gpui::TestAppContext) { fs.save( "/dir/the-file".as_ref(), &new_contents.into(), - NewlineStyle::Unix, + LineEnding::Unix, ) .await .unwrap(); @@ -2464,7 +2464,7 @@ async fn test_buffer_file_changes_on_disk(cx: &mut gpui::TestAppContext) { fs.save( "/dir/the-file".as_ref(), &"\n\n\nAAAA\naaa\nBB\nbbbbb\n".into(), - NewlineStyle::Unix, + LineEnding::Unix, ) .await .unwrap(); @@ -2501,11 +2501,11 @@ async fn test_buffer_line_endings(cx: &mut gpui::TestAppContext) { buffer1.read_with(cx, |buffer, _| { assert_eq!(buffer.text(), "a\nb\nc\n"); - assert_eq!(buffer.newline_style(), NewlineStyle::Unix); + assert_eq!(buffer.line_ending(), LineEnding::Unix); }); buffer2.read_with(cx, |buffer, _| { assert_eq!(buffer.text(), "one\ntwo\nthree\n"); - assert_eq!(buffer.newline_style(), NewlineStyle::Windows); + assert_eq!(buffer.line_ending(), LineEnding::Windows); }); // Change a file's line endings on disk from unix to windows. The buffer's @@ -2513,14 +2513,14 @@ async fn test_buffer_line_endings(cx: &mut gpui::TestAppContext) { fs.save( "/dir/file1".as_ref(), &"aaa\nb\nc\n".into(), - NewlineStyle::Windows, + LineEnding::Windows, ) .await .unwrap(); cx.foreground().run_until_parked(); buffer1.read_with(cx, |buffer, _| { assert_eq!(buffer.text(), "aaa\nb\nc\n"); - assert_eq!(buffer.newline_style(), NewlineStyle::Windows); + assert_eq!(buffer.line_ending(), LineEnding::Windows); }); // Save a file with windows line endings. The file is written correctly. diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 35c9f46851..77dcb064c0 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -24,7 +24,7 @@ use gpui::{ }; use language::{ proto::{deserialize_version, serialize_version}, - Buffer, DiagnosticEntry, NewlineStyle, PointUtf16, Rope, + Buffer, DiagnosticEntry, LineEnding, PointUtf16, Rope, }; use lazy_static::lazy_static; use parking_lot::Mutex; @@ -595,7 +595,7 @@ impl LocalWorktree { let text = buffer.as_rope().clone(); let fingerprint = text.fingerprint(); let version = buffer.version(); - let save = self.write_file(path, text, buffer.newline_style(), cx); + let save = self.write_file(path, text, buffer.line_ending(), cx); let handle = cx.handle(); cx.as_mut().spawn(|mut cx| async move { let entry = save.await?; @@ -636,10 +636,10 @@ impl LocalWorktree { &self, path: impl Into>, text: Rope, - newline_style: NewlineStyle, + line_ending: LineEnding, cx: &mut ModelContext, ) -> Task> { - self.write_entry_internal(path, Some((text, newline_style)), cx) + self.write_entry_internal(path, Some((text, line_ending)), cx) } pub fn delete_entry( @@ -755,7 +755,7 @@ impl LocalWorktree { fn write_entry_internal( &self, path: impl Into>, - text_if_file: Option<(Rope, NewlineStyle)>, + text_if_file: Option<(Rope, LineEnding)>, cx: &mut ModelContext, ) -> Task> { let path = path.into(); @@ -764,8 +764,8 @@ impl LocalWorktree { let fs = self.fs.clone(); let abs_path = abs_path.clone(); async move { - if let Some((text, newline_style)) = text_if_file { - fs.save(&abs_path, &text, newline_style).await + if let Some((text, line_ending)) = text_if_file { + fs.save(&abs_path, &text, line_ending).await } else { fs.create_dir(&abs_path).await } @@ -1654,7 +1654,7 @@ impl language::File for File { buffer_id: u64, text: Rope, version: clock::Global, - newline_style: NewlineStyle, + line_ending: LineEnding, cx: &mut MutableAppContext, ) -> Task> { self.worktree.update(cx, |worktree, cx| match worktree { @@ -1662,7 +1662,7 @@ impl language::File for File { let rpc = worktree.client.clone(); let project_id = worktree.share.as_ref().map(|share| share.project_id); let fingerprint = text.fingerprint(); - let save = worktree.write_file(self.path.clone(), text, newline_style, cx); + let save = worktree.write_file(self.path.clone(), text, line_ending, cx); cx.background().spawn(async move { let entry = save.await?; if let Some(project_id) = project_id { diff --git a/crates/rpc/proto/zed.proto b/crates/rpc/proto/zed.proto index a25e19a526..9269e3bc94 100644 --- a/crates/rpc/proto/zed.proto +++ b/crates/rpc/proto/zed.proto @@ -810,10 +810,10 @@ message BufferState { repeated Diagnostic diagnostics = 6; uint32 diagnostics_timestamp = 7; repeated string completion_triggers = 8; - NewlineStyle newline_style = 9; + LineEnding line_ending = 9; } -enum NewlineStyle { +enum LineEnding { Unix = 0; Windows = 1; }