Skip to content

Commit 1242d57

Browse files
committed
attempt to fix completing pipe application
1 parent 3919533 commit 1242d57

File tree

5 files changed

+193
-24
lines changed

5 files changed

+193
-24
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
562562
with
563563
deprecated = declared.deprecated;
564564
docstring = declared.docstring;
565+
modulePath = ModulePath.toPathWithoutTip declared.modulePath;
565566
}
566567
:: !res
567568
| _ -> ());
@@ -1152,9 +1153,12 @@ let completionToItem {Completion.name; deprecated; docstring; kind} =
11521153
~deprecated ~detail:(detail name kind) ~docstring
11531154

11541155
let completionsGetTypeEnv = function
1155-
| {Completion.kind = Value typ; env} :: _ -> Some (typ, env)
1156-
| {Completion.kind = ObjLabel typ; env} :: _ -> Some (typ, env)
1157-
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (typ, env)
1156+
| {Completion.kind = Value typ; env; modulePath} :: _ ->
1157+
Some (typ, env, modulePath)
1158+
| {Completion.kind = ObjLabel typ; env; modulePath} :: _ ->
1159+
Some (typ, env, modulePath)
1160+
| {Completion.kind = Field ({typ}, _); env; modulePath} :: _ ->
1161+
Some (typ, env, modulePath)
11581162
| _ -> None
11591163

11601164
let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
@@ -1185,7 +1189,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
11851189
~env ~exact:true ~scope
11861190
|> completionsGetTypeEnv
11871191
with
1188-
| Some (typ, env) -> (
1192+
| Some (typ, env, modulePath) -> (
11891193
let rec reconstructFunctionType args tRet =
11901194
match args with
11911195
| [] -> tRet
@@ -1216,7 +1220,10 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12161220
| args, tRet when args <> [] ->
12171221
let args = processApply args labels in
12181222
let retType = reconstructFunctionType args tRet in
1219-
[Completion.create ~name:"dummy" ~env ~kind:(Completion.Value retType)]
1223+
[
1224+
Completion.createWithModulePath ~name:"dummy" ~env
1225+
~kind:(Completion.Value retType) ~modulePath;
1226+
]
12201227
| _ -> [])
12211228
| None -> [])
12221229
| CPField (CPId (path, Module), fieldName) ->
@@ -1231,19 +1238,20 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12311238
~env ~exact:true ~scope
12321239
|> completionsGetTypeEnv
12331240
with
1234-
| Some (typ, env) -> (
1241+
| Some (typ, env, modulePath) -> (
12351242
match typ |> extractRecordType ~env ~package with
12361243
| Some (env, fields, typDecl) ->
12371244
fields
12381245
|> Utils.filterMap (fun field ->
12391246
if checkName field.fname.txt ~prefix:fieldName ~exact then
12401247
Some
1241-
(Completion.create ~name:field.fname.txt ~env
1248+
(Completion.createWithModulePath ~name:field.fname.txt ~env
12421249
~kind:
12431250
(Completion.Field
12441251
( field,
12451252
typDecl.item.decl
1246-
|> Shared.declToString typDecl.name.txt )))
1253+
|> Shared.declToString typDecl.name.txt ))
1254+
~modulePath)
12471255
else None)
12481256
| None -> [])
12491257
| None -> [])
@@ -1254,7 +1262,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12541262
~env ~exact:true ~scope
12551263
|> completionsGetTypeEnv
12561264
with
1257-
| Some (typ, env) -> (
1265+
| Some (typ, env, modulePath) -> (
12581266
match typ |> extractObjectType ~env ~package with
12591267
| Some (env, tObj) ->
12601268
let rec getFields (texp : Types.type_expr) =
@@ -1270,8 +1278,8 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12701278
|> Utils.filterMap (fun (field, typ) ->
12711279
if checkName field ~prefix:label ~exact then
12721280
Some
1273-
(Completion.create ~name:field ~env
1274-
~kind:(Completion.ObjLabel typ))
1281+
(Completion.createWithModulePath ~name:field ~env
1282+
~kind:(Completion.ObjLabel typ) ~modulePath)
12751283
else None)
12761284
| None -> [])
12771285
| None -> [])
@@ -1282,7 +1290,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
12821290
~env ~exact:true ~scope
12831291
|> completionsGetTypeEnv
12841292
with
1285-
| Some (typ, _envNotUsed) -> (
1293+
| Some (typ, env, completionItemModulePath) -> (
12861294
let {
12871295
arrayModulePath;
12881296
optionModulePath;
@@ -1375,7 +1383,20 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
13751383
env
13761384
(* Restore original env for the completion after x->foo()... *);
13771385
})
1378-
| [] -> [])
1386+
| [] ->
1387+
let completions =
1388+
completionItemModulePath @ [funNamePrefix]
1389+
|> getCompletionsForPath ~completionContext:Value ~exact:false
1390+
~package ~opens ~allFiles ~pos ~env ~scope
1391+
in
1392+
completions
1393+
|> List.map (fun (completion : Completion.t) ->
1394+
{
1395+
completion with
1396+
name = completion.name;
1397+
env
1398+
(* Restore original env for the completion after x->foo()... *);
1399+
}))
13791400
| None -> [])
13801401
| None -> [])
13811402

