Search in sources :

Example 41 with PsiWhiteSpace

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

the class UpdateKotlinCopyright method scanFile.

@Override
protected void scanFile() {
    PsiElement first = getFile().getFirstChild();
    PsiElement last = first;
    PsiElement next = first;
    while (next != null) {
        if (next instanceof PsiComment || next instanceof PsiWhiteSpace) {
            next = getNextSibling(next);
        } else {
            break;
        }
        last = next;
    }
    if (first != null) {
        checkComments(first, last, true);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 42 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project idea-handlebars by dmarcotte.

the class HbBlockMismatchFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null || !psiElement.isValid())
        return;
    if (!FileModificationService.getInstance().prepareFileForWrite(psiElement.getContainingFile()))
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    HbBlockMustache blockMustache = (HbBlockMustache) PsiTreeUtil.findFirstParent(psiElement, true, new Condition<PsiElement>() {

        @Override
        public boolean value(PsiElement psiElement) {
            return psiElement instanceof HbBlockMustache;
        }
    });
    if (blockMustache == null) {
        return;
    }
    HbBlockMustache targetBlockMustache = blockMustache;
    // ensure we update the open or close mustache for this block appropriately
    if (myUpdateOpenMustache != (targetBlockMustache instanceof HbOpenBlockMustache)) {
        targetBlockMustache = blockMustache.getPairedElement();
    }
    HbPath path = PsiTreeUtil.findChildOfType(targetBlockMustache, HbPath.class);
    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (path != null && document != null) {
        final TextRange textRange = path.getTextRange();
        document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myCorrectedName);
    }
}
Also used : Condition(com.intellij.openapi.util.Condition) HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbPath(com.dmarcotte.handlebars.psi.HbPath) HbBlockMustache(com.dmarcotte.handlebars.psi.HbBlockMustache) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 43 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project idea-handlebars by dmarcotte.

the class HbErrorFilter method hasWhitespacesInHtmlBetweenErrorAndOpenTokens.

private static boolean hasWhitespacesInHtmlBetweenErrorAndOpenTokens(int offset, TemplateLanguageFileViewProvider viewProvider) {
    PsiElement at = viewProvider.findElementAt(offset, viewProvider.getTemplateDataLanguage());
    if (!(at instanceof PsiWhiteSpace))
        return false;
    PsiElement elementAt = viewProvider.findElementAt(at.getTextRange().getEndOffset() + 1, viewProvider.getBaseLanguage());
    if (elementAt != null && START_TEMPLATE_TOKENS.contains(elementAt.getNode().getElementType()))
        return true;
    return false;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 44 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoMethodSeparatorProvider method findAnchorElement.

@NotNull
private static PsiElement findAnchorElement(@NotNull GoTopLevelDeclaration o) {
    PsiElement result = o;
    PsiElement p = o;
    while ((p = p.getPrevSibling()) != null) {
        if (p instanceof PsiComment) {
            result = p;
        } else if (p instanceof PsiWhiteSpace) {
            if (p.getText().contains("\n\n"))
                return result;
        } else {
            break;
        }
    }
    return result;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with PsiWhiteSpace

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

the class SimpleSurroundDescriptor method getElementsToSurround.

@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    // adjust start/end
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 instanceof PsiWhiteSpace) {
        startOffset = element1.getTextRange().getEndOffset();
    }
    if (element2 instanceof PsiWhiteSpace) {
        endOffset = element2.getTextRange().getStartOffset();
    }
    RegExpElement pattern = findElementAtStrict(file, startOffset, endOffset, RegExpPattern.class);
    if (pattern != null)
        return new RegExpElement[] { pattern };
    RegExpElement branch = findElementAtStrict(file, startOffset, endOffset, RegExpBranch.class);
    if (branch != null)
        return new RegExpElement[] { branch };
    List<PsiElement> atoms = new ArrayList<>();
    RegExpAtom atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class);
    for (; atom != null; atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class)) {
        atoms.add(atom);
        startOffset = atom.getTextRange().getEndOffset();
        // handle embedded whitespace
        if ((element1 = file.findElementAt(startOffset)) instanceof PsiWhiteSpace) {
            startOffset = element1.getTextRange().getEndOffset();
            atoms.add(element1);
        }
    }
    if (startOffset == endOffset && atoms.size() > 0) {
        PsiElement[] elements = PsiUtilCore.toPsiElementArray(atoms);
        if ((atoms.size() == 1 || PsiTreeUtil.findCommonParent(elements) == elements[0].getParent())) {
            return elements;
        }
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : RegExpElement(org.intellij.lang.regexp.psi.RegExpElement) RegExpAtom(org.intellij.lang.regexp.psi.RegExpAtom) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

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