Skip to content

Commit

Permalink
Only restart client if the contents of the watched files have changed (
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Oct 21, 2024
1 parent ec3103d commit 1518ce4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion vscode/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createHash } from "crypto";

import * as vscode from "vscode";
import { CodeLens, State } from "vscode-languageclient/node";

Expand All @@ -22,6 +24,7 @@ export class Workspace implements WorkspaceInterface {
private readonly isMainWorkspace: boolean;
private readonly telemetry: vscode.TelemetryLogger;
private readonly virtualDocuments = new Map<string, string>();
private readonly restartDocumentShas = new Map<string, string>();
private needsRestart = false;
#inhibitRestart = false;
#error = false;
Expand Down Expand Up @@ -366,6 +369,26 @@ export class Workspace implements WorkspaceInterface {
new vscode.RelativePattern(this.workspaceFolder, pattern),
);

// Handler for only triggering restart if the contents of the file have been modified. If the file was just touched,
// but the contents are the same, we don't want to restart
const debouncedRestartWithHashCheck = debounce(async (uri: vscode.Uri) => {
const fileContents = await vscode.workspace.fs.readFile(uri);
const fsPath = uri.fsPath;

const hash = createHash("sha256");
hash.update(fileContents.toString());
const currentSha = hash.digest("hex");

if (this.restartDocumentShas.get(fsPath) !== currentSha) {
this.outputChannel.info(
`Restarting the Ruby LSP because ${pattern} changed`,
);

this.restartDocumentShas.set(fsPath, currentSha);
await this.restart();
}
}, 5000);

const debouncedRestart = debounce(async () => {
this.outputChannel.info(
`Restarting the Ruby LSP because ${pattern} changed`,
Expand All @@ -375,7 +398,7 @@ export class Workspace implements WorkspaceInterface {

context.subscriptions.push(
watcher,
watcher.onDidChange(debouncedRestart),
watcher.onDidChange(debouncedRestartWithHashCheck),
watcher.onDidCreate(debouncedRestart),
watcher.onDidDelete(debouncedRestart),
);
Expand Down

0 comments on commit 1518ce4

Please sign in to comment.