slight improvements to toggle comment feature

This commit is contained in:
Manindra Moharana
2014-07-14 23:19:27 +05:30
parent 76a68bfcb6
commit 2592a1d1af

View File

@@ -1683,6 +1683,79 @@ public class DebugEditor extends JavaEditor implements ActionListener {
errorCheckerService.runManualErrorCheck();
}
}
/**
* Handles toggle comment. Slightly improved from the default implementation
* in {@link processing.app.Editor}
*/
protected void handleCommentUncomment() {
// log("Entering handleCommentUncomment()");
startCompoundEdit();
String prefix = getCommentPrefix();
int prefixLen = prefix.length();
int startLine = textarea.getSelectionStartLine();
int stopLine = textarea.getSelectionStopLine();
int lastLineStart = textarea.getLineStartOffset(stopLine);
int selectionStop = textarea.getSelectionStop();
// If the selection ends at the beginning of the last line,
// then don't (un)comment that line.
if (selectionStop == lastLineStart) {
// Though if there's no selection, don't do that
if (textarea.isSelectionActive()) {
stopLine--;
}
}
// If the text is empty, ignore the user.
// Also ensure that all lines are commented (not just the first)
// when determining whether to comment or uncomment.
boolean commented = true;
for (int i = startLine; commented && (i <= stopLine); i++) {
String lineText = textarea.getLineText(i).trim();
if (lineText.length() == 0)
continue; //ignore blank lines
commented = lineText.startsWith(prefix);
}
// log("Commented: " + commented);
// This is the line start offset of the first line, which is added to
// all other lines while adding a comment. Required when commenting
// lines which have uneven whitespaces in the beginning. Makes the
// commented lines look more uniform.
int lso = Math.abs(textarea.getLineStartNonWhiteSpaceOffset(startLine)
- textarea.getLineStartOffset(startLine));
for (int line = startLine; line <= stopLine; line++) {
int location = textarea.getLineStartNonWhiteSpaceOffset(line);
String lineText = textarea.getLineText(line);
if (lineText.trim().length() == 0)
continue; //ignore blank lines
if (commented) {
// remove a comment
if (lineText.trim().startsWith(prefix + " ")) {
textarea.select(location, location + prefixLen + 1);
} else {
textarea.select(location, location + prefixLen);
}
textarea.setSelectedText("");
} else {
// add a comment
location = textarea.getLineStartOffset(line) + lso;
textarea.select(location, location);
textarea.setSelectedText(prefix + " "); //Add a '// '
}
}
// Subtract one from the end, otherwise selects past the current line.
// (Which causes subsequent calls to keep expanding the selection)
textarea.select(textarea.getLineStartOffset(startLine),
textarea.getLineStopOffset(stopLine) - 1);
stopCompoundEdit();
sketch.setModified(true);
}
// TweakMode code
/**