Skip to content

Commit 3f48a4d

Browse files
authored
Refactor analysis CLI helpers to use source input (#8466)
* Refactor analysis CLI helpers to use source input Move document symbols and interface generation behind CLI helpers that operate on source content instead of reading files internally. Thread parsed max inlay hint length as an option, pass preloaded CMT data into code actions. Update the DocTemplate expected output for the new CMT lookup message emitted during non-compiled code action tests. * add condition to print `can't find module XX`
1 parent 9810ed8 commit 3f48a4d

9 files changed

Lines changed: 51 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
- Remove dead and unreachable compiler error and warning variants; add fixtures for the ones found to be reachable. https://github.com/rescript-lang/rescript/pull/8459
6565
- Convert OCaml codebase to snake case format. https://github.com/rescript-lang/rescript/pull/8456
6666
- Analysis refactor: remove global state `Shared_types.state`. https://github.com/rescript-lang/rescript/pull/8465
67-
67+
- Refactor analysis CLI helpers to use source input. https://github.com/rescript-lang/rescript/pull/8466
6868

6969
# 13.0.0-alpha.4
7070

analysis/bin/main.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ let main () =
148148
Cli.type_definition ~state ~path
149149
~pos:(int_of_string line, int_of_string col)
150150
~debug
151-
| [_; "documentSymbol"; path] -> Document_symbol.command ~path
151+
| [_; "documentSymbol"; path] -> Cli.document_symbol ~path
152152
| [_; "hover"; path; line; col; current_file; supports_markdown_links] ->
153153
Cli.hover ~state ~path
154154
~pos:(int_of_string line, int_of_string col)
@@ -174,6 +174,9 @@ let main () =
174174
| "true" -> true
175175
| _ -> false)
176176
| [_; "inlayHint"; path; line_start; line_end; max_length] ->
177+
let max_length =
178+
try Some (int_of_string max_length) with Failure _ -> None
179+
in
177180
Cli.inlayhint ~state ~path
178181
~pos:(int_of_string line_start, int_of_string line_end)
179182
~max_length ~debug
@@ -214,9 +217,7 @@ let main () =
214217
| [_; "semanticTokens"; current_file] ->
215218
Cli.semantic_tokens ~path:current_file
216219
| [_; "createInterface"; path; cmi_file] ->
217-
`String (Create_interface.command ~state ~path ~cmi_file)
218-
|> Yojson.Safe.pretty_to_string ~std:true
219-
|> print_endline
220+
Cli.create_interface ~path ~cmi_file
220221
| [_; "format"; path] -> Cli.format ~path
221222
| [_; "test"; path] -> Cli.test ~state ~path
222223
| [_; "cmt"; rescript_json; cmt_path] ->

analysis/src/cli.ml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ let code_action ~state ~path ~start_pos ~end_pos ~current_file ~debug =
7878
| None -> print_null ()
7979
| Some source ->
8080
Xform.extract_code_actions ~state ~path ~start_pos ~end_pos ~source
81-
~kind_file ~debug
81+
~kind_file
82+
~full:(Cmt.load_full_cmt_from_path ~state ~path)
83+
~debug
8284
|> List.map (fun c -> Lsp.Types.CodeAction.yojson_of_t c)
8385
|> print_list
8486

@@ -161,6 +163,25 @@ let semantic_tokens ~path =
161163
let tokens = Semantic_tokens.semantic_tokens ~source ~kind_file in
162164
Lsp.Types.SemanticTokens.yojson_of_t tokens |> print_string
163165

166+
let document_symbol ~path =
167+
match Files.read_file path with
168+
| None -> print_null ()
169+
| Some source ->
170+
let kind_file = Files.classify_source_file path in
171+
let symbols = Document_symbol.get_symbols ~source ~kind_file in
172+
print_list (symbols |> List.map Lsp.Types.DocumentSymbol.yojson_of_t)
173+
174+
let create_interface ~path ~cmi_file =
175+
let result =
176+
match Files.read_file path with
177+
| None -> ""
178+
| Some source -> (
179+
match Create_interface.command ~source ~cmi_file with
180+
| Ok content -> content
181+
| Error _ -> "")
182+
in
183+
Printf.printf "%s" result
184+
164185
let test ~state ~path =
165186
Uri.strip_path := true;
166187
match Files.read_file path with
@@ -247,7 +268,7 @@ let test ~state ~path =
247268
Dce_command.command ()
248269
| "doc" ->
249270
print_endline ("DocumentSymbol " ^ path);
250-
Document_symbol.command ~path
271+
document_symbol ~path
251272
| "hig" ->
252273
print_endline ("Highlight " ^ path);
253274
let source = Files.read_file path |> Option.get in
@@ -281,7 +302,7 @@ let test ~state ~path =
281302
let dir = dirname path in
282303
dir ++ parent_dir_name ++ "lib" ++ "bs" ++ "src" ++ name
283304
in
284-
Printf.printf "%s" (Create_interface.command ~state ~path ~cmi_file)
305+
create_interface ~path ~cmi_file
285306
| "ref" ->
286307
print_endline
287308
("References " ^ path ^ " " ^ string_of_int line ^ ":"
@@ -323,10 +344,11 @@ let test ~state ~path =
323344
let source =
324345
Files.read_file current_file |> Option.value ~default:""
325346
in
347+
let full = Cmt.load_full_cmt_from_path ~state ~path in
326348
let kind_file = Files.classify_source_file current_file in
327349
let code_actions =
328350
Xform.extract_code_actions ~state ~path ~start_pos ~end_pos
329-
~source ~kind_file ~debug:true
351+
~source ~kind_file ~full ~debug:true
330352
in
331353
Sys.remove current_file;
332354
code_actions
@@ -404,8 +426,8 @@ let test ~state ~path =
404426
print_endline
405427
("Inlay Hint " ^ path ^ " " ^ string_of_int line_start ^ ":"
406428
^ string_of_int line_end);
407-
inlayhint ~state ~path ~pos:(line_start, line_end) ~max_length:"25"
408-
~debug:false
429+
inlayhint ~state ~path ~pos:(line_start, line_end)
430+
~max_length:(Some 25) ~debug:false
409431
| "cle" ->
410432
print_endline ("Code Lens " ^ path);
411433
code_lens ~state ~path ~debug:false

analysis/src/cmt.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ let full_from_uri ~state ~uri =
5050
let cmt = get_cmt_path ~uri paths in
5151
full_for_cmt ~module_name ~package ~uri cmt
5252
| None ->
53-
prerr_endline ("can't find module " ^ module_name);
53+
if Debug.verbose () then
54+
prerr_endline ("can't find module " ^ module_name);
5455
None))
5556

5657
let full_from_module ~package ~module_name =

analysis/src/create_interface.ml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
module Source_file_extractor = struct
2-
let create ~path =
3-
match Files.read_file path with
4-
| None -> [||]
5-
| Some text -> text |> String.split_on_char '\n' |> Array.of_list
6-
72
let extract lines ~pos_start ~pos_end =
83
let line_start, col_start = pos_start in
94
let line_end, col_end = pos_end in
@@ -319,11 +314,9 @@ let print_signature ~extractor ~signature =
319314
process_signature ~indent:"" signature;
320315
Buffer.contents buf
321316

322-
let command ~state ~path ~cmi_file =
317+
let command ~source ~cmi_file =
323318
match Shared.try_read_cmi cmi_file with
324319
| Some cmi_info ->
325-
(* For reading the config *)
326-
ignore (Cmt.load_full_cmt_from_path ~state ~path);
327-
let extractor = Source_file_extractor.create ~path in
328-
print_signature ~extractor ~signature:cmi_info.cmi_sign
329-
| None -> ""
320+
let extractor = source |> String.split_on_char '\n' |> Array.of_list in
321+
Ok (print_signature ~extractor ~signature:cmi_info.cmi_sign)
322+
| None -> Error ("Failed to read cmi file " ^ cmi_file)

analysis/src/document_symbol.ml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol *)
22

3-
let command ~path =
3+
let get_symbols ~source ~kind_file =
44
let symbols = ref [] in
55
let add_symbol name loc kind =
66
if
@@ -115,17 +115,18 @@ let command ~path =
115115
}
116116
in
117117

118-
(if Filename.check_suffix path ".res" then
118+
(if kind_file = Files.Res then
119119
let parser =
120-
Res_driver.parsing_engine.parse_implementation ~for_printer:false
120+
Res_driver.parsing_engine.parse_implementation_from_source
121+
~for_printer:false
121122
in
122-
let {Res_driver.parsetree = structure} = parser ~filename:path in
123+
let {Res_driver.parsetree = structure} = parser ~source in
123124
iterator.structure iterator structure |> ignore
124125
else
125126
let parser =
126-
Res_driver.parsing_engine.parse_interface ~for_printer:false
127+
Res_driver.parsing_engine.parse_interface_from_source ~for_printer:false
127128
in
128-
let {Res_driver.parsetree = signature} = parser ~filename:path in
129+
let {Res_driver.parsetree = signature} = parser ~source in
129130
iterator.signature iterator signature |> ignore);
130131
let is_inside
131132
({
@@ -182,9 +183,4 @@ let command ~path =
182183
|> add_sorted_symbols_to_children ~sorted_symbols:rest
183184
in
184185
let sorted_symbols = !symbols |> List.sort compare_symbol in
185-
let symbols_with_children =
186-
[] |> add_sorted_symbols_to_children ~sorted_symbols
187-
in
188-
`List (symbols_with_children |> List.map Lsp.Types.DocumentSymbol.yojson_of_t)
189-
|> Yojson.Safe.pretty_to_string ~std:true
190-
|> print_endline
186+
[] |> add_sorted_symbols_to_children ~sorted_symbols

