Search in sources :

Example 46 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project android by JetBrains.

the class AndroidBaseXmlRefactoringAction method getExtractableRange.

@Nullable
private static Pair<PsiElement, PsiElement> getExtractableRange(PsiFile file, int start, int end) {
    PsiElement startElement = file.findElementAt(start);
    PsiElement parent = startElement != null ? startElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getStartOffset() == startElement.getTextRange().getStartOffset()) {
        startElement = parent;
        parent = parent.getParent();
    }
    PsiElement endElement = file.findElementAt(end - 1);
    parent = endElement != null ? endElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getEndOffset() == endElement.getTextRange().getEndOffset()) {
        endElement = parent;
        parent = parent.getParent();
    }
    if (startElement == null || endElement == null) {
        return null;
    }
    final PsiElement commonParent = startElement.getParent();
    if (commonParent == null || !(commonParent instanceof XmlTag) || commonParent != endElement.getParent()) {
        return null;
    }
    PsiElement e = startElement;
    boolean containTag = false;
    while (e != null) {
        if (!(e instanceof XmlText) && !(e instanceof XmlTag) && !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) {
            return null;
        }
        if (e instanceof XmlTag) {
            containTag = true;
        }
        if (e == endElement) {
            break;
        }
        e = e.getNextSibling();
    }
    return e != null && containTag ? Pair.create(startElement, endElement) : null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiFile(com.intellij.psi.PsiFile) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.

the class DartFoldingBuilder method foldFileHeader.

