Skip to content

Commit 269d6f2

Browse files
authored
Cleanup diagnostics for deleted or renamed files (#1653)
When the file is deleted or renamed, remove it from the diagnostic collection. Extract the swift file types to a constant. Issue: #1652
1 parent a3a18c5 commit 269d6f2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/BackgroundCompilation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import { FolderContext } from "./FolderContext";
1919
import { TaskOperation } from "./tasks/TaskQueue";
2020
// eslint-disable-next-line @typescript-eslint/no-require-imports
2121
import debounce = require("lodash.debounce");
22+
import { validFileTypes } from "./utilities/filesystem";
2223

2324
export class BackgroundCompilation implements vscode.Disposable {
2425
private workspaceFileWatcher?: vscode.FileSystemWatcher;
2526
private configurationEventDisposable?: vscode.Disposable;
26-
private validFileTypes = ["swift", "c", "cpp", "h", "hpp", "m", "mm"];
2727
private disposables: vscode.Disposable[] = [];
2828

2929
constructor(private folderContext: FolderContext) {
@@ -44,7 +44,7 @@ export class BackgroundCompilation implements vscode.Disposable {
4444
}
4545

4646
private setupFileWatching() {
47-
const fileTypes = this.validFileTypes.join(",");
47+
const fileTypes = validFileTypes.join(",");
4848
const rootFolders = ["Sources", "Tests", "Snippets", "Plugins"].join(",");
4949
this.disposables.push(
5050
(this.workspaceFileWatcher = vscode.workspace.createFileSystemWatcher(

src/DiagnosticsManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import configuration from "./configuration";
2121
import { SwiftExecution } from "./tasks/SwiftExecution";
2222
import { WorkspaceContext } from "./WorkspaceContext";
2323
import { checkIfBuildComplete, lineBreakRegex } from "./utilities/tasks";
24+
import { validFileTypes } from "./utilities/filesystem";
2425

2526
interface ParsedDiagnostic {
2627
uri: string;
@@ -102,6 +103,17 @@ export class DiagnosticsManager implements vscode.Disposable {
102103
context.outputChannel.log(`${e}`, 'Failed to provide "swiftc" diagnostics')
103104
);
104105
});
106+
const fileTypes = validFileTypes.join(",");
107+
this.workspaceFileWatcher = vscode.workspace.createFileSystemWatcher(
108+
`**/*.{${fileTypes}}`,
109+
true,
110+
true
111+
);
112+
this.onDidDeleteDisposible = this.workspaceFileWatcher.onDidDelete(uri => {
113+
if (this.allDiagnostics.delete(uri.fsPath)) {
114+
this.diagnosticCollection.delete(uri);
115+
}
116+
});
105117
}
106118

107119
/**
@@ -276,6 +288,8 @@ export class DiagnosticsManager implements vscode.Disposable {
276288
this.diagnosticCollection.dispose();
277289
this.onDidStartTaskDisposible.dispose();
278290
this.onDidChangeConfigurationDisposible.dispose();
291+
this.onDidDeleteDisposible.dispose();
292+
this.workspaceFileWatcher.dispose();
279293
}
280294

281295
private includeSwiftcDiagnostics(): boolean {
@@ -454,4 +468,6 @@ export class DiagnosticsManager implements vscode.Disposable {
454468

455469
private onDidStartTaskDisposible: vscode.Disposable;
456470
private onDidChangeConfigurationDisposible: vscode.Disposable;
471+
private onDidDeleteDisposible: vscode.Disposable;
472+
private workspaceFileWatcher: vscode.FileSystemWatcher;
457473
}

src/utilities/filesystem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import * as fs from "fs/promises";
1616
import * as path from "path";
1717

18+
export const validFileTypes = ["swift", "c", "cpp", "h", "hpp", "m", "mm"];
19+
1820
/**
1921
* Checks if a file, directory or symlink exists at the supplied path.
2022
* @param pathComponents The path to check for existence

0 commit comments

Comments
 (0)