Search in sources :

Example 1 with YAMLDocument

use of org.jetbrains.yaml.psi.YAMLDocument 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);
    }
}
Also used : YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument) YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) YAMLValue(org.jetbrains.yaml.psi.YAMLValue) PsiElement(com.intellij.psi.PsiElement)

Example 2 with YAMLDocument

use of org.jetbrains.yaml.psi.YAMLDocument 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;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessingContext(com.intellij.util.ProcessingContext) YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument) YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

YAMLDocument (org.jetbrains.yaml.psi.YAMLDocument)2 YAMLKeyValue (org.jetbrains.yaml.psi.YAMLKeyValue)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 ProcessingContext (com.intellij.util.ProcessingContext)1 NotNull (org.jetbrains.annotations.NotNull)1 YAMLValue (org.jetbrains.yaml.psi.YAMLValue)1