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

Commit 830f0dd

Browse files
committed
First versioon of pipe autocompletion.
1 parent 6924ae7 commit 830f0dd

File tree

1 file changed

+155
-18
lines changed

1 file changed

+155
-18
lines changed

src/rescript-editor-support/NewCompletions.re

Lines changed: 155 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -651,24 +651,161 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
651651
)
652652
);
653653

654-
| Some((_text, _offset, Some(Cpipe(s)))) => [
655-
mkItem(
656-
~name="Foo.pipe",
657-
~kind=9,
658-
~detail=s,
659-
~docstring=None,
660-
~uri="uri",
661-
~pos_lnum=0,
662-
),
663-
mkItem(
664-
~name="Foo.pine",
665-
~kind=9,
666-
~detail=s,
667-
~docstring=None,
668-
~uri="uri",
669-
~pos_lnum=0,
670-
),
671-
]
654+
| Some((text, offset, Some(Cpipe(s)))) =>
655+
let rawOpens = PartialParser.findOpens(text, offset);
656+
let allModules =
657+
package.TopTypes.localModules @ package.dependencyModules;
658+
let parts = Str.split(Str.regexp_string("->"), s);
659+
let items =
660+
getItems(
661+
~full,
662+
~package,
663+
~rawOpens,
664+
~getModule=State.fileForModule(state, ~package),
665+
~allModules,
666+
~pos,
667+
~parts,
668+
);
669+
670+
let removePackageOpens = modulePath =>
671+
switch (modulePath) {
672+
| [toplevel, ...rest] when package.opens |> List.mem(toplevel) => rest
673+
| _ => modulePath
674+
};
675+
676+
let rec removeRawOpen = (rawOpen, modulePath) =>
677+
switch (rawOpen, modulePath) {
678+
| (Tip(_), _) => Some(modulePath)
679+
| (Nested(s, inner), [first, ...restPath]) when s == first =>
680+
removeRawOpen(inner, restPath)
681+
| _ => None
682+
};
683+
684+
let rec removeRawOpens = (rawOpens, modulePath) =>
685+
switch (rawOpens) {
686+
| [rawOpen, ...restOpens] =>
687+
let newModulePath =
688+
switch (removeRawOpen(rawOpen, modulePath)) {
689+
| None => modulePath
690+
| Some(newModulePath) => newModulePath
691+
};
692+
removeRawOpens(restOpens, newModulePath);
693+
| [] => modulePath
694+
};
695+
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+
| [] => []
709+
};
710+
};
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(
746+
(
747+
(
748+
uri,
749+
{
750+
SharedTypes.name: {
751+
txt: name,
752+
loc: {loc_start: {pos_lnum}},
753+
},
754+
docstring,
755+
item,
756+
},
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+
};
784+
785+
| _ => []
786+
};
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;
672809

673810
| Some((_, _, Some(Clabel(_)))) =>
674811
// not supported yet

0 commit comments

Comments
 (0)