-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathAutocompleteResult.ts
145 lines (109 loc) · 3.53 KB
/
AutocompleteResult.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { contract, facade } from "@src/support/util";
import { AutocompleteParsingResult } from "@src/types";
export default class AutocompleteResult {
public result: AutocompleteParsingResult.ContextValue;
private additionalInfo: Record<string, any> = {};
constructor(result: AutocompleteParsingResult.ContextValue) {
this.result = result;
}
public currentParamIsArray(): boolean {
const currentArg = this.param();
if (currentArg === null) {
return false;
}
return currentArg?.type === "array";
}
public currentParamArrayKeys(): string[] {
const param = this.param();
if (typeof param === "undefined" || param.type !== "array") {
return [];
}
return (param as AutocompleteParsingResult.ArrayValue).children.map(
(child) => child.key.value,
);
}
public fillingInArrayKey(): boolean {
return this.param()?.autocompletingKey ?? false;
}
public fillingInArrayValue(): boolean {
return this.param()?.autocompletingValue ?? false;
}
public name() {
// @ts-ignore
return this.result.name ?? null;
}
public isName(name: string | string[]) {
if (Array.isArray(name)) {
return name.includes(this.name());
}
return this.name() === name;
}
public class() {
// @ts-ignore
return this.result.className ?? null;
}
public isClass(className: string | string[]) {
if (Array.isArray(className)) {
return className.includes(this.class());
}
return this.class() === className;
}
public isFacade(className: string) {
return this.isClass(facade(className));
}
public isContract(classNames: string | string[]) {
classNames = Array.isArray(classNames) ? classNames : [classNames];
return classNames.some((className: string) =>
this.isClass(contract(className)),
);
}
public func() {
// @ts-ignore
return this.result.methodName ?? null;
}
public addInfo(key: string, value: any) {
this.additionalInfo[key] = value;
}
public getInfo(key: string) {
return this.additionalInfo[key];
}
public loop(
cb: (context: AutocompleteParsingResult.ContextValue) => boolean | void,
) {
let context = this.result;
let shouldContinue = true;
while (context.parent !== null && shouldContinue) {
shouldContinue = cb(context) ?? true;
context = context.parent;
}
}
public isFunc(funcs: string | string[]) {
funcs = Array.isArray(funcs) ? funcs : [funcs];
return funcs.some((func: string) => this.func() === func);
}
public param(index?: number) {
index = index ?? this.paramIndex();
if (index === null || typeof index === "undefined") {
return null;
}
// @ts-ignore
return this.result.arguments?.children[index]?.children[0];
}
public paramIndex() {
// @ts-ignore
return this.result.arguments?.autocompletingIndex ?? null;
}
public argumentName() {
return this.param()?.name ?? null;
}
public isArgumentNamed(name: string) {
return this.argumentName() === name;
}
public isParamIndex(index: number) {
return this.paramIndex() === index;
}
public paramCount() {
// @ts-ignore
return this.result.arguments?.children.length ?? 0;
}
}