|
1 | 1 | package com.emberjs.hbs |
2 | 2 |
|
3 | 3 | import com.dmarcotte.handlebars.parsing.HbTokenTypes |
4 | | -import com.dmarcotte.handlebars.psi.HbData |
5 | | -import com.dmarcotte.handlebars.psi.HbMustache |
6 | | -import com.dmarcotte.handlebars.psi.HbParam |
7 | | -import com.dmarcotte.handlebars.psi.HbStringLiteral |
| 4 | +import com.dmarcotte.handlebars.psi.* |
8 | 5 | import com.dmarcotte.handlebars.psi.impl.HbBlockWrapperImpl |
9 | 6 | import com.dmarcotte.handlebars.psi.impl.HbPathImpl |
10 | 7 | import com.emberjs.glint.GlintLanguageServiceProvider |
@@ -157,16 +154,32 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() { |
157 | 154 | resolve(followed, result) |
158 | 155 | } |
159 | 156 |
|
160 | | - fun addHelperCompletions(element: PsiElement, result: MutableList<LookupElement>) { |
| 157 | + fun addHelperCompletions(element: PsiElement, result: MutableList<LookupElement>, currentPosition: Int) { |
161 | 158 | val file = EmberUtils.followReferences(element.children.firstOrNull()) |
| 159 | + val hashNames = element.parent.children.filter { it is HbHash }.map { (it as HbHash).hashName } |
| 160 | + val params = element.parent.children.filter { it is HbParam } |
| 161 | + val param = params.getOrNull(currentPosition) |
| 162 | + |
| 163 | + val isLiteral = PsiTreeUtil.findChildOfType(param, HbStringLiteral::class.java) != null |
| 164 | + || PsiTreeUtil.findChildOfType(param, HbNumberLiteral::class.java) != null |
162 | 165 |
|
163 | | - if (file is JSClass) { |
164 | | - val ref = EmberUtils.getComponentReferenceData(file.containingFile) |
165 | | - result.addAll(ref.args.map { LookupElementBuilder.create(it.value + "=") }) |
166 | | - } |
167 | 166 | val map = EmberUtils.getArgsAndPositionals(element) |
168 | | - val named = map.named.map { LookupElementBuilder.create(it + "=") } |
169 | | - result.addAll(named.map { PrioritizedLookupElement.withPriority(it, 100.0) }) |
| 167 | + |
| 168 | + if (!isLiteral) { |
| 169 | + if (file is JSClass) { |
| 170 | + val ref = EmberUtils.getComponentReferenceData(file.containingFile) |
| 171 | + val args = ref.args.filter { !hashNames.contains(it.value) } |
| 172 | + result.addAll(args.map { LookupElementBuilder.create("${it.value}=") }) |
| 173 | + } |
| 174 | + val named = map.named.filter { !hashNames.contains(it) }.map { LookupElementBuilder.create("$it=") } |
| 175 | + result.addAll(named.map { PrioritizedLookupElement.withPriority(it, 100.0) }) |
| 176 | + } |
| 177 | + |
| 178 | + if (currentPosition >= 0) { |
| 179 | + map.positionalOptions.getOrDefault(currentPosition, null) |
| 180 | + ?.map { isLiteral.ifTrue { it.replace("'", "").replace("\"", "") } ?: it } |
| 181 | + ?.map { LookupElementBuilder.create(it) }?.toCollection(result) |
| 182 | + } |
170 | 183 | } |
171 | 184 |
|
172 | 185 | fun addImportPathCompletions(element: PsiElement, result: CompletionResultSet) { |
@@ -253,7 +266,7 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() { |
253 | 266 | val children = PsiTreeUtil.collectElements(f) { it is JSVariable || it is ES6ImportDeclaration } |
254 | 267 | children.forEach { |
255 | 268 | if (it is JSVariable) { |
256 | | - val useScope = JSUseScopeProvider.getUseScopeElement(it) |
| 269 | + val useScope = JSUseScopeProvider.getBlockScopeElement(it) |
257 | 270 | if (useScope.isAncestor(tpl)) { |
258 | 271 | result.addElement(LookupElementBuilder.create(it.name!!)) |
259 | 272 | } |
@@ -317,10 +330,13 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() { |
317 | 330 |
|
318 | 331 | val helperElement = EmberUtils.findFirstHbsParamFromParam(element) |
319 | 332 | if (helperElement != null && parameters.position.parent.prevSibling.elementType != HbTokenTypes.SEP) { |
320 | | - addHelperCompletions(helperElement, result) |
| 333 | + val params = helperElement.parent.children.filter { it is HbParam } |
| 334 | + val currentParam = params.find { it.textRange.contains(element.textRange) } |
| 335 | + val pos = params.indexOf(currentParam) |
| 336 | + addHelperCompletions(helperElement, result, pos) |
321 | 337 | val r = EmberUtils.handleEmberHelpers(helperElement.parent) |
322 | 338 | if (r != null) { |
323 | | - addHelperCompletions(r.children[0].children[0], result) |
| 339 | + addHelperCompletions(r.children[0].children[0], result, pos) |
324 | 340 | } |
325 | 341 | } |
326 | 342 |
|
|
0 commit comments