Search in sources :

Example 1 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project kotlin by JetBrains.

the class KotlinTypedHandler method indentBrace.

/**
     * Copied from
     * @see com.intellij.codeInsight.editorActions.TypedHandler#indentBrace(Project, Editor, char)
     */
private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, char braceChar) {
    final int offset = editor.getCaretModel().getOffset() - 1;
    Document document = editor.getDocument();
    CharSequence chars = document.getCharsSequence();
    if (offset < 0 || chars.charAt(offset) != braceChar)
        return;
    int spaceStart = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
    if (spaceStart < 0 || chars.charAt(spaceStart) == '\n' || chars.charAt(spaceStart) == '\r') {
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        documentManager.commitDocument(document);
        final PsiFile file = documentManager.getPsiFile(document);
        if (file == null || !file.isWritable())
            return;
        PsiElement element = file.findElementAt(offset);
        if (element == null)
            return;
        EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
        HighlighterIterator iterator = highlighter.createIterator(offset);
        FileType fileType = file.getFileType();
        BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
        boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || braceMatcher.isRBraceToken(iterator, chars, fileType);
        if (element.getNode() != null && isBrace) {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    int newOffset = CodeStyleManager.getInstance(project).adjustLineIndent(file, offset);
                    editor.getCaretModel().moveToOffset(newOffset + 1);
                    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                    editor.getSelectionModel().removeSelection();
                }
            });
        }
    }
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Document(com.intellij.openapi.editor.Document) FileType(com.intellij.openapi.fileTypes.FileType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 2 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-plugins by JetBrains.

the class CfmlBraceMatcher method isLBraceToken.

public boolean isLBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
    final IElementType tokenType = iterator.getTokenType();
    PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(tokenType.getLanguage());
    if (matcher != null) {
        BracePair[] pairs = matcher.getPairs();
        for (BracePair pair : pairs) {
            if (pair.getLeftBraceType() == tokenType)
                return true;
        }
    }
    if (!tokenType.getLanguage().equals(CfmlLanguage.INSTANCE)) {
        FileType tokenFileType = iterator.getTokenType().getLanguage().getAssociatedFileType();
        if (tokenFileType != null && tokenFileType != CfmlFileType.INSTANCE) {
            for (FileTypeExtensionPoint<BraceMatcher> ext : Extensions.getExtensions(BraceMatcher.EP_NAME)) {
                if (ext.filetype != null && ext.filetype.equals(tokenFileType.getName())) {
                    return ext.getInstance().isLBraceToken(iterator, fileText, tokenFileType instanceof XmlFileType ? StdFileTypes.HTML : tokenFileType);
                }
            }
        }
    }
    for (BracePair pair : PAIRS) {
        if (pair.getLeftBraceType() == tokenType) {
            return true;
        }
    }
    return tokenType.equals(CfmlTokenTypes.OPENER) && (!CfmlUtil.isEndTagRequired(getTagName(fileText, iterator), null) || findEndTag(fileText, iterator));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) XmlFileType(com.intellij.ide.highlighter.XmlFileType) FileType(com.intellij.openapi.fileTypes.FileType) CfmlFileType(com.intellij.coldFusion.model.files.CfmlFileType) BracePair(com.intellij.lang.BracePair) XmlFileType(com.intellij.ide.highlighter.XmlFileType)

Example 3 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.

the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.

/**
   * Calculates number of unmatched left braces before the given offset.
   *
   * @param editor   target editor
   * @param offset   target offset
   * @param fileType target file type
   * @return number of unmatched braces before the given offset;
   * negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
   * the given offset
   */
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
    if (offset == 0) {
        return -1;
    }
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '{') {
        return -1;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
        return -1;
    }
    Language language = iterator.getTokenType().getLanguage();
    iterator = highlighter.createIterator(0);
    int lBracesBeforeOffset = 0;
    int lBracesAfterOffset = 0;
    int rBracesBeforeOffset = 0;
    int rBracesAfterOffset = 0;
    for (; !iterator.atEnd(); iterator.advance()) {
        IElementType tokenType = iterator.getTokenType();
        if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
            continue;
        }
        boolean beforeOffset = iterator.getStart() < offset;
        if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                lBracesBeforeOffset++;
            } else {
                lBracesAfterOffset++;
            }
        } else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                rBracesBeforeOffset++;
            } else {
                rBracesAfterOffset++;
            }
        }
    }
    return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Language(com.intellij.lang.Language) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 4 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.

