Search in sources :

Example 16 with LeafPsiElement

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

the class GroovyBlockGenerator method getSpockTable.

private static List<LeafPsiElement> getSpockTable(GrStatement statement) {
    List<LeafPsiElement> result = new ArrayList<>();
    statement.accept(new GroovyElementVisitor() {

        @Override
        public void visitBinaryExpression(@NotNull GrBinaryExpression expression) {
            if (isTablePart(expression)) {
                result.add((LeafPsiElement) expression.getOperationToken());
                expression.acceptChildren(this);
            }
        }
    });
    result.sort((l1, l2) -> l1.getStartOffset() - l2.getStartOffset());
    return result;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) ArrayList(java.util.ArrayList)

Example 17 with LeafPsiElement

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

the class GroovyBlockGenerator method alignSpockTable.

private void alignSpockTable(List<GrStatement> group) {
    if (group.size() < 2) {
        return;
    }
    GrStatement inner = group.get(0);
    boolean embedded = inner != null && isTablePart(inner);
    GrStatement first = embedded ? inner : group.get(1);
    List<AlignmentProvider.Aligner> alignments = ContainerUtil.map2List(getSpockTable(first), leaf -> myAlignmentProvider.createAligner(leaf, true, Alignment.Anchor.RIGHT));
    int second = embedded ? 1 : 2;
    for (int i = second; i < group.size(); i++) {
        List<LeafPsiElement> table = getSpockTable(group.get(i));
        for (int j = 0; j < Math.min(table.size(), alignments.size()); j++) {
            alignments.get(j).append(table.get(j));
        }
    }
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 18 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 19 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 20 with LeafPsiElement

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

the class AngularJSReferencesContributor method uiViewPattern.

private static PsiElementPattern.Capture<PsiElement> uiViewPattern() {
    return PlatformPatterns.psiElement(PsiElement.class).and(new FilterPattern(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, @Nullable PsiElement context) {
            if (!(element instanceof PsiElement))
                return false;
            if (element instanceof JSLiteralExpression || element instanceof LeafPsiElement && ((LeafPsiElement) element).getNode().getElementType() == JSTokenTypes.STRING_LITERAL) {
                if (!(((PsiElement) element).getParent() instanceof JSProperty))
                    return false;
                // started typing property, variant
                PsiElement current = moveUpChain((PsiElement) element, JSLiteralExpression.class, JSReferenceExpression.class, JSProperty.class);
                if (!(current instanceof JSProperty) || !acceptablePropertyValue((JSProperty) current))
                    return false;
                current = current.getParent();
                if (current != null && checkParentViewsObject(current))
                    return AngularIndexUtil.hasAngularJS(current.getProject());
            }
            return false;
        }

        private boolean acceptablePropertyValue(JSProperty element) {
            return element.getNameIdentifier() != null && StringUtil.isQuotedString(element.getNameIdentifier().getText()) && (element.getValue() instanceof JSObjectLiteralExpression || element.getValue() instanceof JSReferenceExpression || element.getValue() == null);
        }

        private PsiElement moveUpChain(@Nullable final PsiElement element, @NotNull final Class<? extends PsiElement>... clazz) {
            PsiElement current = element;
            for (Class<? extends PsiElement> aClass : clazz) {
                current = current != null && aClass.isInstance(current.getParent()) ? current.getParent() : current;
            }
            return current;
        }

        private boolean checkParentViewsObject(final PsiElement mustBeObject) {
            if (mustBeObject instanceof JSObjectLiteralExpression) {
                final PsiElement viewsProperty = mustBeObject.getParent();
                if (viewsProperty instanceof JSProperty && "views".equals(((JSProperty) viewsProperty).getName())) {
                    // by now will not go further todo other cases
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    }));
}
Also used : NotNull(org.jetbrains.annotations.NotNull) FilterPattern(com.intellij.psi.filters.position.FilterPattern) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) ElementFilter(com.intellij.psi.filters.ElementFilter) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)25 PsiElement (com.intellij.psi.PsiElement)11 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)4 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)1 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 Document (com.intellij.openapi.editor.Document)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1