Search in sources :

Example 1 with LeafPsiElement

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

the class MavenPluginParamInfo method isSimpleText.

public static boolean isSimpleText(@NotNull XmlText paramValue) {
    PsiElement prevSibling = paramValue.getPrevSibling();
    if (!(prevSibling instanceof LeafPsiElement) || ((LeafPsiElement) prevSibling).getElementType() != XmlTokenType.XML_TAG_END) {
        return false;
    }
    PsiElement nextSibling = paramValue.getNextSibling();
    if (!(nextSibling instanceof LeafPsiElement) || ((LeafPsiElement) nextSibling).getElementType() != XmlTokenType.XML_END_TAG_START) {
        return false;
    }
    return true;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 2 with LeafPsiElement

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

the class StatementEffectFunctionCallQuickFix method replaceExec.

private static void replaceExec(@NotNull final PsiElement expression) {
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(expression.getProject());
    final String expressionText = expression.getText();
    final StringBuilder stringBuilder = new StringBuilder(expressionText + " (");
    final PsiElement next = getNextElement(expression);
    if (next == null) {
        stringBuilder.append(")");
        expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpression.class, stringBuilder.toString()));
        return;
    }
    final String commentText = getComment(next);
    if (next instanceof PyExpressionStatement) {
        final PyExpression expr = ((PyExpressionStatement) next).getExpression();
        if (expr instanceof PyBinaryExpression) {
            final PsiElement operator = ((PyBinaryExpression) expr).getPsiOperator();
            if (operator instanceof LeafPsiElement && ((LeafPsiElement) operator).getElementType() == PyTokenTypes.IN_KEYWORD) {
                addInArguments(stringBuilder, (PyBinaryExpression) expr);
            } else {
                stringBuilder.append(next.getText());
            }
        } else if (expr instanceof PyTupleExpression) {
            final PyExpression[] elements = ((PyTupleExpression) expr).getElements();
            if (elements.length > 1) {
                if (elements[0] instanceof PyBinaryExpression) {
                    addInArguments(stringBuilder, (PyBinaryExpression) elements[0]);
                }
                stringBuilder.append(", ");
                stringBuilder.append(elements[1].getText());
            }
        } else {
            stringBuilder.append(((PyExpressionStatement) next).getExpression().getText());
        }
    } else {
        stringBuilder.append(next.getText());
    }
    next.delete();
    stringBuilder.append(")");
    if (commentText != null) {
        stringBuilder.append(commentText);
    }
    expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpression.class, stringBuilder.toString()));
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 3 with LeafPsiElement

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

the class ShuffleNamesAction method shuffleIds.

private static boolean shuffleIds(PsiFile file, Editor editor) {
    final Map<String, String> map = new THashMap<>();
    final StringBuilder sb = new StringBuilder();
    final StringBuilder quote = new StringBuilder();
    final ArrayList<String> split = new ArrayList<>(100);
    file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof LeafPsiElement) {
                String type = ((LeafPsiElement) element).getElementType().toString();
                String text = element.getText();
                if (text.isEmpty())
                    return;
                for (int i = 0, len = text.length(); i < len / 2; i++) {
                    char c = text.charAt(i);
                    if (c == text.charAt(len - i - 1) && !Character.isLetter(c)) {
                        quote.append(c);
                    } else
                        break;
                }
                boolean isQuoted = quote.length() > 0;
                boolean isNumber = false;
                if (isQuoted || type.equals("ID") || type.contains("IDENT") && !"ts".equals(text) || (isNumber = text.matches("[0-9]+"))) {
                    String replacement = map.get(text);
                    if (replacement == null) {
                        split.addAll(Arrays.asList((isQuoted ? text.substring(quote.length(), text.length() - quote.length()).replace("''", "") : text).split("")));
                        if (!isNumber) {
                            for (ListIterator<String> it = split.listIterator(); it.hasNext(); ) {
                                String s = it.next();
                                if (s.isEmpty()) {
                                    it.remove();
                                    continue;
                                }
                                int c = s.charAt(0);
                                int cap = c & 32;
                                c &= ~cap;
                                c = (char) ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 7) % 26 + 'A') : c) | cap;
                                it.set(String.valueOf((char) c));
                            }
                        }
                        Collections.shuffle(split);
                        if (isNumber && "0".equals(split.get(0))) {
                            split.set(0, "1");
                        }
                        replacement = StringUtil.join(split, "");
                        if (isQuoted) {
                            replacement = quote + replacement + quote.reverse();
                        }
                        map.put(text, replacement);
                    }
                    text = replacement;
                }
                sb.append(text);
                quote.setLength(0);
                split.clear();
            }
            super.visitElement(element);
        }
    });
    editor.getDocument().setText(sb.toString());
    return true;
}
Also used : THashMap(gnu.trove.THashMap) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with LeafPsiElement

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

the class GroovyFoldingBuilder method processImports.

private static void processImports(final List<FoldingDescriptor> descriptors, GrImportStatement[] imports) {
    if (imports.length < 2)
        return;
    PsiElement first = imports[0];
    while (first != null) {
        PsiElement marker = first;
        PsiElement next = first.getNextSibling();
        while (next instanceof GrImportStatement || next instanceof LeafPsiElement) {
            if (next instanceof GrImportStatement)
                marker = next;
            next = next.getNextSibling();
        }
        if (marker != first) {
            int start = first.getTextRange().getStartOffset();
            int end = marker.getTextRange().getEndOffset();
            int tail = "import ".length();
            if (start + tail < end && !JavaFoldingBuilderBase.hasErrorElementsNearby(first.getContainingFile(), start, end)) {
                FoldingDescriptor descriptor = new FoldingDescriptor(first.getNode(), new TextRange(start + tail, end));
                // imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
                descriptor.setCanBeRemovedWhenCollapsed(true);
                descriptors.add(descriptor);
            }
        }
        while (!(next instanceof GrImportStatement) && next != null) next = next.getNextSibling();
        first = next;
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 5 with LeafPsiElement

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

the class GrVariableBaseImpl method delete.

@Override
public void delete() throws IncorrectOperationException {
    PsiElement parent = getParent();
    PsiElement prev = PsiUtil.getPrevNonSpace(this);
    PsiElement next = PsiUtil.getNextNonSpace(this);
    ASTNode parentNode = parent.getNode();
    assert parentNode != null;
    super.delete();
    if (prev != null && prev.getNode() != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
        prev.delete();
    } else if (next instanceof LeafPsiElement && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
        next.delete();
    }
    if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getVariables().length == 0) {
        parent.delete();
    }
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) ASTNode(com.intellij.lang.ASTNode) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

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