@Override public void colorize(TextPresentation presentation, Throwable error) { add(presentation); if (waitForToLineNumber != null) { int offset = presentation.getExtent().getOffset() + presentation.getExtent().getLength(); try { if (waitForToLineNumber != document.getLineOfOffset(offset)) { return; } else { waitForToLineNumber = null; } } catch (BadLocationException e) { e.printStackTrace(); } } ((Command) command).setStyleRanges("[" + currentRanges.toString() + "]"); synchronized (lock) { lock.notifyAll(); } }
/** * Create a runnable for updating the presentation. * <p> * NOTE: Called from background thread. * </p> * * @param textPresentation * the text presentation * @param addedPositions * the added positions * @param removedPositions * the removed positions * @return the runnable or <code>null</code>, if reconciliation should be canceled */ public Runnable createUpdateRunnable(final TextPresentation textPresentation, List<AttributedPosition> addedPositions, List<AttributedPosition> removedPositions) { if (fSourceViewer == null || textPresentation == null) return null; // TODO: do clustering of positions and post multiple fast runnables final AttributedPosition[] added = new AttributedPosition[addedPositions.size()]; addedPositions.toArray(added); final AttributedPosition[] removed = new AttributedPosition[removedPositions.size()]; removedPositions.toArray(removed); if (isCanceled()) return null; Runnable runnable = new Runnable() { public void run() { updatePresentation(textPresentation, added, removed); } }; return runnable; }
public void createPresentation(TextPresentation presentation, ITypedRegion region) { int start= region.getOffset(); int length= 0; boolean firstToken= true; TextAttribute attribute = getTokenTextAttribute(Token.UNDEFINED); scanner.setRange(document,start,region.getLength()); while (true) { IToken resultToken = scanner.nextToken(); if (resultToken.isEOF()) { break; } if(resultToken.equals(Token.UNDEFINED)) { continue; } if (!firstToken) { addRange(presentation,start,length,attribute,true); } firstToken = false; attribute = getTokenTextAttribute(resultToken); start = scanner.getTokenOffset(); length = scanner.getTokenLength(); } addRange(presentation,start,length,attribute,true); }
/** * Adds style information to the given text presentation. * * @param presentation the text presentation to be extended * @param offset the offset of the range to be styled * @param length the length of the range to be styled * @param attr the attribute describing the style of the range to be styled * @param wholeLine the boolean switch to declare that the whole line should be colored */ private void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr, boolean wholeLine) { if (attr != null) { int style= attr.getStyle(); int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL); if(wholeLine) { try { int line = document.getLineOfOffset(offset); int start = document.getLineOffset(line); length = document.getLineLength(line); offset = start; } catch (BadLocationException e) { } } StyleRange styleRange = new StyleRange(offset,length,attr.getForeground(),attr.getBackground(),fontStyle); styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0; styleRange.underline = (style & TextAttribute.UNDERLINE) != 0; presentation.addStyleRange(styleRange); } }
public String updatePresentation( Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight ) { ZDebug.print( 5, "updatePresentation( ", drawable, ", ", hoverInfo, ", ", presentation, ", ", maxWidth, ", ", maxHeight, " )" ); HTMLFormat html = new HTMLFormat(); html.format( hoverInfo ); for( StyleRange style : html.getStyleList() ) { presentation.addStyleRange( style ); } if( drawable instanceof StyledText ) { StyledText styled = (StyledText) drawable; styled.setWordWrap( html.isWordWrap() ); if( !html.isWordWrap() ) { SWTUtil.fontPreference( styled, ExternalPreference.FONT_EDITOR_TEXT ); } } return html.getBuffer(); }
/** * @see IInformationControl#setInformation(String) */ @SuppressWarnings("deprecation") public void setInformation(String content) { if (fPresenter == null) { fText.setText(content); } else { fPresentation.clear(); content= fPresenter.updatePresentation(fShell.getDisplay(), content, fPresentation, Math.max(fMaxWidth, 260), fMaxHeight); if (content != null) { fText.setText(content); TextPresentation.applyTextPresentation(fPresentation, fText); } else { fText.setText(""); //$NON-NLS-1$ } } }
/** * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion) */ public void createPresentation(TextPresentation presentation, ITypedRegion region) { wipeExistingScopes(region); synchronized (getLockObject(fDocument)) { try { fDocument.addPositionCategory(ICommonConstants.SCOPE_CATEGORY); fDocument.addPosition( ICommonConstants.SCOPE_CATEGORY, new TypedPosition(region.getOffset(), region.getLength(), (String) fDefaultTextAttribute .getData())); } catch (Exception e) { IdeLog.logError(CommonEditorPlugin.getDefault(), e); } } addRange(presentation, region.getOffset(), region.getLength(), getTextAttribute(region)); }
@Override protected TextPresentation createPresentation(IRegion damage, IDocument document) { if (IdeLog.isInfoEnabled(CommonEditorPlugin.getDefault(), IDebugScopes.PRESENTATION)) { IdeLog.logInfo( CommonEditorPlugin.getDefault(), MessageFormat .format("Initiating presentation reconciling for region at offset {0}, length {1} in document of length {2}", //$NON-NLS-1$ damage.getOffset(), damage.getLength(), document.getLength()), IDebugScopes.PRESENTATION); } synchronized (this) { delayedRegions.append(damage); } try { return createPresentation(nextDamagedRegion(), document, new NullProgressMonitor()); } finally { triggerDelayedCreatePresentation(); } }
/** * Update the presentation. * * @param textPresentation the text presentation * @param addedPositions the added positions * @param removedPositions the removed positions */ private void updatePresentation(TextPresentation textPresentation, List<Position> addedPositions, List<Position> removedPositions) { Runnable runnable= fJobPresenter.createUpdateRunnable(textPresentation, addedPositions, removedPositions); if (runnable == null) return; JavaEditor editor= fEditor; if (editor == null) return; IWorkbenchPartSite site= editor.getSite(); if (site == null) return; Shell shell= site.getShell(); if (shell == null || shell.isDisposed()) return; Display display= shell.getDisplay(); if (display == null || display.isDisposed()) return; display.asyncExec(runnable); }
/** * Create a runnable for updating the presentation. * <p> * NOTE: Called from background thread. * </p> * @param textPresentation the text presentation * @param addedPositions the added positions * @param removedPositions the removed positions * @return the runnable or <code>null</code>, if reconciliation should be canceled */ public Runnable createUpdateRunnable(final TextPresentation textPresentation, List<Position> addedPositions, List<Position> removedPositions) { if (fSourceViewer == null || textPresentation == null) return null; // TODO: do clustering of positions and post multiple fast runnables final HighlightedPosition[] added= new SemanticHighlightingManager.HighlightedPosition[addedPositions.size()]; addedPositions.toArray(added); final SemanticHighlightingManager.HighlightedPosition[] removed= new SemanticHighlightingManager.HighlightedPosition[removedPositions.size()]; removedPositions.toArray(removed); if (isCanceled()) return null; Runnable runnable= new Runnable() { public void run() { updatePresentation(textPresentation, added, removed); } }; return runnable; }
protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { int yoursStart= offset; int yoursEnd= offset + insertLength -1; yoursEnd= Math.max(yoursStart, yoursEnd); Iterator<?> e= presentation.getAllStyleRangeIterator(); while (e.hasNext()) { StyleRange range= (StyleRange) e.next(); int myStart= range.start; int myEnd= range.start + range.length -1; myEnd= Math.max(myStart, myEnd); if (myEnd < yoursStart) continue; if (myStart < yoursStart) range.length += insertLength; else range.start += insertLength; } }
private static String trim(StringBuffer buffer, TextPresentation presentation) { int length= buffer.length(); int end= length -1; while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) -- end; if (end == -1) return ""; //$NON-NLS-1$ if (end < length -1) buffer.delete(end + 1, length); else end= length; int start= 0; while (start < end && Character.isWhitespace(buffer.charAt(start))) ++ start; buffer.delete(0, start); presentation.setResultWindow(new Region(start, buffer.length())); return buffer.toString(); }
@Override public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { this.display = display; StringBuilder sb = new StringBuilder(); try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // hoverInfo = Utils.escapeHtml(hoverInfo); Document doc = builder.parse( new InputSource(new StringReader("<root>" + hoverInfo + "</root>"))); parseDocument(doc, presentation, sb); } catch (Exception e) { e.printStackTrace(); return hoverInfo; } return sb.toString(); }
private void makeChange(TextPresentation presentation, int point, List<StyleRangeCustom> styles, StringBuilder sb) { List<StyleRangeCustom> activeStyles = Lists.newArrayList(); int end = Integer.MAX_VALUE; for (StyleRangeCustom s : styles) { if (s.start <= point) { if (s.stop > point) { activeStyles.add(s); end = Math.min(end, s.stop); } } else { end = Math.min(end, s.start); } } if (activeStyles.isEmpty()) { return; } StyleRange range = new StyleRange(point, end - point, null, null); for (StyleRangeCustom r : activeStyles) { r.style(range); } presentation.addStyleRange(range); }
@Override protected NSISInformationControl.IInformationPresenter createInformationPresenter() { return new WrappingInformationPresenter("\t\t") { //$NON-NLS-1$ @Override public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { String hoverInfo2 = super.updatePresentation(display, hoverInfo, presentation, maxWidth, maxHeight); int n = hoverInfo2.indexOf(' '); if (n <= 0) { n = hoverInfo2.length(); } presentation.addStyleRange(new StyleRange(0, n, display.getSystemColor(SWT.COLOR_INFO_FOREGROUND), display.getSystemColor(SWT.COLOR_INFO_BACKGROUND), SWT.BOLD)); return hoverInfo2; } }; }
/** * Adds style information to the given text presentation. * * @param presentation * the text presentation to be extended * @param offset * the offset of the range to be styled * @param length * the length of the range to be styled * @param attr * the attribute describing the style of the range to be styled */ protected void addRange( TextPresentation presentation, int offset, int length, TextAttribute attr ) { if ( attr != null ) { int style= attr.getStyle(); int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL); StyleRange range = new StyleRange( offset, length, attr.getForeground( ), attr.getBackground( ), fontStyle ); range.strikeout = ( attr.getStyle( ) & TextAttribute.STRIKETHROUGH ) != 0; range.underline = ( attr.getStyle( ) & TextAttribute.UNDERLINE ) != 0; range.font= attr.getFont(); presentation.addStyleRange( range ); } }
public TooltipInformationControlCreator(IInformationPresenter presenter) { if (presenter == null) { presenter = new AbstractTooltipInformationPresenter() { @Override protected void onUpdatePresentation(String hoverInfo, TextPresentation presentation) { } @Override protected void onHandleClick(Object data) { } }; } this.presenter = presenter; }
/** * Creates the reader and properly puts the presentation into place. */ public Reader createReader(String hoverInfo, TextPresentation presentation) { String str = PyStringUtils.removeWhitespaceColumnsToLeft(hoverInfo); str = correctLineDelimiters(str); List<PyStyleRange> lst = new ArrayList<>(); str = handlePydevTags(lst, str); Collections.sort(lst, new Comparator<PyStyleRange>() { @Override public int compare(PyStyleRange o1, PyStyleRange o2) { return Integer.compare(o1.start, o2.start); } }); for (PyStyleRange pyStyleRange : lst) { presentation.addStyleRange(pyStyleRange); } return new StringReader(str); }
protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { int yoursStart = offset; int yoursEnd = offset + insertLength - 1; yoursEnd = Math.max(yoursStart, yoursEnd); Iterator<StyleRange> e = presentation.getAllStyleRangeIterator(); while (e.hasNext()) { StyleRange range = e.next(); int myStart = range.start; int myEnd = range.start + range.length - 1; myEnd = Math.max(myStart, myEnd); if (myEnd < yoursStart) { continue; } if (myStart < yoursStart) { range.length += insertLength; } else { range.start += insertLength; } } }
/** * Creates the ranges from parsing the code with the PyCodeScanner. * * @param textPresentation this is the container of the style ranges. * @param scanner the scanner used to parse the document. * @param doc document to parse. * @param partitionOffset the offset of the document we should parse. * @param partitionLen the length to be parsed. */ private void createDefaultRanges(TextPresentation textPresentation, PyCodeScanner scanner, Document doc, int partitionOffset, int partitionLen) { scanner.setRange(doc, partitionOffset, partitionLen); IToken nextToken = scanner.nextToken(); while (!nextToken.isEOF()) { Object data = nextToken.getData(); if (data instanceof TextAttribute) { TextAttribute textAttribute = (TextAttribute) data; int offset = scanner.getTokenOffset(); int len = scanner.getTokenLength(); Color foreground = textAttribute.getForeground(); Color background = textAttribute.getBackground(); int style = textAttribute.getStyle(); textPresentation.addStyleRange(new StyleRange(offset, len, foreground, background, style)); } nextToken = scanner.nextToken(); } }
public void testStyleRanges() throws Exception { PyInformationPresenter presenter = new PyInformationPresenter(); TextPresentation presentation = new TextPresentation(); String str = "@foo: <pydev_link link=\"itemPointer\">link</pydev_link> <pydev_hint_bold>bold</pydev_hint_bold>"; Reader reader = presenter.createReader(str, presentation); String handled = StringUtils.readAll(reader); assertEquals("@foo: link bold", handled); Iterator<StyleRange> it = presentation.getAllStyleRangeIterator(); ArrayList<String> tagsReplaced = new ArrayList<String>(); ArrayList<String> expected = new ArrayList<String>(); expected.add("<pydev_link link=\"itemPointer\">"); expected.add("<pydev_hint_bold>"); while (it.hasNext()) { PyStyleRange next = (PyStyleRange) it.next(); tagsReplaced.add(next.tagReplaced); } assertEquals(expected, tagsReplaced); }
/** * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion) */ public void createPresentation( TextPresentation presentation, ITypedRegion region) { addRange( presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute); }
/** * Adds style information to the given text presentation. * * @param presentation the text presentation to be extended * @param offset the offset of the range to be styled * @param length the length of the range to be styled * @param attr the attribute describing the style of the range to be styled */ protected void addRange( TextPresentation presentation, int offset, int length, TextAttribute attr) { if (attr != null) presentation.addStyleRange( new StyleRange( offset, length, attr.getForeground(), attr.getBackground(), attr.getStyle())); }
/** * Fire colorize. * * @param presentation * @param error */ private void fireColorize(TextPresentation presentation, Throwable error) { if (listeners == null) { return; } synchronized (listeners) { for (ITMPresentationReconcilerListener listener : listeners) { listener.colorize(presentation, error); } } }
@Override public void colorize(TextPresentation presentation, Throwable e) { // Command command = commands.get(commands.size() - 1); // if (e != null) { // command.error = e; // } else { // command.ranges = viewer.getTextWidget().getStyleRanges(); // } }
private String add(TextPresentation presentation) { Iterator<StyleRange> ranges = presentation.getAllStyleRangeIterator(); while(ranges.hasNext()) { if (currentRanges.length() > 0) { currentRanges.append(", "); } currentRanges.append(ranges.next()); } return null; }
/** * <p> * Transforms the HTML text from the reader to formatted text. * </p> * * @param reader the reader * @param presentation If not <code>null</code>, formattings will be applied to * the presentation. */ public HTML2TextReader(Reader reader, TextPresentation presentation) { fReader= reader; fBuffer= new StringBuffer(); fIndex= 0; fReadFromBuffer= false; fCharAfterWhiteSpace= -1; fWasWhiteSpace= true; fgTags= new LinkedHashSet<String>(); fgTags.add("b"); fgTags.add("br"); fgTags.add("br/"); fgTags.add("br /"); fgTags.add("div"); fgTags.add("h1"); fgTags.add("h2"); fgTags.add("h3"); fgTags.add("h4"); fgTags.add("h5"); fgTags.add("p"); fgTags.add("dl"); fgTags.add("dt"); fgTags.add("dd"); fgTags.add("li"); fgTags.add("ul"); fgTags.add("pre"); fgTags.add("head"); fgEntityLookup= new LinkedHashMap<String, String>(7); fgEntityLookup.put("lt", "<"); fgEntityLookup.put("gt", ">"); fgEntityLookup.put("nbsp", " "); fgEntityLookup.put("amp", "&"); fgEntityLookup.put("circ", "^"); fgEntityLookup.put("tilde", "~"); fgEntityLookup.put("quot", "\""); fTextPresentation= presentation; }
public void clearColors(){ // clear colors if (getColorList() != null){ Iterator it = getColorList().keySet().iterator(); while (it.hasNext()){ ((TextPresentation)getColorList().get(it.next())).clear(); } } }
private void boldRange(int start, int length, TextPresentation presentation, boolean doItalic) { // We have found a tag and create a new style range int fontStyle = doItalic ? (SWT.BOLD | SWT.ITALIC) : SWT.BOLD; StyleRange range = new StyleRange(start, length, null, null, fontStyle); // Add this style range to the presentation presentation.addStyleRange(range); }
private void highlightXml() { setLineBullet(); TextPresentation tr = new TextPresentation(); StyleRange dsr = new StyleRange(0, text.getCharCount(), Colors.getSystemColor(SWT.COLOR_BLACK), Colors.getSystemColor(SWT.COLOR_WHITE)); tr.setDefaultStyleRange(dsr); List<XmlRegion> regions = new XmlRegionAnalyzer().analyzeXml(text.getText()); List<StyleRange> ranges = computeStyleRanges(regions); tr.replaceStyleRanges(ranges.toArray(new StyleRange[0])); text.setStyleRanges(ranges.toArray(new StyleRange[0])); if (lastFoundIndex !=-1) { StyleRange styleRange = new StyleRange(); styleRange.start = lastFoundIndex; styleRange.length = keyword.length(); styleRange.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW); tr.mergeStyleRange(styleRange); // text.setStyleRange(styleRange); } List<StyleRange> allSrs = new ArrayList<>(); Iterator<StyleRange> it = tr.getAllStyleRangeIterator(); while (it.hasNext()) { StyleRange sr = it.next(); allSrs.add(sr); } text.setStyleRanges(allSrs.toArray(new StyleRange[0])); }