Search in sources :

Example 1 with PsiWhiteSpaceImpl

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

the class GenerateMembersUtil method positionCaret.

/**
   * @see GenerationInfo#positionCaret(Editor, boolean)
   */
public static void positionCaret(@NotNull Editor editor, @NotNull PsiElement firstMember, boolean toEditMethodBody) {
    LOG.assertTrue(firstMember.isValid());
    Project project = firstMember.getProject();
    if (toEditMethodBody) {
        PsiMethod method = (PsiMethod) firstMember;
        PsiCodeBlock body = method.getBody();
        if (body != null) {
            PsiElement firstBodyElement = body.getFirstBodyElement();
            PsiElement l = firstBodyElement;
            while (l instanceof PsiWhiteSpace) l = l.getNextSibling();
            if (l == null)
                l = body;
            PsiElement lastBodyElement = body.getLastBodyElement();
            PsiElement r = lastBodyElement;
            while (r instanceof PsiWhiteSpace) r = r.getPrevSibling();
            if (r == null)
                r = body;
            int start = l.getTextRange().getStartOffset();
            int end = r.getTextRange().getEndOffset();
            boolean adjustLineIndent = false;
            // body is whitespace
            if (start > end && firstBodyElement == lastBodyElement && firstBodyElement instanceof PsiWhiteSpaceImpl) {
                CharSequence chars = ((PsiWhiteSpaceImpl) firstBodyElement).getChars();
                if (chars.length() > 1 && chars.charAt(0) == '\n' && chars.charAt(1) == '\n') {
                    start = end = firstBodyElement.getTextRange().getStartOffset() + 1;
                    adjustLineIndent = true;
                }
            }
            editor.getCaretModel().moveToOffset(Math.min(start, end));
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
            if (start < end) {
                //Not an empty body
                editor.getSelectionModel().setSelection(start, end);
            } else if (adjustLineIndent) {
                Document document = editor.getDocument();
                RangeMarker marker = document.createRangeMarker(start, start);
                PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
                if (marker.isValid()) {
                    CodeStyleManager.getInstance(project).adjustLineIndent(document, marker.getStartOffset());
                }
            }
            return;
        }
    }
    int offset;
    if (firstMember instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) firstMember;
        PsiCodeBlock body = method.getBody();
        if (body == null) {
            offset = method.getTextRange().getStartOffset();
        } else {
            PsiJavaToken lBrace = body.getLBrace();
            assert lBrace != null : firstMember.getText();
            offset = lBrace.getTextRange().getEndOffset();
        }
    } else {
        offset = firstMember.getTextRange().getStartOffset();
    }
    editor.getCaretModel().moveToOffset(offset);
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
}
Also used : Project(com.intellij.openapi.project.Project) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) PsiWhiteSpaceImpl(com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl)

Example 2 with PsiWhiteSpaceImpl

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

the class ASTFactory method whitespace.

@NotNull
public static LeafElement whitespace(final CharSequence text) {
    final PsiWhiteSpaceImpl w = new PsiWhiteSpaceImpl(WHITESPACES.intern(text));
    CodeEditUtil.setNodeGenerated(w, true);
    return w;
}
Also used : PsiWhiteSpaceImpl(com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiWhiteSpaceImpl

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

the class TraverserBasedASTNode method getPsiImpl.

public PsiElement getPsiImpl() {
    IElementType type = getElementType();
    ParserDefinition pd = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage());
    if (pd != null && pd.getWhitespaceTokens().contains(type) || pd == null && type == TokenType.WHITE_SPACE) {
        return new PsiWhiteSpaceImpl(getChars());
    } else if (pd != null && pd.getCommentTokens().contains(type)) {
        return new ASTWrapperPsiComment(this);
    } else if (pd == null || myNode instanceof LighterASTTokenNode) {
        return new ASTWrapperPsiElement(this);
    } else {
        return pd.createElement(this);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTWrapperPsiElement(com.intellij.extapi.psi.ASTWrapperPsiElement) PsiWhiteSpaceImpl(com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl)

Aggregations

PsiWhiteSpaceImpl (com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl)3 ASTWrapperPsiElement (com.intellij.extapi.psi.ASTWrapperPsiElement)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 Project (com.intellij.openapi.project.Project)1 IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1