Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.eclipse.xtext.ui.editor.embedded;
  
  
  import  org.eclipse.core.runtime.Assert;
  import  org.eclipse.jface.text.BadLocationException;
  import  org.eclipse.jface.text.IDocument;
  import  org.eclipse.jface.text.IRegion;
  import  org.eclipse.jface.text.ITextSelection;
 import  org.eclipse.jface.text.ITextViewer;
 import  org.eclipse.jface.text.ITextViewerExtension5;
 import  org.eclipse.jface.text.Region;
 import  org.eclipse.swt.custom.StyledText;


Action for joining two or more lines together by deleting the line delimiters and trimming the whitespace between them.

Author(s):
Sebastian Zarnekow - Initial contribution and API
Since:
2.3
 
 public class TextViewerJoinLinesAction extends TextViewerAction {
 
 	private String fJointnull;

Creates a line joining action.

Parameters:
bundle the resource bundle for UI strings
prefix the prefix for the property keys into bundle
viewer the viewer
joint the string to put between the lines
 
 	public TextViewerJoinLinesAction(ResourceBundle bundleString prefix, ITextViewer viewerString joint) {
 		super(bundleprefixviewer);
 		Assert.isLegal(joint != null);
 		joint;
 		update();
 	}
 
 	public void run() {
 
 		ITextViewer viewergetTextViewer();
 		if (viewer == null)
 			return;
 
 		if (!canModifyViewer())
 			return;
 
 		IDocument documentviewer.getDocument();
 		if (document == null)
 			return;
 
 		ITextSelection selectiongetSelection(viewer);
 		if (selection == null)
 			return;
 
 		int startLineselection.getStartLine();
 		int endLineselection.getEndLine();
 		try {
 			int caretOffsetjoinLines(documentstartLineendLine);
 			if (caretOffset > -1) {
 				StyledText widgetviewer.getTextWidget();
 				widget.setRedraw(false);
 				adjustHighlightRange(viewercaretOffset, 0);
 				viewer.revealRange(caretOffset, 0);
 
 				viewer.setSelectedRange(caretOffset, 0);
 				widget.setRedraw(true);
 			}
 		} catch (BadLocationException e) {
 			// should not happen
 		}
 
 	}

Adjusts the highlight range so that at least the specified range is highlighted.

Subclasses may re-implement this method.

Parameters:
viewer
offset the offset of the range which at least should be highlighted
length the length of the range which at least should be highlighted
 
 	protected void adjustHighlightRange(ITextViewer viewerint offsetint length) {
 		if (viewer == null)
 			return;
 
 		if (viewer instanceof ITextViewerExtension5) {
 			ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
 			extension.exposeModelRange(new Region(offsetlength));
 		} else if (!isVisible(vieweroffsetlength)) {
 			viewer.resetVisibleRegion();
 		}
 	}
	protected boolean isVisible(ITextViewer viewerint offsetint length) {
		if (viewer instanceof ITextViewerExtension5) {
			ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
			IRegion overlapextension.modelRange2WidgetRange(new Region(offsetlength));
			return overlap != null;
		}
		return viewer.overlapsWithVisibleRegion(offsetlength);
	}
	public void update() {
		super.update();
		if (!isEnabled())
			return;
		if (!canModifyViewer()) {
			setEnabled(false);
			return;
		}
		ITextViewer viewergetTextViewer();
		setEnabled(viewer.isEditable());
	}

Joins several text lines to one line.

Parameters:
document the document
startLine the start line
endLine the end line
Returns:
the new caret offset
Throws:
BadLocationException if the document is accessed with wrong line or offset
	private int joinLines(IDocument documentint startLineint endLinethrows BadLocationException {
		if (startLine == document.getNumberOfLines() - 1) {
			// do nothing because we are in the last line
			return -1;
		}
		if (startLine == endLine)
			endLine++; // append join with the next line
		StringBuffer buffernew StringBuffer();
		for (int linestartLineline <= endLineline++) {
			buffer.append(trim(documentlineline == startLine));
			if (line != endLine)
				buffer.append();
		}
		int startLineOffsetdocument.getLineOffset(startLine);
		int endLineOffsetdocument.getLineOffset(endLine)	+ document.getLineLength(endLine) - getLineDelimiterLength(documentendLine);
		String replaceStringbuffer.toString();
		document.replace(startLineOffsetendLineOffset - startLineOffsetreplaceString);
		return startLineOffset + replaceString.length();
	}
	private String trim(IDocument documentint lineboolean ignoreLeadingWhitespacethrows BadLocationException {
		int lineOffsetdocument.getLineOffset(line);
		int lineLengthdocument.getLineLength(line);
		lineLengthlineLength - getLineDelimiterLength(documentline);
		if (!ignoreLeadingWhitespace)
			return document.get(lineOffsetlineLength).trim();
		while (lineLength > 0 && Character.isWhitespace(document.getChar(lineOffset + lineLength - 1)))
			lineLength--;
		return document.get(lineOffsetlineLength);
	}
	private int getLineDelimiterLength(IDocument documentint linethrows BadLocationException {
		String lineDelimiterdocument.getLineDelimiter(line);
		return lineDelimiter != null ? lineDelimiter.length() : 0;
	}
New to GrepCode? Check out our FAQ X