Skip to content

Commit 8fa6209

Browse files
committed
wip wrk command to dump cmt contents
1 parent 46b69ba commit 8fa6209

12 files changed

+48
-20
lines changed

analysis/bin/main.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ let main () =
225225
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
226226
| [_; "cmt"; path] -> CmtViewer.dump path
227227
| [_; "cmt"; line; col; path] ->
228-
let cursor = Some (int_of_string line, int_of_string col) in
229-
CmtViewer.dump ~cursor path
228+
let cursor = (int_of_string line, int_of_string col) in
229+
CmtViewer.dump ~filter:(Cursor cursor) path
230230
| _ ->
231231
prerr_endline help;
232232
exit 1

analysis/src/Cfg.ml

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ let readProjectConfigCache =
2020

2121
let useRevampedCompletion =
2222
ref (Sys.getenv_opt "RESCRIPT_NEW_ANALYSIS_ENGINE" |> Option.is_some)
23+
24+
let isTestWorkmode = ref false

analysis/src/Cmt.ml

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ let fullFromUri ~uri =
2929
else None
3030
in
3131
match incremental with
32-
| Some cmtInfo ->
33-
if Debug.verbose () then Printf.printf "[cmt] Found incremental cmt\n";
34-
Some cmtInfo
32+
| Some cmtInfo -> Some cmtInfo
3533
| None -> (
3634
match Hashtbl.find_opt package.pathsForModule moduleName with
3735
| Some paths ->

analysis/src/CmtViewer.ml

+16-7
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@ let filter_by_cursor cursor (loc : Warnings.loc) : bool =
2020
in
2121
line_in && col_in
2222

23-
let dump ?(cursor = None) path =
23+
type filter = Cursor of (int * int) | Loc of Loc.t
24+
25+
let dump ?filter path =
2426
match Cmt.loadFullCmtFromPath ~path with
2527
| None -> failwith (Format.sprintf "Could not load cmt for %s" path)
2628
| Some full ->
2729
let open SharedTypes in
2830
let open SharedTypes.Stamps in
29-
let filter = filter_by_cursor cursor in
30-
cursor
31-
|> Option.iter (fun (line, col) ->
32-
Printf.printf "Filtering by cursor %d,%d\n" line col);
31+
let applyFilter =
32+
match filter with
33+
| None -> fun _ -> true
34+
| Some (Cursor cursor) -> Loc.hasPos ~pos:cursor
35+
| Some (Loc loc) -> Loc.isInside loc
36+
in
37+
(match filter with
38+
| None -> ()
39+
| Some (Cursor (line, col)) ->
40+
Printf.printf "Filtering by cursor %d,%d\n" line col
41+
| Some (Loc loc) -> Printf.printf "Filtering by loc %s\n" (Loc.toString loc));
3342
let stamps =
3443
full.file.stamps |> getEntries
35-
|> List.filter (fun (_, stamp) -> filter (locOfKind stamp))
44+
|> List.filter (fun (_, stamp) -> applyFilter (locOfKind stamp))
3645
in
3746

3847
let total_stamps = List.length stamps in
@@ -65,7 +74,7 @@ let dump ?(cursor = None) path =
6574
let locItems =
6675
match full.extra with
6776
| {locItems} ->
68-
locItems |> List.filter (fun locItem -> filter locItem.loc)
77+
locItems |> List.filter (fun locItem -> applyFilter locItem.loc)
6978
in
7079

7180
Printf.printf "\nFound %d locItems (typed nodes):\n\n"

analysis/src/Commands.ml

+15-2
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ let test ~path ~debug =
359359
| "db+" -> Log.verbose := true
360360
| "db-" -> Log.verbose := false
361361
| "dv+" -> Debug.debugLevel := Verbose
362+
| "wrk" ->
363+
Cfg.isTestWorkmode := true;
364+
Debug.debugLevel := Verbose
362365
| "dv-" -> Debug.debugLevel := Off
363366
| "in+" -> Cfg.inIncrementalTypecheckingMode := true
364367
| "in-" -> Cfg.inIncrementalTypecheckingMode := false
@@ -382,6 +385,11 @@ let test ~path ~debug =
382385
let currentFile = createCurrentFile () in
383386
if !Cfg.useRevampedCompletion then (
384387
Code_frame.setup (Some Misc.Color.Never);
388+
if !Cfg.isTestWorkmode then (
389+
print_endline "===== CMT CONTENT =====";
390+
CmtViewer.dump path
391+
(*print_endline "\n===== CMT FILTERED BY CURSOR =====";
392+
CmtViewer.dump path ~filter:(Cursor (line, col))*));
385393
let source = Files.readFile currentFile in
386394
let completions =
387395
completionRevamped ~debug ~path ~pos:(line, col) ~currentFile
@@ -392,8 +400,13 @@ let test ~path ~debug =
392400
| Some (completable, completionsText), Some text -> (
393401
match SharedTypes.CompletableRevamped.try_loc completable with
394402
| Some loc ->
395-
Printf.printf "Found Completable: %s\n\n"
396-
(SharedTypes.CompletableRevamped.toString completable);
403+
Printf.printf "Found Completable: %s at type loc: %s\n\n"
404+
(SharedTypes.CompletableRevamped.toString completable)
405+
(Loc.toString loc);
406+
if !Cfg.isTestWorkmode then (
407+
print_endline "\n===== CMT FILTERED BY TYPE LOC =====";
408+
CmtViewer.dump path ~filter:(Loc loc);
409+
print_endline "\n\n");
397410
Code_frame.print ~is_warning:true ~draw_underline:true
398411
~src:text ~start_pos:loc.loc_start ~end_pos:loc.loc_end
399412
|> print_endline;

analysis/src/Loc.ml

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ let rangeOfLoc (loc : t) =
2121
let start = loc |> start |> mkPosition in
2222
let end_ = loc |> end_ |> mkPosition in
2323
{Protocol.start; end_}
24+
25+
let isInside (x : t) (y : t) =
26+
x.loc_start.pos_cnum >= y.loc_start.pos_cnum
27+
&& x.loc_end.pos_cnum <= y.loc_end.pos_cnum
28+
&& x.loc_start.pos_lnum >= y.loc_start.pos_lnum
29+
&& x.loc_end.pos_lnum <= y.loc_end.pos_lnum

tests/analysis_new_tests/tests/test_files/__snapshots__/RecordFieldCompletions.res_Record_field_completion_in_nested_record.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cexpression
1+
Found Completable: Cexpression at type loc: [1:8->1:37]
22

33
1// Record field completion in nested record
44
2let x = TestTypeDefs.nestedTestRecord.

tests/analysis_new_tests/tests/test_files/__snapshots__/RecordFieldCompletions.res_Record_field_completion_in_nested_record_another_level.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cexpression
1+
Found Completable: Cexpression at type loc: [1:8->1:44]
22

33
1// Record field completion in nested record, another level
44
2let x = TestTypeDefs.nestedTestRecord.nested.

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_array.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cpattern
1+
Found Completable: Cpattern at type loc: [3:15->3:28]
22

33
2let someStringArr = ["hello"]
44
3

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_bool.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cpattern
1+
Found Completable: Cpattern at type loc: [1:15->1:19]
22

33
1// Empty case, bool
44
2let x = switch true {

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_record.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cpattern
1+
Found Completable: Cpattern at type loc: [1:15->1:44]
22

33
1// Empty case, record
44
2let x = switch TestTypeDefs.nestedTestRecord {

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_string.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Found Completable: Cpattern
1+
Found Completable: Cpattern at type loc: [2:15->2:18]
22

33
1// Empty case, string
44
2let str = "hello"

0 commit comments

Comments
 (0)