Search in sources :

Example 36 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class PyElementGeneratorImpl method insertItemIntoListRemoveRedundantCommas.

@Override
@NotNull
public PsiElement insertItemIntoListRemoveRedundantCommas(@NotNull final PyElement list, @Nullable final PyExpression afterThis, @NotNull final PyExpression toInsert) {
    // TODO: #insertItemIntoList is probably buggy. In such case, fix it and get rid of this method
    final PsiElement result = insertItemIntoList(list, afterThis, toInsert);
    final LeafPsiElement[] leafs = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class);
    if (leafs != null) {
        final Deque<LeafPsiElement> commas = Queues.newArrayDeque(Collections2.filter(Arrays.asList(leafs), COMMAS_ONLY));
        if (!commas.isEmpty()) {
            final LeafPsiElement lastComma = commas.getLast();
            if (PsiTreeUtil.getNextSiblingOfType(lastComma, PyExpression.class) == null) {
                //Comma has no expression after it
                lastComma.delete();
            }
        }
    }
    return result;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class Xsd2InstanceUtils method processAndSaveAllSchemas.

public static String processAndSaveAllSchemas(@NotNull XmlFile file, @NotNull final Map<String, String> scannedToFileName, @NotNull final SchemaReferenceProcessor schemaReferenceProcessor) {
    final String fileName = file.getName();
    String previous = scannedToFileName.get(fileName);
    if (previous != null)
        return previous;
    scannedToFileName.put(fileName, fileName);
    final StringBuilder result = new StringBuilder();
    file.acceptChildren(new XmlRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement psiElement) {
            super.visitElement(psiElement);
            if (psiElement instanceof LeafPsiElement) {
                final String text = psiElement.getText();
                result.append(text);
            }
        }

        @Override
        public void visitXmlAttribute(XmlAttribute xmlAttribute) {
            boolean replaced = false;
            if (xmlAttribute.isNamespaceDeclaration()) {
                replaced = true;
                final String value = xmlAttribute.getValue();
                result.append(xmlAttribute.getText()).append(" ");
                if (!scannedToFileName.containsKey(value)) {
                    final XmlNSDescriptor nsDescriptor = xmlAttribute.getParent().getNSDescriptor(value, true);
                    if (nsDescriptor != null) {
                        processAndSaveAllSchemas(nsDescriptor.getDescriptorFile(), scannedToFileName, schemaReferenceProcessor);
                    }
                }
            } else if ("schemaLocation".equals(xmlAttribute.getName())) {
                final PsiReference[] references = xmlAttribute.getValueElement().getReferences();
                if (references.length > 0) {
                    PsiElement psiElement = references[0].resolve();
                    if (psiElement instanceof XmlFile) {
                        final String s = processAndSaveAllSchemas(((XmlFile) psiElement), scannedToFileName, schemaReferenceProcessor);
                        if (s != null) {
                            result.append(xmlAttribute.getName()).append("='").append(s).append('\'');
                            replaced = true;
                        }
                    }
                }
            }
            if (!replaced)
                result.append(xmlAttribute.getText());
        }
    });
    final VirtualFile virtualFile = file.getVirtualFile();
    final String content = result.toString();
    byte[] bytes;
    if (virtualFile != null) {
        bytes = content.getBytes(virtualFile.getCharset());
    } else {
        try {
            final String charsetName = XmlUtil.extractXmlEncodingFromProlog(content.getBytes());
            bytes = charsetName != null ? content.getBytes(charsetName) : content.getBytes();
        } catch (UnsupportedEncodingException e) {
            bytes = content.getBytes();
        }
    }
    schemaReferenceProcessor.processSchema(fileName, bytes);
    return fileName;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) PsiReference(com.intellij.psi.PsiReference) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 38 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-postfix-templates by xylo.

