Search in sources :

Example 1 with YAMLValue

use of org.jetbrains.yaml.psi.YAMLValue in project intellij-community by JetBrains.

the class YamlKeyCompletionInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, T item) {
    final PsiElement currentElement = context.getFile().findElementAt(context.getStartOffset());
    assert currentElement != null : "no element at " + context.getStartOffset();
    final YAMLDocument holdingDocument = PsiTreeUtil.getParentOfType(currentElement, YAMLDocument.class);
    assert holdingDocument != null;
    final YAMLValue oldValue = deleteLookupTextAndRetrieveOldValue(context, currentElement);
    final YAMLKeyValue created = createNewEntry(holdingDocument, item);
    context.getEditor().getCaretModel().moveToOffset(created.getTextRange().getEndOffset());
    if (oldValue != null) {
        WriteCommandAction.runWriteCommandAction(context.getProject(), () -> created.setValue(oldValue));
    }
    PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
    if (!isCharAtCaret(context.getEditor(), ' ')) {
        EditorModificationUtil.insertStringAtCaret(context.getEditor(), " ");
    } else {
        context.getEditor().getCaretModel().moveCaretRelatively(1, 0, false, false, true);
    }
}
Also used : YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument) YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) YAMLValue(org.jetbrains.yaml.psi.YAMLValue) PsiElement(com.intellij.psi.PsiElement)

Example 2 with YAMLValue

use of org.jetbrains.yaml.psi.YAMLValue in project intellij-community by JetBrains.

the class YamlKeyCompletionInsertHandler method deleteLookupTextAndRetrieveOldValue.

@Nullable
protected YAMLValue deleteLookupTextAndRetrieveOldValue(InsertionContext context, @NotNull PsiElement elementAtCaret) {
    final YAMLValue oldValue;
    if (elementAtCaret.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) {
        deleteLookupPlain(context);
        return null;
    }
    final YAMLKeyValue keyValue = PsiTreeUtil.getParentOfType(elementAtCaret, YAMLKeyValue.class);
    assert keyValue != null;
    context.commitDocument();
    if (keyValue.getValue() != null) {
        // Save old value somewhere
        final YAMLKeyValue dummyKV = YAMLElementGenerator.getInstance(context.getProject()).createYamlKeyValue("foo", "b");
        dummyKV.setValue(keyValue.getValue());
        oldValue = dummyKV.getValue();
    } else {
        oldValue = null;
    }
    context.setTailOffset(keyValue.getTextRange().getEndOffset());
    WriteCommandAction.runWriteCommandAction(context.getProject(), () -> keyValue.getParentMapping().deleteKeyValue(keyValue));
    return oldValue;
}
Also used : YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) YAMLValue(org.jetbrains.yaml.psi.YAMLValue) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with YAMLValue

use of org.jetbrains.yaml.psi.YAMLValue in project intellij-plugins by JetBrains.

the class BasePathInfo method extractBasePathPair.

@Nullable
private static Pair<YAMLKeyValue, DocumentFragment> extractBasePathPair(@NotNull YAMLDocument yamlDocument) {
    final Ref<Pair<YAMLKeyValue, DocumentFragment>> result = Ref.create(null);
    final YAMLValue value = yamlDocument.getTopLevelValue();
    if (value instanceof YAMLMapping) {
        for (YAMLKeyValue keyValue : ((YAMLMapping) value).getKeyValues()) {
            if (keyValue != null && isBasePathKey(keyValue) && result.isNull()) {
                DocumentFragment valueFragment = JstdConfigFileUtils.extractValueAsDocumentFragment(keyValue);
                result.set(Pair.create(keyValue, valueFragment));
            }
        }
    }
    return result.get();
}
Also used : YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) YAMLValue(org.jetbrains.yaml.psi.YAMLValue) YAMLMapping(org.jetbrains.yaml.psi.YAMLMapping) DocumentFragment(com.intellij.openapi.editor.DocumentFragment) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

YAMLKeyValue (org.jetbrains.yaml.psi.YAMLKeyValue)3 YAMLValue (org.jetbrains.yaml.psi.YAMLValue)3 Nullable (org.jetbrains.annotations.Nullable)2 DocumentFragment (com.intellij.openapi.editor.DocumentFragment)1 Pair (com.intellij.openapi.util.Pair)1 PsiElement (com.intellij.psi.PsiElement)1 YAMLDocument (org.jetbrains.yaml.psi.YAMLDocument)1 YAMLMapping (org.jetbrains.yaml.psi.YAMLMapping)1