/** * Deletes the selection and sets the caret before the deleted range. * * @param selection the selection to delete * @param textWidget the widget * @throws BadLocationException on document access failure * @since 3.5 */ private void deleteSelection(ITextSelection selection, StyledText textWidget) throws BadLocationException { new SelectionProcessor(this).doDelete(selection); }
/** * Returns the text regions covered by the given selection in the given viewer. * * @param viewer the viewer * @param selection the selection * @return the text regions corresponding to <code>selection</code> * @throws BadLocationException if accessing the document failed * @since 3.5 */ public static IRegion[] getCoveredRanges(ITextViewer viewer, ITextSelection selection) throws BadLocationException { return new SelectionProcessor(viewer).getRanges(selection); }
/** * Returns <code>true</code> if the text covered by <code>selection</code> does not contain any * characters in the given viewer. Note the difference to {@link ITextSelection#isEmpty()}, * which returns <code>true</code> only for invalid selections. * * @param viewer the viewer * @param selection the selection * @return <code>true</code> if <code>selection</code> does not contain any text, * <code>false</code> otherwise * @throws BadLocationException if accessing the document failed * @since 3.5 */ public static boolean isEmpty(ITextViewer viewer, ITextSelection selection) throws BadLocationException { return new SelectionProcessor(viewer).isEmpty(selection); }
@Override public void run() { SelectionProcessor processor= new SelectionProcessor(TextViewer.this); try { /* Use the selection instead of the event's coordinates. Is this dangerous? */ ISelection selection= getSelection(); int length= e.text.length(); if (length == 0 && e.character == '\0') { // backspace in StyledText block selection mode... TextEdit edit= processor.backspace(selection); edit.apply(fDocument, TextEdit.UPDATE_REGIONS); ISelection empty= processor.makeEmpty(selection, true); setSelection(empty); } else { int lines= processor.getCoveredLines(selection); String delim= fDocument.getLegalLineDelimiters()[0]; StringBuffer text= new StringBuffer(lines * length + (lines - 1) * delim.length()); text.append(e.text); for (int i= 0; i < lines - 1; i++) { text.append(delim); text.append(e.text); } processor.doReplace(selection, text.toString()); } } catch (BadLocationException x) { if (TRACE_ERRORS) System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.verifyText")); //$NON-NLS-1$ } } });
/** * Returns a text edit describing the text modification that would be executed if the given * selection was replaced by <code>replacement</code>. * * @param selection the selection to replace * @param replacement the replacement text * @return a text edit describing the operation needed to replace <code>selection</code> * @throws BadLocationException if computing the edit failed */ public TextEdit replace(ISelection selection, String replacement) throws BadLocationException { return getImplementation(selection).replace(selection, replacement); }
@Override public void run() { SelectionProcessor processor= new SelectionProcessor(TextViewer.this); Clipboard clipboard= new Clipboard(getDisplay()); try { int lines= processor.getCoveredLines(selection); String delim= fDocument.getLegalLineDelimiters()[0]; StringBuffer text= new StringBuffer(lines * length + (lines - 1) * delim.length()); processor.doReplace(selection, toInsert); } catch (BadLocationException x) { if (TRACE_ERRORS)
@Override public String getText() { IDocument document= getDocument(); if (document != null) { try { return new SelectionProcessor(document, fTabWidth).getText(this); } catch (BadLocationException x) { // ignore and default to super implementation } } return super.getText(); }
/** * Convenience method that applies the edit returned from {@link #delete(ISelection)} to the * underlying document. * * @param selection the selection to delete * @throws BadLocationException if accessing the document failed */ public void doDelete(ISelection selection) throws BadLocationException { TextEdit edit= delete(selection); boolean complex= edit.hasChildren(); if (complex && fRewriteTarget != null) fRewriteTarget.beginCompoundChange(); try { edit.apply(fDocument, TextEdit.UPDATE_REGIONS); if (fSelectionProvider != null) { ISelection empty= makeEmpty(selection, true); fSelectionProvider.setSelection(empty); } } finally { if (complex && fRewriteTarget != null) fRewriteTarget.endCompoundChange(); } }
if (isFirst) { wrapCompoundChange(() -> { SelectionProcessor processor= new SelectionProcessor(TextViewer.this); try { if (length == 0 && e.character == '\0') { TextEdit edit= processor.backspace(selection); edit.apply(fDocument, TextEdit.UPDATE_REGIONS); ISelection empty= processor.makeEmpty(selection, true); setSelection(empty); } else { int lines= processor.getCoveredLines(selection); String delim= fDocument.getLegalLineDelimiters()[0]; StringBuilder text= new StringBuilder(lines * length + (lines - 1) * delim.length()); text.append(e.text); processor.doReplace(selection, text.toString());
/** * Returns a text edit describing the text modification that would be executed if the backspace * key was pressed on the given selection. * * @param selection the selection to delete * @return a text edit describing the operation needed to delete <code>selection</code> * @throws BadLocationException if computing the edit failed */ public TextEdit backspace(ISelection selection) throws BadLocationException { return getImplementation(selection).backspace(selection); }
} else { wrapCompoundChange(() -> { SelectionProcessor processor= new SelectionProcessor(TextViewer.this); Clipboard clipboard= new Clipboard(getDisplay()); try { int lines= processor.getCoveredLines(selection); String delim= fDocument.getLegalLineDelimiters()[0]; StringBuilder text= new StringBuilder(lines * length + (lines - 1) * delim.length()); processor.doReplace(selection, toInsert); } catch (BadLocationException x) { if (TRACE_ERRORS)
@Override public String getText() { IDocument document= getDocument(); if (document != null) { try { return new SelectionProcessor(document, fTabWidth).getText(this); } catch (BadLocationException x) { // ignore and default to super implementation } } return super.getText(); }
/** * Convenience method that applies the edit returned from {@link #delete(ISelection)} to the * underlying document. * * @param selection the selection to delete * @throws BadLocationException if accessing the document failed */ public void doDelete(ISelection selection) throws BadLocationException { TextEdit edit= delete(selection); boolean complex= edit.hasChildren(); if (complex && fRewriteTarget != null) fRewriteTarget.beginCompoundChange(); try { edit.apply(fDocument, TextEdit.UPDATE_REGIONS); if (fSelectionProvider != null) { ISelection empty= makeEmpty(selection, true); fSelectionProvider.setSelection(empty); } } finally { if (complex && fRewriteTarget != null) fRewriteTarget.endCompoundChange(); } }
/** * Returns the text covered by <code>selection</code> * * @param selection the selection * @return the text covered by <code>selection</code> * @throws BadLocationException if computing the edit failed */ public String getText(ISelection selection) throws BadLocationException { return getImplementation(selection).getText(selection); }
/** * Deletes the selection and sets the caret before the deleted range. * * @param selection the selection to delete * @param textWidget the widget * @throws BadLocationException on document access failure * @since 3.5 */ private void deleteSelection(ITextSelection selection, StyledText textWidget) throws BadLocationException { new SelectionProcessor(this).doDelete(selection); }
/** * Returns the text regions covered by the given selection in the given viewer. * * @param viewer the viewer * @param selection the selection * @return the text regions corresponding to <code>selection</code> * @throws BadLocationException if accessing the document failed * @since 3.5 */ public static IRegion[] getCoveredRanges(ITextViewer viewer, ITextSelection selection) throws BadLocationException { return new SelectionProcessor(viewer).getRanges(selection); }
/** * Returns <code>true</code> if the text covered by <code>selection</code> does not contain any * characters in the given viewer. Note the difference to {@link ITextSelection#isEmpty()}, * which returns <code>true</code> only for invalid selections. * * @param viewer the viewer * @param selection the selection * @return <code>true</code> if <code>selection</code> does not contain any text, * <code>false</code> otherwise * @throws BadLocationException if accessing the document failed * @since 3.5 */ public static boolean isEmpty(ITextViewer viewer, ITextSelection selection) throws BadLocationException { return new SelectionProcessor(viewer).isEmpty(selection); }
/** * Returns the number of lines touched by <code>selection</code>. Note that for linear * selections, this is the number of contained delimiters plus 1. * * @param selection the selection * @return the number of lines touched by <code>selection</code> * @throws BadLocationException if accessing the document failed */ public int getCoveredLines(ISelection selection) throws BadLocationException { return getImplementation(selection).getCoveredLines(selection); }
@Override public void run() { try { new SelectionProcessor(TextViewer.this).doDelete(getSelection()); } catch (BadLocationException e) { if (TRACE_ERRORS) System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.delete")); //$NON-NLS-1$ } } });
@Override public IRegion[] getRegions() { IDocument document= getDocument(); if (document != null) { try { return new SelectionProcessor(document, fTabWidth).getRanges(this); } catch (BadLocationException x) { // default to single region behavior } } return new IRegion[] {new Region(getOffset(), getLength())}; } }