55import com .intellij .codeInsight .completion .CompletionResultSet ;
66import com .intellij .codeInsight .completion .CompletionUtil ;
77import com .intellij .codeInsight .lookup .LookupElementBuilder ;
8- import com .intellij .openapi .project .Project ;
98import com .intellij .psi .PsiElement ;
109import com .intellij .psi .impl .source .tree .LeafPsiElement ;
1110import com .intellij .util .ProcessingContext ;
12- import com .jetbrains .jsonSchema .ide .JsonSchemaService ;
13- import com .jetbrains .jsonSchema .impl .JsonSchemaObject ;
1411import fr .adrienbrault .idea .symfony2plugin .util .yaml .YamlHelper ;
1512import org .jetbrains .annotations .NotNull ;
1613
2118 * @author Thomas Schulz <mail@king2500.net>
2219 */
2320public 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 }
0 commit comments