Skip to content

Commit 5b80330

Browse files
committed
Completion for YAML tags, keywords and PHP constants; provide working version
1 parent c3f00f7 commit 5b80330

File tree

4 files changed

+19
-96
lines changed

4 files changed

+19
-96
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/yaml/YamlCompletionContributor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
4545
return super.invokeAutoPopup(position, typeChar);
4646
}
4747

48-
// if (position instanceof LeafPsiElement) {
49-
// if (((LeafPsiElement) position).getElementType() == YAMLTokenTypes.TAG && position.getText().startsWith("!")) {
50-
// return super.invokeAutoPopup(position, typeChar);
51-
// }
52-
// }
5348
return typeChar == '!';
5449
}
5550
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/completion/YamlKeywordsCompletionProvider.java

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import com.intellij.codeInsight.completion.CompletionResultSet;
66
import com.intellij.codeInsight.completion.CompletionUtil;
77
import com.intellij.codeInsight.lookup.LookupElementBuilder;
8-
import com.intellij.openapi.project.Project;
98
import com.intellij.psi.PsiElement;
109
import com.intellij.psi.impl.source.tree.LeafPsiElement;
1110
import com.intellij.util.ProcessingContext;
12-
import com.jetbrains.jsonSchema.ide.JsonSchemaService;
13-
import com.jetbrains.jsonSchema.impl.JsonSchemaObject;
1411
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1512
import org.jetbrains.annotations.NotNull;
1613

