Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions analysis/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand Down Expand Up @@ -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 () =
Expand Down
6 changes: 6 additions & 0 deletions tests/analysis_tests/tests/src/PrepareRename.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let x = 12
// ^pre

let foo = (~xx) => xx + 1
// ^pre

12 changes: 12 additions & 0 deletions tests/analysis_tests/tests/src/expected/PrepareRename.res.txt
Original file line number Diff line number Diff line change
@@ -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"
}

Loading