diff --git a/CHANGELOG.md b/CHANGELOG.md index 5edf1b478..ea51bcd8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ #### :house: Internal - Find `@rescript/runtime` for Rewatch compiler-args call. https://github.com/rescript-lang/rescript-vscode/pull/1125 +- Use `prepareRename` command (when a new enough ReScript version is used) to speed up the `rename` command. https://github.com/rescript-lang/rescript-vscode/pull/1124 ## 1.64.0 diff --git a/server/src/server.ts b/server/src/server.ts index 3dc08eb90..31ca545f7 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -3,6 +3,7 @@ import * as p from "vscode-languageserver-protocol"; import * as v from "vscode-languageserver"; import * as rpc from "vscode-jsonrpc/node"; import * as path from "path"; +import semver from "semver"; import fs from "fs"; import { DidChangeWatchedFilesNotification, @@ -687,6 +688,37 @@ async function prepareRename( // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_prepareRename let params = msg.params as p.PrepareRenameParams; let filePath = fileURLToPath(params.textDocument.uri); + + // `prepareRename` was introduced in 12.0.0-beta.10 + let projectRootPath = utils.findProjectRootOfFile(filePath); + let rescriptVersion = + (projectRootPath && projectsFiles.get(projectRootPath)?.rescriptVersion) || + (await utils.findReScriptVersionForProjectRoot(projectRootPath ?? null)); + + let shouldUsePrepareRenameCommand = false; + if (rescriptVersion != null) { + shouldUsePrepareRenameCommand = + semver.valid(rescriptVersion) != null && + semver.satisfies(rescriptVersion, ">=12.0.0-beta.10", { + includePrerelease: true, + }); + } + + if (shouldUsePrepareRenameCommand) { + let analysisResult = await utils.runAnalysisAfterSanityCheck(filePath, [ + "prepareRename", + filePath, + params.position.line, + params.position.character, + ]); + + return { + jsonrpc: c.jsonrpcVersion, + id: msg.id, + result: analysisResult as p.PrepareRenameResult, + }; + } + let locations: null | p.Location[] = await utils.getReferencesForPosition( filePath, params.position,