Search in sources :

Example 1 with LexerEditorHighlighter

use of com.intellij.openapi.editor.ex.util.LexerEditorHighlighter in project intellij-community by JetBrains.

the class PythonConsoleView method addTextRangeToHistory.

@NotNull
protected String addTextRangeToHistory(@NotNull TextRange textRange, @NotNull EditorEx inputEditor, boolean preserveMarkup) {
    String text;
    EditorHighlighter highlighter;
    if (inputEditor instanceof EditorWindow) {
        PsiFile file = ((EditorWindow) inputEditor).getInjectedFile();
        highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), getProject());
        String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
        highlighter.setText(fullText);
        text = textRange.substring(fullText);
    } else {
        text = inputEditor.getDocument().getText(textRange);
        highlighter = inputEditor.getHighlighter();
    }
    SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
    doAddPromptToHistory(true);
    if (syntax != null) {
        ConsoleViewUtil.printWithHighlighting(this, text, syntax, () -> doAddPromptToHistory(false));
    } else {
        print(text, ConsoleViewContentType.USER_INPUT);
    }
    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) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LexerEditorHighlighter

use of com.intellij.openapi.editor.ex.util.LexerEditorHighlighter in project intellij-community by JetBrains.

the class ResourceBundleEditor method reinitSettings.

private void reinitSettings(final EditorEx editor) {
    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    editor.setColorsScheme(scheme);
    editor.setBorder(BorderFactory.createLineBorder(JBColor.border(), 1));
    EditorSettings settings = editor.getSettings();
    settings.setLineNumbersShown(false);
    settings.setWhitespacesShown(false);
    settings.setLineMarkerAreaShown(false);
    settings.setIndentGuidesShown(false);
    settings.setFoldingOutlineShown(false);
    settings.setAdditionalColumnsCount(0);
    settings.setAdditionalLinesCount(0);
    settings.setRightMarginShown(true);
    settings.setRightMargin(60);
    settings.setVirtualSpace(false);
    editor.setHighlighter(new LexerEditorHighlighter(new PropertiesValueHighlighter(), scheme));
    editor.setVerticalScrollbarVisible(true);
    // disabling default context menu
    editor.setContextMenuGroupId(null);
    editor.addEditorMouseListener(new EditorPopupHandler() {

        @Override
        public void invokePopup(EditorMouseEvent event) {
            if (!event.isConsumed() && event.getArea() == EditorMouseEventArea.EDITING_AREA) {
                DefaultActionGroup group = new DefaultActionGroup();
                group.add(CustomActionsSchema.getInstance().getCorrectedAction(IdeActions.GROUP_CUT_COPY_PASTE));
                group.add(CustomActionsSchema.getInstance().getCorrectedAction(IdeActions.ACTION_EDIT_SOURCE));
                group.addSeparator();
                group.add(new AnAction("Propagate Value Across of Resource Bundle") {

                    @Override
                    public void actionPerformed(AnActionEvent e) {
                        final String valueToPropagate = editor.getDocument().getText();
                        final String currentSelectedProperty = getSelectedPropertyName();
                        if (currentSelectedProperty == null) {
                            return;
                        }
                        ApplicationManager.getApplication().runWriteAction(() -> WriteCommandAction.runWriteCommandAction(myProject, () -> {
                            try {
                                final PropertiesFile[] propertiesFiles = myResourceBundle.getPropertiesFiles().stream().filter(f -> {
                                    final IProperty property = f.findPropertyByKey(currentSelectedProperty);
                                    return property == null || !valueToPropagate.equals(property.getValue());
                                }).toArray(PropertiesFile[]::new);
                                final PsiFile[] filesToPrepare = Arrays.stream(propertiesFiles).map(PropertiesFile::getContainingFile).toArray(PsiFile[]::new);
                                if (FileModificationService.getInstance().preparePsiElementsForWrite(filesToPrepare)) {
                                    for (PropertiesFile file : propertiesFiles) {
                                        myPropertiesInsertDeleteManager.insertOrUpdateTranslation(currentSelectedProperty, valueToPropagate, file);
                                    }
                                    recreateEditorsPanel();
                                }
                            } catch (final IncorrectOperationException e1) {
                                LOG.error(e1);
                            }
                        }));
                    }
                });
                EditorPopupHandler handler = EditorActionUtil.createEditorPopupHandler(group);
                handler.invokePopup(event);
                event.consume();
            }
        }
    });
}
Also used : EditorPopupHandler(com.intellij.util.EditorPopupHandler) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorMouseEvent(com.intellij.openapi.editor.event.EditorMouseEvent) IProperty(com.intellij.lang.properties.IProperty) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) XmlPropertiesFile(com.intellij.lang.properties.xml.XmlPropertiesFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with LexerEditorHighlighter

use of com.intellij.openapi.editor.ex.util.LexerEditorHighlighter in project intellij-community by JetBrains.

the class ImmediatePainter method paintImmediately.

private void paintImmediately(final Graphics g, final int offset, final char c2) {
    final EditorImpl editor = myEditor;
    final Document document = editor.getDocument();
    final LexerEditorHighlighter highlighter = (LexerEditorHighlighter) myEditor.getHighlighter();
    final EditorSettings settings = editor.getSettings();
    final boolean isBlockCursor = editor.isInsertMode() == settings.isBlockCursor();
    final int lineHeight = editor.getLineHeight();
    final int ascent = editor.getAscent();
    final int topOverhang = editor.myView.getTopOverhang();
    final int bottomOverhang = editor.myView.getBottomOverhang();
    final char c1 = offset == 0 ? ' ' : document.getCharsSequence().charAt(offset - 1);
    final List<TextAttributes> attributes = highlighter.getAttributesForPreviousAndTypedChars(document, offset, c2);
    updateAttributes(editor, offset, attributes);
    final TextAttributes attributes1 = attributes.get(0);
    final TextAttributes attributes2 = attributes.get(1);
    if (!(canRender(attributes1) && canRender(attributes2))) {
        return;
    }
    FontLayoutService fontLayoutService = FontLayoutService.getInstance();
    final float width1 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes1.getFontType()), c1);
    final float width2 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes2.getFontType()), c2);
    final Font font1 = EditorUtil.fontForChar(c1, attributes1.getFontType(), editor).getFont();
    final Font font2 = EditorUtil.fontForChar(c1, attributes2.getFontType(), editor).getFont();
    final Point2D p2 = editor.offsetToXY(offset, false);
    float p2x = (float) p2.getX();
    int p2y = (int) p2.getY();
    int width1i = (int) (p2x) - (int) (p2x - width1);
    int width2i = (int) (p2x + width2) - (int) p2x;
    Caret caret = editor.getCaretModel().getPrimaryCaret();
    //noinspection ConstantConditions
    final int caretWidth = isBlockCursor ? editor.getCaretLocations(false)[0].myWidth : JBUI.scale(caret.getVisualAttributes().getWidth(settings.getLineCursorWidth()));
    final float caretShift = isBlockCursor ? 0 : caretWidth == 1 ? 0 : 1 / JBUI.sysScale((Graphics2D) g);
    final Rectangle2D caretRectangle = new Rectangle2D.Float((int) (p2x + width2) - caretShift, p2y - topOverhang, caretWidth, lineHeight + topOverhang + bottomOverhang + (isBlockCursor ? -1 : 0));
    final Rectangle rectangle1 = new Rectangle((int) (p2x - width1), p2y, width1i, lineHeight);
    final Rectangle rectangle2 = new Rectangle((int) p2x, p2y, (int) (width2i + caretWidth - caretShift), lineHeight);
    final Consumer<Graphics> painter = graphics -> {
        EditorUIUtil.setupAntialiasing(graphics);
        fillRect(graphics, rectangle2, attributes2.getBackgroundColor());
        drawChar(graphics, c2, p2x, p2y + ascent, font2, attributes2.getForegroundColor());
        fillRect(graphics, caretRectangle, getCaretColor(editor));
        fillRect(graphics, rectangle1, attributes1.getBackgroundColor());
        drawChar(graphics, c1, p2x - width1, p2y + ascent, font1, attributes1.getForegroundColor());
    };
    final Shape originalClip = g.getClip();
    g.setClip(new Rectangle2D.Float((int) p2x - caretShift, p2y, width2i + caretWidth, lineHeight));
    if (DOUBLE_BUFFERING.asBoolean()) {
        paintWithDoubleBuffering(g, painter);
    } else {
        painter.consume(g);
    }
    g.setClip(originalClip);
    if (PIPELINE_FLUSH.asBoolean()) {
        Toolkit.getDefaultToolkit().sync();
    }
    if (DEBUG.asBoolean()) {
        pause();
    }
}
Also used : RegistryValue(com.intellij.openapi.util.registry.RegistryValue) UIUtil(com.intellij.util.ui.UIUtil) Point2D(java.awt.geom.Point2D) com.intellij.openapi.editor(com.intellij.openapi.editor) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Rectangle2D(java.awt.geom.Rectangle2D) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) JBUI(com.intellij.util.ui.JBUI) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) EffectType(com.intellij.openapi.editor.markup.EffectType) Disposer(com.intellij.openapi.util.Disposer) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) IterationState(com.intellij.openapi.editor.impl.view.IterationState) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) EditorTextField(com.intellij.ui.EditorTextField) BufferedImage(java.awt.image.BufferedImage) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) java.awt(java.awt) VolatileImage(java.awt.image.VolatileImage) EditorUIUtil(com.intellij.openapi.editor.ex.util.EditorUIUtil) List(java.util.List) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) Gray(com.intellij.ui.Gray) JBColor(com.intellij.ui.JBColor) Consumer(com.intellij.util.Consumer) javax.swing(javax.swing) Rectangle2D(java.awt.geom.Rectangle2D) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) Point2D(java.awt.geom.Point2D) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 4 with LexerEditorHighlighter

