Skip to content

Commit d56ced2

Browse files
cristianoczth
authored andcommitted
Put all the logic to find the completionPath in one place.
1 parent c3ecbe5 commit d56ced2

File tree

1 file changed

+66
-86
lines changed

1 file changed

+66
-86
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 66 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,30 +1303,28 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
13031303
} =
13041304
package.builtInCompletionModules
13051305
in
1306-
let getModulePath path =
1307-
let rec loop (path : Path.t) =
1308-
match path with
1309-
| Pident id -> [Ident.name id]
1310-
| Pdot (p, s, _) -> s :: loop p
1311-
| Papply _ -> []
1312-
in
1306+
let getBuiltinTypePath path =
1307+
match path with
1308+
| Path.Pident id when Ident.name id = "array" -> Some arrayModulePath
1309+
| Path.Pident id when Ident.name id = "option" -> Some optionModulePath
1310+
| Path.Pident id when Ident.name id = "string" -> Some stringModulePath
1311+
| Path.Pident id when Ident.name id = "int" -> Some intModulePath
1312+
| Path.Pident id when Ident.name id = "float" -> Some floatModulePath
1313+
| Path.Pident id when Ident.name id = "promise" ->
1314+
Some promiseModulePath
1315+
| Path.Pident id when Ident.name id = "list" -> Some listModulePath
1316+
| Path.Pident id when Ident.name id = "result" -> Some resultModulePath
1317+
| Path.Pident id when Ident.name id = "lazy_t" -> Some ["Lazy"]
1318+
| Path.Pident id when Ident.name id = "char" -> Some ["Char"]
1319+
| _ -> None
1320+
in
1321+
let rec expandPath (path : Path.t) =
13131322
match path with
1314-
| Path.Pident id when Ident.name id = "array" -> arrayModulePath
1315-
| Path.Pident id when Ident.name id = "option" -> optionModulePath
1316-
| Path.Pident id when Ident.name id = "string" -> stringModulePath
1317-
| Path.Pident id when Ident.name id = "int" -> intModulePath
1318-
| Path.Pident id when Ident.name id = "float" -> floatModulePath
1319-
| Path.Pident id when Ident.name id = "promise" -> promiseModulePath
1320-
| Path.Pident id when Ident.name id = "list" -> listModulePath
1321-
| Path.Pident id when Ident.name id = "result" -> resultModulePath
1322-
| Path.Pident id when Ident.name id = "lazy_t" -> ["Lazy"]
1323-
| Path.Pident id when Ident.name id = "char" -> ["Char"]
1324-
| _ -> (
1325-
match loop path with
1326-
| _ :: rest -> List.rev rest
1327-
| [] -> [])
1323+
| Pident id -> [Ident.name id]
1324+
| Pdot (p, s, _) -> s :: expandPath p
1325+
| Papply _ -> []
13281326
in
1329-
let getConstrPath typ =
1327+
let getTypePath typ =
13301328
match typ.Types.desc with
13311329
| Tconstr (path, _typeArgs, _)
13321330
| Tlink {desc = Tconstr (path, _typeArgs, _)}
@@ -1335,12 +1333,6 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
13351333
Some path
13361334
| _ -> None
13371335
in
1338-
let fromType typ =
1339-
match getConstrPath typ with
1340-
| None -> None
1341-
| Some path -> Some (getModulePath path)
1342-
in
1343-
let lhsPath = fromType typ in
13441336
let rec removeRawOpen rawOpen modulePath =
13451337
match (rawOpen, modulePath) with
13461338
| [_], _ -> Some modulePath
@@ -1357,64 +1349,52 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
13571349
| Some mp -> mp)
13581350
| [] -> modulePath
13591351
in
1360-
match lhsPath with
1361-
| Some lhsPath -> (
1362-
match lhsPath with
1363-
| _ :: _ ->
1364-
let lhsPathMinusOpens =
1365-
lhsPath
1366-
|> removeRawOpens package.opens
1367-
|> removeRawOpens rawOpens |> String.concat "."
1368-
in
1369-
let completionName name =
1370-
if lhsPathMinusOpens = "" then name
1371-
else lhsPathMinusOpens ^ "." ^ name
1372-
in
1373-
let completions =
1374-
lhsPath @ [funNamePrefix]
1375-
|> getCompletionsForPath ~completionContext:Value ~exact:false
1376-
~package ~opens ~allFiles ~pos ~env ~scope
1377-
in
1378-
completions
1379-
|> List.map (fun (completion : Completion.t) ->
1380-
{
1381-
completion with
1382-
name = completionName completion.name;
1383-
env
1384-
(* Restore original env for the completion after x->foo()... *);
1385-
})
1386-
| [] ->
1387-
(* Module paths coming directly from a completion item is prefixed with
1388-
file module name it was found in. We pluck that off here if the env
1389-
we're in is the same as the completion item was found in. This ensures
1390-
that a correct qualified path can be produced. *)
1391-
let completionPath =
1392-
match ModulePath.toFullPath completionItemModulePath with
1393-
| topModule :: rest when topModule = env.file.moduleName -> rest
1394-
| modulePath -> modulePath
1395-
in
1396-
let completionPathMinusOpens =
1397-
completionPath
1398-
|> removeRawOpens package.opens
1399-
|> removeRawOpens rawOpens |> String.concat "."
1400-
in
1401-
let completionName name =
1402-
if completionPathMinusOpens = "" then name
1403-
else completionPathMinusOpens ^ "." ^ name
1404-
in
1405-
let completions =
1406-
completionPath @ [funNamePrefix]
1407-
|> getCompletionsForPath ~completionContext:Value ~exact:false
1408-
~package ~opens ~allFiles ~pos ~env ~scope
1409-
in
1410-
completions
1411-
|> List.map (fun (completion : Completion.t) ->
1412-
{
1413-
completion with
1414-
name = completionName completion.name;
1415-
env
1416-
(* Restore original env for the completion after x->foo()... *);
1417-
}))
1352+
let completionPath =
1353+
match getTypePath typ with
1354+
| Some typePath -> (
1355+
match getBuiltinTypePath typePath with
1356+
| Some path -> Some path
1357+
| None -> (
1358+
match expandPath typePath with
1359+
| _ :: rest when rest <> [] ->
1360+
(* Assume a non-empty type path is coming from the compiler and
1361+
can be used as-is. *)
1362+
Some (List.rev rest)
1363+
| _ -> (
1364+
(* Module paths coming directly from a completion item is prefixed with
1365+
file module name it was found in. We pluck that off here if the env
1366+
we're in is the same as the completion item was found in. This ensures
1367+
that a correct qualified path can be produced. *)
1368+
match ModulePath.toFullPath completionItemModulePath with
1369+
| topModule :: rest when topModule = env.file.moduleName ->
1370+
Some rest
1371+
| path -> Some path)))
1372+
| None -> None
1373+
in
1374+
match completionPath with
1375+
| Some completionPath ->
1376+
let completionPathMinusOpens =
1377+
completionPath
1378+
|> removeRawOpens package.opens
1379+
|> removeRawOpens rawOpens |> String.concat "."
1380+
in
1381+
let completionName name =
1382+
if completionPathMinusOpens = "" then name
1383+
else completionPathMinusOpens ^ "." ^ name
1384+
in
1385+
let completions =
1386+
completionPath @ [funNamePrefix]
1387+
|> getCompletionsForPath ~completionContext:Value ~exact:false
1388+
~package ~opens ~allFiles ~pos ~env ~scope
1389+
in
1390+
completions
1391+
|> List.map (fun (completion : Completion.t) ->
1392+
{
1393+
completion with
1394+
name = completionName completion.name;
1395+
env
1396+
(* Restore original env for the completion after x->foo()... *);
1397+
})
14181398
| None -> [])
14191399
| None -> [])
14201400

0 commit comments

Comments
 (0)