Skip to content

Commit b8a1b83

Browse files
refactor: move apply_all_fix to a function
1 parent 3068888 commit b8a1b83

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

crates/lsp/src/lib.rs

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -509,62 +509,74 @@ impl<L: LSPLang> Backend<L> {
509509
format!("Running ExecuteCommand {}", command),
510510
)
511511
.await;
512-
513-
let text_doc: TextDocumentItem = match serde_json::from_value(arguments.first()?.clone()) {
514-
Ok(value) => value,
515-
Err(e) => {
516-
self
517-
.client
518-
.log_message(
519-
MessageType::ERROR,
520-
format!("JSON deserialization error: {}", e),
521-
)
522-
.await;
523-
return None;
524-
}
525-
};
526-
527-
let uri = text_doc.uri;
528-
let path = match uri.to_file_path() {
529-
Ok(path) => path,
530-
Err(_) => {
531-
self
532-
.client
533-
.log_message(MessageType::ERROR, "Invalid URI format")
534-
.await;
535-
return None;
536-
}
537-
};
538-
let version = text_doc.version;
539-
let text = text_doc.text;
540-
541-
let lang = Self::infer_lang_from_uri(&uri)?;
542-
543-
let root = AstGrep::new(text, lang);
544-
let versioned = VersionedAst { version, root };
545-
546-
let diagnostics = self.get_diagnostics(&uri, &versioned).await?;
547-
let error_id_to_ranges = build_error_id_to_ranges(diagnostics);
512+
self.on_apply_all_fix(arguments).await?;
513+
None
514+
}
515+
_ => {
548516
self
549517
.client
550-
.log_message(MessageType::LOG, "Parsing doc.")
518+
.log_message(
519+
MessageType::LOG,
520+
format!("Unrecognized command: {}", command),
521+
)
551522
.await;
523+
None
524+
}
525+
}
526+
}
552527

553-
let changes =
554-
self.compute_all_fixes(TextDocumentIdentifier::new(uri), error_id_to_ranges, path);
528+
async fn on_apply_all_fix(&self, arguments: Vec<Value>) -> Option<()> {
529+
let text_doc: TextDocumentItem = match serde_json::from_value(arguments.first()?.clone()) {
530+
Ok(value) => value,
531+
Err(e) => {
555532
self
556533
.client
557-
.apply_edit(WorkspaceEdit {
558-
changes,
559-
document_changes: None,
560-
change_annotations: None,
561-
})
562-
.await
563-
.ok()?;
534+
.log_message(
535+
MessageType::ERROR,
536+
format!("JSON deserialization error: {}", e),
537+
)
538+
.await;
539+
return None;
540+
}
541+
};
564542

565-
None
543+
let uri = text_doc.uri;
544+
let path = match uri.to_file_path() {
545+
Ok(path) => path,
546+
Err(_) => {
547+
self
548+
.client
549+
.log_message(MessageType::ERROR, "Invalid URI format")
550+
.await;
551+
return None;
566552
}
567-
_ => None,
568-
}
553+
};
554+
let version = text_doc.version;
555+
let text = text_doc.text;
556+
557+
let lang = Self::infer_lang_from_uri(&uri)?;
558+
559+
let root = AstGrep::new(text, lang);
560+
let versioned = VersionedAst { version, root };
561+
562+
let diagnostics = self.get_diagnostics(&uri, &versioned).await?;
563+
let error_id_to_ranges = build_error_id_to_ranges(diagnostics);
564+
self
565+
.client
566+
.log_message(MessageType::LOG, "Parsing doc.")
567+
.await;
568+
569+
let changes =
570+
self.compute_all_fixes(TextDocumentIdentifier::new(uri), error_id_to_ranges, path);
571+
self
572+
.client
573+
.apply_edit(WorkspaceEdit {
574+
changes,
575+
document_changes: None,
576+
change_annotations: None,
577+
})
578+
.await
579+
.ok()?;
580+
None
569581
}
570582
}

0 commit comments

Comments
 (0)