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());
});
}
}
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);
}
}
Aggregations