analysis/src/hint.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ let loc_item_to_type_hint ~state ~full:{file; package} loc_item =
3232
| _ -> None
3333

3434
let inlay ~source ~kind_file ~pos ~max_length ~full ~state ~debug =
35-
let maxlen = try Some (int_of_string max_length) with Failure _ -> None in
3635
let hints = ref [] in
3736
let start_line, end_line = pos in
3837
let push loc kind =
@@ -103,7 +102,7 @@ let inlay ~source ~kind_file ~pos ~max_length ~full ~state ~debug =
103102
Lsp.Types.InlayHint.create ~position ~kind ~paddingLeft:true
104103
~paddingRight:false ~label:(`String label) ()
105104
in
106-
match maxlen with
105+
match max_length with
107106
| Some value ->
108107
if String.length label > value then None else Some result
109108
| None -> Some result)

analysis/src/xform.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ let parse_interface ~source =
915915
(structure, print_signature_item)
916916

917917
let extract_code_actions ~state ~path ~start_pos ~end_pos ~source ~kind_file
918-
~debug =
918+
~full ~debug =
919919
let pos = start_pos in
920920
let code_actions = ref [] in
921921
match kind_file with
@@ -934,7 +934,7 @@ let extract_code_actions ~state ~path ~start_pos ~end_pos ~source ~kind_file
934934

935935
(* This Code Action needs type info *)
936936
let () =
937-
match Cmt.load_full_cmt_from_path ~state ~path with
937+
match full with
938938
| Some full ->
939939
Add_type_annotation.xform ~path ~pos ~full ~structure ~code_actions
940940
~debug;

tests/analysis_tests/tests/not_compiled/expected/DocTemplate.res.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Xform not_compiled/DocTemplate.res 3:3
2-
can't find module DocTemplate
32
Hit: Add Documentation template
43

54
TextDocumentEdit: DocTemplate.res
@@ -17,7 +16,6 @@ type rec t = A | B
1716
and e = C
1817

1918
Xform not_compiled/DocTemplate.res 6:15
20-
can't find module DocTemplate
2119
Hit: Add Documentation template
2220

2321
TextDocumentEdit: DocTemplate.res
@@ -33,7 +31,6 @@ newText:
3331
@unboxed type name = Name(string)
3432

3533
Xform not_compiled/DocTemplate.res 8:4
36-
can't find module DocTemplate
3734
Hit: Add Documentation template
3835

3936
TextDocumentEdit: DocTemplate.res
@@ -49,7 +46,6 @@ newText:
4946
let a = 1
5047

5148
Xform not_compiled/DocTemplate.res 10:4
52-
can't find module DocTemplate
5349
Hit: Add Documentation template
5450

5551
TextDocumentEdit: DocTemplate.res
@@ -65,7 +61,6 @@ newText:
6561
let inc = x => x + 1
6662

6763
Xform not_compiled/DocTemplate.res 12:7
68-
can't find module DocTemplate
6964
Hit: Add Documentation template
7065

7166
TextDocumentEdit: DocTemplate.res
@@ -109,7 +104,6 @@ newText:
109104

110105

111106
Xform not_compiled/DocTemplate.res 14:6
112-
can't find module DocTemplate
113107
Hit: Add Documentation template
114108

115109
TextDocumentEdit: DocTemplate.res
@@ -149,7 +143,6 @@ newText:
149143

150144

151145
Xform not_compiled/DocTemplate.res 18:2
152-
can't find module DocTemplate
153146
Hit: Add Documentation template
154147

155148
TextDocumentEdit: DocTemplate.res

0 commit comments

Comments
 (0)