Skip to content

Commit

Permalink
refactor: move apply_all_fix to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Apr 3, 2024
1 parent 3068888 commit b8a1b83
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions crates/lsp/src/lib.rs
Expand Up @@ -509,62 +509,74 @@ impl<L: LSPLang> Backend<L> {
format!("Running ExecuteCommand {}", command),
)
.await;

let text_doc: TextDocumentItem = match serde_json::from_value(arguments.first()?.clone()) {
Ok(value) => value,
Err(e) => {
self
.client
.log_message(
MessageType::ERROR,
format!("JSON deserialization error: {}", e),
)
.await;
return None;
}
};

let uri = text_doc.uri;
let path = match uri.to_file_path() {
Ok(path) => path,
Err(_) => {
self
.client
.log_message(MessageType::ERROR, "Invalid URI format")
.await;
return None;
}
};
let version = text_doc.version;
let text = text_doc.text;

let lang = Self::infer_lang_from_uri(&uri)?;

let root = AstGrep::new(text, lang);
let versioned = VersionedAst { version, root };

let diagnostics = self.get_diagnostics(&uri, &versioned).await?;
let error_id_to_ranges = build_error_id_to_ranges(diagnostics);
self.on_apply_all_fix(arguments).await?;
None
}
_ => {
self
.client
.log_message(MessageType::LOG, "Parsing doc.")
.log_message(
MessageType::LOG,
format!("Unrecognized command: {}", command),
)
.await;
None
}
}
}

let changes =
self.compute_all_fixes(TextDocumentIdentifier::new(uri), error_id_to_ranges, path);
async fn on_apply_all_fix(&self, arguments: Vec<Value>) -> Option<()> {
let text_doc: TextDocumentItem = match serde_json::from_value(arguments.first()?.clone()) {
Ok(value) => value,
Err(e) => {
self
.client
.apply_edit(WorkspaceEdit {
changes,
document_changes: None,
change_annotations: None,
})
.await
.ok()?;
.log_message(
MessageType::ERROR,
format!("JSON deserialization error: {}", e),
)
.await;
return None;
}
};

None
let uri = text_doc.uri;
let path = match uri.to_file_path() {
Ok(path) => path,
Err(_) => {
self
.client
.log_message(MessageType::ERROR, "Invalid URI format")
.await;
return None;
}
_ => None,
}
};
let version = text_doc.version;
let text = text_doc.text;

let lang = Self::infer_lang_from_uri(&uri)?;

let root = AstGrep::new(text, lang);
let versioned = VersionedAst { version, root };

let diagnostics = self.get_diagnostics(&uri, &versioned).await?;
let error_id_to_ranges = build_error_id_to_ranges(diagnostics);
self
.client
.log_message(MessageType::LOG, "Parsing doc.")
.await;

let changes =
self.compute_all_fixes(TextDocumentIdentifier::new(uri), error_id_to_ranges, path);
self
.client
.apply_edit(WorkspaceEdit {
changes,
document_changes: None,
change_annotations: None,
})
.await
.ok()?;
None
}
}

0 comments on commit b8a1b83

Please sign in to comment.