fix(oxc_vscode): vscode extension - check on file change (not on file save) (#1525)

1. Closed https://github.com/oxc-project/oxc/issues/1518

[录屏 2023年11月24日
12时08分14秒.webm](https://github.com/oxc-project/oxc/assets/17974631/6ff42edf-b837-466a-a9fa-a1d1244dfd45)
This commit is contained in:
IWANABETHATGUY 2023-11-27 12:20:50 +08:00 committed by GitHub
parent 85e8c8bad5
commit 8251a343aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 12 deletions

View file

@ -155,10 +155,14 @@ impl IsolatedLintHandler {
Self::process_diagnostics(&rx_error) Self::process_diagnostics(&rx_error)
} }
pub fn run_single(&self, path: &Path) -> Option<Vec<DiagnosticReport>> { pub fn run_single(
&self,
path: &Path,
content: Option<String>,
) -> Option<Vec<DiagnosticReport>> {
if Self::is_wanted_ext(path) { if Self::is_wanted_ext(path) {
Some( Some(
Self::lint_path(&self.linter, path, Arc::clone(&self.plugin)).map_or( Self::lint_path(&self.linter, path, Arc::clone(&self.plugin), content).map_or(
vec![], vec![],
|(p, errors)| { |(p, errors)| {
errors.into_iter().map(|e| e.into_diagnostic_report(&p)).collect() errors.into_iter().map(|e| e.into_diagnostic_report(&p)).collect()
@ -201,7 +205,7 @@ impl IsolatedLintHandler {
let linter = Arc::clone(&linter); let linter = Arc::clone(&linter);
let plugin = Arc::clone(&plugin); let plugin = Arc::clone(&plugin);
rayon::spawn(move || { rayon::spawn(move || {
if let Some(diagnostics) = Self::lint_path(&linter, &path, plugin) { if let Some(diagnostics) = Self::lint_path(&linter, &path, plugin, None) {
tx_error.send(diagnostics).unwrap(); tx_error.send(diagnostics).unwrap();
} }
drop(tx_error); drop(tx_error);
@ -228,9 +232,12 @@ impl IsolatedLintHandler {
linter: &Linter, linter: &Linter,
path: &Path, path: &Path,
plugin: Plugin, plugin: Plugin,
source_text: Option<String>,
) -> Option<(PathBuf, Vec<ErrorWithPosition>)> { ) -> Option<(PathBuf, Vec<ErrorWithPosition>)> {
let source_text = let source_text = source_text.unwrap_or_else(|| {
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path:?}")); fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path:?}"))
});
let allocator = Allocator::default(); let allocator = Allocator::default();
let source_type = let source_type =
SourceType::from_path(path).unwrap_or_else(|_| panic!("Incorrect {path:?}")); SourceType::from_path(path).unwrap_or_else(|_| panic!("Incorrect {path:?}"));
@ -379,7 +386,12 @@ impl ServerLinter {
.run_full() .run_full()
} }
pub fn run_single(&self, root_uri: &Url, uri: &Url) -> Option<Vec<DiagnosticReport>> { pub fn run_single(
&self,
root_uri: &Url,
uri: &Url,
content: Option<String>,
) -> Option<Vec<DiagnosticReport>> {
let options = LintOptions { let options = LintOptions {
paths: vec![root_uri.to_file_path().unwrap()], paths: vec![root_uri.to_file_path().unwrap()],
ignore_path: "node_modules".into(), ignore_path: "node_modules".into(),
@ -393,6 +405,6 @@ impl ServerLinter {
Arc::clone(&self.linter), Arc::clone(&self.linter),
Arc::clone(&self.plugin), Arc::clone(&self.plugin),
) )
.run_single(&uri.to_file_path().unwrap()) .run_single(&uri.to_file_path().unwrap(), content)
} }
} }

View file

@ -79,15 +79,18 @@ impl LanguageServer for Backend {
async fn did_save(&self, params: DidSaveTextDocumentParams) { async fn did_save(&self, params: DidSaveTextDocumentParams) {
debug!("oxc server did save"); debug!("oxc server did save");
self.handle_file_update(params.text_document.uri).await; self.handle_file_update(params.text_document.uri, None).await;
} }
/// When the document changed, it may not be written to disk, so we should
/// get the file context from the language client
async fn did_change(&self, params: DidChangeTextDocumentParams) { async fn did_change(&self, params: DidChangeTextDocumentParams) {
self.handle_file_update(params.text_document.uri).await; let content = params.content_changes.first().map(|c| c.text.clone());
self.handle_file_update(params.text_document.uri, content).await;
} }
async fn did_open(&self, params: DidOpenTextDocumentParams) { async fn did_open(&self, params: DidOpenTextDocumentParams) {
self.handle_file_update(params.text_document.uri).await; self.handle_file_update(params.text_document.uri, None).await;
} }
async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> { async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
@ -156,10 +159,10 @@ impl Backend {
.await; .await;
} }
async fn handle_file_update(&self, uri: Url) { async fn handle_file_update(&self, uri: Url, content: Option<String>) {
if let Some(Some(root_uri)) = self.root_uri.get() { if let Some(Some(root_uri)) = self.root_uri.get() {
self.server_linter.make_plugin(root_uri); self.server_linter.make_plugin(root_uri);
if let Some(diagnostics) = self.server_linter.run_single(root_uri, &uri) { if let Some(diagnostics) = self.server_linter.run_single(root_uri, &uri, content) {
self.client self.client
.publish_diagnostics( .publish_diagnostics(
uri.clone(), uri.clone(),