Skip to content

Commit dcbccb8

Browse files
committed
add autocompletion for locales
1 parent 4e70da2 commit dcbccb8

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

php-templates/translations.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
public $directoriesToWatch = [];
1616

17+
public $languages = [];
18+
1719
public function all()
1820
{
1921
$final = [];
@@ -24,13 +26,21 @@ public function all()
2426
$dotKey = $val["k"];
2527
$final[$dotKey] ??= [];
2628

29+
if (!in_array($val["la"], $this->languages)) {
30+
$this->languages[] = $val["la"];
31+
}
32+
2733
$final[$dotKey][$val["la"]] = $val["vs"];
2834
}
2935
} else {
3036
foreach ($value["vs"] as $v) {
3137
$dotKey = "{$value["k"]}.{$v['k']}";
3238
$final[$dotKey] ??= [];
3339

40+
if (!in_array($$value["la"], $this->languages)) {
41+
$this->languages[] = $$value["la"];
42+
}
43+
3444
$final[$dotKey][$value["la"]] = $v['arr'];
3545
}
3646
}
@@ -345,6 +355,7 @@ protected function fromPhpFile($file, $path, $namespace)
345355
echo json_encode([
346356
'default' => \Illuminate\Support\Facades\App::currentLocale(),
347357
'translations' => $translator->all(),
358+
'languages' => $translator->languages,
348359
'paths' => array_keys($translator->paths),
349360
'values' => array_keys($translator->values),
350361
'params' => array_map(fn($p) => json_decode($p, true), array_keys($translator->paramResults)),

src/features/translation.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,39 @@ const toFind: FeatureTag = [
1818
{
1919
class: [contract("Translation\\Translator")],
2020
method: ["get", "choice"],
21-
argumentIndex: 0,
22-
},
21+
argumentIndex: 0,
22+
},
2323
{
2424
class: facade("Lang"),
2525
method: ["has", "hasForLocale", "get", "getForLocale", "choice"],
2626
argumentIndex: [0, 1],
2727
},
2828
{
2929
method: ["__", "trans", "@lang"],
30-
argumentIndex: [0, 1],
30+
argumentIndex: [0, 1, 2],
3131
},
3232
];
3333

34-
const getLang = (item: AutocompleteParsingResult.MethodCall): string | undefined => {
35-
const locale = (item.arguments.children as AutocompleteParsingResult.Argument[]).find(
36-
(arg) => arg.name === "locale",
37-
);
34+
const getLang = (
35+
item: AutocompleteParsingResult.MethodCall,
36+
): string | undefined => {
37+
const locale = (
38+
item.arguments.children as AutocompleteParsingResult.Argument[]
39+
).find((arg, i) => arg.name === "locale" || i === 2);
3840

39-
return locale?.children.length ?
40-
(locale.children as AutocompleteParsingResult.StringValue[])[0].value : undefined;
41+
return locale?.children.length
42+
? (locale.children as AutocompleteParsingResult.StringValue[])[0].value
43+
: undefined;
4144
};
4245

43-
const getTranslationItemByLang = (translation: TranslationItem, lang?: string) => {
44-
return translation[lang ?? getTranslations().items.default]
45-
?? translation[Object.keys(translation)[0]];
46+
const getTranslationItemByLang = (
47+
translation: TranslationItem,
48+
lang?: string,
49+
) => {
50+
return (
51+
translation[lang ?? getTranslations().items.default] ??
52+
translation[Object.keys(translation)[0]]
53+
);
4654
};
4755

4856
export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
@@ -63,8 +71,8 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
6371
}
6472

6573
const def = getTranslationItemByLang(
66-
translation,
67-
getLang(item as AutocompleteParsingResult.MethodCall)
74+
translation,
75+
getLang(item as AutocompleteParsingResult.MethodCall),
6876
);
6977

7078
return new vscode.DocumentLink(
@@ -150,6 +158,22 @@ export const completionProvider = {
150158
return this.getParameterCompletionItems(result, document, position);
151159
}
152160

161+
if (result.isParamIndex(2) || result.isArgumentNamed("locale")) {
162+
return getTranslations().items.languages.map((lang) => {
163+
let completionItem = new vscode.CompletionItem(
164+
lang,
165+
vscode.CompletionItemKind.Value,
166+
);
167+
168+
completionItem.range = document.getWordRangeAtPosition(
169+
position,
170+
wordMatchRegex,
171+
);
172+
173+
return completionItem;
174+
});
175+
}
176+
153177
const totalTranslationItems = Object.entries(
154178
getTranslations().items.translations,
155179
).length;
@@ -169,7 +193,8 @@ export const completionProvider = {
169193
if (totalTranslationItems < 200) {
170194
// This will bomb if we have too many translations,
171195
// 200 is an arbitrary but probably safe number
172-
completionItem.detail = getTranslationItemByLang(translations).value;
196+
completionItem.detail =
197+
getTranslationItemByLang(translations).value;
173198
}
174199

175200
return completionItem;

src/parser/AutocompleteResult.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export default class AutocompleteResult {
117117
return this.param()?.name ?? null;
118118
}
119119

120+
public isArgumentNamed(name: string) {
121+
return this.argumentName() === name;
122+
}
123+
120124
public isParamIndex(index: number) {
121125
return this.paramIndex() === index;
122126
}

src/repositories/translations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface TranslationGroupResult {
1717
translations: {
1818
[key: string]: TranslationItem;
1919
};
20+
languages: string[];
2021
}
2122

2223
interface TranslationGroupPhpResult {
@@ -30,6 +31,7 @@ interface TranslationGroupPhpResult {
3031
paths: string[];
3132
values: string[];
3233
to_watch: string[];
34+
languages: string[];
3335
}
3436

3537
let dirsToWatch: string[] | null = null;
@@ -63,6 +65,7 @@ const load = () => {
6365
return {
6466
default: res.default,
6567
translations: result,
68+
languages: res.languages,
6669
};
6770
});
6871
};

src/templates/translations.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ $translator = new class
1414
1515
public $directoriesToWatch = [];
1616
17+
public $languages = [];
18+
1719
public function all()
1820
{
1921
$final = [];
@@ -24,13 +26,21 @@ $translator = new class
2426
$dotKey = $val["k"];
2527
$final[$dotKey] ??= [];
2628
29+
if (!in_array($val["la"], $this->languages)) {
30+
$this->languages[] = $val["la"];
31+
}
32+
2733
$final[$dotKey][$val["la"]] = $val["vs"];
2834
}
2935
} else {
3036
foreach ($value["vs"] as $v) {
3137
$dotKey = "{$value["k"]}.{$v['k']}";
3238
$final[$dotKey] ??= [];
3339
40+
if (!in_array($$value["la"], $this->languages)) {
41+
$this->languages[] = $$value["la"];
42+
}
43+
3444
$final[$dotKey][$value["la"]] = $v['arr'];
3545
}
3646
}
@@ -345,6 +355,7 @@ $translator = new class
345355
echo json_encode([
346356
'default' => \\Illuminate\\Support\\Facades\\App::currentLocale(),
347357
'translations' => $translator->all(),
358+
'languages' => $translator->languages,
348359
'paths' => array_keys($translator->paths),
349360
'values' => array_keys($translator->values),
350361
'params' => array_map(fn($p) => json_decode($p, true), array_keys($translator->paramResults)),

0 commit comments

Comments
 (0)