Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polish: move prepareRename to analysis binary #657

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions analysis/src/Cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] ->
Expand Down
21 changes: 21 additions & 0 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand Down Expand Up @@ -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 ^ ":"
Expand Down
8 changes: 8 additions & 0 deletions analysis/tests/src/PrepareRename.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let a = 1
// ^pre

let b = 2 and c = 3
// ^pre

let d = 0
//^pre
9 changes: 9 additions & 0 deletions analysis/tests/src/expected/PrepareRename.res.txt
Original file line number Diff line number Diff line change
@@ -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

50 changes: 13 additions & 37 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,52 +540,28 @@ 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;
}

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) {
Expand Down
11 changes: 0 additions & 11 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down