Use `Buffer::update` and `Buffer::update_encoding` to set the `encoding`

R Aadarsh created

field of `Buffer`

Change summary

crates/encodings/Cargo.toml          |  2 +-
crates/encodings_ui/src/selectors.rs | 16 ++++------------
crates/language/src/buffer.rs        | 14 +++-----------
crates/project/src/buffer_store.rs   |  4 +++-
crates/worktree/src/worktree.rs      |  4 ++--
5 files changed, 13 insertions(+), 27 deletions(-)

Detailed changes

crates/encodings/Cargo.toml 🔗

@@ -5,8 +5,8 @@ publish.workspace = true
 edition.workspace = true
 
 [dependencies]
-encoding_rs.workspace = true
 anyhow.workspace = true
+encoding_rs.workspace = true
 
 [lints]
 workspace = true

crates/encodings_ui/src/selectors.rs 🔗

@@ -463,16 +463,10 @@ pub mod encoding {
                     .unwrap();
 
                 let reload = buffer.update(cx, |buffer, cx| buffer.reload(cx));
-                // Since the encoding will be accessed in `reload`,
-                // the lock must be released before calling `reload`.
-                // By limiting the scope, we ensure that it is released
 
-                {
-                    let buffer = buffer.read(cx);
-
-                    let buffer_encoding = buffer.encoding.clone();
-                    buffer_encoding.set(encoding_from_name(&current_selection.clone()));
-                }
+                buffer.update(cx, |buffer, _| {
+                    buffer.update_encoding(encoding_from_name(&current_selection).into())
+                });
 
                 self.dismissed(window, cx);
 
@@ -560,9 +554,7 @@ pub mod encoding {
                             })
                         {
                             buffer
-                                .read_with(cx, |buffer, _| {
-                                    buffer.encoding.set(encoding);
-                                })
+                                .update(cx, |buffer, _| buffer.update_encoding(encoding.into()))
                                 .log_err();
                         }
                     })

crates/language/src/buffer.rs 🔗

@@ -128,7 +128,6 @@ pub struct Buffer {
     change_bits: Vec<rc::Weak<Cell<bool>>>,
     _subscriptions: Vec<gpui::Subscription>,
     pub encoding: Arc<Encoding>,
-    pub observe_file_encoding: Option<gpui::Subscription>,
 }
 
 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -1031,7 +1030,6 @@ impl Buffer {
             change_bits: Default::default(),
             _subscriptions: Vec::new(),
             encoding: Arc::new(Encoding::new(encodings::UTF_8)),
-            observe_file_encoding: None,
         }
     }
 
@@ -2937,15 +2935,9 @@ impl Buffer {
         !self.has_edits_since(&self.preview_version)
     }
 
-    /// Update the `encoding` field, whenever the `encoding` field of the file changes
-    pub fn update_encoding(&mut self) {
-        if let Some(file) = self.file() {
-            if let Some(encoding) = file.encoding() {
-                self.encoding.set(encoding.get());
-            } else {
-                self.encoding.set(encodings::UTF_8);
-            };
-        }
+    /// Update the buffer
+    pub fn update_encoding(&mut self, encoding: Encoding) {
+        self.encoding.set(encoding.get());
     }
 }
 

crates/project/src/buffer_store.rs 🔗

@@ -704,7 +704,9 @@ impl LocalBufferStore {
                 anyhow::Ok(())
             })??;
 
-            buffer.update(cx, |buffer, _| buffer.encoding.set(encoding.get()))?;
+            buffer.update(cx, |buffer, _| {
+                buffer.update_encoding(encoding.get().into())
+            })?;
 
             Ok(buffer)
         })

crates/worktree/src/worktree.rs 🔗

@@ -3085,7 +3085,7 @@ impl PartialEq for File {
             && self.entry_id == other.entry_id
             && self.is_local == other.is_local
             && self.is_private == other.is_private
-            && if let Some(encoding) = &self.encoding
+            && (if let Some(encoding) = &self.encoding
                 && let Some(other_encoding) = &other.encoding
             {
                 if encoding.get() != other_encoding.get() {
@@ -3095,7 +3095,7 @@ impl PartialEq for File {
                 }
             } else {
                 true
-            }
+            })
         {
             true
         } else {