From 1518ce4db452d788f19a8174b2dab678b28a2e70 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Mon, 21 Oct 2024 15:55:53 -0400 Subject: [PATCH] Only restart client if the contents of the watched files have changed (#2745) --- vscode/src/workspace.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/vscode/src/workspace.ts b/vscode/src/workspace.ts index 516620974..ef70d4117 100644 --- a/vscode/src/workspace.ts +++ b/vscode/src/workspace.ts @@ -1,3 +1,5 @@ +import { createHash } from "crypto"; + import * as vscode from "vscode"; import { CodeLens, State } from "vscode-languageclient/node"; @@ -22,6 +24,7 @@ export class Workspace implements WorkspaceInterface { private readonly isMainWorkspace: boolean; private readonly telemetry: vscode.TelemetryLogger; private readonly virtualDocuments = new Map(); + private readonly restartDocumentShas = new Map(); private needsRestart = false; #inhibitRestart = false; #error = false; @@ -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`, @@ -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), );