Search in sources :

Example 1 with YAMLFile

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

the class YAMLHashImpl method addNewKey.

@Override
protected void addNewKey(@NotNull YAMLKeyValue key) {
    PsiElement anchor = null;
    for (PsiElement child = getLastChild(); child != null; child = child.getPrevSibling()) {
        final IElementType type = child.getNode().getElementType();
        if (type == YAMLTokenTypes.COMMA || type == YAMLTokenTypes.LBRACE) {
            anchor = child;
        }
    }
    addAfter(key, anchor);
    final YAMLFile dummyFile = YAMLElementGenerator.getInstance(getProject()).createDummyYamlWithText("{,}");
    final PsiElement comma = dummyFile.findElementAt(1);
    assert comma != null && comma.getNode().getElementType() == YAMLTokenTypes.COMMA;
    addAfter(comma, key);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) YAMLFile(org.jetbrains.yaml.psi.YAMLFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with YAMLFile

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

the class YAMLElementGenerator method createColon.

@NotNull
public PsiElement createColon() {
    final YAMLFile file = createDummyYamlWithText("? foo : bar");
    final PsiElement at = file.findElementAt("? foo ".length());
    assert at != null && at.getNode().getElementType() == YAMLTokenTypes.COLON;
    return at;
}
Also used : YAMLFile(org.jetbrains.yaml.psi.YAMLFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with YAMLFile

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

the class AddTestSectionAction method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    if (file instanceof YAMLFile && file.isValid()) {
        final YAMLFile yamlFile = (YAMLFile) file;
        ApplicationManager.getApplication().runWriteAction(() -> {
            PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
            Document document = manager.getDocument(yamlFile);
            int length = document.getTextLength();
            document.insertString(length, "\n" + "test:");
            manager.commitDocument(document);
        });
    }
}
Also used : YAMLFile(org.jetbrains.yaml.psi.YAMLFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 4 with YAMLFile

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

the class YAMLScalarElementManipulator method handleContentChange.

@Override
public YAMLScalarImpl handleContentChange(@NotNull YAMLScalarImpl element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    try {
        final List<Pair<TextRange, String>> encodeReplacements = element.getEncodeReplacements(newContent);
        final StringBuilder builder = new StringBuilder();
        final String oldText = element.getText();
        builder.append(oldText.subSequence(0, range.getStartOffset()));
        builder.append(YAMLScalarImpl.processReplacements(newContent, encodeReplacements));
        builder.append(oldText.subSequence(range.getEndOffset(), oldText.length()));
        final YAMLFile dummyYamlFile = YAMLElementGenerator.getInstance(element.getProject()).createDummyYamlWithText(builder.toString());
        final YAMLScalar newScalar = PsiTreeUtil.collectElementsOfType(dummyYamlFile, YAMLScalar.class).iterator().next();
        final PsiElement result = element.replace(newScalar);
        if (!(result instanceof YAMLScalarImpl)) {
            throw new AssertionError("Inserted YAML scalar, but it isn't a scalar after insertion :(");
        }
        return ((YAMLScalarImpl) result);
    } catch (IllegalArgumentException e) {
        final PsiElement newElement = element.replace(YAMLElementGenerator.getInstance(element.getProject()).createYamlDoubleQuotedString());
        if (!(newElement instanceof YAMLQuotedTextImpl)) {
            throw new AssertionError("Could not replace with dummy scalar");
        }
        return handleContentChange((YAMLScalarImpl) newElement, newContent);
    }
}
Also used : YAMLFile(org.jetbrains.yaml.psi.YAMLFile) YAMLScalar(org.jetbrains.yaml.psi.YAMLScalar) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Aggregations

YAMLFile (org.jetbrains.yaml.psi.YAMLFile)4 PsiElement (com.intellij.psi.PsiElement)3 Document (com.intellij.openapi.editor.Document)1 Pair (com.intellij.openapi.util.Pair)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1 YAMLScalar (org.jetbrains.yaml.psi.YAMLScalar)1