@Override protected void updateTitle(@Nullable final PsiVariable variable, final String value) { final PsiElement declarationScope = variable != null ? ((PsiParameter) variable).getDeclarationScope() : null; if (declarationScope instanceof PsiMethod) { final PsiMethod psiMethod = (PsiMethod) declarationScope; final StringBuilder buf = new StringBuilder(); buf.append(psiMethod.getName()).append(" ("); boolean frst = true; final List<TextRange> ranges2Remove = new ArrayList<>(); TextRange addedRange = null; for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) { if (frst) { frst = false; } else { buf.append(", "); } int startOffset = buf.length(); if (myMustBeFinal || myPanel.isGenerateFinal()) { buf.append("final "); } buf.append(parameter.getType().getPresentableText()) .append(" ") .append(variable == parameter ? value : parameter.getName()); int endOffset = buf.length(); if (variable == parameter) { addedRange = new TextRange(startOffset, endOffset); } else if (myPanel.isParamToRemove(parameter)) { ranges2Remove.add(new TextRange(startOffset, endOffset)); } } buf.append(")"); setPreviewText(buf.toString()); final MarkupModel markupModel = DocumentMarkupModel.forDocument(getPreviewEditor().getDocument(), myProject, true); markupModel.removeAllHighlighters(); for (TextRange textRange : ranges2Remove) { markupModel.addRangeHighlighter( textRange.getStartOffset(), textRange.getEndOffset(), 0, getTestAttributesForRemoval(), HighlighterTargetArea.EXACT_RANGE); } markupModel.addRangeHighlighter( addedRange.getStartOffset(), addedRange.getEndOffset(), 0, getTextAttributesForAdd(), HighlighterTargetArea.EXACT_RANGE); revalidate(); } }
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 highlightNotification( final Notification notification, String message, final int line1, final int line2) { final MarkupModel markupModel = myLogEditor.getValue().getMarkupModel(); TextAttributes bold = new TextAttributes(null, null, null, null, Font.BOLD); final List<RangeHighlighter> lineColors = new ArrayList<RangeHighlighter>(); for (int line = line1; line < line2; line++) { final RangeHighlighter lineHighlighter = markupModel.addLineHighlighter(line, HighlighterLayer.CARET_ROW + 1, bold); Color color = notification.getType() == NotificationType.ERROR ? JBColor.RED : notification.getType() == NotificationType.WARNING ? JBColor.YELLOW : JBColor.GREEN; lineHighlighter.setErrorStripeMarkColor(color); lineHighlighter.setErrorStripeTooltip(message); lineColors.add(lineHighlighter); } final Runnable removeHandler = new Runnable() { @Override public void run() { for (RangeHighlighter color : lineColors) { markupModel.removeHighlighter(color); } TextAttributes attributes = EditorColorsManager.getInstance() .getGlobalScheme() .getAttributes(ConsoleViewContentType.LOG_EXPIRED_ENTRY); for (int line = line1; line < line2; line++) { markupModel.addLineHighlighter(line, HighlighterLayer.CARET_ROW + 1, attributes); } TextAttributes italic = new TextAttributes(null, null, null, null, Font.ITALIC); for (int line = line1; line < line2; line++) { for (RangeHighlighter highlighter : myHyperlinkSupport.getValue().findAllHyperlinksOnLine(line)) { markupModel.addRangeHighlighter( highlighter.getStartOffset(), highlighter.getEndOffset(), HighlighterLayer.CARET_ROW + 2, italic, HighlighterTargetArea.EXACT_RANGE); myHyperlinkSupport.getValue().removeHyperlink(highlighter); } } } }; if (!notification.isExpired()) { myProjectModel.removeHandlers.put(notification, removeHandler); } else { removeHandler.run(); } }
public void testRangeHighlighterDisposeVsRemoveAllConflict() throws Exception { Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); MarkupModel markupModel = DocumentMarkupModel.forDocument(document, ourProject, true); RangeMarker m = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); assertTrue(m.isValid()); markupModel.removeAllHighlighters(); assertFalse(m.isValid()); assertEmpty(markupModel.getAllHighlighters()); m.dispose(); assertFalse(m.isValid()); }
public static void clearMyHighlights(Document document, Project project) { MarkupModel markupModel = DocumentMarkupModel.forDocument(document, project, true); for (RangeHighlighter highlighter : markupModel.getAllHighlighters()) { Object tooltip = highlighter.getErrorStripeTooltip(); if (!(tooltip instanceof HighlightInfo)) { continue; } HighlightInfo info = (HighlightInfo) tooltip; if (info.type == HighlightInfoType.ELEMENT_UNDER_CARET_READ || info.type == HighlightInfoType.ELEMENT_UNDER_CARET_WRITE) { highlighter.dispose(); } } }
public void testRangeHighlightersRecreateBug() throws Exception { Document document = EditorFactory.getInstance().createDocument("[xxxxxxxxxxxxxx]"); MarkupModel markupModel = DocumentMarkupModel.forDocument(document, ourProject, true); for (int i = 0; i < 2; i++) { RangeMarker m = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); RangeMarker m2 = markupModel.addRangeHighlighter(2, 7, 0, null, HighlighterTargetArea.EXACT_RANGE); RangeMarker m3 = markupModel.addRangeHighlighter(1, 6, 0, null, HighlighterTargetArea.EXACT_RANGE); markupModel.removeAllHighlighters(); } }
public void printToHistory(String text, final TextAttributes attributes) { ApplicationManager.getApplication().assertIsDispatchThread(); text = StringUtil.convertLineSeparators(text); final boolean scrollToEnd = shouldScrollHistoryToEnd(); final Document history = myHistoryViewer.getDocument(); final MarkupModel markupModel = DocumentMarkupModel.forDocument(history, myProject, true); final int offset = history.getTextLength(); appendToHistoryDocument(history, text); markupModel.addRangeHighlighter( offset, history.getTextLength(), HighlighterLayer.SYNTAX, attributes, HighlighterTargetArea.EXACT_RANGE); if (scrollToEnd) { scrollHistoryToEnd(); } queueUiUpdate(scrollToEnd); }
public void printToHistory(@NotNull final List<Pair<String, TextAttributes>> attributedText) { ApplicationManager.getApplication().assertIsDispatchThread(); if (LOG.isDebugEnabled()) { LOG.debug("printToHistory(): " + attributedText.size()); } final boolean scrollToEnd = shouldScrollHistoryToEnd(); final int[] offsets = new int[attributedText.size() + 1]; int i = 0; offsets[i] = 0; final StringBuilder sb = new StringBuilder(); for (final Pair<String, TextAttributes> pair : attributedText) { sb.append(StringUtil.convertLineSeparators(pair.getFirst())); offsets[++i] = sb.length(); } final DocumentEx history = myHistoryViewer.getDocument(); final int oldHistoryLength = history.getTextLength(); appendToHistoryDocument(history, sb.toString()); assert oldHistoryLength + offsets[i] == history.getTextLength() : "unexpected history length " + oldHistoryLength + " " + offsets[i] + " " + history.getTextLength(); LOG.debug("printToHistory(): text processed"); final MarkupModel markupModel = DocumentMarkupModel.forDocument(history, myProject, true); i = 0; for (final Pair<String, TextAttributes> pair : attributedText) { markupModel.addRangeHighlighter( oldHistoryLength + offsets[i], oldHistoryLength + offsets[i + 1], HighlighterLayer.SYNTAX, pair.getSecond(), HighlighterTargetArea.EXACT_RANGE); ++i; } LOG.debug("printToHistory(): markup added"); if (scrollToEnd) { scrollHistoryToEnd(); } queueUiUpdate(scrollToEnd); LOG.debug("printToHistory(): completed"); }
public static void clearHighlightingAndLineMarkers( final Editor editor, @NotNull Project project) { final MarkupModel markupModel = DocumentMarkupModel.forDocument(editor.getDocument(), project, true); for (RangeHighlighter highlighter : markupModel.getAllHighlighters()) { Object tooltip = highlighter.getErrorStripeTooltip(); if (!(tooltip instanceof HighlightInfo)) { continue; } if (((HighlightInfo) tooltip).type == TYPE) { highlighter.dispose(); } } clearLineMarkers(editor); }
@NotNull private static RangeHighlighter createHighlighter(MarkupModel mm, TextRange range) { final RangeHighlighter highlighter = mm.addRangeHighlighter( range.getStartOffset(), range.getEndOffset(), 0, null, HighlighterTargetArea.EXACT_RANGE); highlighter.setCustomRenderer(RENDERER); return highlighter; }
public void addComment( Editor editor, ChangeInfo changeInfo, String revisionId, Project project, Comment comment) { if (editor == null) return; MarkupModel markup = editor.getMarkupModel(); RangeHighlighter rangeHighlighter = null; if (comment.range != null) { rangeHighlighter = highlightRangeComment(comment.range, editor, project); } int lineCount = markup.getDocument().getLineCount(); int line = comment.line - 1; if (line < 0) { line = 0; } if (line > lineCount - 1) { line = lineCount - 1; } if (line >= 0) { final RangeHighlighter highlighter = markup.addLineHighlighter(line, HighlighterLayer.ERROR + 1, null); CommentGutterIconRenderer iconRenderer = new CommentGutterIconRenderer( this, editor, gerritUtil, selectedRevisions, addCommentActionBuilder, comment, changeInfo, revisionId, highlighter, rangeHighlighter); highlighter.setGutterIconRenderer(iconRenderer); } }
@NotNull public static RangeHighlighter createRangeHighlighter( @NotNull Range range, @NotNull TextRange textRange, @NotNull MarkupModel markupModel) { TextAttributes attributes = getTextAttributes(range); final RangeHighlighter highlighter = markupModel.addRangeHighlighter( textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.FIRST - 1, attributes, HighlighterTargetArea.LINES_IN_RANGE); highlighter.setThinErrorStripeMark(true); highlighter.setGreedyToLeft(true); highlighter.setGreedyToRight(true); highlighter.setErrorStripeTooltip(getTooltipText(range)); return highlighter; }
@NotNull private static RangeHighlighter createHighlighter( final MarkupModel mm, @NotNull final TextRange range, final Color color) { final RangeHighlighter highlighter = mm.addRangeHighlighter( range.getStartOffset(), range.getEndOffset(), 0, null, HighlighterTargetArea.LINES_IN_RANGE); highlighter.setLineMarkerRenderer( new LineMarkerRenderer() { @Override public void paint(Editor editor, Graphics g, Rectangle r) { g.setColor(color); g.fillRect(r.x, r.y, 2, r.height); } }); return highlighter; }
private void addVCGutterIcons( VCOutputFile vco, Editor editor, Project project, MyProverListener listener) { if (!editor.isDisposed()) { highlighters.clear(); MarkupModel markup = editor.getMarkupModel(); RESOLVEPluginController controller = RESOLVEPluginController.getInstance(project); markup.removeAllHighlighters(); // A mapping from [line number] -> [vc_1, .., vc_j] Map<Integer, List<VC>> byLine = vco.getVCsGroupedByLineNumber(); List<RangeHighlighter> vcRelatedHighlighters = new ArrayList<>(); for (Map.Entry<Integer, List<VC>> vcsByLine : byLine.entrySet()) { List<AnAction> actionsPerVC = new ArrayList<>(); // create clickable actions for each vc for (VC vc : vcsByLine.getValue()) { actionsPerVC.add( new VCNavigationAction(listener, vc.getNumber() + "", vc.getExplanation())); } highlighter = markup.addLineHighlighter( vcsByLine.getKey() - 1, HighlighterLayer.ELEMENT_UNDER_CARET, null); highlighter.setGutterIconRenderer( new GutterIconRenderer() { @NotNull @Override public Icon getIcon() { return RESOLVEIcons.VC; } @Override public boolean equals(Object obj) { return false; } @Override public int hashCode() { return 0; } @Override public boolean isNavigateAction() { return true; } @Nullable public ActionGroup getPopupMenuActions() { DefaultActionGroup g = new DefaultActionGroup(); g.addAll(actionsPerVC); return g; } @Nullable public AnAction getClickAction() { return null; } }); vcRelatedHighlighters.add(highlighter); highlighters.add(highlighter); } editor .getDocument() .addDocumentListener( new DocumentListener() { @Override public void beforeDocumentChange(DocumentEvent event) {} @Override public void documentChanged(DocumentEvent event) { // remove all highlighters for (RangeHighlighter h : vcRelatedHighlighters) { markup.removeHighlighter(h); } VerifierPanel verifierPanel = controller.getVerifierPanel(); controller.getVerifierWindow().hide(null); verifierPanel.revertToBaseGUI(); } }); } }
@Nullable public static List<LineMarkerInfo> getLineMarkers(Document document, Project project) { ApplicationManager.getApplication().assertIsDispatchThread(); MarkupModel markup = DocumentMarkupModel.forDocument(document, project, true); return markup.getUserData(MARKERS_IN_EDITOR_DOCUMENT_KEY); }
public static void setLineMarkers( @NotNull Document document, List<LineMarkerInfo> lineMarkers, Project project) { ApplicationManager.getApplication().assertIsDispatchThread(); MarkupModel markup = DocumentMarkupModel.forDocument(document, project, true); markup.putUserData(MARKERS_IN_EDITOR_DOCUMENT_KEY, lineMarkers); }
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; }
private RangeHighlighter createRangeHighlighter( final long date, final MarkupModel markupModel, final boolean coverageByTestApplicable, final TreeMap<Integer, LineData> executableLines, @Nullable final String className, final int line, final int lineNumberInCurrent, @NotNull final CoverageSuitesBundle coverageSuite, Object[] lines) { EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme(); final TextAttributes attributes = scheme.getAttributes(CoverageLineMarkerRenderer.getAttributesKey(line, executableLines)); TextAttributes textAttributes = null; if (attributes.getBackgroundColor() != null) { textAttributes = attributes; } final int startOffset = myDocument.getLineStartOffset(lineNumberInCurrent); final int endOffset = myDocument.getLineEndOffset(lineNumberInCurrent); final RangeHighlighter highlighter = markupModel.addRangeHighlighter( startOffset, endOffset, HighlighterLayer.SELECTION - 1, textAttributes, HighlighterTargetArea.LINES_IN_RANGE); final Function<Integer, Integer> newToOldConverter = newLine -> { if (myEditor == null) return -1; final TIntIntHashMap oldLineMapping = getNewToOldLineMapping(date); return oldLineMapping != null ? oldLineMapping.get(newLine.intValue()) : newLine.intValue(); }; final Function<Integer, Integer> oldToNewConverter = newLine -> { if (myEditor == null) return -1; final TIntIntHashMap newLineMapping = getOldToNewLineMapping(date); return newLineMapping != null ? newLineMapping.get(newLine.intValue()) : newLine.intValue(); }; final CoverageLineMarkerRenderer markerRenderer = coverageSuite .getCoverageEngine() .getLineMarkerRenderer( line, className, executableLines, coverageByTestApplicable, coverageSuite, newToOldConverter, oldToNewConverter, CoverageDataManager.getInstance(myProject).isSubCoverageActive()); highlighter.setLineMarkerRenderer(markerRenderer); final LineData lineData = className != null ? (LineData) lines[line + 1] : null; if (lineData != null && lineData.getStatus() == LineCoverage.NONE) { highlighter.setErrorStripeMarkColor(markerRenderer.getErrorStripeColor(myEditor)); highlighter.setThinErrorStripeMark(true); highlighter.setGreedyToLeft(true); highlighter.setGreedyToRight(true); } return highlighter; }