1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.LinkedList ; 14 import java.util.List ; 15 import java.util.ResourceBundle ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.BadPartitioningException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IDocumentExtension3; 23 import org.eclipse.jface.text.ITextSelection; 24 import org.eclipse.jface.text.ITypedRegion; 25 26 import org.eclipse.ui.texteditor.ITextEditor; 27 28 import org.eclipse.jdt.ui.text.IJavaPartitions; 29 30 31 37 public class AddBlockCommentAction extends BlockCommentAction { 38 39 48 public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 49 super(bundle, prefix, editor); 50 } 51 52 55 protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException { 56 int selectionOffset= selection.getOffset(); 57 int selectionEndOffset= selectionOffset + selection.getLength(); 58 List edits= new LinkedList (); 59 ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, selectionOffset, false); 60 61 handleFirstPartition(partition, edits, factory, selectionOffset); 62 63 while (partition.getOffset() + partition.getLength() < selectionEndOffset) { 64 partition= handleInteriorPartition(partition, edits, factory, docExtension); 65 } 66 67 handleLastPartition(partition, edits, factory, selectionEndOffset); 68 69 executeEdits(edits); 70 } 71 72 81 private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException { 82 83 int partOffset= partition.getOffset(); 84 String partType= partition.getType(); 85 86 Assert.isTrue(partOffset <= offset, "illegal partition"); 88 if (partType == IDocument.DEFAULT_CONTENT_TYPE) { 90 edits.add(factory.createEdit(offset, 0, getCommentStart())); 92 } else if (isSpecialPartition(partType)) { 93 edits.add(factory.createEdit(partOffset, 0, getCommentStart())); 95 } 97 } 98 99 123 private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException { 124 125 String partType= partition.getType(); 127 int partEndOffset= partition.getOffset() + partition.getLength(); 128 int tokenLength= getCommentStart().length(); 129 130 boolean wasJavadoc= false; 132 if (partType == IJavaPartitions.JAVA_DOC) { 133 134 wasJavadoc= true; 135 136 } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) { 137 138 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); 141 } 142 143 partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false); 145 partType= partition.getType(); 146 147 if (wasJavadoc) { 149 150 if (partType == IDocument.DEFAULT_CONTENT_TYPE 153 || isSpecialPartition(partType)) { 154 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart())); 155 } 156 157 } else { 159 if (partType == IJavaPartitions.JAVA_DOC) { 160 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd())); 162 } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) { 163 edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); } 166 } 167 168 return partition; 169 } 170 171 182 private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException { 183 184 String partType= partition.getType(); 185 186 if (partType == IDocument.DEFAULT_CONTENT_TYPE) { 187 edits.add(factory.createEdit(endOffset, 0, getCommentEnd())); 189 } else if (isSpecialPartition(partType)) { 190 edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd())); 192 } 193 194 } 195 196 205 private boolean isSpecialPartition(String partType) { 206 return partType == IJavaPartitions.JAVA_CHARACTER 207 || partType == IJavaPartitions.JAVA_STRING 208 || partType == IJavaPartitions.JAVA_SINGLE_LINE_COMMENT; 209 } 210 211 214 protected boolean isValidSelection(ITextSelection selection) { 215 return selection != null && !selection.isEmpty() && selection.getLength() > 0; 216 } 217 218 } 219 | Popular Tags |