Skip to content

Commit 83a7d74

Browse files
committed
add range params
1 parent 58d3d67 commit 83a7d74

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

analysis/examples/larger-project/.merlin

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
####{BSB GENERATED: NO EDIT
2-
FLG -ppx '/Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/darwin/bsc.exe -as-ppx -bs-jsx 3'
3-
S /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/lib/ocaml
4-
B /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/lib/ocaml
2+
FLG -ppx '/home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/linux/bsc.exe -as-ppx -bs-jsx 3'
3+
S /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/lib/ocaml
4+
B /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/rescript/lib/ocaml
55
FLG -w +a-4-9-20-40-41-42-50-61-102
6-
S /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/@rescript/react/lib/ocaml
7-
B /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/@rescript/react/lib/ocaml
8-
S /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/@glennsl/bs-json/lib/ocaml
9-
B /Users/cristianocalcagno/GitHub/rescript-vscode/analysis/examples/larger-project/node_modules/@glennsl/bs-json/lib/ocaml
6+
S /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/@rescript/react/lib/ocaml
7+
B /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/@rescript/react/lib/ocaml
8+
S /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/@glennsl/bs-json/lib/ocaml
9+
B /home/pedro/Desktop/Projects/rescript-vscode/analysis/examples/larger-project/node_modules/@glennsl/bs-json/lib/ocaml
1010
S src
1111
B lib/bs/src
1212
S src/exception

analysis/src/Cli.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ let main () =
9494
Commands.hover ~path
9595
~pos:(int_of_string line, int_of_string col)
9696
~currentFile ~debug:false
97-
| [_; "inlayHint"; path; maxLength] ->
98-
Commands.inlayhint ~path ~maxLength ~debug:false
97+
| [_; "inlayHint"; path; line_start; line_end; maxLength] ->
98+
Commands.inlayhint ~path
99+
~pos:(int_of_string line_start, int_of_string line_end)
100+
~maxLength ~debug:false
99101
| [_; "codeAction"; path; line; col; currentFile] ->
100102
Commands.codeAction ~path
101103
~pos:(int_of_string line, int_of_string col)

analysis/src/Commands.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ let completion ~debug ~path ~pos ~currentFile =
2828
|> List.map Protocol.stringifyCompletionItem
2929
|> Protocol.array)
3030

31-
let inlayhint ~path ~maxLength ~debug =
32-
let result = Hint.inlay ~path ~maxLength ~debug |> Protocol.array in
31+
let inlayhint ~path ~pos ~maxLength ~debug =
32+
let result = Hint.inlay ~path ~pos ~maxLength ~debug |> Protocol.array in
3333
print_endline result
3434

3535
let hover ~path ~pos ~currentFile ~debug =
@@ -386,7 +386,7 @@ let test ~path =
386386
(Protocol.stringifyRange range)
387387
indent indent newText)))
388388
| "dia" -> diagnosticSyntax ~path
389-
| "hint" -> inlayhint ~path ~maxLength:"25" ~debug:true
389+
| "hint" -> inlayhint ~path ~pos:(0, 20) ~maxLength:"25" ~debug:true
390390
| _ -> ());
391391
print_newline ())
392392
in

analysis/src/Hint.ml

