# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: D:\workspace\nb72-src\main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: java.editor/src/org/netbeans/modules/editor/java/SelectCodeElementAction.java --- java.editor/src/org/netbeans/modules/editor/java/SelectCodeElementAction.java Base (BASE) +++ java.editor/src/org/netbeans/modules/editor/java/SelectCodeElementAction.java Locally Modified (Based On LOCAL) @@ -50,28 +50,34 @@ import java.awt.event.ActionEvent; import java.io.IOException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.MissingResourceException; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.Action; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.Caret; +import javax.swing.text.Document; import javax.swing.text.JTextComponent; import org.netbeans.api.java.source.Task; +import javax.swing.text.StyledDocument; +import org.netbeans.api.java.source.Comment; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.JavaSource.Phase; +import org.netbeans.api.java.source.TreeUtilities; import org.netbeans.api.progress.ProgressUtils; import org.netbeans.editor.BaseAction; +import org.openide.text.NbDocument; import org.openide.util.Exceptions; import org.openide.util.NbBundle; /** - * Code selection according to syntax tree. + * Code selection according to syntax tree. It also supports JavaDoc. * - * TODO: javadoc selection - * * @author Miloslav Metelka, Jan Pokorsky */ final class SelectCodeElementAction extends BaseAction { @@ -175,9 +181,9 @@ private void select(SelectionInfo selectionInfo) { Caret caret = target.getCaret(); markIgnoreNextCaretUpdate(); - caret.setDot(selectionInfo.getStartOffset()); + caret.setDot(selectionInfo.getEndOffset()); markIgnoreNextCaretUpdate(); - caret.moveDot(selectionInfo.getEndOffset()); + caret.moveDot(selectionInfo.getStartOffset()); } private void markIgnoreNextCaretUpdate() { @@ -215,13 +221,54 @@ int caretPos = target.getCaretPosition(); positions.add(new SelectionInfo(caretPos, caretPos)); SourcePositions sp = ci.getTrees().getSourcePositions(); - TreePath tp = ci.getTreeUtilities().pathFor(caretPos); + final TreeUtilities treeUtilities = ci.getTreeUtilities(); + TreePath tp = treeUtilities.pathFor(caretPos); for (Tree tree: tp) { int startPos = (int)sp.getStartPosition(tp.getCompilationUnit(), tree); int endPos = (int)sp.getEndPosition(tp.getCompilationUnit(), tree); positions.add(new SelectionInfo(startPos, endPos)); + + //Support selection of JavaDoc + int docBegin = Integer.MAX_VALUE; + { + for (Comment comment : treeUtilities.getComments(tree, true)) { + docBegin = Math.min(comment.pos(), docBegin); + } + } + int docEnd = Integer.MIN_VALUE; + { + for (Comment comment : treeUtilities.getComments(tree, false)) { + docEnd = Math.max(comment.endPos(), docEnd); + } + } + if (docBegin != Integer.MAX_VALUE && docEnd != Integer.MIN_VALUE) { + positions.add(new SelectionInfo(docBegin, docEnd)); + } else if (docBegin == Integer.MAX_VALUE && docEnd != Integer.MIN_VALUE) { + positions.add(new SelectionInfo(startPos, docEnd)); + } else if (docBegin != Integer.MAX_VALUE && docEnd == Integer.MIN_VALUE) { + positions.add(new SelectionInfo(docBegin, endPos)); + } } - return positions.toArray(new SelectionInfo[positions.size()]); + //sort selectioninfo by their start + SortedSet orderedPositions = new TreeSet(new Comparator() { + @Override + public int compare(SelectionInfo o1, SelectionInfo o2) { + return o2.getStartOffset() - o1.getStartOffset(); + } + }); + orderedPositions.addAll(positions); + //for each selectioninfo add its line selection + if (target.getDocument() instanceof StyledDocument) { + StyledDocument doc = (StyledDocument) target.getDocument(); + + for (SelectionInfo selectionInfo : positions) { + int startOffset = NbDocument.findLineOffset(doc, NbDocument.findLineNumber(doc, selectionInfo.getStartOffset())); + int endOffset = NbDocument.findLineOffset(doc, NbDocument.findLineNumber(doc, selectionInfo.getEndOffset()) + 1); + orderedPositions.add(new SelectionInfo(startOffset, endOffset)); + } + } + + return orderedPositions.toArray(new SelectionInfo[orderedPositions.size()]); } public void run() {