Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit d731ecc

Browse files
committed
Keep completing when changing an existing prefix after ->, and cleanup.
1 parent d2f0601 commit d731ecc

File tree

1 file changed

+79
-108
lines changed

1 file changed

+79
-108
lines changed

src/rescript-editor-support/NewCompletions.re

Lines changed: 79 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
655655
let rawOpens = PartialParser.findOpens(text, offset);
656656
let allModules =
657657
package.TopTypes.localModules @ package.dependencyModules;
658-
let parts = Str.split(Str.regexp_string("->"), s);
659-
let items =
658+
659+
let getItems = parts =>
660660
getItems(
661661
~full,
662662
~package,
@@ -667,6 +667,23 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
667667
~parts,
668668
);
669669

670+
let getLhsType = (~lhs, ~partialName) => {
671+
switch (getItems([lhs])) {
672+
| [(_uri, {SharedTypes.item: Value(t)}), ..._] =>
673+
Some((t, partialName))
674+
| _ => None
675+
};
676+
};
677+
678+
let lhsType =
679+
switch (Str.split(Str.regexp_string("->"), s)) {
680+
| [lhs] => getLhsType(~lhs, ~partialName="")
681+
| [lhs, partialName] => getLhsType(~lhs, ~partialName)
682+
| _ =>
683+
// Only allow one ->
684+
None
685+
};
686+
670687
let removePackageOpens = modulePath =>
671688
switch (modulePath) {
672689
| [toplevel, ...rest] when package.opens |> List.mem(toplevel) => rest
@@ -693,119 +710,73 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
693710
| [] => modulePath
694711
};
695712

696-
let pipeItems =
697-
switch (items) {
698-
| [(uri, {SharedTypes.item: Value(t)}), ..._] =>
699-
let getModulePath = path => {
700-
let rec loop = (path: Path.t) =>
701-
switch (path) {
702-
| Pident(id) => [Ident.name(id)]
703-
| Pdot(p, s, _) => [s, ...loop(p)]
704-
| Papply(_) => []
705-
};
706-
switch (loop(path)) {
707-
| [_, ...rest] => List.rev(rest)
708-
| [] => []
713+
switch (lhsType) {
714+
| Some((t, partialName)) =>
715+
let getModulePath = path => {
716+
let rec loop = (path: Path.t) =>
717+
switch (path) {
718+
| Pident(id) => [Ident.name(id)]
719+
| Pdot(p, s, _) => [s, ...loop(p)]
720+
| Papply(_) => []
709721
};
722+
switch (loop(path)) {
723+
| [_, ...rest] => List.rev(rest)
724+
| [] => []
710725
};
711-
let modulePath =
712-
switch (t.desc) {
713-
| Tconstr(path, _, _) => getModulePath(path)
714-
| Tlink({desc: Tconstr(path, _, _)}) => getModulePath(path)
715-
| _ => []
716-
};
717-
switch (modulePath) {
718-
| [_, ..._] =>
719-
let modulePathMinusOpens =
720-
modulePath
721-
|> removePackageOpens
722-
|> removeRawOpens(rawOpens)
723-
|> String.concat(".");
724-
let completionName = name =>
725-
modulePathMinusOpens == ""
726-
? name : modulePathMinusOpens ++ "." ++ name;
727-
let parts = modulePath @ [""];
728-
let items =
729-
getItems(
730-
~full,
731-
~package,
732-
~rawOpens,
733-
~getModule=State.fileForModule(state, ~package),
734-
~allModules,
735-
~pos,
736-
~parts,
737-
);
738-
items
739-
|> List.filter(((_, {item})) =>
740-
switch (item) {
741-
| Value(_) => true
742-
| _ => false
743-
}
744-
)
745-
|> List.map(
726+
};
727+
let modulePath =
728+
switch (t.desc) {
729+
| Tconstr(path, _, _) => getModulePath(path)
730+
| Tlink({desc: Tconstr(path, _, _)}) => getModulePath(path)
731+
| _ => []
732+
};
733+
switch (modulePath) {
734+
| [_, ..._] =>
735+
let modulePathMinusOpens =
736+
modulePath
737+
|> removePackageOpens
738+
|> removeRawOpens(rawOpens)
739+
|> String.concat(".");
740+
let completionName = name =>
741+
modulePathMinusOpens == ""
742+
? name : modulePathMinusOpens ++ "." ++ name;
743+
let parts = modulePath @ [partialName];
744+
let items = getItems(parts);
745+
items
746+
|> List.filter(((_, {item})) =>
747+
switch (item) {
748+
| Value(_) => true
749+
| _ => false
750+
}
751+
)
752+
|> List.map(
753+
(
746754
(
747-
(
748-
uri,
749-
{
750-
SharedTypes.name: {
751-
txt: name,
752-
loc: {loc_start: {pos_lnum}},
753-
},
754-
docstring,
755-
item,
755+
uri,
756+
{
757+
SharedTypes.name: {
758+
txt: name,
759+
loc: {loc_start: {pos_lnum}},
756760
},
757-
),
758-
) =>
759-
mkItem(
760-
~name=completionName(name),
761-
~kind=kindToInt(item),
762-
~detail=detail(name, item),
763-
~docstring,
764-
~uri,
765-
~pos_lnum,
766-
)
767-
);
768-
769-
| path => [
770-
mkItem(
771-
~name=
772-
"can't complete type: "
773-
++ Shared.typeToString(t)
774-
++ " in: "
775-
++ (path |> String.concat(".")),
776-
~kind=9,
777-
~detail=s,
778-
~docstring=None,
779-
~uri,
780-
~pos_lnum=0,
781-
),
782-
]
783-
};
761+
docstring,
762+
item,
763+
},
764+
),
765+
) =>
766+
mkItem(
767+
~name=completionName(name),
768+
~kind=kindToInt(item),
769+
~detail=detail(name, item),
770+
~docstring,
771+
~uri,
772+
~pos_lnum,
773+
)
774+
);
784775

785776
| _ => []
786777
};
787-
788-
// let dummyItems = [
789-
// mkItem(
790-
// ~name="Foo.pipe",
791-
// ~kind=9,
792-
// ~detail=s,
793-
// ~docstring=None,
794-
// ~uri="uri",
795-
// ~pos_lnum=0,
796-
// ),
797-
// mkItem(
798-
// ~name="Foo.pine",
799-
// ~kind=9,
800-
// ~detail=s,
801-
// ~docstring=None,
802-
// ~uri="uri",
803-
// ~pos_lnum=0,
804-
// ),
805-
// ];
806-
let dummyItems = [];
807-
808-
dummyItems @ pipeItems;
778+
| None => []
779+
};
809780

810781
| Some((_, _, Some(Clabel(_)))) =>
811782
// not supported yet

0 commit comments

Comments
 (0)