public void run(IAction action){
//checks to see if you can edit the document
try {
if(editor != null && editor.isEditable()){
// make the edit atomic so one undo undos
IRewriteTarget target = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class);
if (target != null)
target.beginCompoundChange();
String openComment = "<!--- ";
String closeComment = " --->";
int selectionLength = 0;
// Get the document
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
String lineEnding = doc.getLineDelimiter(0);
// Get the selection
ISelection sel = editor.getSelectionProvider().getSelection();
ITextSelection selectioner = (ITextSelection) sel;
// Get the partition
ITypedRegion partition = doc.getPartition(((ITextSelection)sel).getOffset());
String partType = partition.getType();
if (partType.equals(CFPartitionScanner.CF_SCRIPT) || partType.equals(CFPartitionScanner.CF_SCRIPT_COMMENT_BLOCK)
|| partType.equals(CFPartitionScanner.JAVADOC_COMMENT) || partType.equals(CFPartitionScanner.CF_SCRIPT_COMMENT)) {
openComment = "/*";
closeComment = "*/";
}
// if we already are in a comment partition, remove it, else add it
if (partType.equals(CFPartitionScanner.CF_COMMENT) || partType.equals(CFPartitionScanner.CF_SCRIPT_COMMENT_BLOCK)
|| partType.equals(CFPartitionScanner.JAVADOC_COMMENT) || partType.equals(CFPartitionScanner.CF_SCRIPT_COMMENT)) {
// Track the position in the document that the actual comments blocks are
int openCommentStart = partition.getOffset();
int closeCommentStart = partition.getOffset();
// Find the full partition selection string
String selection = doc.get().substring(partition.getOffset(), partition.getOffset() + partition.getLength());
// Find the opening comment information with optional space at the end
Pattern pattern = Pattern.compile("^(" + openComment.replace("*", "[*]") + "[ ]?[" + lineEnding + "]?)");
Matcher matcher = pattern.matcher(selection);
if(matcher.find()) {
openComment = matcher.group();
openCommentStart += matcher.start();
}
// Find the closing comment information with optional space at the beginning
pattern = Pattern.compile("([" + lineEnding + "]?[ ]?" + closeComment.replace("*", "[*]") + ")$");
matcher = pattern.matcher(selection);
if(matcher.find()) {
closeComment = matcher.group();
closeCommentStart += matcher.start();
}
// Only get rid of the newline characters if the open and close both have them
// Check to see if the open has a newline but the close doesn't
if (selectioner.getText().endsWith(lineEnding) && selectioner.getText().startsWith(openComment + lineEnding)) {
openComment = openComment.substring(lineEnding.length() - 1, openComment.length() - lineEnding.length());
}
FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc);
// Find and replace the closing comment first so that it doesn't get messed up with positions
finder.find(closeCommentStart, closeComment, true, false, false, false);
finder.replace("", false);
// Find and replace the opening comment so that it doesn't affect the closing comment replace
finder.find(openCommentStart, openComment, true, false, false, false);
finder.replace("", false);
selectionLength = selection.length() - openComment.length() - closeComment.length() + 1;
} else {
if(selectioner.getText().endsWith("\n") && !selectioner.getText().startsWith("\n")){
// add newlines if this looks like a newline-to-newline comment, to be pretty
openComment = openComment.concat("\n");
closeComment = closeComment.concat("\n");
}
selectionLength = this.enclose(doc,(ITextSelection)sel, openComment, closeComment);
// move the caret somewhere
}
if (selectioner.getLength() == 0) {
editor.setHighlightRange(selectioner.getOffset() + openComment.length(), 1, true);
} else {
editor.selectAndReveal(selectioner.getOffset(), selectionLength);
}
if (target != null)
target.endCompoundChange();
}
} catch (BadLocationException e) {
e.printStackTrace(System.err);
}