Search in sources :

Example 16 with EditorHighlighter

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

the class CfmlTypedHandlerTest method testEditing.

public void testEditing() throws Throwable {
    @NonNls final String s1 = "<table bgcolor=\"#FFFFFF\"><cfoutput>\n" + "  <div id=\"#bColumn2";
    String s2 = "\" />\n" + "</cfoutput></table>";
    String s = s1 + s2;
    final Document doc = new DocumentImpl(s);
    EditorEx editor = (EditorEx) EditorFactory.getInstance().createEditor(doc);
    try {
        EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
        editor.setHighlighter(highlighter);
        CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> doc.insertString(s1.length(), "#")), "", null);
        List tokensAfterUpdate = getAllTokens(highlighter);
        highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
        editor.setHighlighter(highlighter);
        List tokensWithoutUpdate = getAllTokens(highlighter);
        TestCase.assertEquals(tokensWithoutUpdate, tokensAfterUpdate);
    } finally {
        EditorFactory.getInstance().releaseEditor(editor);
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) EditorEx(com.intellij.openapi.editor.ex.EditorEx) List(java.util.List) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 17 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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 18 with EditorHighlighter

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

the class ImplementationViewComponent method update.

public void update(@NotNull final PsiElement[] elements, final int index) {
    update(elements, (psiElements, fileDescriptors) -> {
        if (myEditor.isDisposed())
            return false;
        if (psiElements.length == 0)
            return false;
        final Project project = psiElements[0].getProject();
        myElements = psiElements;
        myIndex = index < myElements.length ? index : 0;
        PsiFile psiFile = getContainingFile(myElements[myIndex]);
        VirtualFile virtualFile = psiFile.getVirtualFile();
        EditorHighlighter highlighter;
        if (virtualFile != null)
            highlighter = HighlighterFactory.createHighlighter(project, virtualFile);
        else {
            // some artificial psi file, lets do best we can
            String fileName = psiFile.getName();
            highlighter = HighlighterFactory.createHighlighter(project, fileName);
        }
        ((EditorEx) myEditor).setHighlighter(highlighter);
        if (myElements.length > 1) {
            myFileChooser.setVisible(true);
            myCountLabel.setVisible(true);
            myLabel.setVisible(false);
            myFileChooser.setModel(new DefaultComboBoxModel(fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()])));
            updateRenderer(project);
        } else {
            myFileChooser.setVisible(false);
            myCountLabel.setVisible(false);
            VirtualFile file = psiFile.getVirtualFile();
            if (file != null) {
                myLabel.setIcon(getIconForFile(psiFile));
                myLabel.setForeground(FileStatusManager.getInstance(project).getStatus(file).getColor());
                myLabel.setText(file.getPresentableName());
                myLabel.setBorder(new CompoundBorder(IdeBorderFactory.createRoundedBorder(), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5)));
                myLabel.setVisible(true);
            }
        }
        updateControls();
        revalidate();
        repaint();
        return true;
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CompoundBorder(javax.swing.border.CompoundBorder) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 19 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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)

Example 20 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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)

Aggregations

EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)63 EditorEx (com.intellij.openapi.editor.ex.EditorEx)36 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)36 Document (com.intellij.openapi.editor.Document)13 IElementType (com.intellij.psi.tree.IElementType)11 Editor (com.intellij.openapi.editor.Editor)8 FileType (com.intellij.openapi.fileTypes.FileType)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)8 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)7 PsiFile (com.intellij.psi.PsiFile)7 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)6 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)5 PsiElement (com.intellij.psi.PsiElement)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 Project (com.intellij.openapi.project.Project)4 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)3 EditorWindow (com.intellij.injected.editor.EditorWindow)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)3