mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Improve JoinLinesAction to remove excess whitespace
This commit is contained in:
parent
fea3d0a6d1
commit
ea0d728a40
1 changed files with 30 additions and 24 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2005 Todd Papaioannou
|
* Copyright (c) 2005, 2006 Todd Papaioannou and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -16,6 +16,7 @@ import java.util.ResourceBundle;
|
||||||
|
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
|
import org.eclipse.jface.text.IRegion;
|
||||||
import org.eclipse.jface.text.ITextSelection;
|
import org.eclipse.jface.text.ITextSelection;
|
||||||
import org.eclipse.jface.viewers.ISelection;
|
import org.eclipse.jface.viewers.ISelection;
|
||||||
import org.eclipse.jface.viewers.ISelectionProvider;
|
import org.eclipse.jface.viewers.ISelectionProvider;
|
||||||
|
@ -28,7 +29,7 @@ import org.eclipse.ui.texteditor.TextEditorAction;
|
||||||
* that, when invoked, will join the current and next line together.
|
* that, when invoked, will join the current and next line together.
|
||||||
*
|
*
|
||||||
* @author Todd Papaioannou (toddp@acm.org)
|
* @author Todd Papaioannou (toddp@acm.org)
|
||||||
* @version $Date: 2005/11/10 15:20:17 $
|
* @version $Date: 2006/12/05 09:21:59 $
|
||||||
* @see org.eclipse.ui.texteditor.TextEditorAction
|
* @see org.eclipse.ui.texteditor.TextEditorAction
|
||||||
*/
|
*/
|
||||||
public class JoinLinesAction extends TextEditorAction {
|
public class JoinLinesAction extends TextEditorAction {
|
||||||
|
@ -59,9 +60,6 @@ public class JoinLinesAction extends TextEditorAction {
|
||||||
* It achieves this by determining which line of the editor's document
|
* It achieves this by determining which line of the editor's document
|
||||||
* we are on, and then replacing the delimiter at the end of the line
|
* we are on, and then replacing the delimiter at the end of the line
|
||||||
* with nothing.
|
* with nothing.
|
||||||
*
|
|
||||||
* TODO: Currently, we don't bother to remove any excess whitespace.
|
|
||||||
* Doing so in the future might be a nice touch.
|
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
|
@ -82,25 +80,33 @@ public class JoinLinesAction extends TextEditorAction {
|
||||||
|
|
||||||
// Now, figure out the end line of the selection
|
// Now, figure out the end line of the selection
|
||||||
int currentLine = theSelection.getEndLine();
|
int currentLine = theSelection.getEndLine();
|
||||||
|
if (currentLine < theDocument.getNumberOfLines() - 1) {
|
||||||
// What's the offset of this line in the document?
|
// compute the region of whitespace between the two adjacent lines
|
||||||
int lineOffset = theDocument.getLineOffset(currentLine);
|
IRegion currentLineRegion= theDocument.getLineInformation(currentLine);
|
||||||
|
IRegion nextLineRegion= theDocument.getLineInformation(currentLine + 1);
|
||||||
// And the length of the line?
|
int startOffset= currentLineRegion.getOffset() + currentLineRegion.getLength();
|
||||||
int lineLength = theDocument.getLineLength(currentLine);
|
while (startOffset > currentLineRegion.getOffset()) {
|
||||||
|
if (Character.isWhitespace(theDocument.getChar(startOffset - 1))) {
|
||||||
// What delimeter do we have?
|
--startOffset;
|
||||||
String delim = theDocument.getLineDelimiter(currentLine);
|
} else {
|
||||||
|
break;
|
||||||
if (delim != null) {
|
}
|
||||||
// How long is it?
|
}
|
||||||
int delimLength = delim.length();
|
int endOffset= nextLineRegion.getOffset();
|
||||||
|
while (endOffset < nextLineRegion.getOffset() + nextLineRegion.getLength()) {
|
||||||
// Now back track to the last real char in the line
|
if (Character.isWhitespace(theDocument.getChar(endOffset))) {
|
||||||
int newLineEnd = lineOffset + lineLength - delimLength;
|
++endOffset;
|
||||||
|
} else {
|
||||||
// Replace the delimter char(s) with nothing
|
break;
|
||||||
theDocument.replace(newLineEnd, delimLength, null);
|
}
|
||||||
|
}
|
||||||
|
if (endOffset == nextLineRegion.getOffset() + nextLineRegion.getLength()) {
|
||||||
|
// special case: empty next line - don't insert trailing space
|
||||||
|
theDocument.replace(startOffset, endOffset - startOffset, null);
|
||||||
|
} else {
|
||||||
|
// Replace the whitespace region with a single space
|
||||||
|
theDocument.replace(startOffset, endOffset - startOffset, " "); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue