diff --git a/CHANGELOG.md b/CHANGELOG.md index 16f798216..5fec14498 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Prefer opened `Belt` modules in autocomplete when `-open Belt` is detected in `bsconfig`. https://github.com/rescript-lang/rescript-vscode/pull/673 - Improve precision in signature help. You now do not need to type anything into the argument for it to highlight. https://github.com/rescript-lang/rescript-vscode/pull/675 - Remove redundant function name in signature help, to clean up what's shown to the user some. https://github.com/rescript-lang/rescript-vscode/pull/678 +- Migrate `prepareRename` to analysis. https://github.com/rescript-lang/rescript-vscode/pull/657 - Show docstrings in hover for record fields and variant constructors. https://github.com/rescript-lang/rescript-vscode/pull/694 #### :bug: Bug Fix diff --git a/analysis/src/Cli.ml b/analysis/src/Cli.ml index 407e9cde2..d7450a893 100644 --- a/analysis/src/Cli.ml +++ b/analysis/src/Cli.ml @@ -10,6 +10,7 @@ API examples: ./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true ./rescript-editor-analysis.exe references src/MyFile.res 10 2 ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo + ./rescript-editor-analysis.exe prepareRename src/MyFile.res 10 2 ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res ./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25 ./rescript-editor-analysis.exe codeLens src/MyFile.res @@ -51,6 +52,10 @@ Options: ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo + prepareRename: Validity of a rename operation at a given location. + + ./rescript-editor-analysis.exe prepareRename src/MyFile.res 10 2 + semanticTokens: return token semantic highlighting info for MyFile.res ./rescript-editor-analysis.exe semanticTokens src/MyFile.res @@ -140,6 +145,10 @@ let main () = Commands.rename ~path ~pos:(int_of_string line, int_of_string col) ~newName ~debug:false + | [_; "prepareRename"; path; line; col] -> + Commands.prepareRename ~path + ~pos:(int_of_string line, int_of_string col) + ~debug:false | [_; "semanticTokens"; currentFile] -> SemanticTokens.semanticTokens ~currentFile | [_; "createInterface"; path; cmiFile] -> diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index db671ca08..63fbfdea4 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -253,6 +253,20 @@ let rename ~path ~pos ~newName ~debug = in print_endline result +let prepareRename ~path ~pos ~debug = + let currentLoc = + match Cmt.loadFullCmtFromPath ~path with + | None -> None + | Some full -> ( + match References.getLocItem ~full ~pos ~debug with + | None -> None + | Some {loc} -> Some (Utils.cmtLocToRange loc)) + in + (match currentLoc with + | None -> Protocol.null + | Some range -> range |> Protocol.stringifyRange) + |> print_endline + let format ~path = if Filename.check_suffix path ".res" then let {Res_driver.parsetree = structure; comments; diagnostics} = @@ -382,6 +396,13 @@ let test ~path = ^ string_of_int col ^ " " ^ newName) in rename ~path ~pos:(line, col) ~newName ~debug:true + | "pre" -> + let () = + print_endline + ("PrepareRename " ^ path ^ " " ^ string_of_int line ^ ":" + ^ string_of_int col) + in + prepareRename ~path ~pos:(line, col) ~debug:true | "typ" -> print_endline ("TypeDefinition " ^ path ^ " " ^ string_of_int line ^ ":" diff --git a/analysis/tests/src/PrepareRename.res b/analysis/tests/src/PrepareRename.res new file mode 100644 index 000000000..cce33d260 --- /dev/null +++ b/analysis/tests/src/PrepareRename.res @@ -0,0 +1,8 @@ +let a = 1 +// ^pre + +let b = 2 and c = 3 +// ^pre + +let d = 0 +//^pre diff --git a/analysis/tests/src/expected/PrepareRename.res.txt b/analysis/tests/src/expected/PrepareRename.res.txt new file mode 100644 index 000000000..378f7f07a --- /dev/null +++ b/analysis/tests/src/expected/PrepareRename.res.txt @@ -0,0 +1,9 @@ +PrepareRename src/PrepareRename.res 0:4 +{"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}} + +PrepareRename src/PrepareRename.res 3:14 +{"start": {"line": 3, "character": 14}, "end": {"line": 3, "character": 15}} + +PrepareRename src/PrepareRename.res 6:2 +null + diff --git a/server/src/server.ts b/server/src/server.ts index 4a8691426..99882ab49 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -540,16 +540,13 @@ function references(msg: p.RequestMessage) { // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references let params = msg.params as p.ReferenceParams; let filePath = fileURLToPath(params.textDocument.uri); - let result: typeof p.ReferencesRequest.type = utils.getReferencesForPosition( + let response = utils.runAnalysisCommand(filePath, [ + "references", filePath, - params.position - ); - let response: p.ResponseMessage = { - jsonrpc: c.jsonrpcVersion, - id: msg.id, - result, - // error: code and message set in case an exception happens during the definition request. - }; + params.position.line, + params.position.character, + ], msg, false); + return response; } @@ -557,35 +554,14 @@ function prepareRename(msg: p.RequestMessage): p.ResponseMessage { // 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); - let locations: null | p.Location[] = utils.getReferencesForPosition( + let response = utils.runAnalysisCommand(filePath, [ + "prepareRename", filePath, - params.position - ); - let result: p.Range | null = null; - if (locations !== null) { - locations.forEach((loc) => { - if ( - path.normalize(fileURLToPath(loc.uri)) === - path.normalize(fileURLToPath(params.textDocument.uri)) - ) { - let { start, end } = loc.range; - let pos = params.position; - if ( - start.character <= pos.character && - start.line <= pos.line && - end.character >= pos.character && - end.line >= pos.line - ) { - result = loc.range; - } - } - }); - } - return { - jsonrpc: c.jsonrpcVersion, - id: msg.id, - result, - }; + params.position.line, + params.position.character, + ], msg, false); + + return response } function rename(msg: p.RequestMessage) { diff --git a/server/src/utils.ts b/server/src/utils.ts index cb40d9b03..1a0968a23 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -175,17 +175,6 @@ export let runAnalysisCommand = ( return response; }; -export let getReferencesForPosition = ( - filePath: p.DocumentUri, - position: p.Position -) => - runAnalysisAfterSanityCheck(filePath, [ - "references", - filePath, - position.line, - position.character, - ]); - export const toCamelCase = (text: string): string => { return text .replace(/(?:^\w|[A-Z]|\b\w)/g, (s: string) => s.toUpperCase())