Search in sources :

Example 1 with YAMLKeyValue

use of org.jetbrains.yaml.psi.YAMLKeyValue 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 YAMLKeyValue

use of org.jetbrains.yaml.psi.YAMLKeyValue 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 YAMLKeyValue

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

the class YAMLElementGenerator method createSpace.

@NotNull
public PsiElement createSpace() {
    final YAMLKeyValue keyValue = createYamlKeyValue("foo", "bar");
    final ASTNode whitespaceNode = keyValue.getNode().findChildByType(TokenType.WHITE_SPACE);
    assert whitespaceNode != null;
    return whitespaceNode.getPsi();
}
Also used : YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with YAMLKeyValue

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

the class YAMLMappingModificationTest method doMappingTest.

private void doMappingTest(final String key, final String content) {
    myFixture.configureByFile(getTestName(true) + ".yml");
    final int offset = myFixture.getCaretOffset();
    final PsiElement elementAtCaret = myFixture.getFile().findElementAt(offset);
    final YAMLMapping mapping = PsiTreeUtil.getParentOfType(elementAtCaret, YAMLMapping.class, false);
    if (mapping == null) {
        WriteCommandAction.runWriteCommandAction(getProject(), () -> {
            YAMLUtil.createI18nRecord((YAMLFile) myFixture.getFile(), new String[] { key }, content);
        });
    } else {
        assertNotNull(mapping);
        //final int indent = YAMLUtil.getIndentInThisLine(mapping);
        //final String indentString = StringUtil.repeatSymbol(' ', indent);
        final YAMLKeyValue dummyKeyValue = YAMLElementGenerator.getInstance(myFixture.getProject()).createYamlKeyValue(key, content);
        assertNotNull(dummyKeyValue);
        WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> mapping.putKeyValue(dummyKeyValue));
    }
    assertSameLinesWithFile(getTestDataPath() + getTestName(true) + ".txt", myFixture.getFile().getText(), false);
}
Also used : YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) YAMLMapping(org.jetbrains.yaml.psi.YAMLMapping) PsiElement(com.intellij.psi.PsiElement)

Example 5 with YAMLKeyValue

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

the class JstdConfigFileReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(JstdConfigFileUtils.CONFIG_FILE_ELEMENT_PATTERN, new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            final YAMLKeyValue keyValue = ObjectUtils.tryCast(element, YAMLKeyValue.class);
            if (keyValue == null) {
                return PsiReference.EMPTY_ARRAY;
            }
            final YAMLDocument yamlDocument = ObjectUtils.tryCast(keyValue.getParent(), YAMLDocument.class);
            if (yamlDocument == null) {
                return PsiReference.EMPTY_ARRAY;
            }
            final BasePathInfo basePathInfo = new BasePathInfo(yamlDocument);
            if (BasePathInfo.isBasePathKey(keyValue)) {
                PsiReference basePathRef = createBasePathRef(basePathInfo);
                if (basePathRef != null) {
                    return new PsiReference[] { basePathRef };
                }
            } else if (JstdConfigFileUtils.isTopLevelKeyWithInnerFileSequence(keyValue)) {
                VirtualFile basePath = basePathInfo.getBasePath();
                if (basePath != null) {
                    List<PsiReference> references = Lists.newArrayList();
                    addReferencesForKeyValueWithInnerFileSequence(basePathInfo, keyValue, references);
                    return references.toArray(new PsiReference[references.size()]);
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessingContext(com.intellij.util.ProcessingContext) YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument) YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

YAMLKeyValue (org.jetbrains.yaml.psi.YAMLKeyValue)11 PsiElement (com.intellij.psi.PsiElement)5 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 YAMLValue (org.jetbrains.yaml.psi.YAMLValue)3 DocumentFragment (com.intellij.openapi.editor.DocumentFragment)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)2 HashSet (com.intellij.util.containers.HashSet)2 YAMLDocument (org.jetbrains.yaml.psi.YAMLDocument)2 YAMLMapping (org.jetbrains.yaml.psi.YAMLMapping)2 ASTNode (com.intellij.lang.ASTNode)1 Module (com.intellij.openapi.module.Module)1 Pair (com.intellij.openapi.util.Pair)1 ProcessingContext (com.intellij.util.ProcessingContext)1 DartSdk (com.jetbrains.lang.dart.sdk.DartSdk)1