+17-16
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ let locItemToTypeHint ~full:{file; package} locItem =
3232
| `Field -> fromType t))
3333
| _ -> None
3434

35-
let inlay ~path ~maxLength ~debug =
36-
let maxlen = try Some(int_of_string maxLength) with Failure _ -> None in
35+
let inlay ~path ~pos ~maxLength ~debug =
36+
let maxlen = try Some (int_of_string maxLength) with Failure _ -> None in
3737
let hints = ref [] in
38+
let start_line, end_line = pos in
39+
let push loc kind =
40+
let range = Utils.cmtLocToRange loc in
41+
if start_line <= range.end_.line && end_line >= range.start.line then
42+
hints := (range, kind) :: !hints
43+
in
3844
let rec processFunction (exp : Parsetree.expression) =
3945
match exp.pexp_desc with
4046
| Pexp_fun (_, _, pat_exp, e) -> (
4147
match pat_exp with
4248
| {ppat_desc = Ppat_var _} ->
43-
hints := (pat_exp.ppat_loc, Type) :: !hints;
49+
push pat_exp.ppat_loc Type;
4450
processFunction e
4551
| _ -> processFunction e)
4652
| _ -> ()
@@ -59,18 +65,17 @@ let inlay ~path ~maxLength ~debug =
5965
| Pexp_send _ | Pexp_field _ | Pexp_open _ );
6066
};
6167
} ->
62-
hints := (vb.pvb_pat.ppat_loc, Type) :: !hints
68+
push vb.pvb_pat.ppat_loc Type
6369
| {pvb_pat = {ppat_desc = Ppat_tuple tuples}} ->
6470
List.iter
65-
(fun (tuple : Parsetree.pattern) ->
66-
hints := (tuple.ppat_loc, Type) :: !hints)
71+
(fun (tuple : Parsetree.pattern) -> push tuple.ppat_loc Type)
6772
tuples
6873
| {
6974
pvb_pat = {ppat_desc = Ppat_var _};
7075
pvb_expr = {pexp_desc = Pexp_fun (_, _, pat, e)};
7176
} ->
7277
(match pat with
73-
| {ppat_desc = Ppat_var _} -> hints := (pat.ppat_loc, Type) :: !hints
78+
| {ppat_desc = Ppat_var _} -> push pat.ppat_loc Type
7479
| _ -> ());
7580
processFunction e
7681
| _ -> ());
@@ -82,14 +87,9 @@ let inlay ~path ~maxLength ~debug =
8287
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
8388
in
8489
let {Res_driver.parsetree = structure} = parser ~filename:path in
85-
iterator.structure iterator structure |> ignore
86-
else
87-
let parser = Res_driver.parsingEngine.parseInterface ~forPrinter:false in
88-
let {Res_driver.parsetree = signature} = parser ~filename:path in
89-
iterator.signature iterator signature |> ignore);
90+
iterator.structure iterator structure |> ignore);
9091
!hints
91-
|> List.filter_map (fun (locOfName, hintKind) ->
92-
let range = Utils.cmtLocToRange locOfName in
92+
|> List.filter_map (fun ((range : Protocol.range), hintKind) ->
9393
match Cmt.fullFromPath ~path with
9494
| None -> None
9595
| Some full -> (
@@ -116,6 +116,7 @@ let inlay ~path ~maxLength ~debug =
116116
}
117117
in
118118
match maxlen with
119-
| Some value -> if (String.length label) > value then None else Some(result)
120-
| None -> Some(result))
119+
| Some value ->
120+
if String.length label > value then None else Some result
121+
| None -> Some result)
121122
| None -> None)))

server/src/server.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DidCloseTextDocumentNotification,
1313
DidChangeConfigurationNotification,
1414
InitializeParams,
15+
InlayHintParams,
1516
} from "vscode-languageserver-protocol";
1617
import * as utils from "./utils";
1718
import * as codeActions from "./codeActions";
@@ -71,7 +72,7 @@ let projectsFiles: Map<
7172
let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
7273

7374
// will be properly defined later depending on the mode (stdio/node-rpc)
74-
let send: (msg: p.Message) => void = (_) => {};
75+
let send: (msg: p.Message) => void = (_) => { };
7576

7677
let findBinaryDirPathFromProjectRoot = (
7778
directory: p.DocumentUri // This must be a directory and not a file!
@@ -395,7 +396,7 @@ function inlayHint(msg: p.RequestMessage) {
395396

396397
const response = utils.runAnalysisCommand(
397398
filePath,
398-
["inlayHint", filePath, extensionConfiguration.inlayHints.maxLength],
399+
["inlayHint", filePath, params.range.start.line, params.range.end.line, extensionConfiguration.inlayHints.maxLength],
399400
msg
400401
);
401402
return response;
@@ -1080,7 +1081,11 @@ function onMessage(msg: p.Message) {
10801081
} else if (msg.method === openCompiledFileRequest.method) {
10811082
send(openCompiledFile(msg));
10821083
} else if (msg.method === p.InlayHintRequest.method) {
1083-
send(inlayHint(msg));
1084+
let params = msg.params as InlayHintParams;
1085+
let extName = path.extname(params.textDocument.uri);
1086+
if (extName === c.resExt) {
1087+
send(inlayHint(msg));
1088+
}
10841089
} else {
10851090
let response: p.ResponseMessage = {
10861091
jsonrpc: c.jsonrpcVersion,

0 commit comments

Comments
 (0)