public int getLineAtOffset( int iOffset ) { return styledText.getLineAtOffset( iOffset ); }
private void execCurrent() { clearData(); String cmd = inputCmd.getLine(inputCmd.getLineAtOffset(inputCmd .getCaretOffset())); execCmd(cmd); }
private void execNext() { clearData(); int start = inputCmd.getLineAtOffset(inputCmd.getCaretOffset()); int end = inputCmd.getLineCount(); for (int i = start; i < end; i++) { String cmd = inputCmd.getLine(i); if (execCmd(cmd)) break; } } private void clearData(){
/** * Returns <code>true</code> if the widget has a selection spanning multiple lines, * <code>false</code> otherwise. * * @param textWidget the text widget to check * @return <code>true</code> if <code>textWidget</code> has a multiline selection, * <code>false</code> otherwise * @since 3.5 */ private boolean hasMultiLineSelection(StyledText textWidget) { Point selection= textWidget.getSelection(); try { int startLine= textWidget.getLineAtOffset(selection.x); int endLine= textWidget.getLineAtOffset(selection.y); return startLine != endLine; } catch (IllegalArgumentException e) { // ignore - apparently, the widget has a stale selection // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=273721 return false; } }
/** * Returns <code>true</code> if the widget has a selection spanning multiple lines, * <code>false</code> otherwise. * * @param textWidget the text widget to check * @return <code>true</code> if <code>textWidget</code> has a multiline selection, * <code>false</code> otherwise * @since 3.5 */ private boolean hasMultiLineSelection(StyledText textWidget) { Point selection= textWidget.getSelection(); try { int startLine= textWidget.getLineAtOffset(selection.x); int endLine= textWidget.getLineAtOffset(selection.y); return startLine != endLine; } catch (IllegalArgumentException e) { // ignore - apparently, the widget has a stale selection // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=273721 return false; } }
@Override public void mouseScrolled(MouseEvent e) { int caretOffset = fMinimapTextWidget.getOffsetAtPoint(new Point(0, fTopIndexY)); int lineIndex = fMinimapTextWidget.getLineAtOffset(caretOffset); if (e.count > 0) { lineIndex = Math.max(0, lineIndex - NB_LINES_SCROLL); } else { lineIndex = Math.min(fMinimapTextWidget.getCharCount(), lineIndex + NB_LINES_SCROLL); } caretOffset = fMinimapTextWidget.getOffsetAtLine(lineIndex); fMinimapTextWidget.setCaretOffset(caretOffset); }
final StyledText text = new StyledText(shell, SWT.NONE); text.setText("lalala\n\nlalala\n\nlalala\n\nlalala\n\nlalala\n\nlalala\n\n"); text.addLineStyleListener(new LineStyleListener() { @Override public void lineGetStyle(LineStyleEvent e) { // Set the line number e.bulletIndex = text.getLineAtOffset(e.lineOffset); // Set the style, 12 pixles wide for each digit StyleRange style = new StyleRange(); style.metrics = new GlyphMetrics(0, 0, Integer.toString(text.getLineCount() + 1).length() * 12); // Create and set the bullet e.bullet = new Bullet(ST.BULLET_NUMBER, style); // Apply the offset e.bulletIndex += YOUR_OFFSET; // I used 3 here } });
void updateStatusBar() { int offset = styledText.getCaretOffset(); int lineIndex = styledText.getLineAtOffset(offset); String insertLabel = getResourceString(insert ? "Insert" : "Overwrite"); //$NON-NLS-1$ //$NON-NLS-2$ statusBar.setText(getResourceString("Offset") //$NON-NLS-1$ + offset + " " //$NON-NLS-1$ + getResourceString("Line") //$NON-NLS-1$ + lineIndex + "\t" //$NON-NLS-1$ + insertLabel); }
private void updateEditorTopIndex() { int caretOffset = fMinimapTextWidget.getCaretOffset(); int lineAtOffset = fMinimapTextWidget.getLineAtOffset(caretOffset); int newTopIndex = lineAtOffset; if (fEditorViewer instanceof ITextViewerExtension5) { // adjust offset according folded content newTopIndex = ((ITextViewerExtension5) fEditorViewer).widgetLine2ModelLine(lineAtOffset); } fEditorViewer.setTopIndex(newTopIndex); }
void setBullet(int type) { Point selection = styledText.getSelection(); int lineStart = styledText.getLineAtOffset(selection.x); int lineEnd = styledText.getLineAtOffset(selection.y); StyleRange styleRange = new StyleRange(); styleRange.metrics = new GlyphMetrics(0, 0, BULLET_WIDTH); Bullet bullet = new Bullet(type, styleRange); bullet.text = "."; for (int lineIndex = lineStart; lineIndex <= lineEnd; lineIndex++) { Bullet oldBullet = styledText.getLineBullet(lineIndex); styledText.setLineBullet(lineIndex, 1, oldBullet != null ? null : bullet); } }
@Override public void lineGetStyle(LineStyleEvent event) { StyleRange range = new StyleRange(); range.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_GRAY); int maxNumLine = styledText.getLineCount(); int bulletLength = Integer.toString(maxNumLine).length(); // right padding int widthBullet = ((bulletLength + 1) * styledText.getLineHeight()) / 2; range.metrics = new GlyphMetrics(0, 0, widthBullet); event.bullet = new Bullet(ST.BULLET_TEXT, range); event.bullet.text = String.format("%" + bulletLength + "s", styledText.getLineAtOffset(event.lineOffset) + 1); }
@Override public void lineGetStyle(LineStyleEvent event) { StyledText styledText = (StyledText) event.widget; // Using ST.BULLET_NUMBER sometimes results in weird alignment. // event.bulletIndex = styledText.getLineAtOffset(event.lineOffset); StyleRange styleRange = new StyleRange(); styleRange.foreground = Display.getCurrent().getSystemColor( SWT.COLOR_GRAY); int maxLine = styledText.getLineCount(); int bulletLength = Integer.toString(maxLine).length(); // Width of number character is half the height in monospaced font, add // 1 character width for right padding. int bulletWidth = (bulletLength + 1) * styledText.getLineHeight() / 2; styleRange.metrics = new GlyphMetrics(0, 0, bulletWidth); event.bullet = new Bullet(ST.BULLET_TEXT, styleRange); // getLineAtOffset() returns a zero-based line index. int bulletLine = styledText.getLineAtOffset(event.lineOffset) + 1; event.bullet.text = String.format("%" + bulletLength + "s", bulletLine); }
public static void textEditorPaint(ActionContext actionContext){ Event event = (Event) actionContext.get("event"); StyledText text = (StyledText) event.widget; int line = text.getLineAtOffset(text.getCaretOffset()) + 1; int carOffset = text.getCaretOffset(); int lineOffset = text.getOffsetAtLine(line - 1); Label statusLabel = (Label) actionContext.get("statusLabel"); statusLabel.setText("" + line + ":" + (carOffset - lineOffset + 1)); statusLabel.update(); }
public void run() { ITextEditor editor= getTextEditor(); if (!(editor instanceof AbstractTextEditor)) return; ISourceViewer viewer= ((AbstractTextEditor)editor).getSourceViewer(); if (viewer == null) return; StyledText st= viewer.getTextWidget(); if (st == null) return; // compute the number of lines displayed int height= st.getClientArea().height; int lineHeight= st.getLineHeight(); int caretOffset= st.getCaretOffset(); int caretLine= st.getLineAtOffset(caretOffset); int topLine= Math.max(0, (caretLine - (height / (lineHeight * 2)))); st.setTopIndex(topLine); } }
void updateErrorToolTip(StyledTextComp wText, int x, int y) { StyledText canvas = wText.getStyledText(); String errorText = (String) canvas.getData("lastErrorMessage"); // if there's no errors, bail out if (errorText == null) { canvas.setToolTipText(null); return; } // shield against SWT anomalies try { // if there are errors, see if the user hovers over the error line int line = canvas.getLineAtOffset(canvas.getOffsetAtLocation(new Point(1, y))); if (line == (Integer) canvas.getData("lastErrorLine")) { canvas.setToolTipText(errorText); return; } } catch (IllegalArgumentException ignored) { } // in case something went wrong or the line was not correct, no tooltip canvas.setToolTipText(null); }
public static void textKeyDown(ActionContext actionContext){ Event event = (Event) actionContext.get("event"); StyledText text = (StyledText) event.widget; int line = text.getLineAtOffset(text.getCaretOffset()) + 1; int carOffset = text.getCaretOffset(); int lineOffset = text.getOffsetAtLine(line - 1); ((Label) text.getData("lineLabel")).setText("" + line + ":" + (carOffset - lineOffset + 1)); ((Label) text.getData("lineLabel")).update(); if(event.keyCode == 's' && event.stateMask == SWT.CTRL){ SwtListener okButtonSelection = (SwtListener) actionContext.get("okButtonSelection"); if(okButtonSelection != null){ okButtonSelection.handleEvent(null); } } }
/** * Returns the document line to keep visually stable. If the caret line is (partially) * visible, it is returned, otherwise the topmost (partially) visible line is returned. * * @return the visually stable line of this viewer state */ private int getStableLine() { int stableLine; // the model line that we try to keep stable int caretLine= getTextWidget().getLineAtOffset(getTextWidget().getCaretOffset()); if (caretLine < JFaceTextUtil.getPartialTopIndex(getTextWidget()) || caretLine > JFaceTextUtil.getPartialBottomIndex(getTextWidget())) { stableLine= JFaceTextUtil.getPartialTopIndex(TextViewer.this); } else { stableLine= widgetLine2ModelLine(caretLine); } return stableLine; }
/** * Returns the document line to keep visually stable. If the caret line is (partially) * visible, it is returned, otherwise the topmost (partially) visible line is returned. * * @return the visually stable line of this viewer state */ private int getStableLine() { int stableLine; // the model line that we try to keep stable int caretLine= getTextWidget().getLineAtOffset(getTextWidget().getCaretOffset()); if (caretLine < JFaceTextUtil.getPartialTopIndex(getTextWidget()) || caretLine > JFaceTextUtil.getPartialBottomIndex(getTextWidget())) { stableLine= JFaceTextUtil.getPartialTopIndex(TextViewer.this); } else { stableLine= widgetLine2ModelLine(caretLine); } return stableLine; }
public void run() { try { Point selection= fTextViewer.getTextWidget().getSelection(); IDocument document= fTextViewer.getDocument(); fLastLine= document.getLineOfOffset(document.getLength()) + 1; int startLine= selection == null ? 1 : fTextViewer.getTextWidget().getLineAtOffset(selection.x) + 1; String title= ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1; String message= MessageFormat.format(ConsoleMessages.TextViewerGotoLineAction_Enter_line_number__8, new Object[] {new Integer(fLastLine)}); String value= Integer.toString(startLine); Shell activeShell= fTextViewer.getTextWidget().getShell(); InputDialog d= new InputDialog(activeShell, title, message, value, new NumberValidator()); if (d.open() == Window.OK) { try { int line= Integer.parseInt(d.getValue()); gotoLine(line - 1); } catch (NumberFormatException x) { ConsolePlugin.errorDialog(activeShell, ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); // } } } catch (BadLocationException x) { ConsolePlugin.errorDialog(fTextViewer.getTextWidget().getShell(), ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); // return; } } }
/** * Opens a sticky change ruler hover for the caret line. Does nothing if no change hover is * available. * * @since 3.5 */ private void showChangeRulerInformation() { IVerticalRuler ruler= getVerticalRuler(); if (!(ruler instanceof CompositeRuler) || fLineColumn == null) return; CompositeRuler compositeRuler= (CompositeRuler)ruler; // fake a mouse move (some hovers rely on this to determine the hovered line): int x= fLineColumn.getControl().getLocation().x; ISourceViewer sourceViewer= getSourceViewer(); StyledText textWidget= sourceViewer.getTextWidget(); int caretOffset= textWidget.getCaretOffset(); int caretLine= textWidget.getLineAtOffset(caretOffset); int y= textWidget.getLinePixel(caretLine); compositeRuler.setLocationOfLastMouseButtonActivity(x, y); IAnnotationHover hover= fLineColumn.getHover(); showFocusedRulerHover(hover, sourceViewer, caretOffset); }