Skip to content

Commit

Permalink
Refresh script without looking at the previous state on did_save
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Mar 25, 2024
1 parent 10264a4 commit 1d80cd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 11 additions & 1 deletion crates/core/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Script {
})
}

/// Reparses AST based on the script document.
/// Reparses AST based on the previous script state and changes made to the document.
/// Clear document's edit history.
pub fn update(&mut self, doc: &mut ScriptDocument) -> Result<(), ScriptError> {
for edit in &doc.edits {
Expand All @@ -45,6 +45,16 @@ impl Script {
Ok(())
}

/// Reparses AST based on the script document alone.
/// The range of the entire script will be different after the operation.
pub fn refresh(&mut self, doc: &ScriptDocument) -> Result<(), ScriptError> {
let current_tree = Self::parse_rope(&doc.rope, None)?;
let prev_tree = std::mem::replace(&mut self.current_tree, current_tree);
self.prev_tree = Some(prev_tree);

Ok(())
}

fn parse_rope(rope: &Rope, prev_tree: Option<&Tree>) -> Result<Tree, ScriptError> {
use ScriptError::*;

Expand Down
6 changes: 4 additions & 2 deletions crates/lsp/src/providers/document_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ pub async fn did_save(backend: &Backend, params: lsp::DidSaveTextDocumentParams)


// replace the doc content completely
let mut doc_buff = backend
let doc_buff = backend
.doc_buffers
.entry(doc_path.clone())
.insert(ScriptDocument::from_str(&params.text.unwrap()));

if let Some(mut script) = backend.scripts.get_mut(&doc_path) {
if let Err(err) = script.update(&mut doc_buff) {
// do a fresh reparse without caring about the previous state
// as a fail-safe in case of bad edits or document being changed outside of the editor
if let Err(err) = script.refresh(&doc_buff) {
backend.log_error(err).await;
}

Expand Down

0 comments on commit 1d80cd6

Please sign in to comment.