@@ -672,15 +672,19 @@ let localValueCompletions ~pos ~(env : QueryEnv.t) suffix =
672
672
{(emptyDeclared c.cname.txt) with item = Constructor (c, t)}))
673
673
else results
674
674
in
675
- if suffix = " " || not (isCapitalized suffix) then
676
- results
677
- @ completionForDeclareds ~pos env.file.stamps.values suffix (fun v ->
678
- Value v)
679
- @ completionForDeclareds ~pos env.file.stamps.types suffix (fun t -> Type t)
680
- @ (completionForFields env.exported.types env.file.stamps.types suffix
681
- |> List. map (fun (f , t ) ->
682
- {(emptyDeclared f.fname.txt) with item = Field (f, t)}))
683
- else results
675
+ let results =
676
+ if suffix = " " || not (isCapitalized suffix) then
677
+ results
678
+ @ completionForDeclareds ~pos env.file.stamps.values suffix (fun v ->
679
+ Value v)
680
+ @ completionForDeclareds ~pos env.file.stamps.types suffix (fun t ->
681
+ Type t)
682
+ @ (completionForFields env.exported.types env.file.stamps.types suffix
683
+ |> List. map (fun (f , t ) ->
684
+ {(emptyDeclared f.fname.txt) with item = Field (f, t)}))
685
+ else results
686
+ in
687
+ results |> List. map (fun r -> (r, env))
684
688
685
689
let valueCompletions ~(env : QueryEnv.t ) suffix =
686
690
Log. log (" - Completing in " ^ Uri2. toString env.file.uri);
@@ -705,17 +709,20 @@ let valueCompletions ~(env : QueryEnv.t) suffix =
705
709
{(emptyDeclared c.cname.txt) with item = Constructor (c, t)})))
706
710
else results
707
711
in
708
- if suffix = " " || not (isCapitalized suffix) then (
709
- Log. log " -- not capitalized" ;
710
- results
711
- @ completionForExporteds env.exported.values env.file.stamps.values suffix
712
- (fun v -> Value v)
713
- @ completionForExporteds env.exported.types env.file.stamps.types suffix
714
- (fun t -> Type t)
715
- @ (completionForFields env.exported.types env.file.stamps.types suffix
716
- |> List. map (fun (f , t ) ->
717
- {(emptyDeclared f.fname.txt) with item = Field (f, t)})))
718
- else results
712
+ let results =
713
+ if suffix = " " || not (isCapitalized suffix) then (
714
+ Log. log " -- not capitalized" ;
715
+ results
716
+ @ completionForExporteds env.exported.values env.file.stamps.values suffix
717
+ (fun v -> Value v)
718
+ @ completionForExporteds env.exported.types env.file.stamps.types suffix
719
+ (fun t -> Type t)
720
+ @ (completionForFields env.exported.types env.file.stamps.types suffix
721
+ |> List. map (fun (f , t ) ->
722
+ {(emptyDeclared f.fname.txt) with item = Field (f, t)})))
723
+ else results
724
+ in
725
+ results |> List. map (fun r -> (r, env))
719
726
720
727
let attributeCompletions ~(env : QueryEnv.t ) ~suffix =
721
728
let results = [] in
@@ -726,15 +733,18 @@ let attributeCompletions ~(env : QueryEnv.t) ~suffix =
726
733
suffix (fun m -> Module m)
727
734
else results
728
735
in
729
- if suffix = " " || not (isCapitalized suffix) then
730
- results
731
- @ completionForExporteds env.exported.values env.file.stamps.values suffix
732
- (fun v -> Value v)
733
- (* completionForExporteds(env.exported.types, env.file.stamps.types, suffix, t => Type(t)) @ *)
734
- @ (completionForFields env.exported.types env.file.stamps.types suffix
735
- |> List. map (fun (f , t ) ->
736
- {(emptyDeclared f.fname.txt) with item = Field (f, t)}))
737
- else results
736
+ let results =
737
+ if suffix = " " || not (isCapitalized suffix) then
738
+ results
739
+ @ completionForExporteds env.exported.values env.file.stamps.values suffix
740
+ (fun v -> Value v)
741
+ (* completionForExporteds(env.exported.types, env.file.stamps.types, suffix, t => Type(t)) @ *)
742
+ @ (completionForFields env.exported.types env.file.stamps.types suffix
743
+ |> List. map (fun (f , t ) ->
744
+ {(emptyDeclared f.fname.txt) with item = Field (f, t)}))
745
+ else results
746
+ in
747
+ results |> List. map (fun r -> (r, env))
738
748
739
749
(* TODO filter out things that are defined after the current position *)
740
750
let resolveRawOpens ~env ~rawOpens ~package =
@@ -804,7 +814,7 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
804
814
(fun results env ->
805
815
let completionsFromThisOpen = valueCompletions ~env suffix in
806
816
List. filter
807
- (fun declared ->
817
+ (fun ( declared , _env ) ->
808
818
if Hashtbl. mem alreadyUsedIdentifiers declared.name.txt then
809
819
false
810
820
else (
@@ -819,7 +829,7 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
819
829
allFiles |> FileSet. elements
820
830
|> Utils. filterMap (fun name ->
821
831
if Utils. startsWith name suffix && not (String. contains name '-' )
822
- then Some {(emptyDeclared name) with item = FileModule name}
832
+ then Some ( {(emptyDeclared name) with item = FileModule name}, env)
823
833
else None )
824
834
in
825
835
locallyDefinedValues @ valuesFromOpens @ localModuleNames
@@ -863,15 +873,16 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
863
873
(Some (env, fields, typ))
864
874
with
865
875
| None -> []
866
- | Some (_env , fields , typ ) ->
876
+ | Some (env , fields , typ ) ->
867
877
fields
868
878
|> Utils. filterMap (fun field ->
869
879
if Utils. startsWith field.fname.txt lastField then
870
880
Some
871
- {
872
- (emptyDeclared field.fname.txt) with
873
- item = Field (field, typ);
874
- }
881
+ ( {
882
+ (emptyDeclared field.fname.txt) with
883
+ item = Field (field, typ);
884
+ },
885
+ env )
875
886
else None ))))
876
887
| None -> [] )
877
888
| QualifiedRecordAccess path -> (
@@ -924,7 +935,7 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
924
935
let declareds = processDotPath ~exact: true (componentPath @ [" make" ]) in
925
936
let labels =
926
937
match declareds with
927
- | {SharedTypes. item = Value typ } :: _ ->
938
+ | ( {SharedTypes. item = Value typ } , _env ) :: _ ->
928
939
let rec getFields (texp : Types.type_expr ) =
929
940
match texp.desc with
930
941
| Tfield (name , _ , t1 , t2 ) ->
@@ -984,7 +995,9 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
984
995
(* TODO(#107): figure out why we're getting duplicates. *)
985
996
declareds |> Utils. dedup
986
997
|> List. map
987
- (fun {SharedTypes. name = {txt = name } ; deprecated; docstring; item} ->
998
+ (fun
999
+ ({SharedTypes. name = {txt = name } ; deprecated; docstring; item} , _env )
1000
+ ->
988
1001
mkItem ~name ~kind: (kindToInt item) ~deprecated
989
1002
~detail: (detail name item) ~docstring )
990
1003
| Cpipe (pipe , partialName ) -> (
@@ -1044,8 +1057,7 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
1044
1057
match String. split_on_char '.' pipeId with
1045
1058
| x :: fieldNames -> (
1046
1059
match [x] |> processDotPath ~exact: true with
1047
- | {SharedTypes. item = Value typ } :: _ -> (
1048
- let env = QueryEnv. fromFile full.file in
1060
+ | ({SharedTypes. item = Value typ } , env ) :: _ -> (
1049
1061
match getFields ~env ~typ fieldNames with
1050
1062
| None -> None
1051
1063
| Some (typ1 , _env1 ) -> fromType typ1)
@@ -1096,10 +1108,12 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
1096
1108
let dotpath = modulePath @ [partialName] in
1097
1109
let declareds = dotpath |> processDotPath ~exact: false in
1098
1110
declareds
1099
- |> List. filter (fun {item} ->
1111
+ |> List. filter (fun ( {item} , _env ) ->
1100
1112
match item with Value _ -> true | _ -> false )
1101
1113
|> List. map
1102
- (fun {SharedTypes. name = {txt = name } ; deprecated; docstring; item}
1114
+ (fun
1115
+ ( {SharedTypes. name = {txt = name } ; deprecated; docstring; item} ,
1116
+ _env )
1103
1117
->
1104
1118
mkItem ~name: (completionName name) ~kind: (kindToInt item)
1105
1119
~detail: (detail name item) ~deprecated ~docstring )
@@ -1152,7 +1166,7 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
1152
1166
| Clabel (funPath , prefix , identsSeen ) ->
1153
1167
let labels =
1154
1168
match funPath |> processDotPath ~exact: true with
1155
- | {SharedTypes. item = Value typ } :: _ ->
1169
+ | ( {SharedTypes. item = Value typ } , _env ) :: _ ->
1156
1170
let rec getLabels (t : Types.type_expr ) =
1157
1171
match t.desc with
1158
1172
| Tlink t1 | Tsubst t1 -> getLabels t1
@@ -1200,8 +1214,8 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
1200
1214
in
1201
1215
let env0 = QueryEnv. fromFile full.file in
1202
1216
let env, fields =
1203
- match [ lhs] |> processDotPath ~exact: true with
1204
- | {SharedTypes. item = Value typ } :: _ -> getObjectFields ~env: env0 typ
1217
+ match lhs |> processDotPath ~exact: true with
1218
+ | ( {SharedTypes. item = Value typ } , env ) :: _ -> getObjectFields ~env typ
1205
1219
| _ -> (env0, [] )
1206
1220
in
1207
1221
let labels = resolvePath ~env fields path in
@@ -1225,7 +1239,9 @@ let getCompletable ~textOpt ~pos =
1225
1239
match PartialParser. findCompletable text offset with
1226
1240
| None -> None
1227
1241
| Some completable ->
1228
- let rawOpens = PartialParser. findOpens text offset in
1242
+ let offsetFromLineStart = offset - snd pos in
1243
+ (* try to avoid confusion e.g. unclosed quotes at current position *)
1244
+ let rawOpens = PartialParser. findOpens text offsetFromLineStart in
1229
1245
Some (completable, rawOpens)))
1230
1246
1231
1247
let computeCompletions ~completable ~full ~pos ~rawOpens =
@@ -1239,17 +1255,17 @@ let computeCompletions ~completable ~full ~pos ~rawOpens =
1239
1255
Take the last position before pos if any, or just return the first element. *)
1240
1256
let rec prioritize decls =
1241
1257
match decls with
1242
- | d1 :: d2 :: rest ->
1258
+ | ( d1 , e1 ) :: ( d2 , e2 ) :: rest ->
1243
1259
let pos2 = d2.extentLoc.loc_start |> Utils. tupleOfLexing in
1244
- if pos2 > = pos then prioritize (d1 :: rest)
1260
+ if pos2 > = pos then prioritize ((d1, e1) :: rest)
1245
1261
else
1246
1262
let pos1 = d1.extentLoc.loc_start |> Utils. tupleOfLexing in
1247
- if pos1 < = pos2 then prioritize (d2 :: rest)
1248
- else prioritize (d1 :: rest)
1263
+ if pos1 < = pos2 then prioritize ((d2, e2) :: rest)
1264
+ else prioritize ((d1, e1) :: rest)
1249
1265
| [] | [_] -> decls
1250
1266
in
1251
1267
declareds
1252
- |> List. filter (fun {SharedTypes. name = {txt} } -> txt = last)
1268
+ |> List. filter (fun ( {SharedTypes. name = {txt} } , _env ) -> txt = last)
1253
1269
|> prioritize
1254
1270
| _ -> declareds
1255
1271
in
0 commit comments