Search in sources :

Example 11 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.

the class CfmlAttributeNamesCompletionProvider method addCompletions.

public void addCompletions(@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    String tagName = "";
    while (element != null && !(element instanceof CfmlTag) && !(element instanceof CfmlComponent) && !(element instanceof CfmlPropertyImpl)) {
        PsiElement prevNode = element.getPrevSibling();
        PsiElement superPrevNode = prevNode != null ? prevNode.getPrevSibling() : null;
        if (superPrevNode != null && superPrevNode.getText().equalsIgnoreCase("property")) {
            tagName = "cfproperty";
            break;
        }
        element = element.getParent();
    }
    if (element == null) {
        return;
    }
    if (tagName.isEmpty()) {
        tagName = element instanceof CfmlTag ? ((CfmlTag) element).getTagName() : element instanceof CfmlPropertyImpl ? "cfproperty" : "cfcomponent";
    }
    Set<String> excluded = new HashSet<>();
    final CfmlAttributeImpl[] attributes = PsiTreeUtil.getChildrenOfType(element, CfmlAttributeImpl.class);
    if (attributes != null) {
        for (CfmlAttributeImpl attribute : attributes) {
            excluded.add(attribute.getAttributeName());
        }
    }
    for (CfmlAttributeDescription s : CfmlUtil.getAttributes(tagName, element.getProject())) {
        if (s.getName() == null) {
            continue;
        }
        if (excluded.contains(s.getName())) {
            continue;
        }
        result.addElement(TailTypeDecorator.withTail(LookupElementBuilder.create(s.getName()).withCaseSensitivity(false), new TailType() {

            public int processTail(Editor editor, int tailOffset) {
                HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(tailOffset);
                if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.WHITE_SPACE)
                    iterator.advance();
                if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.ASSIGN) {
                    iterator.advance();
                } else {
                    editor.getDocument().insertString(tailOffset, "=\"\"");
                    return moveCaret(editor, tailOffset, 2);
                }
                int offset = iterator.getStart();
                if (!iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.WHITE_SPACE)
                    iterator.advance();
                if (!iterator.atEnd() && CfmlTokenTypes.STRING_ELEMENTS.contains(iterator.getTokenType())) {
                    return tailOffset;
                }
                editor.getDocument().insertString(offset, "\"\"");
                return moveCaret(editor, tailOffset, offset - tailOffset + 1);
            }
        }));
    }
}
Also used : TailType(com.intellij.codeInsight.TailType) CfmlAttributeDescription(com.intellij.coldFusion.model.info.CfmlAttributeDescription) CfmlAttributeImpl(com.intellij.coldFusion.model.psi.impl.CfmlAttributeImpl) CfmlComponent(com.intellij.coldFusion.model.psi.CfmlComponent) CfmlPropertyImpl(com.intellij.coldFusion.model.psi.impl.CfmlPropertyImpl) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) HashSet(com.intellij.util.containers.HashSet)

Example 12 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.

the class CfmlTypedHandler method beforeCharTyped.

public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
    PsiFile cfmlFile = file.getViewProvider().getPsi(CfmlLanguage.INSTANCE);
    if (isNotCfmlFile(cfmlFile, editor)) {
        return Result.CONTINUE;
    }
    int offset = editor.getCaretModel().getOffset();
    if (c == '{') {
        CfmlBraceMatcher braceMatcher = new CfmlBraceMatcher();
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        if (!braceMatcher.isLBraceToken(iterator, editor.getDocument().getCharsSequence(), fileType)) {
            EditorModificationUtil.insertStringAtCaret(editor, "}", true, 0);
        // return Result.STOP;
        }
        return Result.CONTINUE;
    }
    if (c == '#') {
        if (ourEnableDoublePoundInsertion && CfmlEditorUtil.countSharpsBalance(editor) == 0) {
            char charAtOffset = DocumentUtils.getCharAt(editor.getDocument(), offset);
            if (charAtOffset == '#') {
                EditorModificationUtil.moveCaretRelatively(editor, 1);
                return Result.STOP;
            }
            EditorModificationUtil.insertStringAtCaret(editor, "#", true, 0);
        }
    } else if (c == '>') {
        if (((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset()).getTokenType() == CfmlTokenTypes.COMMENT || ((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset()).getTokenType().getLanguage() != CfmlLanguage.INSTANCE) {
            return Result.CONTINUE;
        }
        insertCloseTagIfNeeded(editor, cfmlFile, project);
        return Result.STOP;
    }
    return Result.CONTINUE;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiFile(com.intellij.psi.PsiFile) CfmlBraceMatcher(com.intellij.coldFusion.UI.editorActions.matchers.CfmlBraceMatcher) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 13 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-plugins by JetBrains.

the class DartTypeHandler method handleDartGT.

// todo extract helper method, duplicated code in com.intellij.codeInsight.editorActions.JavaTypedHandler.handleJavaGT()
private static boolean handleDartGT(final Editor editor, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
    if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET)
        return false;
    int offset = editor.getCaretModel().getOffset();
    if (offset == editor.getDocument().getTextLength())
        return false;
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    if (iterator.getTokenType() != gt)
        return false;
    while (!iterator.atEnd() && !invalidInsideReference.contains(iterator.getTokenType())) {
        iterator.advance();
    }
    if (!iterator.atEnd() && invalidInsideReference.contains(iterator.getTokenType()))
        iterator.retreat();
    int balance = 0;
    while (!iterator.atEnd() && balance >= 0) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == lt) {
            balance--;
        } else if (tokenType == gt) {
            balance++;
        } else if (invalidInsideReference.contains(tokenType)) {
            break;
        }
        iterator.retreat();
    }
    if (balance == 0) {
        EditorModificationUtil.moveCaretRelatively(editor, 1);
        return true;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 14 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class BraceMatchingUtil method getMatchedBraceOffset.

@TestOnly
public static int getMatchedBraceOffset(@NotNull Editor editor, boolean forward, @NotNull PsiFile file) {
    Document document = editor.getDocument();
    int offset = editor.getCaretModel().getOffset();
    EditorHighlighter editorHighlighter = BraceHighlightingHandler.getLazyParsableHighlighterIfAny(file.getProject(), editor, file);
    HighlighterIterator iterator = editorHighlighter.createIterator(offset);
    boolean matched = matchBrace(document.getCharsSequence(), file.getFileType(), iterator, forward);
    assert matched;
    return iterator.getStart();
}
Also used : Document(com.intellij.openapi.editor.Document) FileTypeExtensionPoint(com.intellij.openapi.fileTypes.FileTypeExtensionPoint) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) TestOnly(org.jetbrains.annotations.TestOnly)

