Skip to content

Commit 9302ec1

Browse files
committed
mcp identifier info command wip
1 parent 81779fd commit 9302ec1

File tree

5 files changed

+158
-37
lines changed

5 files changed

+158
-37
lines changed

analysis/bin/main.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ let main () =
113113
| [_; "mcp"; "loc-info"; path; line; col] ->
114114
Mcp.LocInfo.locInfo ~path ~pos:(int_of_string line, int_of_string col)
115115
|> print_endline
116+
| [_; "mcp"; "identifier-info"; identifier; path] ->
117+
Mcp.IdentifierInfo.identifierInfo ~identifier ~path ~maybe_line:None
118+
~maybe_col:None
119+
|> print_endline
116120
| [_; "cache-project"; rootPath] -> (
117121
Cfg.readProjectConfigCache := false;
118122
let uri = Uri.fromPath rootPath in

analysis/src/Commands.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,17 @@ let test ~path =
439439
let currentFile = createCurrentFile () in
440440
Mcp.LocInfo.locInfo ~path ~pos:(line, col) |> print_endline;
441441
Sys.remove currentFile
442+
| "mif" ->
443+
print_endline
444+
("MCP identifier info " ^ path ^ " " ^ string_of_int line ^ ":"
445+
^ string_of_int col);
446+
let currentFile = createCurrentFile () in
447+
let identifier = String.sub rest 3 (String.length rest - 3) in
448+
let identifier = String.trim identifier in
449+
Mcp.IdentifierInfo.identifierInfo ~identifier ~path ~maybe_line:None
450+
~maybe_col:None
451+
|> print_endline;
452+
Sys.remove currentFile
442453
| "xfm" ->
443454
let currentFile = createCurrentFile () in
444455
(* +2 is to ensure that the character ^ points to is what's considered the end of the selection. *)

analysis/src/Mcp.ml

Lines changed: 110 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@ module Utils = struct
88
match content with
99
| None -> None
1010
| Some content -> Some (wrapInTag content ~tag)
11+
12+
let print_type_with_related_types ~full t =
13+
let {TypeUtils.ExpandType.mainTypes; relatedTypes} =
14+
TypeUtils.ExpandType.expandTypes t ~full
15+
in
16+
Some
17+
(Printf.sprintf
18+
"<main_types>\n\
19+
%s\n\
20+
</main_types>\n\n\
21+
<related_types>\n\
22+
%s\n\
23+
</related_types>"
24+
(mainTypes
25+
|> List.map (fun (input : TypeUtils.ExpandType.expandTypeInput) ->
26+
match input with
27+
| TypeUtils.ExpandType.TypeExpr {typeExpr} ->
28+
Shared.typeToString typeExpr
29+
| TypeUtils.ExpandType.TypeDecl {name; typeDecl} ->
30+
Shared.declToString name.txt typeDecl)
31+
|> String.concat "\n\n")
32+
(relatedTypes
33+
|> List.map (fun (input : TypeUtils.ExpandType.expandTypeInput) ->
34+
match input with
35+
| TypeUtils.ExpandType.TypeExpr {typeExpr} ->
36+
Shared.typeToString typeExpr
37+
| TypeUtils.ExpandType.TypeDecl {name; typeDecl} ->
38+
Shared.declToString name.txt typeDecl)
39+
|> String.concat "\n\n"))
1140
end
1241

1342
module LocInfo = struct
@@ -99,42 +128,13 @@ module LocInfo = struct
99128
showModule ~docstring:file.structure.docstring
100129
~name:file.moduleName ~file ~package None)
101130
| Typed (name, t, _) ->
102-
let {TypeUtils.ExpandType.mainTypes; relatedTypes} =
103-
TypeUtils.ExpandType.expandTypes
104-
(TypeUtils.ExpandType.TypeExpr
105-
{
106-
typeExpr = t;
107-
name = Some (Location.mkloc name locItem.loc);
108-
env = QueryEnv.fromFile full.file;
109-
})
110-
~full
111-
in
112-
Some
113-
(Printf.sprintf
114-
"<main_types>\n\
115-
%s\n\
116-
</main_types>\n\n\
117-
<related_types>\n\
118-
%s\n\
119-
</related_types>"
120-
(mainTypes
121-
|> List.map
122-
(fun (input : TypeUtils.ExpandType.expandTypeInput) ->
123-
match input with
124-
| TypeUtils.ExpandType.TypeExpr {typeExpr} ->
125-
Shared.typeToString typeExpr
126-
| TypeUtils.ExpandType.TypeDecl {name; typeDecl} ->
127-
Shared.declToString name.txt typeDecl)
128-
|> String.concat "\n\n")
129-
(relatedTypes
130-
|> List.map
131-
(fun (input : TypeUtils.ExpandType.expandTypeInput) ->
132-
match input with
133-
| TypeUtils.ExpandType.TypeExpr {typeExpr} ->
134-
Shared.typeToString typeExpr
135-
| TypeUtils.ExpandType.TypeDecl {name; typeDecl} ->
136-
Shared.declToString name.txt typeDecl)
137-
|> String.concat "\n\n"))
131+
Utils.print_type_with_related_types ~full
132+
(TypeUtils.ExpandType.TypeExpr
133+
{
134+
typeExpr = t;
135+
name = Some (Location.mkloc name locItem.loc);
136+
env = QueryEnv.fromFile full.file;
137+
})
138138
| Constant t ->
139139
Some
140140
(match t with
@@ -150,3 +150,77 @@ module LocInfo = struct
150150
| None -> "No result."
151151
| Some s -> s
152152
end
153+
154+
module IdentifierInfo = struct
155+
let identifierInfo ~identifier ~path ~maybe_line ~maybe_col =
156+
(* TODO: Pull out the right scope + opens if these are set. *)
157+
ignore maybe_line;
158+
ignore maybe_col;
159+
160+
let result =
161+
match Cmt.loadFullCmtFromPath ~path with
162+
| None -> None
163+
| Some full ->
164+
let env = QueryEnv.fromFile full.file in
165+
let scope = Scope.create () in
166+
let opens = [] in
167+
let pos = (0, 0) in
168+
let path = identifier |> String.split_on_char '.' in
169+
let hd_opt l = List.nth_opt l 0 in
170+
let print_completion_item (item : Completion.t) =
171+
match item.kind with
172+
| Value t ->
173+
Some
174+
(Printf.sprintf "Value %s:\n<value>\n%s\n</value>" identifier
175+
(match
176+
Utils.print_type_with_related_types ~full
177+
(TypeUtils.ExpandType.TypeExpr
178+
{
179+
typeExpr = t;
180+
name = Some (Location.mknoloc item.name);
181+
env = QueryEnv.fromFile full.file;
182+
})
183+
with
184+
| None -> ""
185+
| Some s -> s))
186+
| Type {decl; name} ->
187+
Some
188+
(Printf.sprintf "Type %s:\n<type>\n%s\n</type>" identifier
189+
(Shared.declToString name decl))
190+
(*| Module {module_} -> Some (LocInfo.showModule)*)
191+
| _ -> None
192+
in
193+
let value_completions =
194+
CompletionBackEnd.getCompletionsForPath ~exact:true ~debug:false ~full
195+
~completionContext:Value ~pos ~env ~scope ~opens path
196+
|> hd_opt
197+
in
198+
let type_completions =
199+
CompletionBackEnd.getCompletionsForPath ~exact:true ~debug:false ~full
200+
~completionContext:Type ~pos ~env ~scope ~opens path
201+
|> hd_opt
202+
in
203+
let module_completions =
204+
CompletionBackEnd.getCompletionsForPath ~exact:true ~debug:false ~full
205+
~completionContext:Module ~pos ~env ~scope ~opens path
206+
|> hd_opt
207+
in
208+
Some
209+
([
210+
(match value_completions with
211+
| None -> None
212+
| Some c -> print_completion_item c);
213+
(match type_completions with
214+
| None -> None
215+
| Some c -> print_completion_item c);
216+
(match module_completions with
217+
| None -> None
218+
| Some c -> print_completion_item c);
219+
]
220+
|> List.filter_map (fun x -> x)
221+
|> String.concat "\n\n")
222+
in
223+
match result with
224+
| None -> "No result."
225+
| Some s -> s
226+
end

tests/analysis_tests/tests/src/MCP.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ let xx = {
2222
variant: One,
2323
}
2424

25+
type xx = Hello
26+
2527
let ft = () => xx
2628

2729
let ff = ft()
2830
// ^mli
31+
32+
// ^mif MCP.xx
33+
// ^mif Rxjs.Subscriber.t

tests/analysis_tests/tests/src/expected/MCP.res.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MCP loc info src/MCP.res 26:4
1+
MCP loc info src/MCP.res 28:4
22
<main_types>
33
TestType.second
44

@@ -11,3 +11,30 @@ type first = {one: bool, two?: string}
1111
type variant = One | Two(string) | Three(bool)
1212
</related_types>
1313

14+
MCP identifier info src/MCP.res 30:3
15+
Value MCP.xx:
16+
<value>
17+
<main_types>
18+
TestType.second
19+
20+
type second = {first: first, variant: variant}
21+
</main_types>
22+
23+
<related_types>
24+
type first = {one: bool, two?: string}
25+
26+
type variant = One | Two(string) | Three(bool)
27+
</related_types>
28+
</value>
29+
30+
Type MCP.xx:
31+
<type>
32+
type xx = Hello
33+
</type>
34+
35+
MCP identifier info src/MCP.res 31:3
36+
Type Rxjs.Subscriber.t:
37+
<type>
38+
type t<'t> = {next: 't => unit}
39+
</type>
40+

0 commit comments

Comments
 (0)