@Nullable
private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Document document) {
    PsiElement firstComment = dartFile.getFirstChild();
    if (firstComment instanceof PsiWhiteSpace)
        firstComment = firstComment.getNextSibling();
    if (!(firstComment instanceof PsiComment))
        return null;
    boolean containsCustomRegionMarker = false;
    PsiElement nextAfterComments = firstComment;
    while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) {
        containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments);
        nextAfterComments = nextAfterComments.getNextSibling();
    }
    if (nextAfterComments == null)
        return null;
    if (nextAfterComments instanceof DartLibraryStatement || nextAfterComments instanceof DartPartStatement || nextAfterComments instanceof DartPartOfStatement || nextAfterComments instanceof DartImportOrExportStatement) {
        if (nextAfterComments.getPrevSibling() instanceof PsiWhiteSpace)
            nextAfterComments = nextAfterComments.getPrevSibling();
        if (nextAfterComments.equals(firstComment))
            return null;
        final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextRange().getStartOffset(), nextAfterComments.getTextRange().getStartOffset());
        if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) {
            if (!containsCustomRegionMarker) {
                descriptors.add(new FoldingDescriptor(dartFile, fileHeaderCommentsRange));
            }
            return fileHeaderCommentsRange;
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.

the class DartFoldingBuilder method foldConsequentStatements.

private static <T extends PsiElement> void foldConsequentStatements(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Class<T> aClass) {
    final T firstStatement = PsiTreeUtil.getChildOfType(dartFile, aClass);
    if (firstStatement == null)
        return;
    PsiElement lastStatement = firstStatement;
    PsiElement nextElement = firstStatement;
    while (aClass.isInstance(nextElement) || nextElement instanceof PsiComment || nextElement instanceof PsiWhiteSpace) {
        if (aClass.isInstance(nextElement)) {
            lastStatement = nextElement;
        }
        nextElement = nextElement.getNextSibling();
    }
    if (lastStatement != firstStatement) {
        // after "import " or "export " or "part "
        final int startOffset = firstStatement.getTextRange().getStartOffset() + firstStatement.getFirstChild().getTextLength() + 1;
        final int endOffset = lastStatement.getTextRange().getEndOffset();
        final FoldingDescriptor descriptor = new FoldingDescriptor(firstStatement, TextRange.create(startOffset, endOffset));
        if (aClass == DartImportOrExportStatement.class) {
            // 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);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 49 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.

the class DartTypeHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
    myAfterTypeOrComponentName = false;
    myAfterDollarInStringInterpolation = false;
    if (fileType != DartFileType.INSTANCE)
        return Result.CONTINUE;
    if (c == '<' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
        TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
        myAfterTypeOrComponentName = isAfterTypeOrComponentName(file, editor.getCaretModel().getOffset());
        return Result.CONTINUE;
    }
    if (c == '>' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
        if (handleDartGT(editor, DartTokenTypes.LT, DartTokenTypes.GT, INVALID_INSIDE_REFERENCE)) {
            return Result.STOP;
        }
        return Result.CONTINUE;
    }
    if (c == '{') {
        TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
        final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
        if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isAfterDollarInStringInterpolation(element)) {
            myAfterDollarInStringInterpolation = true;
            return Result.CONTINUE;
        }
        PsiElement nextLeaf = element == null ? null : PsiTreeUtil.nextLeaf(element);
        if (nextLeaf instanceof PsiWhiteSpace) {
            nextLeaf = PsiTreeUtil.nextLeaf(nextLeaf);
        }
        if (PsiTreeUtil.getParentOfType(element, DartLazyParseableBlock.class, false) != null || nextLeaf != null && nextLeaf.getText().equals("=>")) {
            // Use case 1: manually wrapping code with {} in 'if', 'while', etc (if (a) <caret>return;). Closing '}' will be auto-inserted on Enter.
            // Use case 2: manual transformation of arrow block to standard (foo()<caret> => 499;)
            EditorModificationUtil.insertStringAtCaret(editor, "{");
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 50 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.

the class DartEnterInDocLineCommentHandler method preprocessEnter.

// EnterInLineCommentHandler doesn't work well enough for Dart doc comments
@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffsetRef, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
    if (file.getLanguage() != DartLanguage.INSTANCE && !HtmlUtil.isHtmlFile(file))
        return Result.Continue;
    final int caretOffset = caretOffsetRef.get().intValue();
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
    final PsiElement psiAtOffset = file.findElementAt(caretOffset);
    final PsiElement probablyDocComment = psiAtOffset instanceof PsiWhiteSpace && psiAtOffset.getText().startsWith("\n") ? psiAtOffset.getPrevSibling() : psiAtOffset == null && caretOffset > 0 && caretOffset == document.getTextLength() ? file.findElementAt(caretOffset - 1) : psiAtOffset;
    if (probablyDocComment != null && probablyDocComment.getTextRange().getStartOffset() < caretOffset && probablyDocComment.getNode().getElementType() == DartTokenTypesSets.SINGLE_LINE_DOC_COMMENT) {
        final CharSequence text = document.getCharsSequence();
        final int offset = CharArrayUtil.shiftForward(text, caretOffset, " \t");
        if (StringUtil.startsWith(text, offset, DartDocUtil.SINGLE_LINE_DOC_COMMENT)) {
            caretOffsetRef.set(offset);
        } else {
            final String docText = StringUtil.trimStart(probablyDocComment.getText(), DartDocUtil.SINGLE_LINE_DOC_COMMENT);
            final int spacesBeforeText = StringUtil.isEmptyOrSpaces(docText) ? 1 : StringUtil.countChars(docText, ' ', 0, true);
            final int spacesToAdd = Math.max(0, spacesBeforeText - StringUtil.countChars(text, ' ', caretOffset, true));
            document.insertString(caretOffset, DartDocUtil.SINGLE_LINE_DOC_COMMENT + StringUtil.repeatSymbol(' ', spacesToAdd));
            caretAdvance.set(spacesBeforeText);
        }
        return Result.Default;
    }
    return Result.Continue;
}
Also used : Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)90 PsiElement (com.intellij.psi.PsiElement)82 TextRange (com.intellij.openapi.util.TextRange)23 ASTNode (com.intellij.lang.ASTNode)15 PsiFile (com.intellij.psi.PsiFile)15 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 PsiComment (com.intellij.psi.PsiComment)13 IElementType (com.intellij.psi.tree.IElementType)13 Document (com.intellij.openapi.editor.Document)12 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 Editor (com.intellij.openapi.editor.Editor)5 Language (com.intellij.lang.Language)4 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)4 XmlTag (com.intellij.psi.xml.XmlTag)4 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)3 XmlFile (com.intellij.psi.xml.XmlFile)3