From 2592a1d1afd7cc9eb52ebac476e7a0de7ab9abb1 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Mon, 14 Jul 2014 23:19:27 +0530 Subject: [PATCH] slight improvements to toggle comment feature --- .../mode/experimental/DebugEditor.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/pdex/src/processing/mode/experimental/DebugEditor.java b/pdex/src/processing/mode/experimental/DebugEditor.java index 8d1f83368..b56310c5c 100755 --- a/pdex/src/processing/mode/experimental/DebugEditor.java +++ b/pdex/src/processing/mode/experimental/DebugEditor.java @@ -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 /**