diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d20016821..bc8bde3b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ #### :house: Internal +- Add a `prepareRename` command the LSP can use for faster renames. https://github.com/rescript-lang/rescript/pull/7847 + # 12.0.0-beta.9 #### :boom: Breaking Change diff --git a/analysis/bin/main.ml b/analysis/bin/main.ml index f25cb78115..a1c6d9bc6e 100644 --- a/analysis/bin/main.ml +++ b/analysis/bin/main.ml @@ -11,6 +11,7 @@ API examples: ./rescript-editor-analysis.exe documentSymbol src/Foo.res ./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 prepareRename src/MyFile.res 10 2 ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res ./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25 @@ -49,6 +50,10 @@ Options: ./rescript-editor-analysis.exe references src/MyFile.res 10 2 + prepareRename: quickly compute the identifier range (and placeholder) for item at line 10 column 2 without scanning references: + + ./rescript-editor-analysis.exe prepareRename src/MyFile.res 10 2 + rename: rename all appearances of item in MyFile.res at line 10 column 2 with foo: ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo @@ -197,6 +202,10 @@ let main () = Commands.references ~path ~pos:(int_of_string line, int_of_string col) ~debug + | [_; "prepareRename"; path; line; col] -> + Commands.prepareRename ~path + ~pos:(int_of_string line, int_of_string col) + ~debug | [_; "rename"; path; line; col; newName] -> Commands.rename ~path ~pos:(int_of_string line, int_of_string col) diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index a4ac9168f5..40799348ec 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -275,6 +275,30 @@ let rename ~path ~pos ~newName ~debug = in print_endline result +let prepareRename ~path ~pos ~debug = + match Cmt.loadFullCmtFromPath ~path with + | None -> print_endline Protocol.null + | Some full -> ( + match References.getLocItem ~full ~pos ~debug with + | None -> print_endline Protocol.null + | Some locItem -> + let range = Utils.cmtLocToRange locItem.loc in + let placeholderOpt = + match locItem.locType with + | Typed (name, _, _) | TopLevelModule name | TypeDefinition (name, _, _) + -> + Some name + | _ -> None + in + let fields = + [("range", Some (Protocol.stringifyRange range))] + @ + match placeholderOpt with + | None -> [] + | Some s -> [("placeholder", Some (Protocol.wrapInQuotes s))] + in + print_endline (Protocol.stringifyObject fields)) + let format ~path = if Filename.check_suffix path ".res" then let {Res_driver.parsetree = structure; comments; diagnostics} = @@ -415,6 +439,11 @@ let test ~path = ("References " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col); references ~path ~pos:(line, col) ~debug:true + | "pre" -> + print_endline + ("PrepareRename " ^ path ^ " " ^ string_of_int line ^ ":" + ^ string_of_int col); + prepareRename ~path ~pos:(line, col) ~debug:true | "ren" -> let newName = String.sub rest 4 (len - mlen - 4) in let () = diff --git a/tests/analysis_tests/tests/src/PrepareRename.res b/tests/analysis_tests/tests/src/PrepareRename.res new file mode 100644 index 0000000000..64aa584451 --- /dev/null +++ b/tests/analysis_tests/tests/src/PrepareRename.res @@ -0,0 +1,6 @@ +let x = 12 +// ^pre + +let foo = (~xx) => xx + 1 +// ^pre + diff --git a/tests/analysis_tests/tests/src/expected/PrepareRename.res.txt b/tests/analysis_tests/tests/src/expected/PrepareRename.res.txt new file mode 100644 index 0000000000..bb418cc27a --- /dev/null +++ b/tests/analysis_tests/tests/src/expected/PrepareRename.res.txt @@ -0,0 +1,12 @@ +PrepareRename src/PrepareRename.res 0:4 +{ + "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}, + "placeholder": "x" + } + +PrepareRename src/PrepareRename.res 3:19 +{ + "range": {"start": {"line": 3, "character": 19}, "end": {"line": 3, "character": 21}}, + "placeholder": "xx" + } +