@@ -1438,7 +1459,7 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
14381459
| Cjsx (componentPath, prefix, identsSeen) ->
14391460
let labels =
14401461
match componentPath @ ["make"] |> findTypeOfValue with
1441-
| Some (typ, make_env) ->
1462+
| Some (typ, make_env, _) ->
14421463
let rec getFieldsV3 (texp : Types.type_expr) =
14431464
match texp.desc with
14441465
| Tfield (name, _, t1, t2) ->
@@ -1774,7 +1795,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
17741795
~env ~exact:true ~scope
17751796
|> completionsGetTypeEnv
17761797
with
1777-
| Some (typ, _env) ->
1798+
| Some (typ, _env, _) ->
17781799
if debug then
17791800
Printf.printf "Found type for function %s\n"
17801801
(typ |> Shared.typeToString);

analysis/src/Hover.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
152152
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
153153
| _ -> (
154154
match CompletionBackEnd.completionsGetTypeEnv completions with
155-
| Some (typ, _env) ->
155+
| Some (typ, _env, _) ->
156156
let typeString, _docstring =
157157
hoverWithExpandedTypes ~docstring:"" ~file ~package
158158
~supportsMarkdownLinks typ

analysis/src/SharedTypes.ml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ module ModulePath = struct
2222
| NotVisible -> current
2323
in
2424
loop modulePath [tipName]
25+
26+
let toPathWithoutTip modulePath : path =
27+
let rec loop modulePath current =
28+
match modulePath with
29+
| File _ -> current
30+
| IncludedModule (_, inner) -> loop inner current
31+
| ExportedModule {name; modulePath = inner} -> loop inner (name :: current)
32+
| NotVisible -> current
33+
in
34+
loop modulePath []
2535
end
2636

2737
type field = {stamp: int; fname: string Location.loc; typ: Types.type_expr}
@@ -253,10 +263,14 @@ module Completion = struct
253263
deprecated: string option;
254264
docstring: string list;
255265
kind: kind;
266+
modulePath: string list;
256267
}
257268

258269
let create ~name ~kind ~env =
259-
{name; env; deprecated = None; docstring = []; kind}
270+
{name; env; deprecated = None; docstring = []; kind; modulePath = []}
271+
272+
let createWithModulePath ~name ~kind ~env ~modulePath =
273+
{name; env; deprecated = None; docstring = []; kind; modulePath}
260274

261275
(* https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion *)
262276
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemKind *)

analysis/tests/src/CompletionPipeChain.res

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ let f = int->Integer.increment(2)
3838
// ^com
3939

4040
// let _ = int->Integer.decrement(t => t - 1)->
41-
//
41+
// ^com
4242

4343
// let _ = int->Integer.increment(2)->Integer.decrement(t => t - 1)->
44-
//
44+
// ^com
4545

4646
// let _ = int->Integer.increment(2)->SuperFloat.fromInteger->
47-
//
47+
// ^com
4848

49-
// let _ = int->Integer.incremebt(2)->SuperFloat.fromInteger->t
50-
//
49+
// let _ = int->Integer.increment(2)->SuperFloat.fromInteger->t
50+
// ^com

