Search in sources :

Example 1 with CaretRectangle

use of com.intellij.openapi.editor.impl.EditorImpl.CaretRectangle in project intellij-community by JetBrains.

the class ArgumentHintLayer method paintComponent.

@Override
protected void paintComponent(final Graphics g) {
    super.paintComponent(g);
    final Pair<Boolean, Argument> nextArg = myNextArg;
    if (nextArg == null) {
        // Nothing to show
        return;
    }
    /**
     *
     * We should display argument right after trimmed (spaces removed) document end or cursor position what ever comes first.
     *
     * We also need to add prompt length.
     */
    final EditorImpl consoleEditor = PyUtil.as(myConsole.getConsoleEditor(), EditorImpl.class);
    if (consoleEditor == null) {
        /**
       * We can't calculate anything if editor is not {@link EditorImpl})
       */
        Logger.getInstance(ArgumentHintLayer.class).warn("Bad editor: " + myConsole.getConsoleEditor());
        return;
    }
    final int consoleFontType = consoleEditor.getCaretModel().getTextAttributes().getFontType();
    final FontMetrics consoleFontMetrics = consoleEditor.getFontMetrics(consoleFontType);
    final Font consoleFont = consoleFontMetrics.getFont();
    // Copy rendering hints
    final Graphics2D sourceGraphics2 = PyUtil.as(consoleEditor.getComponent().getGraphics(), Graphics2D.class);
    if (sourceGraphics2 != null && g instanceof Graphics2D) {
        ((Graphics2D) g).setRenderingHints(sourceGraphics2.getRenderingHints());
    }
    final boolean argumentRequired = nextArg.first;
    final String argumentText = nextArg.second.getHelp().getHelpString();
    g.setFont(consoleFont);
    g.setColor(argumentRequired ? myRequiredColor : myOptionalColor);
    final String textToShow = wrapBracesIfNeeded(argumentRequired, StringUtil.isEmpty(argumentText) ? PyBundle.message("commandLine.argumentHint.defaultName") : argumentText);
    // Update caret position (if known)
    final CaretRectangle[] locations = consoleEditor.getCaretLocations(true);
    if (locations != null) {
        final CaretRectangle rectangle = locations[0];
        myCaretPositionPx = rectangle.myPoint.x;
    }
    final int consoleEditorTop = consoleEditor.getComponent().getLocation().y;
    final double textHeight = Math.floor(consoleFont.getStringBounds(textToShow, consoleFontMetrics.getFontRenderContext()).getY());
    // pixels in position should be integer, anyway
    @SuppressWarnings("NumericCastThatLosesPrecision") final int y = (int) (consoleEditorTop - textHeight);
    // We should take scrolling into account to prevent argument "flying" over text when user scrolls it, like "position:fixed" in css
    final Point scrollLocation = consoleEditor.getContentComponent().getLocation();
    final int spaceWidth = EditorUtil.getSpaceWidth(consoleFontType, consoleEditor);
    // Remove whitespaces on the end of document
    /**
     * TODO: This is actually copy/paste with {@link com.intellij.openapi.editor.actions.EditorActionUtil#moveCaretToLineEnd}.
     * Need to merge somehow.
     */
    final String trimmedDocument = StringUtil.trimTrailing(consoleEditor.getDocument().getText());
    final double trimmedDocumentWidth = consoleFont.getStringBounds(trimmedDocument, consoleFontMetrics.getFontRenderContext()).getWidth();
    // pixels in position should be integer, anyway
    @SuppressWarnings("NumericCastThatLosesPrecision") final int contentWidth = (int) Math.ceil(trimmedDocumentWidth + consoleEditor.getPrefixTextWidthInPixels());
    g.drawString(textToShow, Math.max(myCaretPositionPx, contentWidth) + scrollLocation.x + spaceWidth, y + scrollLocation.y);
}
Also used : Argument(com.jetbrains.commandInterface.command.Argument) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) CaretRectangle(com.intellij.openapi.editor.impl.EditorImpl.CaretRectangle)

Aggregations

EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)1 CaretRectangle (com.intellij.openapi.editor.impl.EditorImpl.CaretRectangle)1 Argument (com.jetbrains.commandInterface.command.Argument)1