/** * Returns a list of pairs of x coordinates for visual ranges representing given logical range. If * <code>startOffset == endOffset</code>, a pair of equal numbers is returned, corresponding to * target position. Target offsets are supposed to be located on the same visual line. */ private TFloatArrayList logicalRangeToVisualRanges(int startOffset, int endOffset) { assert startOffset <= endOffset; TFloatArrayList result = new TFloatArrayList(); for (VisualLineFragmentsIterator.Fragment fragment : VisualLineFragmentsIterator.create(myView, startOffset, false)) { int minOffset = fragment.getMinOffset(); int maxOffset = fragment.getMaxOffset(); if (startOffset == endOffset) { if (startOffset >= minOffset && startOffset <= maxOffset) { float x = fragment.offsetToX(startOffset); result.add(x); result.add(x); break; } } else if (startOffset < maxOffset && endOffset > minOffset) { float x1 = fragment.offsetToX(Math.max(minOffset, startOffset)); float x2 = fragment.offsetToX(Math.min(maxOffset, endOffset)); if (x1 > x2) { float tmp = x1; x1 = x2; x2 = tmp; } if (result.isEmpty() || x1 > result.get(result.size() - 1)) { result.add(x1); result.add(x2); } else { result.set(result.size() - 1, x2); } } } return result; }
@Override public void layoutContainer(final Container parent) { final int componentCount = parent.getComponentCount(); if (componentCount == 0) return; final EditorEx history = myHistoryViewer; final EditorEx editor = componentCount == 2 ? myConsoleEditor : null; if (editor == null) { parent.getComponent(0).setBounds(parent.getBounds()); return; } final Dimension panelSize = parent.getSize(); if (panelSize.getHeight() <= 0) return; final Dimension historySize = history.getContentSize(); final Dimension editorSize = editor.getContentSize(); final Dimension newEditorSize = new Dimension(); // deal with width final int width = Math.max(editorSize.width, historySize.width); newEditorSize.width = width + editor.getScrollPane().getHorizontalScrollBar().getHeight(); history.getSoftWrapModel().forceAdditionalColumnsUsage(); editor .getSettings() .setAdditionalColumnsCount( 2 + (width - editorSize.width) / EditorUtil.getSpaceWidth(Font.PLAIN, editor)); history .getSettings() .setAdditionalColumnsCount( 2 + (width - historySize.width) / EditorUtil.getSpaceWidth(Font.PLAIN, history)); // deal with height if (historySize.width == 0) historySize.height = 0; final int minHistorySize = historySize.height > 0 ? 2 * history.getLineHeight() + (myShowSeparatorLine ? SEPARATOR_THICKNESS : 0) : 0; final int minEditorSize = editor.isViewer() ? 0 : editor.getLineHeight(); final int editorPreferred = editor.isViewer() ? 0 : Math.max(minEditorSize, editorSize.height); final int historyPreferred = Math.max(minHistorySize, historySize.height); if (panelSize.height < minEditorSize) { newEditorSize.height = panelSize.height; } else if (panelSize.height < editorPreferred) { newEditorSize.height = panelSize.height - minHistorySize; } else if (panelSize.height < editorPreferred + historyPreferred) { newEditorSize.height = editorPreferred; } else { newEditorSize.height = editorPreferred == 0 ? 0 : panelSize.height - historyPreferred; } final Dimension newHistorySize = new Dimension(width, panelSize.height - newEditorSize.height); // apply editor .getComponent() .setBounds(0, newHistorySize.height, panelSize.width, newEditorSize.height); myForceScrollToEnd.compareAndSet(false, shouldScrollHistoryToEnd()); history.getComponent().setBounds(0, 0, panelSize.width, newHistorySize.height); }
private static void duplicateHighlighters( MarkupModel to, MarkupModel from, int offset, TextRange textRange) { for (RangeHighlighter rangeHighlighter : from.getAllHighlighters()) { if (!rangeHighlighter.isValid()) continue; Object tooltip = rangeHighlighter.getErrorStripeTooltip(); HighlightInfo highlightInfo = tooltip instanceof HighlightInfo ? (HighlightInfo) tooltip : null; if (highlightInfo != null) { if (highlightInfo.getSeverity() != HighlightSeverity.INFORMATION) continue; if (highlightInfo.type.getAttributesKey() == EditorColors.IDENTIFIER_UNDER_CARET_ATTRIBUTES) continue; } final int localOffset = textRange.getStartOffset(); final int start = Math.max(rangeHighlighter.getStartOffset(), localOffset) - localOffset; final int end = Math.min(rangeHighlighter.getEndOffset(), textRange.getEndOffset()) - localOffset; if (start > end) continue; final RangeHighlighter h = to.addRangeHighlighter( start + offset, end + offset, rangeHighlighter.getLayer(), rangeHighlighter.getTextAttributes(), rangeHighlighter.getTargetArea()); ((RangeHighlighterEx) h) .setAfterEndOfLine(((RangeHighlighterEx) rangeHighlighter).isAfterEndOfLine()); } }
private void paintVirtualSelectionIfNecessary( Graphics2D g, Map<Integer, Couple<Integer>> virtualSelectionMap, int columnStart, float xStart, float xEnd, int y) { int visualLine = myView.yToVisualLine(y); Couple<Integer> selectionRange = virtualSelectionMap.get(visualLine); if (selectionRange == null || selectionRange.second <= columnStart) return; float startX = selectionRange.first <= columnStart ? xStart : myView.visualPositionToXY(new VisualPosition(visualLine, selectionRange.first)).x; float endX = Math.min( xEnd, myView.visualPositionToXY(new VisualPosition(visualLine, selectionRange.second)).x); paintBackground( g, myEditor.getColorsScheme().getColor(EditorColors.SELECTION_BACKGROUND_COLOR), startX, y, endX - startX); }
private void paintCaret(Graphics2D g_) { EditorImpl.CaretRectangle[] locations = myEditor.getCaretLocations(true); if (locations == null) return; Graphics2D g = IdeBackgroundUtil.getOriginalGraphics(g_); int lineHeight = myView.getLineHeight(); EditorSettings settings = myEditor.getSettings(); Color caretColor = myEditor.getColorsScheme().getColor(EditorColors.CARET_COLOR); if (caretColor == null) caretColor = new JBColor(CARET_DARK, CARET_LIGHT); g.setColor(caretColor); for (EditorImpl.CaretRectangle location : locations) { int x = location.myPoint.x; int y = location.myPoint.y; Caret caret = location.myCaret; boolean isRtl = location.myIsRtl; if (myEditor.isInsertMode() != settings.isBlockCursor()) { int lineWidth = JBUI.scale(settings.getLineCursorWidth()); g.fillRect(x, y, lineWidth, lineHeight); if (myDocument.getTextLength() > 0 && caret != null && !myView.getLineLayout(caret.getLogicalPosition().line).isLtr()) { g.fillPolygon( new int[] { isRtl ? x + lineWidth : x, isRtl ? x + lineWidth - CARET_DIRECTION_MARK_SIZE : x + CARET_DIRECTION_MARK_SIZE, isRtl ? x + lineWidth : x }, new int[] {y, y, y + CARET_DIRECTION_MARK_SIZE}, 3); } } else { int width = location.myWidth; int startX = Math.max(0, isRtl ? x - width : x); g.fillRect(startX, y, width, lineHeight - 1); if (myDocument.getTextLength() > 0 && caret != null) { int targetVisualColumn = caret.getVisualPosition().column; for (VisualLineFragmentsIterator.Fragment fragment : VisualLineFragmentsIterator.create(myView, caret.getVisualLineStart(), false)) { int startVisualColumn = fragment.getStartVisualColumn(); int endVisualColumn = fragment.getEndVisualColumn(); if (startVisualColumn < targetVisualColumn && endVisualColumn > targetVisualColumn || startVisualColumn == targetVisualColumn && !isRtl || endVisualColumn == targetVisualColumn && isRtl) { g.setColor(ColorUtil.isDark(caretColor) ? CARET_LIGHT : CARET_DARK); fragment.draw( g, startX, y + myView.getAscent(), targetVisualColumn - startVisualColumn - (isRtl ? 1 : 0), targetVisualColumn - startVisualColumn + (isRtl ? 0 : 1)); break; } } } } } }
private void paintComposedTextDecoration(Graphics2D g) { TextRange composedTextRange = myEditor.getComposedTextRange(); if (composedTextRange != null) { Point p1 = myView.offsetToXY( Math.min(composedTextRange.getStartOffset(), myDocument.getTextLength()), true, false); Point p2 = myView.offsetToXY( Math.min(composedTextRange.getEndOffset(), myDocument.getTextLength()), false, true); int y = p1.y + myView.getAscent() + 1; g.setStroke(IME_COMPOSED_TEXT_UNDERLINE_STROKE); g.setColor(myEditor.getColorsScheme().getDefaultForeground()); UIUtil.drawLine(g, p1.x, y, p2.x, y); } }
void repaintCarets() { EditorImpl.CaretRectangle[] locations = myEditor.getCaretLocations(false); if (locations == null) return; int lineHeight = myView.getLineHeight(); for (EditorImpl.CaretRectangle location : locations) { int x = location.myPoint.x; int y = location.myPoint.y; int width = Math.max(location.myWidth, CARET_DIRECTION_MARK_SIZE); myEditor.getContentComponent().repaintEditorComponent(x - width, y, width * 2, lineHeight); } }
void paint(Graphics2D g) { Rectangle clip = g.getClipBounds(); if (myEditor.getContentComponent().isOpaque()) { g.setColor(myEditor.getBackgroundColor()); g.fillRect(clip.x, clip.y, clip.width, clip.height); } if (paintPlaceholderText(g)) { paintCaret(g); return; } int startLine = myView.yToVisualLine(Math.max(clip.y, 0)); int endLine = myView.yToVisualLine(Math.max(clip.y + clip.height, 0)); int startOffset = myView.visualPositionToOffset(new VisualPosition(startLine, 0)); int endOffset = myView.visualPositionToOffset(new VisualPosition(endLine + 1, 0, true)); ClipDetector clipDetector = new ClipDetector(myEditor, clip); paintBackground(g, clip, startLine, endLine); paintRightMargin(g, clip); paintCustomRenderers(g, startOffset, endOffset); MarkupModelEx docMarkup = (MarkupModelEx) DocumentMarkupModel.forDocument(myDocument, myEditor.getProject(), true); paintLineMarkersSeparators(g, clip, docMarkup, startOffset, endOffset); paintLineMarkersSeparators(g, clip, myEditor.getMarkupModel(), startOffset, endOffset); paintTextWithEffects(g, clip, startLine, endLine); paintHighlightersAfterEndOfLine(g, docMarkup, startOffset, endOffset); paintHighlightersAfterEndOfLine(g, myEditor.getMarkupModel(), startOffset, endOffset); paintBorderEffect(g, clipDetector, myEditor.getHighlighter(), startOffset, endOffset); paintBorderEffect(g, clipDetector, docMarkup, startOffset, endOffset); paintBorderEffect(g, clipDetector, myEditor.getMarkupModel(), startOffset, endOffset); paintCaret(g); paintComposedTextDecoration(g); }
private void paintLineMarkerSeparator(RangeHighlighter marker, Rectangle clip, Graphics g) { Color separatorColor = marker.getLineSeparatorColor(); LineSeparatorRenderer lineSeparatorRenderer = marker.getLineSeparatorRenderer(); if (separatorColor == null && lineSeparatorRenderer == null) { return; } int line = myDocument.getLineNumber( marker.getLineSeparatorPlacement() == SeparatorPlacement.TOP ? marker.getStartOffset() : marker.getEndOffset()); int visualLine = myView.logicalToVisualPosition( new LogicalPosition( line + (marker.getLineSeparatorPlacement() == SeparatorPlacement.TOP ? 0 : 1), 0), false) .line; int y = myView.visualLineToY(visualLine) - 1; int endShift = clip.x + clip.width; EditorSettings settings = myEditor.getSettings(); if (settings.isRightMarginShown() && myEditor.getColorsScheme().getColor(EditorColors.RIGHT_MARGIN_COLOR) != null) { endShift = Math.min( endShift, settings.getRightMargin(myEditor.getProject()) * myView.getPlainSpaceWidth()); } g.setColor(separatorColor); if (lineSeparatorRenderer != null) { lineSeparatorRenderer.drawLine(g, 0, endShift, y); } else { UIUtil.drawLine(g, 0, y, endShift, y); } }
private void paintLineFragments( Graphics2D g, Rectangle clip, int visualLine, int y, LineFragmentPainter painter) { float x = visualLine == 0 ? myView.getPrefixTextWidthInPixels() : 0; int offset = myView.visualPositionToOffset(new VisualPosition(visualLine, 0)); int visualLineEndOffset = myView.visualPositionToOffset(new VisualPosition(visualLine, Integer.MAX_VALUE, true)); IterationState it = null; int prevEndOffset = -1; boolean firstFragment = true; int maxColumn = 0; for (VisualLineFragmentsIterator.Fragment fragment : VisualLineFragmentsIterator.create(myView, offset, false)) { int fragmentStartOffset = fragment.getStartOffset(); int start = fragmentStartOffset; int end = fragment.getEndOffset(); x = fragment.getStartX(); if (firstFragment) { firstFragment = false; SoftWrap softWrap = myEditor.getSoftWrapModel().getSoftWrap(offset); if (softWrap != null) { prevEndOffset = offset; it = new IterationState( myEditor, offset == 0 ? 0 : offset - 1, visualLineEndOffset, true, false, false, false); if (it.getEndOffset() <= offset) { it.advance(); } painter.paintBeforeLineStart( g, it.getStartOffset() == offset ? it.getBeforeLineStartBackgroundAttributes() : it.getMergedAttributes(), fragment.getStartVisualColumn(), fragment.getStartX(), y); } } FoldRegion foldRegion = fragment.getCurrentFoldRegion(); if (foldRegion == null) { if (start != prevEndOffset) { it = new IterationState( myEditor, start, fragment.isRtl() ? offset : visualLineEndOffset, true, false, false, fragment.isRtl()); } prevEndOffset = end; assert it != null; while (fragment.isRtl() ? start > end : start < end) { if (fragment.isRtl() ? it.getEndOffset() >= start : it.getEndOffset() <= start) { assert !it.atEnd(); it.advance(); } TextAttributes attributes = it.getMergedAttributes(); int curEnd = fragment.isRtl() ? Math.max(it.getEndOffset(), end) : Math.min(it.getEndOffset(), end); float xNew = fragment.offsetToX(x, start, curEnd); painter.paint( g, fragment, fragment.isRtl() ? fragmentStartOffset - start : start - fragmentStartOffset, fragment.isRtl() ? fragmentStartOffset - curEnd : curEnd - fragmentStartOffset, attributes, x, xNew, y); x = xNew; start = curEnd; } } else { float xNew = fragment.getEndX(); painter.paint( g, fragment, 0, fragment.getEndVisualColumn() - fragment.getStartVisualColumn(), getFoldRegionAttributes(foldRegion), x, xNew, y); x = xNew; prevEndOffset = -1; it = null; } maxColumn = fragment.getEndVisualColumn(); } if (it == null || it.getEndOffset() != visualLineEndOffset) { it = new IterationState( myEditor, visualLineEndOffset == offset ? visualLineEndOffset : visualLineEndOffset - 1, visualLineEndOffset, true, false, false, false); } if (!it.atEnd()) { it.advance(); } assert it.atEnd(); painter.paintAfterLineEnd(g, clip, it, maxColumn, x, y); }
private void paintBorderEffect( Graphics2D g, ClipDetector clipDetector, int startOffset, int endOffset, TextAttributes attributes) { if (!clipDetector.rangeCanBeVisible(startOffset, endOffset)) return; int startLine = myDocument.getLineNumber(startOffset); int endLine = myDocument.getLineNumber(endOffset); if (startLine + 1 == endLine && startOffset == myDocument.getLineStartOffset(startLine) && endOffset == myDocument.getLineStartOffset(endLine)) { // special case of line highlighters endLine--; endOffset = myDocument.getLineEndOffset(endLine); } boolean rounded = attributes.getEffectType() == EffectType.ROUNDED_BOX; int lineHeight = myView.getLineHeight() - 1; g.setColor(attributes.getEffectColor()); VisualPosition startPosition = myView.offsetToVisualPosition(startOffset, true, false); VisualPosition endPosition = myView.offsetToVisualPosition(endOffset, false, true); if (startPosition.line == endPosition.line) { int y = myView.visualLineToY(startPosition.line); TFloatArrayList ranges = adjustedLogicalRangeToVisualRanges(startOffset, endOffset); for (int i = 0; i < ranges.size() - 1; i += 2) { int startX = (int) ranges.get(i); int endX = (int) ranges.get(i + 1); if (rounded) { UIUtil.drawRectPickedOut(g, startX, y, endX - startX, lineHeight); } else { g.drawRect(startX, y, endX - startX, lineHeight); } } } else { int maxWidth = myView.getMaxWidthInLineRange(startPosition.line, endPosition.line) - 1; TFloatArrayList leadingRanges = adjustedLogicalRangeToVisualRanges( startOffset, myView.visualPositionToOffset( new VisualPosition(startPosition.line, Integer.MAX_VALUE, true))); TFloatArrayList trailingRanges = adjustedLogicalRangeToVisualRanges( myView.visualPositionToOffset(new VisualPosition(endPosition.line, 0)), endOffset); if (!leadingRanges.isEmpty() && !trailingRanges.isEmpty()) { boolean containsInnerLines = endPosition.line > startPosition.line + 1; int leadingTopY = myView.visualLineToY(startPosition.line); int leadingBottomY = leadingTopY + lineHeight; int trailingTopY = myView.visualLineToY(endPosition.line); int trailingBottomY = trailingTopY + lineHeight; float start = 0; float end = 0; float leftGap = leadingRanges.get(0) - (containsInnerLines ? 0 : trailingRanges.get(0)); int adjustY = leftGap == 0 ? 2 : leftGap > 0 ? 1 : 0; // avoiding 1-pixel gap between aligned lines for (int i = 0; i < leadingRanges.size() - 1; i += 2) { start = leadingRanges.get(i); end = leadingRanges.get(i + 1); if (i > 0) { drawLine(g, leadingRanges.get(i - 1), leadingBottomY, start, leadingBottomY, rounded); } drawLine(g, start, leadingBottomY + (i == 0 ? adjustY : 0), start, leadingTopY, rounded); if ((i + 2) < leadingRanges.size()) { drawLine(g, start, leadingTopY, end, leadingTopY, rounded); drawLine(g, end, leadingTopY, end, leadingBottomY, rounded); } } end = Math.max(end, maxWidth); drawLine(g, start, leadingTopY, end, leadingTopY, rounded); drawLine(g, end, leadingTopY, end, trailingTopY - 1, rounded); float targetX = trailingRanges.get(trailingRanges.size() - 1); drawLine(g, end, trailingTopY - 1, targetX, trailingTopY - 1, rounded); adjustY = end == targetX ? -2 : -1; // for lastX == targetX we need to avoid a gap when rounding is used for (int i = trailingRanges.size() - 2; i >= 0; i -= 2) { start = trailingRanges.get(i); end = trailingRanges.get(i + 1); drawLine(g, end, trailingTopY + (i == 0 ? adjustY : 0), end, trailingBottomY, rounded); drawLine(g, end, trailingBottomY, start, trailingBottomY, rounded); drawLine(g, start, trailingBottomY, start, trailingTopY, rounded); if (i > 0) { drawLine(g, start, trailingTopY, trailingRanges.get(i - 1), trailingTopY, rounded); } } float lastX = start; if (containsInnerLines) { if (start > 0) { drawLine(g, start, trailingTopY, start, trailingTopY - 1, rounded); drawLine(g, start, trailingTopY - 1, 0, trailingTopY - 1, rounded); drawLine(g, 0, trailingTopY - 1, 0, leadingBottomY + 1, rounded); } else { drawLine(g, start, trailingTopY, 0, leadingBottomY + 1, rounded); } lastX = 0; } targetX = leadingRanges.get(0); if (lastX < targetX) { drawLine(g, lastX, leadingBottomY + 1, targetX, leadingBottomY + 1, rounded); } else { drawLine(g, lastX, leadingBottomY + 1, lastX, leadingBottomY, rounded); drawLine(g, lastX, leadingBottomY, targetX, leadingBottomY, rounded); } } } }
protected String addTextRangeToHistory( TextRange textRange, final EditorEx consoleEditor, boolean preserveMarkup) { final Document history = myHistoryViewer.getDocument(); final MarkupModel markupModel = DocumentMarkupModel.forDocument(history, myProject, true); if (myPrompt != null) { appendToHistoryDocument(history, myPrompt); } markupModel.addRangeHighlighter( history.getTextLength() - StringUtil.length(myPrompt), history.getTextLength(), HighlighterLayer.SYNTAX, ConsoleViewContentType.USER_INPUT.getAttributes(), HighlighterTargetArea.EXACT_RANGE); final int localStartOffset = textRange.getStartOffset(); String text; EditorHighlighter highlighter; if (consoleEditor instanceof EditorWindow) { EditorWindow editorWindow = (EditorWindow) consoleEditor; EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme(); PsiFile file = editorWindow.getInjectedFile(); final VirtualFile virtualFile = file.getVirtualFile(); assert virtualFile != null; highlighter = HighlighterFactory.createHighlighter(virtualFile, scheme, getProject()); String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null); highlighter.setText(fullText); text = textRange.substring(fullText); } else { text = consoleEditor.getDocument().getText(textRange); highlighter = consoleEditor.getHighlighter(); } // offset can be changed after text trimming after insert due to buffer constraints appendToHistoryDocument(history, text); int offset = history.getTextLength() - text.length(); final HighlighterIterator iterator = highlighter.createIterator(localStartOffset); final int localEndOffset = textRange.getEndOffset(); while (!iterator.atEnd()) { final int itStart = iterator.getStart(); if (itStart > localEndOffset) break; final int itEnd = iterator.getEnd(); if (itEnd >= localStartOffset) { final int start = Math.max(itStart, localStartOffset) - localStartOffset + offset; final int end = Math.min(itEnd, localEndOffset) - localStartOffset + offset; markupModel.addRangeHighlighter( start, end, HighlighterLayer.SYNTAX, iterator.getTextAttributes(), HighlighterTargetArea.EXACT_RANGE); } iterator.advance(); } if (preserveMarkup) { duplicateHighlighters( markupModel, DocumentMarkupModel.forDocument(consoleEditor.getDocument(), myProject, true), offset, textRange); duplicateHighlighters(markupModel, consoleEditor.getMarkupModel(), offset, textRange); } if (!text.endsWith("\n")) { appendToHistoryDocument(history, "\n"); } return text; }