Search in sources :

Example 26 with EditorHighlighter

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

the class TypedHandler method indentBrace.

private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, final char braceChar) {
    final int offset = editor.getCaretModel().getOffset() - 1;
    final 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);
        final FileType fileType = file.getFileType();
        BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
        boolean rBraceToken = braceMatcher.isRBraceToken(iterator, chars, fileType);
        final boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || rBraceToken;
        int lBraceOffset = -1;
        if (CodeInsightSettings.getInstance().REFORMAT_BLOCK_ON_RBRACE && rBraceToken && braceMatcher.isStructuralBrace(iterator, chars, fileType) && offset > 0) {
            lBraceOffset = BraceMatchingUtil.findLeftLParen(highlighter.createIterator(offset - 1), braceMatcher.getOppositeBraceTokenType(iterator.getTokenType()), editor.getDocument().getCharsSequence(), fileType);
        }
        if (element.getNode() != null && isBrace) {
            final int finalLBraceOffset = lBraceOffset;
            ApplicationManager.getApplication().runWriteAction(() -> {
                try {
                    int newOffset;
                    if (finalLBraceOffset != -1) {
                        RangeMarker marker = document.createRangeMarker(offset, offset + 1);
                        CodeStyleManager.getInstance(project).reformatRange(file, finalLBraceOffset, offset, true);
                        newOffset = marker.getStartOffset();
                        marker.dispose();
                    } else {
                        newOffset = CodeStyleManager.getInstance(project).adjustLineIndent(file, offset);
                    }
                    editor.getCaretModel().moveToOffset(newOffset + 1);
                    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                    editor.getSelectionModel().removeSelection();
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            });
        }
    }
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) NontrivialBraceMatcher(com.intellij.codeInsight.highlighting.NontrivialBraceMatcher) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 27 with EditorHighlighter

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

the class DiffUtil method initEmptyEditorHighlighter.

@NotNull
public static EditorHighlighter initEmptyEditorHighlighter(@NotNull CharSequence text) {
    EditorHighlighter highlighter = createEmptyEditorHighlighter();
    highlighter.setText(text);
    return highlighter;
}
Also used : EmptyEditorHighlighter(com.intellij.openapi.editor.ex.util.EmptyEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 28 with EditorHighlighter

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

the class TestEditorManagerImpl method openTextEditor.

@Override
public Editor openTextEditor(@NotNull OpenFileDescriptor descriptor, boolean focusEditor) {
    final VirtualFile file = descriptor.getFile();
    Editor editor = myVirtualFile2Editor.get(file);
    if (editor == null) {
        PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
        LOG.assertTrue(psiFile != null, file);
        Document document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile);
        LOG.assertTrue(document != null, psiFile);
        editor = EditorFactory.getInstance().createEditor(document, myProject);
        final EditorHighlighter highlighter = HighlighterFactory.createHighlighter(myProject, file);
        ((EditorEx) editor).setHighlighter(highlighter);
        ((EditorEx) editor).setFile(file);
        myVirtualFile2Editor.put(file, editor);
    }
    if (descriptor.getOffset() >= 0) {
        editor.getCaretModel().moveToOffset(descriptor.getOffset());
    } else if (descriptor.getLine() >= 0 && descriptor.getColumn() >= 0) {
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(descriptor.getLine(), descriptor.getColumn()));
    }
    editor.getSelectionModel().removeSelection();
    myActiveFile = file;
    return editor;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiFile(com.intellij.psi.PsiFile) com.intellij.openapi.fileEditor(com.intellij.openapi.fileEditor) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 29 with EditorHighlighter

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

the class DetailViewImpl method createEditor.

@NotNull
protected Editor createEditor(@Nullable Project project, Document document, VirtualFile file) {
    EditorEx editor = (EditorEx) EditorFactory.getInstance().createViewer(document, project);
    final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(file, scheme, project);
    editor.setFile(file);
    editor.setHighlighter(highlighter);
    EditorSettings settings = editor.getSettings();
    settings.setAnimatedScrolling(false);
    settings.setRefrainFromScrolling(false);
    settings.setLineNumbersShown(true);
    settings.setFoldingOutlineShown(false);
    if (settings instanceof SettingsImpl) {
        ((SettingsImpl) settings).setSoftWrapAppliancePlace(SoftWrapAppliancePlaces.PREVIEW);
    }
    editor.getFoldingModel().setFoldingEnabled(false);
    return editor;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) SettingsImpl(com.intellij.openapi.editor.impl.SettingsImpl) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with EditorHighlighter

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

the class JavaBraceMatcherTest method testBinaryStatement.

public void testBinaryStatement() {
    myFixture.configureByText("a.java", "import java.util.ArrayList;" + "class A {" + "  int i = 3 <caret>< 4 ? 5 > 6 : 1 : 1 : 1;" + "}");
    final Editor editor = myFixture.getEditor();
    final EditorHighlighter editorHighlighter = ((EditorEx) editor).getHighlighter();
    final HighlighterIterator iterator = editorHighlighter.createIterator(editor.getCaretModel().getOffset());
    boolean matched = BraceMatchingUtil.matchBrace(editor.getDocument().getCharsSequence(), myFixture.getFile().getFileType(), iterator, true);
    assertFalse(matched);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) Editor(com.intellij.openapi.editor.Editor) 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