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