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