the class CustomFileTypeFoldingBuilder method buildBraceMatcherBasedFolding.

public static void buildBraceMatcherBasedFolding(List<FoldingDescriptor> descriptors, PsiElement root, Document document, SyntaxHighlighter highlighter) {
    LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
    editorHighlighter.setText(document.getText());
    FileType fileType = root.getContainingFile().getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
    TextRange totalRange = root.getTextRange();
    final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    String editorText = document.getText();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType)) {
            stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && braceMatcher.isStructuralBrace(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            TextRange range = new TextRange(last.first, iterator.getEnd());
            if (StringUtil.countChars(document.getText(range), '\n') >= 3) {
                descriptors.add(new FoldingDescriptor(root, range));
            }
        }
        iterator.advance();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) FileType(com.intellij.openapi.fileTypes.FileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 5 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-community by JetBrains.

the class TypedHandler method handleAfterLParen.

private static void handleAfterLParen(@NotNull Editor editor, @NotNull FileType fileType, char lparenChar) {
    int offset = editor.getCaretModel().getOffset();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    boolean atEndOfDocument = offset == editor.getDocument().getTextLength();
    if (!atEndOfDocument)
        iterator.retreat();
    if (iterator.atEnd())
        return;
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    if (iterator.atEnd())
        return;
    IElementType braceTokenType = iterator.getTokenType();
    final CharSequence fileText = editor.getDocument().getCharsSequence();
    if (!braceMatcher.isLBraceToken(iterator, fileText, fileType))
        return;
    if (!iterator.atEnd()) {
        iterator.advance();
        if (!iterator.atEnd() && !BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType(braceTokenType, iterator.getTokenType(), fileType)) {
            return;
        }
        iterator.retreat();
    }
    int lparenOffset = BraceMatchingUtil.findLeftmostLParen(iterator, braceTokenType, fileText, fileType);
    if (lparenOffset < 0)
        lparenOffset = 0;
    iterator = ((EditorEx) editor).getHighlighter().createIterator(lparenOffset);
    boolean matched = BraceMatchingUtil.matchBrace(fileText, fileType, iterator, true, true);
    if (!matched) {
        String text;
        if (lparenChar == '(') {
            text = ")";
        } else if (lparenChar == '[') {
            text = "]";
        } else if (lparenChar == '<') {
            text = ">";
        } else if (lparenChar == '{') {
            text = "}";
        } else {
            throw new AssertionError("Unknown char " + lparenChar);
        }
        editor.getDocument().insertString(offset, text);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NontrivialBraceMatcher(com.intellij.codeInsight.highlighting.NontrivialBraceMatcher) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)12 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)9 FileType (com.intellij.openapi.fileTypes.FileType)9 IElementType (com.intellij.psi.tree.IElementType)8 EditorEx (com.intellij.openapi.editor.ex.EditorEx)6 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 CfmlFileType (com.intellij.coldFusion.model.files.CfmlFileType)3 XmlFileType (com.intellij.ide.highlighter.XmlFileType)3 BracePair (com.intellij.lang.BracePair)3 PairedBraceMatcher (com.intellij.lang.PairedBraceMatcher)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 TextRange (com.intellij.openapi.util.TextRange)3 Trinity (com.intellij.openapi.util.Trinity)3 LinkedList (java.util.LinkedList)3 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)2 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)2 PsiFile (com.intellij.psi.PsiFile)2 Language (com.intellij.lang.Language)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1