use of org.jetbrains.yaml.psi.YAMLValue 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.YAMLValue 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.YAMLValue in project intellij-plugins by JetBrains.
the class BasePathInfo method extractBasePathPair.
@Nullable
private static Pair<YAMLKeyValue, DocumentFragment> extractBasePathPair(@NotNull YAMLDocument yamlDocument) {
final Ref<Pair<YAMLKeyValue, DocumentFragment>> result = Ref.create(null);
final YAMLValue value = yamlDocument.getTopLevelValue();
if (value instanceof YAMLMapping) {
for (YAMLKeyValue keyValue : ((YAMLMapping) value).getKeyValues()) {
if (keyValue != null && isBasePathKey(keyValue) && result.isNull()) {
DocumentFragment valueFragment = JstdConfigFileUtils.extractValueAsDocumentFragment(keyValue);
result.set(Pair.create(keyValue, valueFragment));
}
}
}
return result.get();
}
Aggregations