Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Completion for YAML tags, keywords and PHP constants; provide working…
… version
  • Loading branch information
Haehnchen committed Sep 1, 2019
commit 5b8033012ed9ef0d8dcbe79279cb43d32905ae4d
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
return super.invokeAutoPopup(position, typeChar);
}

// if (position instanceof LeafPsiElement) {
// if (((LeafPsiElement) position).getElementType() == YAMLTokenTypes.TAG && position.getText().startsWith("!")) {
// return super.invokeAutoPopup(position, typeChar);
// }
// }
return typeChar == '!';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.CompletionUtil;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.util.ProcessingContext;
import com.jetbrains.jsonSchema.ide.JsonSchemaService;
import com.jetbrains.jsonSchema.impl.JsonSchemaObject;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.jetbrains.annotations.NotNull;

Expand All @@ -21,69 +18,21 @@
* @author Thomas Schulz <mail@king2500.net>
*/
public class YamlKeywordsCompletionProvider extends CompletionProvider<CompletionParameters> {

private static final String YAML_TYPE_NULL = "null";
private static final String YAML_TYPE_BOOL = "bool";
private static final String YAML_TYPE_DOUBLE = "double";

private static final String YAML_KEYWORD_TILDE = "~";
private static final String YAML_KEYWORD_NULL = "null";
private static final String YAML_KEYWORD_TRUE = "true";
private static final String YAML_KEYWORD_FALSE = "false";
private static final String YAML_KEYWORD_INF = ".inf";

private final static HashMap<String, String> yamlKeywords = new HashMap<String, String>() {{
put(YAML_KEYWORD_TILDE, YAML_TYPE_NULL);
put(YAML_KEYWORD_NULL, YAML_TYPE_NULL);
put(YAML_KEYWORD_TRUE, YAML_TYPE_BOOL);
put(YAML_KEYWORD_FALSE, YAML_TYPE_BOOL);
put(YAML_KEYWORD_INF, YAML_TYPE_DOUBLE);
private final static Map<String, String> YAML_KEYWORDS = new HashMap<String, String>() {{
put("~", "null");
put("null", "null");
put("true", "bool");
put("false", "bool");
put(".inf", "double");
}};

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {

PsiElement psiElement = parameters.getPosition();

Project project = psiElement.getProject();
final JsonSchemaService jsonSchemaService = JsonSchemaService.Impl.get(project);
JsonSchemaObject jsonRootSchema = jsonSchemaService.getSchemaObject(parameters.getOriginalFile().getVirtualFile());

//final List<JsonSchemaType> yamlTypes = new ArrayList<>();

if (jsonRootSchema != null) {
// for now we should not show any keywords, when a JSON Schema is active for this YAML file
return;

/* For 2019.2+
see https://github.com/JetBrains/intellij-community/commit/3fa4357b2fa419710c1819040aa561a53c886d2b

PsiElement position = parameters.getPosition();
JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(position, jsonRootSchema);
if (walker != null) {
final PsiElement checkable = walker.findElementToCheck(position);
final ThreeState isName = walker.isName(checkable);
final JsonPointerPosition jsonPointerPosition = walker.findPosition(checkable, isName == ThreeState.NO);
if (jsonPointerPosition != null) {
final Collection<JsonSchemaObject> schemas = new JsonSchemaResolver(project, jsonRootSchema, jsonPointerPosition).resolve();
schemas.forEach(schema -> {
if (isName == ThreeState.NO) {
if (schema.getTypeVariants() != null) {
yamlTypes.addAll(schema.getTypeVariants());
} else if (schema.getType() != null) {
yamlTypes.add(schema.getType());
}
}
});
}
}
*/
}


if (psiElement instanceof LeafPsiElement) {
// // Don't complete after tag (at end of line)
// // key: !my_tag <caret>\n
// Don't complete after tag (at end of line)
// key: !my_tag <caret>\n
if (YamlHelper.isElementAfterYamlTag(psiElement)) {
return;
}
Expand All @@ -100,36 +49,17 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
if (prefix.contains(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
prefix = prefix.substring(0, prefix.indexOf(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED));
}

result = result.withPrefixMatcher(prefix);
}

/*
boolean isYamlNullable = yamlTypes.contains(JsonSchemaType._null);
boolean isYamlNumber = yamlTypes.contains(JsonSchemaType._number) || yamlTypes.contains(JsonSchemaType._integer);
boolean elementHasType = yamlTypes.size() > 0;
*/

for (Map.Entry<String, String> entry : yamlKeywords.entrySet()) {
for (Map.Entry<String, String> entry : YAML_KEYWORDS.entrySet()) {
String yamlKeyword = entry.getKey();
String yamlType = entry.getValue();

/*if (elementHasType) {
if (YAML_KEYWORD_TILDE.equals(yamlKeyword) && !isYamlNullable) {
continue;
}
if (YAML_KEYWORD_NULL.equals(yamlKeyword) && isYamlNullable) {
continue;
}
if (YAML_TYPE_BOOL.equals(yamlType)) {
continue;
}
if (YAML_TYPE_DOUBLE.equals(yamlType) && !isYamlNumber) {
continue;
}
}*/

LookupElementBuilder lookupElement = LookupElementBuilder.create(yamlKeyword)
.withTypeText(yamlType);

result.addElement(lookupElement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,27 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi

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

lookupElement = lookupElement.withInsertHandler(InsertQuotesInsertHandler.getInstance());
}

result.addElement(lookupElement);
}
}

static class InsertSpaceInsertHandler implements InsertHandler<LookupElement> {
private static final InsertQuotesInsertHandler instance = new InsertQuotesInsertHandler();
private static final InsertSpaceInsertHandler instance = new InsertSpaceInsertHandler();

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

public static InsertQuotesInsertHandler getInstance() {
public static InsertSpaceInsertHandler getInstance() {
return instance;
}
}
Expand All @@ -197,7 +197,7 @@ static class InsertQuotesInsertHandler implements InsertHandler<LookupElement> {

@Override
public void handleInsert(InsertionContext context, @NotNull LookupElement lookupElement) {
EditorModificationUtil.insertStringAtCaret(context.getEditor(), "''", false, 1);
EditorModificationUtil.insertStringAtCaret(context.getEditor(), " ''", false, 1);
}

public static InsertQuotesInsertHandler getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,7 @@ public static boolean isElementAfterYamlTag(PsiElement psiElement) {
}
}

PsiElement tagElement = PsiTreeUtil.findSiblingBackward(psiElement, YAMLTokenTypes.TAG, null);

return tagElement != null;
return PsiTreeUtil.findSiblingBackward(psiElement, YAMLTokenTypes.TAG, null) != null;
}

/**
Expand Down