protected static List<TextRange> getSelectedRanges(@NotNull SelectionModel selectionModel) { final List<TextRange> ranges = new SmartList<TextRange>(); if (selectionModel.hasSelection()) { TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); ranges.add(range); } else if (selectionModel.hasBlockSelection()) { int[] starts = selectionModel.getBlockSelectionStarts(); int[] ends = selectionModel.getBlockSelectionEnds(); for (int i = 0; i < starts.length; i++) { ranges.add(TextRange.create(starts[i], ends[i])); } } return ranges; }
private void updateInSelectionHighlighters() { if (mySearchResults.getEditor() == null) return; final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel(); int[] starts = selectionModel.getBlockSelectionStarts(); int[] ends = selectionModel.getBlockSelectionEnds(); final HashSet<RangeHighlighter> toRemove = new HashSet<RangeHighlighter>(); Set<RangeHighlighter> toAdd = new HashSet<RangeHighlighter>(); for (RangeHighlighter highlighter : myHighlighters) { boolean intersectsWithSelection = false; for (int i = 0; i < starts.length; ++i) { TextRange selectionRange = new TextRange(starts[i], ends[i]); intersectsWithSelection = selectionRange.intersects(highlighter.getStartOffset(), highlighter.getEndOffset()) && selectionRange.getEndOffset() != highlighter.getStartOffset() && highlighter.getEndOffset() != selectionRange.getStartOffset(); if (intersectsWithSelection) break; } final Object userData = highlighter.getUserData(IN_SELECTION_KEY); if (userData != null) { if (!intersectsWithSelection) { if (userData == IN_SELECTION2) { HighlightManager.getInstance(mySearchResults.getProject()) .removeSegmentHighlighter(mySearchResults.getEditor(), highlighter); toRemove.add(highlighter); } else { highlighter.putUserData(IN_SELECTION_KEY, null); } } } else if (intersectsWithSelection) { TextRange cursor = mySearchResults.getCursor(); if (cursor != null && highlighter.getStartOffset() == cursor.getStartOffset() && highlighter.getEndOffset() == cursor.getEndOffset()) continue; final RangeHighlighter toAnnotate = highlightRange( new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()), new TextAttributes(null, null, Color.WHITE, EffectType.ROUNDED_BOX, 0), toAdd); highlighter.putUserData(IN_SELECTION_KEY, IN_SELECTION1); toAnnotate.putUserData(IN_SELECTION_KEY, IN_SELECTION2); } } myHighlighters.removeAll(toRemove); myHighlighters.addAll(toAdd); }
private String getSyntaxInfo() { final StringBuilder builder = new StringBuilder(); final Editor editor = myFixture.getEditor(); String selectedText = editor.getSelectionModel().getSelectedText(true); assertNotNull(selectedText); final String text = StringUtil.convertLineSeparators(selectedText); TextWithMarkupProcessor processor = new TextWithMarkupProcessor() { @Override void createResult(SyntaxInfo syntaxInfo, Editor editor) { final ColorRegistry colorRegistry = syntaxInfo.getColorRegistry(); assertEquals(JBColor.BLACK, colorRegistry.dataById(syntaxInfo.getDefaultForeground())); assertEquals(JBColor.WHITE, colorRegistry.dataById(syntaxInfo.getDefaultBackground())); assertEquals(getFontSize(), syntaxInfo.getFontSize()); syntaxInfo.processOutputInfo( new MarkupHandler() { @Override public void handleText(int startOffset, int endOffset) throws Exception { builder .append("text=") .append(text.substring(startOffset, endOffset)) .append('\n'); } @Override public void handleForeground(int foregroundId) throws Exception { builder .append("foreground=") .append(colorRegistry.dataById(foregroundId)) .append(','); } @Override public void handleBackground(int backgroundId) throws Exception { builder .append("background=") .append(colorRegistry.dataById(backgroundId)) .append(','); } @Override public void handleFont(int fontNameId) throws Exception { assertEquals(1, fontNameId); } @Override public void handleStyle(int style) throws Exception { builder.append("fontStyle=").append(style).append(','); } @Override public boolean canHandleMore() { return true; } }); } }; SelectionModel selectionModel = editor.getSelectionModel(); processor.collectTransferableData( myFixture.getFile(), editor, selectionModel.getBlockSelectionStarts(), selectionModel.getBlockSelectionEnds()); return builder.toString(); }