@@ -21,69 +18,21 @@
2118
* @author Thomas Schulz <mail@king2500.net>
2219
*/
2320
public class YamlKeywordsCompletionProvider extends CompletionProvider<CompletionParameters> {
24-
25-
private static final String YAML_TYPE_NULL = "null";
26-
private static final String YAML_TYPE_BOOL = "bool";
27-
private static final String YAML_TYPE_DOUBLE = "double";
28-
29-
private static final String YAML_KEYWORD_TILDE = "~";
30-
private static final String YAML_KEYWORD_NULL = "null";
31-
private static final String YAML_KEYWORD_TRUE = "true";
32-
private static final String YAML_KEYWORD_FALSE = "false";
33-
private static final String YAML_KEYWORD_INF = ".inf";
34-
35-
private final static HashMap<String, String> yamlKeywords = new HashMap<String, String>() {{
36-
put(YAML_KEYWORD_TILDE, YAML_TYPE_NULL);
37-
put(YAML_KEYWORD_NULL, YAML_TYPE_NULL);
38-
put(YAML_KEYWORD_TRUE, YAML_TYPE_BOOL);
39-
put(YAML_KEYWORD_FALSE, YAML_TYPE_BOOL);
40-
put(YAML_KEYWORD_INF, YAML_TYPE_DOUBLE);
21+
private final static Map<String, String> YAML_KEYWORDS = new HashMap<String, String>() {{
22+
put("~", "null");
23+
put("null", "null");
24+
put("true", "bool");
25+
put("false", "bool");
26+
put(".inf", "double");
4127
}};
4228

4329
@Override
4430
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
45-
4631
PsiElement psiElement = parameters.getPosition();
4732

48-
Project project = psiElement.getProject();
49-
final JsonSchemaService jsonSchemaService = JsonSchemaService.Impl.get(project);
50-
JsonSchemaObject jsonRootSchema = jsonSchemaService.getSchemaObject(parameters.getOriginalFile().getVirtualFile());
51-
52-
//final List<JsonSchemaType> yamlTypes = new ArrayList<>();
53-
54-
if (jsonRootSchema != null) {
55-
// for now we should not show any keywords, when a JSON Schema is active for this YAML file
56-
return;
57-
58-
/* For 2019.2+
59-
see https://github.com/JetBrains/intellij-community/commit/3fa4357b2fa419710c1819040aa561a53c886d2b
60-
61-
PsiElement position = parameters.getPosition();
62-
JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(position, jsonRootSchema);
63-
if (walker != null) {
64-
final PsiElement checkable = walker.findElementToCheck(position);
65-
final ThreeState isName = walker.isName(checkable);
66-
final JsonPointerPosition jsonPointerPosition = walker.findPosition(checkable, isName == ThreeState.NO);
67-
if (jsonPointerPosition != null) {
68-
final Collection<JsonSchemaObject> schemas = new JsonSchemaResolver(project, jsonRootSchema, jsonPointerPosition).resolve();
69-
schemas.forEach(schema -> {
70-
if (isName == ThreeState.NO) {
71-
if (schema.getTypeVariants() != null) {
72-
yamlTypes.addAll(schema.getTypeVariants());
73-
} else if (schema.getType() != null) {
74-
yamlTypes.add(schema.getType());
75-
}
76-
}
77-
});
78-
}
79-
}
80-
*/
81-
}
82-
83-
8433
if (psiElement instanceof LeafPsiElement) {
85-
// // Don't complete after tag (at end of line)
86-
// // key: !my_tag <caret>\n
34+
// Don't complete after tag (at end of line)
35+
// key: !my_tag <caret>\n
8736
if (YamlHelper.isElementAfterYamlTag(psiElement)) {
8837
return;
8938
}
@@ -100,36 +49,17 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
10049
if (prefix.contains(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
10150
prefix = prefix.substring(0, prefix.indexOf(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED));
10251
}
52+
10353
result = result.withPrefixMatcher(prefix);
10454
}
10555

106-
/*
107-
boolean isYamlNullable = yamlTypes.contains(JsonSchemaType._null);
108-
boolean isYamlNumber = yamlTypes.contains(JsonSchemaType._number) || yamlTypes.contains(JsonSchemaType._integer);
109-
boolean elementHasType = yamlTypes.size() > 0;
110-
*/
111-
112-
for (Map.Entry<String, String> entry : yamlKeywords.entrySet()) {
56+
for (Map.Entry<String, String> entry : YAML_KEYWORDS.entrySet()) {
11357
String yamlKeyword = entry.getKey();
11458
String yamlType = entry.getValue();
11559

116-
/*if (elementHasType) {
117-
if (YAML_KEYWORD_TILDE.equals(yamlKeyword) && !isYamlNullable) {
118-
continue;
119-
}
120-
if (YAML_KEYWORD_NULL.equals(yamlKeyword) && isYamlNullable) {
121-
continue;
122-
}
123-
if (YAML_TYPE_BOOL.equals(yamlType)) {
124-
continue;
125-
}
126-
if (YAML_TYPE_DOUBLE.equals(yamlType) && !isYamlNumber) {
127-
continue;
128-
}
129-
}*/
130-
13160
LookupElementBuilder lookupElement = LookupElementBuilder.create(yamlKeyword)
13261
.withTypeText(yamlType);
62+
13363
result.addElement(lookupElement);
13464
}
13565
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/completion/YamlTagCompletionProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,27 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
167167

168168
// Show !php/object only when invoking completion 2 times and we're not inside services
169169
// (see parse flags in comment above)
170-
if (tag.equals(TAG_PHP_OBJECT))
170+
if (tag.equals(TAG_PHP_OBJECT)) {
171171
if (!allTags || isServices) {
172172
continue;
173-
} else {
174-
// key: !php/object '<caret>'
175-
lookupElement = lookupElement.withInsertHandler(InsertQuotesInsertHandler.getInstance());
176173
}
177174

175+
lookupElement = lookupElement.withInsertHandler(InsertQuotesInsertHandler.getInstance());
176+
}
177+
178178
result.addElement(lookupElement);
179179
}
180180
}
181181

182182
static class InsertSpaceInsertHandler implements InsertHandler<LookupElement> {
183-
private static final InsertQuotesInsertHandler instance = new InsertQuotesInsertHandler();
183+
private static final InsertSpaceInsertHandler instance = new InsertSpaceInsertHandler();
184184

185185
@Override
186186
public void handleInsert(InsertionContext context, @NotNull LookupElement lookupElement) {
187187
EditorModificationUtil.insertStringAtCaret(context.getEditor(), " ", false, 1);
188188
}
189189

190-
public static InsertQuotesInsertHandler getInstance() {
190+
public static InsertSpaceInsertHandler getInstance() {
191191
return instance;
192192
}
193193
}
@@ -197,7 +197,7 @@ static class InsertQuotesInsertHandler implements InsertHandler<LookupElement> {
197197

198198
@Override
199199
public void handleInsert(InsertionContext context, @NotNull LookupElement lookupElement) {
200-
EditorModificationUtil.insertStringAtCaret(context.getEditor(), "''", false, 1);
200+
EditorModificationUtil.insertStringAtCaret(context.getEditor(), " ''", false, 1);
201201
}
202202

203203
public static InsertQuotesInsertHandler getInstance() {

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/yaml/YamlHelper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,9 +1263,7 @@ public static boolean isElementAfterYamlTag(PsiElement psiElement) {
12631263
}
12641264
}
12651265

1266-
PsiElement tagElement = PsiTreeUtil.findSiblingBackward(psiElement, YAMLTokenTypes.TAG, null);
1267-
1268-
return tagElement != null;
1266+
return PsiTreeUtil.findSiblingBackward(psiElement, YAMLTokenTypes.TAG, null) != null;
12691267
}
12701268

12711269
/**

0 commit comments

Comments
 (0)