the class CptAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof LeafPsiElement) {
        final LeafPsiElement psiElement = (LeafPsiElement) element;
        if (psiElement.getElementType().equals(CptTypes.CLASS_NAME)) {
            final String className = element.getText();
            SupportedLanguages.getCptLang(element).ifPresent(lang -> {
                final CptLangAnnotator annotator = lang.getAnnotator();
                if (!annotator.isMatchingType(psiElement, className)) {
                    holder.createErrorAnnotation(element.getTextRange(), "Class not found");
                }
            });
        }
    }
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 39 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-elixir by KronicDeth.

the class Block method getSpacing.

/**
 * Returns a spacing object indicating what spaces and/or line breaks are added between two
 * specified children of this block.
 *
 * @param child1 the first child for which spacing is requested;
 *               <code>null</code> if given <code>'child2'</code> block is the first document block
 * @param child2 the second child for which spacing is requested.
 * @return the spacing instance, or null if no special spacing is required. If null is returned,
 * the formatter does not insert or delete spaces between the child blocks, but may insert
 * a line break if the line wraps at the position between the child blocks.
 * @see Spacing#createSpacing(int, int, int, boolean, int)
 * @see Spacing#getReadOnlySpacing()
 */
@Nullable
@Override
public Spacing getSpacing(@Nullable com.intellij.formatting.Block child1, @NotNull com.intellij.formatting.Block child2) {
    Spacing spacing = null;
    // Prevent `_ &&& &2` from becoming ` _ &&&& 2`, which has no meaning
    if (child1 instanceof ASTBlock && child2 instanceof ASTBlock) {
        ASTBlock child1ASTBlock = (ASTBlock) child1;
        ASTNode child1Node = child1ASTBlock.getNode();
        if (child1Node instanceof LeafPsiElement) {
            LeafPsiElement child1LeafPsiElement = (LeafPsiElement) child1Node;
            // capture (`&`) or and symbol operators (`&&` or `&&&`)
            if (child1LeafPsiElement.charAt(child1LeafPsiElement.getTextLength() - 1) == '&') {
                ASTBlock child2ASTBlock = (ASTBlock) child2;
                ASTNode firstLeafElementASTNode = child2ASTBlock.getNode().findLeafElementAt(0);
                if (firstLeafElementASTNode != null && firstLeafElementASTNode instanceof LeafPsiElement && ((LeafPsiElement) firstLeafElementASTNode).charAt(0) == '&') {
                    spacing = Spacing.createSpacing(1, 1, 0, true, 0);
                }
            }
        }
    }
    if (spacing == null) {
        spacing = spacingBuilder.getSpacing(this, child1, child2);
    }
    return spacing;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project Intellij-Plugin by getgauge.

the class SpecInspectionProviderTest method testGetElementReturnsSpecHeading.

@Test
public void testGetElementReturnsSpecHeading() throws Exception {
    PsiElement e = mock(PsiElement.class);
    LeafPsiElement leafPsiElement = mock(LeafPsiElement.class);
    when(leafPsiElement.getElementType()).thenReturn(SpecTokenTypes.SPEC_HEADING);
    when(e.getParent()).thenReturn(leafPsiElement);
    PsiElement element = new SpecInspectionProvider().getElement(e);
    assertEquals(leafPsiElement, element);
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Test(org.junit.Test)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)50 PsiElement (com.intellij.psi.PsiElement)29 Nullable (org.jetbrains.annotations.Nullable)8 IElementType (com.intellij.psi.tree.IElementType)7 Test (org.junit.Test)7 PsiFile (com.intellij.psi.PsiFile)6 TextRange (com.intellij.openapi.util.TextRange)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 NotNull (org.jetbrains.annotations.NotNull)5 Objects (com.google.common.base.Objects)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 PsiUtils (com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 BlazeRunConfigurationProducerTestCase (com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase)4 Info (com.intellij.execution.lineMarker.RunLineMarkerContributor.Info)4 AllIcons (com.intellij.icons.AllIcons)4 PsiReference (com.intellij.psi.PsiReference)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)3