Search in sources :

Example 86 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 87 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)

Example 88 with PsiWhiteSpace

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

the class DartDocUtil method getMultilineDocCommentText.

@NotNull
private static String getMultilineDocCommentText(@NotNull final DartDocComment docComment) {
    final StringBuilder buf = new StringBuilder();
    boolean afterAsterisk = false;
    for (PsiElement child = docComment.getFirstChild(); child != null; child = child.getNextSibling()) {
        final IElementType elementType = child.getNode().getElementType();
        final String text = child.getText();
        if (elementType != DartTokenTypesSets.MULTI_LINE_DOC_COMMENT_START && elementType != DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK && elementType != DartTokenTypesSets.MULTI_LINE_COMMENT_END) {
            int newLinesCount;
            if (child instanceof PsiWhiteSpace && (newLinesCount = StringUtil.countNewLines(text)) > 0) {
                buf.append(StringUtil.repeatSymbol('\n', newLinesCount));
            } else {
                if (afterAsterisk && text.startsWith(" ")) {
                    buf.append(text.substring(1));
                } else {
                    buf.append(text);
                }
            }
        }
        afterAsterisk = elementType == DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK;
    }
    return buf.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with PsiWhiteSpace

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

the class DartRefactoringUtil method findExpressionInRange.

@Nullable
public static DartExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset) {
    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();
    }
    DartExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, DartExpression.class);
    if (expression == null || expression.getTextRange().getEndOffset() != endOffset)
        return null;
    if (expression instanceof DartReference && expression.getParent() instanceof DartCallExpression)
        return null;
    return expression;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 90 with PsiWhiteSpace

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

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)

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