use of com.intellij.openapi.editor.ex.util.LexerEditorHighlighter in project intellij-community by JetBrains.

the class EditorHighlighterCache method getEditorHighlighterForCachesBuilding.

@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
    if (document == null) {
        return null;
    }
    final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
    final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);
    if (someEditorHighlighter instanceof LexerEditorHighlighter && ((LexerEditorHighlighter) someEditorHighlighter).isValid()) {
        return someEditorHighlighter;
    }
    document.putUserData(ourSomeEditorSyntaxHighlighter, null);
    return null;
}
Also used : LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with LexerEditorHighlighter

use of com.intellij.openapi.editor.ex.util.LexerEditorHighlighter 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)

Aggregations

LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)13 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)5 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)4 TextRange (com.intellij.openapi.util.TextRange)4 NotNull (org.jetbrains.annotations.NotNull)4 EditorWindow (com.intellij.injected.editor.EditorWindow)3 EditorEx (com.intellij.openapi.editor.ex.EditorEx)3 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)3 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)2 Language (com.intellij.lang.Language)2 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)2 Editor (com.intellij.openapi.editor.Editor)2 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)2 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)2 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)2 EffectType (com.intellij.openapi.editor.markup.EffectType)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 Processor (com.intellij.util.Processor)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 java.awt (java.awt)2