Skip to content

Commit d163d29

Browse files
committed
refactor completion
1 parent 30da51e commit d163d29

File tree

2 files changed

+65
-56
lines changed

2 files changed

+65
-56
lines changed

analysis/src/Commands.ml

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ let dump files =
3333

3434
let completion ~path ~line ~col ~currentFile =
3535
let uri = Uri2.fromPath path in
36+
let pos = (line, col) in
3637
let result =
3738
let textOpt = Files.readFile currentFile in
38-
NewCompletions.computeCompletions ~uri ~textOpt ~pos:(line, col)
39+
let completionItems =
40+
match NewCompletions.getCompletable ~textOpt ~pos with
41+
| None -> []
42+
| Some (completable, rawOpens) ->
43+
NewCompletions.computeCompletions ~completable ~pos ~rawOpens ~uri
44+
in
45+
completionItems
3946
|> List.map Protocol.stringifyCompletionItem
4047
|> Protocol.array
4148
in
@@ -143,38 +150,41 @@ let references ~path ~line ~col =
143150

144151
let documentSymbol ~path =
145152
let uri = Uri2.fromPath path in
146-
match ProcessCmt.getFullFromCmt ~uri with
147-
| None -> print_endline Protocol.null
148-
| Some {file} ->
149-
let open SharedTypes in
150-
let rec getItems {topLevel} =
151-
let rec getItem = function
152-
| MValue v -> (v |> SharedTypes.variableKind, [])
153-
| MType (t, _) -> (t.decl |> SharedTypes.declarationKind, [])
154-
| Module (Structure contents) -> (Module, getItems contents)
155-
| Module (Constraint (_, modTypeItem)) -> getItem (Module modTypeItem)
156-
| Module (Ident _) -> (Module, [])
153+
let result =
154+
match ProcessCmt.getFullFromCmt ~uri with
155+
| None -> Protocol.null
156+
| Some {file} ->
157+
let open SharedTypes in
158+
let rec getItems {topLevel} =
159+
let rec getItem = function
160+
| MValue v -> (v |> SharedTypes.variableKind, [])
161+
| MType (t, _) -> (t.decl |> SharedTypes.declarationKind, [])
162+
| Module (Structure contents) -> (Module, getItems contents)
163+
| Module (Constraint (_, modTypeItem)) -> getItem (Module modTypeItem)
164+
| Module (Ident _) -> (Module, [])
165+
in
166+
let fn {name = {txt}; extentLoc; item} =
167+
let item, siblings = getItem item in
168+
if extentLoc.loc_ghost then siblings
169+
else (txt, extentLoc, item) :: siblings
170+
in
171+
let x = topLevel |> List.map fn |> List.concat in
172+
x
157173
in
158-
let fn {name = {txt}; extentLoc; item} =
159-
let item, siblings = getItem item in
160-
if extentLoc.loc_ghost then siblings
161-
else (txt, extentLoc, item) :: siblings
174+
let allSymbols =
175+
getItems file.contents
176+
|> List.map (fun (name, loc, kind) ->
177+
Protocol.stringifyDocumentSymbolItem
178+
{
179+
name;
180+
location =
181+
{uri = Uri2.toString uri; range = Utils.cmtLocToRange loc};
182+
kind = SharedTypes.symbolKind kind;
183+
})
162184
in
163-
let x = topLevel |> List.map fn |> List.concat in
164-
x
165-
in
166-
let allSymbols =
167-
getItems file.contents
168-
|> List.map (fun (name, loc, kind) ->
169-
Protocol.stringifyDocumentSymbolItem
170-
{
171-
name;
172-
location =
173-
{uri = Uri2.toString uri; range = Utils.cmtLocToRange loc};
174-
kind = SharedTypes.symbolKind kind;
175-
})
176-
in
177-
print_endline ("[\n" ^ (allSymbols |> String.concat ",\n") ^ "\n]")
185+
"[\n" ^ (allSymbols |> String.concat ",\n") ^ "\n]"
186+
in
187+
print_endline result
178188

179189
let rename ~path ~line ~col ~newName =
180190
let uri = Uri2.fromPath path in

analysis/src/NewCompletions.ml

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,33 +1133,32 @@ let processCompletable ~findItems ~full ~package ~rawOpens
11331133
Utils.startsWith name prefix && not (List.mem name identsSeen))
11341134
|> List.map mkLabel
11351135

1136-
let computeCompletions ~uri ~textOpt ~pos =
1136+
let getCompletable ~textOpt ~pos =
11371137
match textOpt with
1138-
| None -> []
1138+
| None -> None
11391139
| Some text -> (
11401140
match PartialParser.positionToOffset text pos with
1141-
| None -> []
1141+
| None -> None
11421142
| Some offset -> (
11431143
match PartialParser.findCompletable text offset with
1144-
| None -> []
1145-
| Some completable -> (
1146-
match ProcessCmt.getFullFromCmt ~uri with
1147-
| None -> []
1148-
| Some full ->
1149-
let rawOpens = PartialParser.findOpens text offset in
1150-
let package = full.package in
1151-
let allFiles =
1152-
FileSet.union package.projectFiles package.dependenciesFiles
1153-
in
1154-
let findItems ~exact parts =
1155-
let items =
1156-
getItems ~full ~package ~rawOpens ~allFiles ~pos ~parts
1157-
in
1158-
match parts |> List.rev with
1159-
| last :: _ when exact ->
1160-
items
1161-
|> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
1162-
| _ -> items
1163-
in
1164-
completable |> processCompletable ~findItems ~full ~package ~rawOpens)
1165-
))
1144+
| None -> None
1145+
| Some completable ->
1146+
let rawOpens = PartialParser.findOpens text offset in
1147+
Some (completable, rawOpens)))
1148+
1149+
let computeCompletions ~completable ~pos ~rawOpens ~uri =
1150+
match ProcessCmt.getFullFromCmt ~uri with
1151+
| None -> []
1152+
| Some full ->
1153+
let package = full.package in
1154+
let allFiles =
1155+
FileSet.union package.projectFiles package.dependenciesFiles
1156+
in
1157+
let findItems ~exact parts =
1158+
let items = getItems ~full ~package ~rawOpens ~allFiles ~pos ~parts in
1159+
match parts |> List.rev with
1160+
| last :: _ when exact ->
1161+
items |> List.filter (fun {SharedTypes.name = {txt}} -> txt = last)
1162+
| _ -> items
1163+
in
1164+
completable |> processCompletable ~findItems ~full ~package ~rawOpens

0 commit comments

Comments
 (0)