Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(harper-ls): remove deleted file/s diagnostics #304

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions harper-ls/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ use tower_lsp::lsp_types::notification::PublishDiagnostics;
use tower_lsp::lsp_types::{
CodeActionOrCommand, CodeActionParams, CodeActionProviderCapability, CodeActionResponse,
Command, ConfigurationItem, Diagnostic, DidChangeConfigurationParams,
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
DidSaveTextDocumentParams, ExecuteCommandOptions, ExecuteCommandParams, InitializeParams,
DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
DidChangeWatchedFilesRegistrationOptions, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, DidSaveTextDocumentParams, ExecuteCommandOptions,
ExecuteCommandParams, FileChangeType, FileSystemWatcher, GlobPattern, InitializeParams,
InitializeResult, InitializedParams, MessageType, PublishDiagnosticsParams, Range,
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions,
TextDocumentSyncSaveOptions, Url,
Registration, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
TextDocumentSyncOptions, TextDocumentSyncSaveOptions, Url, WatchKind,
};
use tower_lsp::{Client, LanguageServer};
use tracing::{error, info};
use tracing::{error, info, warn};

use crate::config::Config;
use crate::diagnostics::{lint_to_code_actions, lints_to_diagnostics};
Expand Down Expand Up @@ -372,6 +374,27 @@ impl LanguageServer for Backend {
.await;

self.pull_config().await;

let did_change_watched_files = Registration {
id: "workspace/didChangeWatchedFiles".to_owned(),
method: "workspace/didChangeWatchedFiles".to_owned(),
register_options: Some(
serde_json::to_value(DidChangeWatchedFilesRegistrationOptions {
watchers: vec![FileSystemWatcher {
glob_pattern: GlobPattern::String("**/*".to_owned()),
kind: Some(WatchKind::Delete),
}],
})
.unwrap(),
),
};
if let Err(err) = self
.client
.register_capability(vec![did_change_watched_files])
.await
{
warn!("Unable to register watch file capability: {}", err);
}
}

async fn did_save(&self, params: DidSaveTextDocumentParams) {
Expand Down Expand Up @@ -411,6 +434,38 @@ impl LanguageServer for Backend {

async fn did_close(&self, _params: DidCloseTextDocumentParams) {}

async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
let mut doc_lock = self.doc_state.lock().await;
let mut urls_to_clear = Vec::new();

for change in &params.changes {
if change.typ != FileChangeType::DELETED {
continue;
}

doc_lock.retain(|url, _| {
// `change.uri` could be a directory so use `starts_with` instead of `==`.
let to_remove = url.as_str().starts_with(change.uri.as_str());

if to_remove {
urls_to_clear.push(url.clone());
}

!to_remove
});
}

for url in &urls_to_clear {
self.client
.send_notification::<PublishDiagnostics>(PublishDiagnosticsParams {
uri: url.clone(),
diagnostics: vec![],
version: None,
})
.await;
}
}

async fn execute_command(&self, params: ExecuteCommandParams) -> Result<Option<Value>> {
let mut string_args = params
.arguments
Expand Down
39 changes: 37 additions & 2 deletions packages/vscode-plugin/src/tests/suite/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Extension } from 'vscode';

import { ConfigurationTarget, Uri, workspace } from 'vscode';
import { commands, ConfigurationTarget, Uri, workspace } from 'vscode';

import {
activateHarper,
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Integration >', () => {
const config = workspace.getConfiguration('harper-ls.linters');
await config.update('repeated_words', false, ConfigurationTarget.Workspace);
// Wait for `harper-ls` to update diagnostics
await sleep(250);
await sleep(300);

compareActualVsExpectedDiagnostics(
getActualDiagnostics(markdownUri),
Expand All @@ -61,4 +61,39 @@ describe('Integration >', () => {
// Set config back to default value
await config.update('repeated_words', true, ConfigurationTarget.Workspace);
});

it('updates diagnostics when files are deleted', async () => {
mcecode marked this conversation as resolved.
Show resolved Hide resolved
const markdownContent = await workspace.fs.readFile(markdownUri);

// Delete file through VSCode
await commands.executeCommand('workbench.files.action.showActiveFileInExplorer');
await commands.executeCommand('deleteFile');
// Wait for `harper-ls` to update diagnostics
await sleep(450);

compareActualVsExpectedDiagnostics(
getActualDiagnostics(markdownUri),
createExpectedDiagnostics()
);

// Restore and reopen deleted file
await workspace.fs.writeFile(markdownUri, markdownContent);
await openFile('integration.md');
// Wait for `harper-ls` to update diagnostics
await sleep(75);

// Delete file directly
await workspace.fs.delete(markdownUri);
// Wait for `harper-ls` to update diagnostics
await sleep(450);

compareActualVsExpectedDiagnostics(
getActualDiagnostics(markdownUri),
createExpectedDiagnostics()
);

// Restore and reopen deleted file
await workspace.fs.writeFile(markdownUri, markdownContent);
await openFile('integration.md');
});
});
Loading