Search in sources :

Example 51 with EditorHighlighter

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

the class LanguageConsoleImpl method printWithHighlighting.

public static String printWithHighlighting(@NotNull LanguageConsoleView console, @NotNull Editor inputEditor, @NotNull TextRange textRange) {
    String text;
    EditorHighlighter highlighter;
    if (inputEditor instanceof EditorWindow) {
        PsiFile file = ((EditorWindow) inputEditor).getInjectedFile();
        highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
        String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
        highlighter.setText(fullText);
        text = textRange.substring(fullText);
    } else {
        text = inputEditor.getDocument().getText(textRange);
        highlighter = ((EditorEx) inputEditor).getHighlighter();
    }
    SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
    ((LanguageConsoleImpl) console).doAddPromptToHistory();
    if (syntax != null) {
        ConsoleViewUtil.printWithHighlighting(console, text, syntax);
    } else {
        console.print(text, ConsoleViewContentType.USER_INPUT);
    }
    console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
    return text;
}
Also used : PsiFile(com.intellij.psi.PsiFile) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 52 with EditorHighlighter

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

the class BaseIndentEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffset, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
    Result res = shouldSkipWithResult(file, editor, dataContext);
    if (res != null) {
        return res;
    }
    final Document document = editor.getDocument();
    int caret = editor.getCaretModel().getOffset();
    final int lineNumber = document.getLineNumber(caret);
    final int lineStartOffset = document.getLineStartOffset(lineNumber);
    final int previousLineStartOffset = lineNumber > 0 ? document.getLineStartOffset(lineNumber - 1) : lineStartOffset;
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    final HighlighterIterator iterator = highlighter.createIterator(caret - 1);
    final IElementType type = getNonWhitespaceElementType(iterator, lineStartOffset, previousLineStartOffset);
    final CharSequence editorCharSequence = document.getCharsSequence();
    final CharSequence lineIndent = editorCharSequence.subSequence(lineStartOffset, EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, lineNumber));
    // Enter in line comment
    if (type == myLineCommentType) {
        final String restString = editorCharSequence.subSequence(caret, document.getLineEndOffset(lineNumber)).toString();
        if (!StringUtil.isEmptyOrSpaces(restString)) {
            final String linePrefix = lineIndent + myLineCommentPrefix;
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + linePrefix);
            editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, linePrefix.length()));
            return Result.Stop;
        } else if (iterator.getStart() < lineStartOffset) {
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
            return Result.Stop;
        }
    }
    if (!myWorksWithFormatter && LanguageFormatting.INSTANCE.forLanguage(myLanguage) != null) {
        return Result.Continue;
    } else {
        if (myIndentTokens.contains(type)) {
            final String newIndent = getNewIndent(file, document, lineIndent);
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + newIndent);
            return Result.Stop;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, calcLogicalLength(editor, lineIndent)));
        return Result.Stop;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 53 with EditorHighlighter

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

the class GStringBackspaceHandlerDelegate method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    if (c != '{')
        return;
    if (!(file instanceof GroovyFile))
        return;
    final int offset = editor.getCaretModel().getOffset();
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    if (offset < 1)
        return;
    HighlighterIterator iterator = highlighter.createIterator(offset);
    if (iterator.getTokenType() != GroovyTokenTypes.mRCURLY)
        return;
    iterator.retreat();
    if (iterator.getStart() < 1 || iterator.getTokenType() != GroovyTokenTypes.mLCURLY)
        return;
    editor.getDocument().deleteString(offset, offset + 1);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 54 with EditorHighlighter

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

the class TodoTreeBuilder method getHighlighter.

/**
   * @return {@code SelectInEditorManager} for the specified {@code psiFile}. Highlighters are
   *         lazy created and initialized.
   */
public EditorHighlighter getHighlighter(PsiFile psiFile, Document document) {
    VirtualFile file = psiFile.getVirtualFile();
    if (myFile2Highlighter.containsKey(file)) {
        return myFile2Highlighter.get(file);
    } else {
        EditorHighlighter highlighter = HighlighterFactory.createHighlighter(UsageTreeColorsScheme.getInstance().getScheme(), file.getName(), myProject);
        highlighter.setText(document.getCharsSequence());
        myFile2Highlighter.put(file, highlighter);
        return highlighter;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 55 with EditorHighlighter

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

the class RParenthTailType method getExistingRParenthOffset.

private static int getExistingRParenthOffset(final Editor editor, final int tailOffset) {
    final Document document = editor.getDocument();
    if (tailOffset >= document.getTextLength())
        return -1;
    final CharSequence charsSequence = document.getCharsSequence();
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    int existingRParenthOffset = -1;
    for (HighlighterIterator iterator = highlighter.createIterator(tailOffset); !iterator.atEnd(); iterator.advance()) {
        final IElementType tokenType = iterator.getTokenType();
        if ((!(tokenType instanceof IJavaElementType) || !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)) && tokenType != TokenType.WHITE_SPACE) {
            final int start = iterator.getStart();
            if (iterator.getEnd() == start + 1 && ')' == charsSequence.charAt(start)) {
                existingRParenthOffset = start;
            }
            break;
        }
    }
    if (existingRParenthOffset >= 0) {
        final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(editor.getProject());
        psiDocumentManager.commitDocument(document);
        TextRange range = getRangeToCheckParensBalance(psiDocumentManager.getPsiFile(document), document, editor.getCaretModel().getOffset());
        int balance = calcParensBalance(document, highlighter, range.getStartOffset(), range.getEndOffset());
        if (balance > 0) {
            return -1;
        }
    }
    return existingRParenthOffset;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) IJavaElementType(com.intellij.psi.tree.java.IJavaElementType) 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