Skip to content

Commit 70e39a9

Browse files
committed
more robust index mapping for args
1 parent dcbccb8 commit 70e39a9

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

src/features/translation.ts

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,86 @@ const toFind: FeatureTag = [
1818
{
1919
class: [contract("Translation\\Translator")],
2020
method: ["get", "choice"],
21-
argumentIndex: 0,
2221
},
2322
{
2423
class: facade("Lang"),
25-
method: ["has", "hasForLocale", "get", "getForLocale", "choice"],
26-
argumentIndex: [0, 1],
24+
method: ["has", "hasForLocale", "get", "choice"],
2725
},
2826
{
2927
method: ["__", "trans", "@lang"],
30-
argumentIndex: [0, 1, 2],
3128
},
3229
];
3330

31+
type ArgIndexMap = Record<string, Record<string, number>>;
32+
33+
const paramArgIndexes: ArgIndexMap = {
34+
[contract("Translation\\Translator")]: {
35+
get: 1,
36+
choice: 2,
37+
},
38+
"": {
39+
__: 1,
40+
trans: 1,
41+
"@lang": 1,
42+
},
43+
};
44+
45+
const localeArgIndexes: ArgIndexMap = {
46+
[contract("Translation\\Translator")]: {
47+
get: 2,
48+
choice: 3,
49+
},
50+
"": {
51+
__: 2,
52+
trans: 2,
53+
"@lang": 2,
54+
},
55+
};
56+
57+
facade("Lang").forEach((cl) => {
58+
paramArgIndexes[cl] = {
59+
get: 1,
60+
choice: 2,
61+
};
62+
63+
localeArgIndexes[cl] = {
64+
has: 1,
65+
hasForLocale: 1,
66+
get: 2,
67+
choice: 3,
68+
};
69+
});
70+
71+
const getFromMapping = (
72+
className: string | null,
73+
methodName: string | null,
74+
mapping: ArgIndexMap,
75+
) => {
76+
return mapping[className ?? ""][methodName ?? ""] ?? null;
77+
};
78+
79+
const getLocaleArgIndex = (
80+
className: string | null,
81+
methodName: string | null,
82+
) => {
83+
return getFromMapping(className, methodName, localeArgIndexes);
84+
};
85+
86+
const getParamArgIndex = (
87+
className: string | null,
88+
methodName: string | null,
89+
) => {
90+
return getFromMapping(className, methodName, paramArgIndexes);
91+
};
92+
3493
const getLang = (
3594
item: AutocompleteParsingResult.MethodCall,
3695
): string | undefined => {
96+
const localeArgIndex = getLocaleArgIndex(item.className, item.methodName);
97+
3798
const locale = (
3899
item.arguments.children as AutocompleteParsingResult.Argument[]
39-
).find((arg, i) => arg.name === "locale" || i === 2);
100+
).find((arg, i) => arg.name === "locale" || i === localeArgIndex);
40101

41102
return locale?.children.length
42103
? (locale.children as AutocompleteParsingResult.StringValue[])[0].value
@@ -154,11 +215,17 @@ export const completionProvider = {
154215
token: vscode.CancellationToken,
155216
context: vscode.CompletionContext,
156217
): vscode.CompletionItem[] {
157-
if (result.isParamIndex(1)) {
218+
const localeArgIndex = getLocaleArgIndex(result.class(), result.func());
219+
const paramArgIndex = getParamArgIndex(result.class(), result.func());
220+
221+
if (result.isParamIndex(paramArgIndex ?? -1)) {
158222
return this.getParameterCompletionItems(result, document, position);
159223
}
160224

161-
if (result.isParamIndex(2) || result.isArgumentNamed("locale")) {
225+
if (
226+
result.isParamIndex(localeArgIndex ?? -1) ||
227+
result.isArgumentNamed("locale")
228+
) {
162229
return getTranslations().items.languages.map((lang) => {
163230
let completionItem = new vscode.CompletionItem(
164231
lang,
@@ -174,6 +241,10 @@ export const completionProvider = {
174241
});
175242
}
176243

244+
if (!result.isParamIndex(0)) {
245+
return [];
246+
}
247+
177248
const totalTranslationItems = Object.entries(
178249
getTranslations().items.translations,
179250
).length;

0 commit comments

Comments
 (0)