Search in sources :

Example 1 with YAMLScalar

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

the class YAMLScalarConversionTest method doTest.

private void doTest(Class<? extends YAMLScalar>... unsupportedClasses) {
    final PsiFile file = myFixture.configureByFile("sampleDocument.yml");
    Collection<YAMLScalar> scalars = PsiTreeUtil.collectElementsOfType(file, YAMLScalar.class);
    assertEquals(5, scalars.size());
    final String text;
    try {
        text = FileUtil.loadFile(new File(getTestDataPath() + getTestName(true) + ".txt"), true);
    } catch (IOException e) {
        fail(e.toString());
        return;
    }
    for (YAMLScalar scalar : scalars) {
        boolean isUnsupported = ((Computable<Boolean>) () -> {
            for (Class<? extends YAMLScalar> aClass : unsupportedClasses) {
                if (aClass == scalar.getClass()) {
                    return true;
                }
            }
            return false;
        }).compute();
        final ElementManipulator<YAMLScalar> manipulator = ElementManipulators.getManipulator(scalar);
        assertNotNull(manipulator);
        WriteCommandAction.runWriteCommandAction(getProject(), () -> {
            final YAMLScalar newElement = manipulator.handleContentChange(scalar, text);
            assertEquals(isUnsupported + ";" + newElement.getClass() + ";" + scalar.getClass(), isUnsupported, newElement.getClass() != scalar.getClass());
            assertEquals("Failed at " + scalar.getClass() + " (became " + newElement.getClass() + "): ", text, newElement.getTextValue());
        });
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) YAMLScalar(org.jetbrains.yaml.psi.YAMLScalar) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Computable(com.intellij.openapi.util.Computable)

Example 2 with YAMLScalar

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

YAMLScalar (org.jetbrains.yaml.psi.YAMLScalar)2 Computable (com.intellij.openapi.util.Computable)1 Pair (com.intellij.openapi.util.Pair)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 File (java.io.File)1 IOException (java.io.IOException)1 YAMLFile (org.jetbrains.yaml.psi.YAMLFile)1