Search in sources :

Example 81 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class GroovyLiteralCopyPasteProcessor method findLiteralTokenType.

@Override
@Nullable
protected PsiElement findLiteralTokenType(PsiFile file, int selectionStart, int selectionEnd) {
    PsiElement elementAtSelectionStart = file.findElementAt(selectionStart);
    if (elementAtSelectionStart == null) {
        return null;
    }
    IElementType elementType = elementAtSelectionStart.getNode().getElementType();
    if ((elementType == GroovyTokenTypes.mREGEX_END || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_END || elementType == GroovyTokenTypes.mGSTRING_END) && elementAtSelectionStart.getTextOffset() == selectionStart) {
        elementAtSelectionStart = elementAtSelectionStart.getPrevSibling();
        if (elementAtSelectionStart == null)
            return null;
        elementType = elementAtSelectionStart.getNode().getElementType();
    }
    if (elementType == GroovyTokenTypes.mDOLLAR) {
        elementAtSelectionStart = elementAtSelectionStart.getParent();
        elementType = elementAtSelectionStart.getNode().getElementType();
    }
    if (!isStringLiteral(elementAtSelectionStart)) {
        return null;
    }
    if (elementAtSelectionStart.getTextRange().getEndOffset() < selectionEnd) {
        final PsiElement elementAtSelectionEnd = file.findElementAt(selectionEnd);
        if (elementAtSelectionEnd == null) {
            return null;
        }
        if (elementAtSelectionEnd.getNode().getElementType() == elementType && elementAtSelectionEnd.getTextRange().getStartOffset() < selectionEnd) {
            return elementAtSelectionStart;
        }
    }
    final TextRange textRange = elementAtSelectionStart.getTextRange();
    //content elements don't have quotes, so they are shorter than whole string literals
    if (elementType == GroovyTokenTypes.mREGEX_CONTENT || elementType == GroovyTokenTypes.mGSTRING_CONTENT || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT || elementType == GroovyElementTypes.GSTRING_INJECTION) {
        selectionStart++;
        selectionEnd--;
    }
    if (textRange.getLength() > 0 && (selectionStart <= textRange.getStartOffset() || selectionEnd >= textRange.getEndOffset())) {
        return null;
    }
    if (elementType == GroovyElementTypes.GSTRING_CONTENT) {
        elementAtSelectionStart = elementAtSelectionStart.getFirstChild();
    }
    return elementAtSelectionStart;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 82 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class GroovyLiteralCopyPasteProcessor method escapeCharCharacters.

@NotNull
@Override
protected String escapeCharCharacters(@NotNull String s, @NotNull PsiElement token) {
    if (s.isEmpty())
        return s;
    IElementType tokenType = token.getNode().getElementType();
    if (tokenType == GroovyTokenTypes.mREGEX_CONTENT || tokenType == GroovyTokenTypes.mREGEX_LITERAL) {
        return GrStringUtil.escapeSymbolsForSlashyStrings(s);
    }
    if (tokenType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT || tokenType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        return GrStringUtil.escapeSymbolsForDollarSlashyStrings(s);
    }
    if (tokenType == GroovyTokenTypes.mGSTRING_CONTENT || tokenType == GroovyTokenTypes.mGSTRING_LITERAL || tokenType == GroovyElementTypes.GSTRING_INJECTION) {
        boolean singleLine = !GrStringUtil.findContainingLiteral(token).getText().contains("\"\"\"");
        StringBuilder b = new StringBuilder();
        GrStringUtil.escapeStringCharacters(s.length(), s, singleLine ? "\"" : "", singleLine, true, b);
        GrStringUtil.unescapeCharacters(b, singleLine ? "'" : "'\"", true);
        LOG.assertTrue(b.length() > 0, "s=" + s);
        for (int i = b.length() - 2; i >= 0; i--) {
            if (b.charAt(i) == '$') {
                final char next = b.charAt(i + 1);
                if (next != '{' && !Character.isLetter(next)) {
                    b.insert(i, '\\');
                }
            }
        }
        if (b.charAt(b.length() - 1) == '$') {
            b.insert(b.length() - 1, '\\');
        }
        return b.toString();
    }
    if (tokenType == GroovyTokenTypes.mSTRING_LITERAL) {
        return GrStringUtil.escapeSymbolsForString(s, !token.getText().contains("'''"), false);
    }
    return super.escapeCharCharacters(s, token);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NotNull(org.jetbrains.annotations.NotNull)

Example 83 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class GroovyLiteralSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    if (e instanceof GrListOrMap)
        return result;
    int startOffset = -1;
    int endOffset = -1;
    final String text = e.getText();
    final int stringOffset = e.getTextOffset();
    final IElementType elementType = e.getNode().getElementType();
    if (elementType == GroovyTokenTypes.mGSTRING_CONTENT || elementType == GroovyTokenTypes.mREGEX_CONTENT || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT) {
        int cur;
        int index = -1;
        while (true) {
            cur = text.indexOf('\n', index + 1);
            if (cur < 0 || cur + stringOffset > cursorOffset)
                break;
            index = cur;
        }
        if (index >= 0) {
            startOffset = stringOffset + index + 1;
        }
        index = text.indexOf('\n', cursorOffset - stringOffset);
        if (index >= 0) {
            endOffset = stringOffset + index + 1;
        }
    }
    if (startOffset >= 0 && endOffset >= 0) {
        result.add(new TextRange(startOffset, endOffset));
    }
    final String content = GrStringUtil.removeQuotes(text);
    final int offset = stringOffset + text.indexOf(content);
    result.add(new TextRange(offset, offset + content.length()));
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 84 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class StubsTest method doTest.

public void doTest() throws Exception {
    final List<String> data = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
    String fileText = data.get(0);
    PsiFile psiFile = TestUtils.createPseudoPhysicalGroovyFile(getProject(), fileText);
    ASTNode node = psiFile.getNode();
    Assert.assertNotNull(node);
    IElementType type = node.getElementType();
    Assert.assertTrue(type instanceof IStubFileElementType);
    IStubFileElementType stubFileType = (IStubFileElementType) type;
    StubBuilder builder = stubFileType.getBuilder();
    StubElement element = builder.buildStubTree(psiFile);
    StringBuffer buffer = new StringBuffer();
    getStubsTreeImpl(element, buffer, "");
    String stubTree = buffer.toString().trim();
    assertEquals(data.get(1), stubTree);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) StubElement(com.intellij.psi.stubs.StubElement) StubBuilder(com.intellij.psi.StubBuilder)

Example 85 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class PySplitIfIntention method isAvailable.

public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!(file instanceof PyFile)) {
        return false;
    }
    PsiElement elementAtOffset = file.findElementAt(editor.getCaretModel().getOffset());
    if (elementAtOffset == null || elementAtOffset.getNode() == null) {
        return false;
    }
    // PY-745
    final IElementType elementType = elementAtOffset.getNode().getElementType();
    if (elementType == PyTokenTypes.COLON) {
        elementAtOffset = elementAtOffset.getPrevSibling();
        elementAtOffset = PyPsiUtils.getPrevNonCommentSibling(elementAtOffset, false);
    } else if (elementType == PyTokenTypes.IF_KEYWORD) {
        elementAtOffset = elementAtOffset.getNextSibling();
        elementAtOffset = PyPsiUtils.getNextNonCommentSibling(elementAtOffset, false);
    }
    PsiElement element = PsiTreeUtil.getParentOfType(elementAtOffset, PyBinaryExpression.class, false);
    if (element == null) {
        return false;
    }
    while (element.getParent() instanceof PyBinaryExpression) {
        element = element.getParent();
    }
    if (((PyBinaryExpression) element).getOperator() != PyTokenTypes.AND_KEYWORD || ((PyBinaryExpression) element).getRightExpression() == null) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PyIfPart)) {
        return false;
    }
    setText(PyBundle.message("INTN.split.if.text"));
    return true;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)843 ASTNode (com.intellij.lang.ASTNode)127 PsiBuilder (com.intellij.lang.PsiBuilder)121 Nullable (org.jetbrains.annotations.Nullable)100 NotNull (org.jetbrains.annotations.NotNull)78 PsiElement (com.intellij.psi.PsiElement)77 TextRange (com.intellij.openapi.util.TextRange)43 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)26 ArrayList (java.util.ArrayList)22 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 Lexer (com.intellij.lexer.Lexer)17 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)15 NonNls (org.jetbrains.annotations.NonNls)14 Document (com.intellij.openapi.editor.Document)13 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)13 LighterASTNode (com.intellij.lang.LighterASTNode)12 BracePair (com.intellij.lang.BracePair)10 Project (com.intellij.openapi.project.Project)9 PsiFile (com.intellij.psi.PsiFile)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9