analysis/tests/src/expected/CompletionPipeChain.res.txt

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,144 @@ Completable: Cpath Value[toFlt](Nolabel)->
4747
Complete src/CompletionPipeChain.res 33:38
4848
posCursor:[33:38] posNoWhite:[33:37] Found expr:[33:11->0:-1]
4949
Completable: Cpath Value[Integer, increment](Nolabel, Nolabel)->
50-
[]
50+
[{
51+
"label": "toInt",
52+
"kind": 12,
53+
"tags": [],
54+
"detail": "t => int",
55+
"documentation": null
56+
}, {
57+
"label": "increment",
58+
"kind": 12,
59+
"tags": [],
60+
"detail": "(t, int) => t",
61+
"documentation": null
62+
}, {
63+
"label": "decrement",
64+
"kind": 12,
65+
"tags": [],
66+
"detail": "(t, int => int) => t",
67+
"documentation": null
68+
}, {
69+
"label": "make",
70+
"kind": 12,
71+
"tags": [],
72+
"detail": "int => t",
73+
"documentation": null
74+
}]
5175

5276
Complete src/CompletionPipeChain.res 36:38
5377
posCursor:[36:38] posNoWhite:[36:37] Found expr:[36:11->0:-1]
5478
Completable: Cpath Value[Integer, increment](Nolabel, Nolabel)->
55-
[]
79+
[{
80+
"label": "toInt",
81+
"kind": 12,
82+
"tags": [],
83+
"detail": "t => int",
84+
"documentation": null
85+
}, {
86+
"label": "increment",
87+
"kind": 12,
88+
"tags": [],
89+
"detail": "(t, int) => t",
90+
"documentation": null
91+
}, {
92+
"label": "decrement",
93+
"kind": 12,
94+
"tags": [],
95+
"detail": "(t, int => int) => t",
96+
"documentation": null
97+
}, {
98+
"label": "make",
99+
"kind": 12,
100+
"tags": [],
101+
"detail": "int => t",
102+
"documentation": null
103+
}]
104+
105+
Complete src/CompletionPipeChain.res 39:47
106+
posCursor:[39:47] posNoWhite:[39:46] Found expr:[39:11->0:-1]
107+
Completable: Cpath Value[Integer, decrement](Nolabel, Nolabel)->
108+
[{
109+
"label": "toInt",
110+
"kind": 12,
111+
"tags": [],
112+
"detail": "t => int",
113+
"documentation": null
114+
}, {
115+
"label": "increment",
116+
"kind": 12,
117+
"tags": [],
118+
"detail": "(t, int) => t",
119+
"documentation": null
120+
}, {
121+
"label": "decrement",
122+
"kind": 12,
123+
"tags": [],
124+
"detail": "(t, int => int) => t",
125+
"documentation": null
126+
}, {
127+
"label": "make",
128+
"kind": 12,
129+
"tags": [],
130+
"detail": "int => t",
131+
"documentation": null
132+
}]
133+
134+
Complete src/CompletionPipeChain.res 42:69
135+
posCursor:[42:69] posNoWhite:[42:68] Found expr:[42:11->0:-1]
136+
Completable: Cpath Value[Integer, decrement](Nolabel, Nolabel)->
137+
[{
138+
"label": "toInt",
139+
"kind": 12,
140+
"tags": [],
141+
"detail": "t => int",
142+
"documentation": null
143+
}, {
144+
"label": "increment",
145+
"kind": 12,
146+
"tags": [],
147+
"detail": "(t, int) => t",
148+
"documentation": null
149+
}, {
150+
"label": "decrement",
151+
"kind": 12,
152+
"tags": [],
153+
"detail": "(t, int => int) => t",
154+
"documentation": null
155+
}, {
156+
"label": "make",
157+
"kind": 12,
158+
"tags": [],
159+
"detail": "int => t",
160+
"documentation": null
161+
}]
162+
163+
Complete src/CompletionPipeChain.res 45:62
164+
posCursor:[45:62] posNoWhite:[45:61] Found expr:[45:11->0:-1]
165+
Completable: Cpath Value[SuperFloat, fromInteger](Nolabel)->
166+
[{
167+
"label": "fromInteger",
168+
"kind": 12,
169+
"tags": [],
170+
"detail": "Integer.t => t",
171+
"documentation": null
172+
}, {
173+
"label": "toInteger",
174+
"kind": 12,
175+
"tags": [],
176+
"detail": "t => Integer.t",
177+
"documentation": null
178+
}]
179+
180+
Complete src/CompletionPipeChain.res 48:63
181+
posCursor:[48:63] posNoWhite:[48:62] Found expr:[48:11->48:63]
182+
Completable: Cpath Value[SuperFloat, fromInteger](Nolabel)->t
183+
[{
184+
"label": "toInteger",
185+
"kind": 12,
186+
"tags": [],
187+
"detail": "t => Integer.t",
188+
"documentation": null
189+
}]
56190

0 commit comments

Comments
 (0)