Example 15 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class TodoItemNode method update.

@Override
public void update(PresentationData presentation) {
    TodoItem todoItem = getValue().getTodoItem();
    RangeMarker myRangeMarker = getValue().getRangeMarker();
    if (!todoItem.getFile().isValid() || !myRangeMarker.isValid() || myRangeMarker.getStartOffset() == myRangeMarker.getEndOffset()) {
        myRangeMarker.dispose();
        setValue(null);
        return;
    }
    myHighlightedRegions.clear();
    // Update name
    Document document = getValue().getDocument();
    CharSequence chars = document.getCharsSequence();
    int startOffset = myRangeMarker.getStartOffset();
    int endOffset = myRangeMarker.getEndOffset();
    LOG.assertTrue(startOffset > -1);
    LOG.assertTrue(startOffset <= document.getTextLength());
    LOG.assertTrue(endOffset > -1);
    LOG.assertTrue(endOffset < document.getTextLength() + 1);
    int lineNumber = document.getLineNumber(startOffset);
    LOG.assertTrue(lineNumber > -1);
    LOG.assertTrue(lineNumber < document.getLineCount());
    int lineStartOffset = document.getLineStartOffset(lineNumber);
    LOG.assertTrue(lineStartOffset > -1);
    LOG.assertTrue(lineStartOffset <= startOffset);
    LOG.assertTrue(lineStartOffset <= document.getTextLength());
    int columnNumber = startOffset - lineStartOffset;
    LOG.assertTrue(columnNumber > -1);
    while (lineStartOffset < document.getTextLength() && (chars.charAt(lineStartOffset) == '\t' || chars.charAt(lineStartOffset) == ' ')) {
        lineStartOffset++;
    }
    int lineEndOffset = document.getLineEndOffset(lineNumber);
    LOG.assertTrue(lineEndOffset >= 0);
    LOG.assertTrue(lineEndOffset <= document.getTextLength());
    String lineColumnPrefix = "(" + (lineNumber + 1) + ", " + (columnNumber + 1) + ") ";
    String highlightedText = chars.subSequence(lineStartOffset, Math.min(lineEndOffset, chars.length())).toString();
    String newName = lineColumnPrefix + highlightedText;
    // Update icon
    Icon newIcon = todoItem.getPattern().getAttributes().getIcon();
    // Update highlighted regions
    myHighlightedRegions.clear();
    EditorHighlighter highlighter = myBuilder.getHighlighter(todoItem.getFile(), document);
    HighlighterIterator iterator = highlighter.createIterator(lineStartOffset);
    while (!iterator.atEnd()) {
        int start = Math.max(iterator.getStart(), lineStartOffset);
        int end = Math.min(iterator.getEnd(), lineEndOffset);
        if (lineEndOffset < start || lineEndOffset < end) {
            break;
        }
        TextAttributes attributes = iterator.getTextAttributes();
        int fontType = attributes.getFontType();
        if ((fontType & Font.BOLD) != 0) {
            // suppress bold attribute
            attributes = attributes.clone();
            attributes.setFontType(fontType & ~Font.BOLD);
        }
        HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + start - lineStartOffset, lineColumnPrefix.length() + end - lineStartOffset, attributes);
        myHighlightedRegions.add(region);
        iterator.advance();
    }
    TextAttributes attributes = todoItem.getPattern().getAttributes().getTextAttributes();
    HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + startOffset - lineStartOffset, lineColumnPrefix.length() + endOffset - lineStartOffset, attributes);
    myHighlightedRegions.add(region);
    //
    presentation.setPresentableText(newName);
    presentation.setIcon(newIcon);
}
Also used : TodoItem(com.intellij.psi.search.TodoItem) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) HighlightedRegion(com.intellij.ui.HighlightedRegion) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Aggregations

HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)83 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)33 IElementType (com.intellij.psi.tree.IElementType)33 Document (com.intellij.openapi.editor.Document)12 FileType (com.intellij.openapi.fileTypes.FileType)12 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)9 TextRange (com.intellij.openapi.util.TextRange)8 Editor (com.intellij.openapi.editor.Editor)7 PsiElement (com.intellij.psi.PsiElement)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 PsiFile (com.intellij.psi.PsiFile)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)4 Project (com.intellij.openapi.project.Project)4 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 Trinity (com.intellij.openapi.util.Trinity)3 ArrayList (java.util.ArrayList)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2