@@ -18,25 +18,86 @@ const toFind: FeatureTag = [
18
18
{
19
19
class : [ contract ( "Translation\\Translator" ) ] ,
20
20
method : [ "get" , "choice" ] ,
21
- argumentIndex : 0 ,
22
21
} ,
23
22
{
24
23
class : facade ( "Lang" ) ,
25
- method : [ "has" , "hasForLocale" , "get" , "getForLocale" , "choice" ] ,
26
- argumentIndex : [ 0 , 1 ] ,
24
+ method : [ "has" , "hasForLocale" , "get" , "choice" ] ,
27
25
} ,
28
26
{
29
27
method : [ "__" , "trans" , "@lang" ] ,
30
- argumentIndex : [ 0 , 1 , 2 ] ,
31
28
} ,
32
29
] ;
33
30
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
+
34
93
const getLang = (
35
94
item : AutocompleteParsingResult . MethodCall ,
36
95
) : string | undefined => {
96
+ const localeArgIndex = getLocaleArgIndex ( item . className , item . methodName ) ;
97
+
37
98
const locale = (
38
99
item . arguments . children as AutocompleteParsingResult . Argument [ ]
39
- ) . find ( ( arg , i ) => arg . name === "locale" || i === 2 ) ;
100
+ ) . find ( ( arg , i ) => arg . name === "locale" || i === localeArgIndex ) ;
40
101
41
102
return locale ?. children . length
42
103
? ( locale . children as AutocompleteParsingResult . StringValue [ ] ) [ 0 ] . value
@@ -154,11 +215,17 @@ export const completionProvider = {
154
215
token : vscode . CancellationToken ,
155
216
context : vscode . CompletionContext ,
156
217
) : 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 ) ) {
158
222
return this . getParameterCompletionItems ( result , document , position ) ;
159
223
}
160
224
161
- if ( result . isParamIndex ( 2 ) || result . isArgumentNamed ( "locale" ) ) {
225
+ if (
226
+ result . isParamIndex ( localeArgIndex ?? - 1 ) ||
227
+ result . isArgumentNamed ( "locale" )
228
+ ) {
162
229
return getTranslations ( ) . items . languages . map ( ( lang ) => {
163
230
let completionItem = new vscode . CompletionItem (
164
231
lang ,
@@ -174,6 +241,10 @@ export const completionProvider = {
174
241
} ) ;
175
242
}
176
243
244
+ if ( ! result . isParamIndex ( 0 ) ) {
245
+ return [ ] ;
246
+ }
247
+
177
248
const totalTranslationItems = Object . entries (
178
249
getTranslations ( ) . items . translations ,
179
250
) . length ;
0 commit comments