Skip to content

Commit 553bd9c

Browse files
authored
Add prepareRename command (#7847)
* add prepareRename command * changelog
1 parent 2d589b7 commit 553bd9c

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
- Improve error message for trying to define a type inside a function. https://github.com/rescript-lang/rescript/pull/7843
4848
- Refactor CLI to use spawn for better signal handling in watch mode. https://github.com/rescript-lang/rescript/pull/7844
4949

50+
- Add a `prepareRename` command the LSP can use for faster renames. https://github.com/rescript-lang/rescript/pull/7847
51+
5052
# 12.0.0-beta.9
5153

5254
#### :boom: Breaking Change

analysis/bin/main.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ API examples:
1111
./rescript-editor-analysis.exe documentSymbol src/Foo.res
1212
./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true
1313
./rescript-editor-analysis.exe references src/MyFile.res 10 2
14+
./rescript-editor-analysis.exe prepareRename src/MyFile.res 10 2
1415
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
1516
./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res
1617
./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25
@@ -49,6 +50,10 @@ Options:
4950

5051
./rescript-editor-analysis.exe references src/MyFile.res 10 2
5152

53+
prepareRename: quickly compute the identifier range (and placeholder) for item at line 10 column 2 without scanning references:
54+
55+
./rescript-editor-analysis.exe prepareRename src/MyFile.res 10 2
56+
5257
rename: rename all appearances of item in MyFile.res at line 10 column 2 with foo:
5358

5459
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
@@ -197,6 +202,10 @@ let main () =
197202
Commands.references ~path
198203
~pos:(int_of_string line, int_of_string col)
199204
~debug
205+
| [_; "prepareRename"; path; line; col] ->
206+
Commands.prepareRename ~path
207+
~pos:(int_of_string line, int_of_string col)
208+
~debug
200209
| [_; "rename"; path; line; col; newName] ->
201210
Commands.rename ~path
202211
~pos:(int_of_string line, int_of_string col)

analysis/src/Commands.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,30 @@ let rename ~path ~pos ~newName ~debug =
275275
in
276276
print_endline result
277277

278+
let prepareRename ~path ~pos ~debug =
279+
match Cmt.loadFullCmtFromPath ~path with
280+
| None -> print_endline Protocol.null
281+
| Some full -> (
282+
match References.getLocItem ~full ~pos ~debug with
283+
| None -> print_endline Protocol.null
284+
| Some locItem ->
285+
let range = Utils.cmtLocToRange locItem.loc in
286+
let placeholderOpt =
287+
match locItem.locType with
288+
| Typed (name, _, _) | TopLevelModule name | TypeDefinition (name, _, _)
289+
->
290+
Some name
291+
| _ -> None
292+
in
293+
let fields =
294+
[("range", Some (Protocol.stringifyRange range))]
295+
@
296+
match placeholderOpt with
297+
| None -> []
298+
| Some s -> [("placeholder", Some (Protocol.wrapInQuotes s))]
299+
in
300+
print_endline (Protocol.stringifyObject fields))
301+
278302
let format ~path =
279303
if Filename.check_suffix path ".res" then
280304
let {Res_driver.parsetree = structure; comments; diagnostics} =
@@ -415,6 +439,11 @@ let test ~path =
415439
("References " ^ path ^ " " ^ string_of_int line ^ ":"
416440
^ string_of_int col);
417441
references ~path ~pos:(line, col) ~debug:true
442+
| "pre" ->
443+
print_endline
444+
("PrepareRename " ^ path ^ " " ^ string_of_int line ^ ":"
445+
^ string_of_int col);
446+
prepareRename ~path ~pos:(line, col) ~debug:true
418447
| "ren" ->
419448
let newName = String.sub rest 4 (len - mlen - 4) in
420449
let () =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let x = 12
2+
// ^pre
3+
4+
let foo = (~xx) => xx + 1
5+
// ^pre
6+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PrepareRename src/PrepareRename.res 0:4
2+
{
3+
"range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}},
4+
"placeholder": "x"
5+
}
6+
7+
PrepareRename src/PrepareRename.res 3:19
8+
{
9+
"range": {"start": {"line": 3, "character": 19}, "end": {"line": 3, "character": 21}},
10+
"placeholder": "xx"
11+
}
12+

0 commit comments

Comments
 (0)