Search in sources :

Example 31 with PsiWhiteSpace

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

the class ParenthesesInsertHandler method findNextToken.

@Nullable
protected PsiElement findNextToken(@NotNull InsertionContext context) {
    final PsiFile file = context.getFile();
    PsiElement element = file.findElementAt(context.getTailOffset());
    if (element instanceof PsiWhiteSpace) {
        if (!myAllowParametersOnNextLine && element.getText().contains("\n")) {
            return null;
        }
        element = file.findElementAt(element.getTextRange().getEndOffset());
    }
    return element;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with PsiWhiteSpace

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

the class ParameterInfoUtils method findParentOfTypeWithStopElements.

@SafeVarargs
@Nullable
public static <T extends PsiElement> T findParentOfTypeWithStopElements(PsiFile file, int offset, Class<T> parentClass, @NotNull Class<? extends PsiElement>... stopAt) {
    PsiElement element = file.findElementAt(offset);
    if (element == null)
        return null;
    T parentOfType = PsiTreeUtil.getParentOfType(element, parentClass, true, stopAt);
    if (element instanceof PsiWhiteSpace) {
        parentOfType = PsiTreeUtil.getParentOfType(PsiTreeUtil.prevLeaf(element), parentClass, true, stopAt);
    }
    return parentOfType;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with PsiWhiteSpace

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

the class ShowIntentionsPass method getActionsToShow.

public static void getActionsToShow(@NotNull final Editor hostEditor, @NotNull final PsiFile hostFile, @NotNull final IntentionsInfo intentions, int passIdToShowIntentionsFor) {
    final PsiElement psiElement = hostFile.findElementAt(hostEditor.getCaretModel().getOffset());
    LOG.assertTrue(psiElement == null || psiElement.isValid(), psiElement);
    int offset = hostEditor.getCaretModel().getOffset();
    final Project project = hostFile.getProject();
    List<HighlightInfo.IntentionActionDescriptor> fixes = getAvailableFixes(hostEditor, hostFile, passIdToShowIntentionsFor);
    final DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
    final Document hostDocument = hostEditor.getDocument();
    HighlightInfo infoAtCursor = ((DaemonCodeAnalyzerImpl) codeAnalyzer).findHighlightByOffset(hostDocument, offset, true);
    if (infoAtCursor == null) {
        intentions.errorFixesToShow.addAll(fixes);
    } else {
        final boolean isError = infoAtCursor.getSeverity() == HighlightSeverity.ERROR;
        for (HighlightInfo.IntentionActionDescriptor fix : fixes) {
            if (fix.isError() && isError) {
                intentions.errorFixesToShow.add(fix);
            } else {
                intentions.inspectionFixesToShow.add(fix);
            }
        }
    }
    for (final IntentionAction action : IntentionManager.getInstance().getAvailableIntentionActions()) {
        Pair<PsiFile, Editor> place = ShowIntentionActionsHandler.chooseBetweenHostAndInjected(hostFile, hostEditor, (psiFile, editor) -> ShowIntentionActionsHandler.availableFor(psiFile, editor, action));
        if (place != null) {
            List<IntentionAction> enableDisableIntentionAction = new ArrayList<>();
            enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action));
            enableDisableIntentionAction.add(new IntentionHintComponent.EditIntentionSettingsAction(action));
            HighlightInfo.IntentionActionDescriptor descriptor = new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null);
            if (!fixes.contains(descriptor)) {
                intentions.intentionsToShow.add(descriptor);
            }
        }
    }
    if (HighlightingLevelManager.getInstance(project).shouldInspect(hostFile)) {
        PsiElement intentionElement = psiElement;
        int intentionOffset = offset;
        if (psiElement instanceof PsiWhiteSpace && offset == psiElement.getTextRange().getStartOffset() && offset > 0) {
            final PsiElement prev = hostFile.findElementAt(offset - 1);
            if (prev != null && prev.isValid()) {
                intentionElement = prev;
                intentionOffset = offset - 1;
            }
        }
        if (intentionElement != null && intentionElement.getManager().isInProject(intentionElement)) {
            collectIntentionsFromDoNotShowLeveledInspections(project, hostFile, intentionElement, intentionOffset, intentions);
        }
    }
    final int line = hostDocument.getLineNumber(offset);
    MarkupModelEx model = (MarkupModelEx) DocumentMarkupModel.forDocument(hostDocument, project, true);
    List<RangeHighlighterEx> result = new ArrayList<>();
    Processor<RangeHighlighterEx> processor = Processors.cancelableCollectProcessor(result);
    model.processRangeHighlightersOverlappingWith(hostDocument.getLineStartOffset(line), hostDocument.getLineEndOffset(line), processor);
    GutterIntentionAction.addActions(hostEditor, intentions, project, result);
    boolean cleanup = appendCleanupCode(intentions.inspectionFixesToShow, hostFile);
    if (!cleanup) {
        appendCleanupCode(intentions.errorFixesToShow, hostFile);
    }
    EditorNotificationActions.collectDescriptorsForEditor(hostEditor, intentions.notificationActionsToShow);
    intentions.filterActions(hostFile);
}
Also used : ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) IntentionHintComponent(com.intellij.codeInsight.intention.impl.IntentionHintComponent) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Editor(com.intellij.openapi.editor.Editor) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 34 with PsiWhiteSpace

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

the class PsiBasedFormatterModelWithShiftIndentInside method replaceWithPsiInLeaf.

@Override
protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) {
    if (!myCanModifyAllWhiteSpaces) {
        if (leafElement.getElementType() == TokenType.WHITE_SPACE)
            return null;
        ASTNode prevNode = TreeUtil.prevLeaf(leafElement);
        if (prevNode != null) {
            IElementType type = prevNode.getElementType();
            if (type == TokenType.WHITE_SPACE) {
                final String text = prevNode.getText();
                @NonNls final String cdataStartMarker = "<![CDATA[";
                final int cdataPos = text.indexOf(cdataStartMarker);
                if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) {
                    whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, text, cdataPos);
                    if (whiteSpace == null)
                        return null;
                }
                prevNode = TreeUtil.prevLeaf(prevNode);
                type = prevNode != null ? prevNode.getElementType() : null;
            }
            @NonNls final String cdataEndMarker = "]]>";
            if (type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) {
                final ASTNode at = findElementAt(prevNode.getStartOffset());
                if (at != null && at.getPsi() instanceof PsiWhiteSpace) {
                    final String s = at.getText();
                    final int cdataEndPos = s.indexOf(cdataEndMarker);
                    whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos);
                    leafElement = at;
                } else {
                    whiteSpace = null;
                }
                if (whiteSpace == null)
                    return null;
            }
        }
    }
    FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange);
    return whiteSpace;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NonNls(org.jetbrains.annotations.NonNls) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 35 with PsiWhiteSpace

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

the class DuplocatorUtil method isIgnoredNode.

public static boolean isIgnoredNode(PsiElement element) {
    if (element instanceof PsiWhiteSpace || element instanceof PsiErrorElement || element instanceof PsiComment) {
        return true;
    }
    if (!(element instanceof LeafElement)) {
        return false;
    }
    if (CharArrayUtil.containsOnlyWhiteSpaces(element.getText())) {
        return true;
    }
    EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element);
    if (descriptorProvider == null) {
        return false;
    }
    final IElementType elementType = ((LeafElement) element).getElementType();
    return descriptorProvider.getIgnoredTokens().contains(elementType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiComment(com.intellij.psi.PsiComment) EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) 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