From 98ef05d768d287fef2eb790e0a8a6e9a72832e00 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 25 Jul 2023 13:15:36 -0500 Subject: Prefer RopeSlice to &Rope in helix_core::syntax Pascal and I discussed this and we think it's generally better to take a 'RopeSlice' rather than a '&Rope'. The code block rendering function in the markdown component module is a good example for how this can be useful: we can remove an allocation of a rope and instead directly turn a '&str' into a 'RopeSlice' which is very cheap. A change to prefer 'RopeSlice' to '&Rope' whenever the rope isn't modified would be nice, but it would be a very large diff (around 500+ 500-). Starting off with just the syntax functions seems like a nice middle-ground, and we can remove a Rope allocation because of it. Co-authored-by: Pascal Kuthe --- helix-view/src/document.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'helix-view') diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index b08370f9..af7e3a7e 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -642,7 +642,7 @@ impl Document { ) -> Self { let (encoding, has_bom) = encoding_with_bom_info.unwrap_or((encoding::UTF_8, false)); let line_ending = config.load().default_line_ending.into(); - let changes = ChangeSet::new(&text); + let changes = ChangeSet::new(text.slice(..)); let old_state = None; Self { @@ -938,7 +938,7 @@ impl Document { ) -> Option> { config_loader .language_config_for_file_name(self.path.as_ref()?) - .or_else(|| config_loader.language_config_for_shebang(self.text())) + .or_else(|| config_loader.language_config_for_shebang(self.text().slice(..))) } /// Detect the indentation used in the file, or otherwise defaults to the language indentation @@ -1030,7 +1030,7 @@ impl Document { ) { if let (Some(language_config), Some(loader)) = (language_config, loader) { if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) { - self.syntax = Syntax::new(&self.text, highlight_config, loader); + self.syntax = Syntax::new(self.text.slice(..), highlight_config, loader); } self.language = Some(language_config); @@ -1165,7 +1165,11 @@ impl Document { // update tree-sitter syntax tree if let Some(syntax) = &mut self.syntax { // TODO: no unwrap - let res = syntax.update(&old_doc, &self.text, transaction.changes()); + let res = syntax.update( + old_doc.slice(..), + self.text.slice(..), + transaction.changes(), + ); if res.is_err() { log::error!("TS parser failed, disabeling TS for the current buffer: {res:?}"); self.syntax = None; @@ -1288,7 +1292,7 @@ impl Document { if success { // reset changeset to fix len - self.changes = ChangeSet::new(self.text()); + self.changes = ChangeSet::new(self.text().slice(..)); // Sync with changes with the jumplist selections. view.sync_changes(self); } @@ -1371,7 +1375,7 @@ impl Document { } if success { // reset changeset to fix len - self.changes = ChangeSet::new(self.text()); + self.changes = ChangeSet::new(self.text().slice(..)); // Sync with changes with the jumplist selections. view.sync_changes(self); } @@ -1394,7 +1398,7 @@ impl Document { return; } - let new_changeset = ChangeSet::new(self.text()); + let new_changeset = ChangeSet::new(self.text().slice(..)); let changes = std::mem::replace(&mut self.changes, new_changeset); // Instead of doing this messy merge we could always commit, and based on transaction // annotations either add a new layer or compose into the previous one. -- cgit v1.2.3-70-g09d2