This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 215552
Collapse All | Expand All

(-)java.editor/src/org/netbeans/modules/editor/java/SelectCodeElementAction.java (-7 / +54 lines)
Lines 50-77 Link Here
50
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionEvent;
51
import java.io.IOException;
51
import java.io.IOException;
52
import java.util.ArrayList;
52
import java.util.ArrayList;
53
import java.util.Comparator;
53
import java.util.List;
54
import java.util.List;
54
import java.util.MissingResourceException;
55
import java.util.MissingResourceException;
56
import java.util.SortedSet;
57
import java.util.TreeSet;
55
import java.util.concurrent.atomic.AtomicBoolean;
58
import java.util.concurrent.atomic.AtomicBoolean;
56
import javax.swing.Action;
59
import javax.swing.Action;
57
import javax.swing.event.CaretEvent;
60
import javax.swing.event.CaretEvent;
58
import javax.swing.event.CaretListener;
61
import javax.swing.event.CaretListener;
59
import javax.swing.text.Caret;
62
import javax.swing.text.Caret;
63
import javax.swing.text.Document;
60
import javax.swing.text.JTextComponent;
64
import javax.swing.text.JTextComponent;
61
import org.netbeans.api.java.source.Task;
65
import org.netbeans.api.java.source.Task;
66
import javax.swing.text.StyledDocument;
67
import org.netbeans.api.java.source.Comment;
62
import org.netbeans.api.java.source.CompilationController;
68
import org.netbeans.api.java.source.CompilationController;
63
import org.netbeans.api.java.source.JavaSource;
69
import org.netbeans.api.java.source.JavaSource;
64
import org.netbeans.api.java.source.JavaSource.Phase;
70
import org.netbeans.api.java.source.JavaSource.Phase;
71
import org.netbeans.api.java.source.TreeUtilities;
65
import org.netbeans.api.progress.ProgressUtils;
72
import org.netbeans.api.progress.ProgressUtils;
66
import org.netbeans.editor.BaseAction;
73
import org.netbeans.editor.BaseAction;
74
import org.openide.text.NbDocument;
67
import org.openide.util.Exceptions;
75
import org.openide.util.Exceptions;
68
import org.openide.util.NbBundle;
76
import org.openide.util.NbBundle;
69
77
70
/**
78
/**
71
 * Code selection according to syntax tree.
79
 * Code selection according to syntax tree. It also supports JavaDoc.
72
 *
80
 *
73
 * TODO: javadoc selection
74
 *
75
 * @author Miloslav Metelka, Jan Pokorsky
81
 * @author Miloslav Metelka, Jan Pokorsky
76
 */
82
 */
77
final class SelectCodeElementAction extends BaseAction {
83
final class SelectCodeElementAction extends BaseAction {
Lines 175-183 Link Here
175
        private void select(SelectionInfo selectionInfo) {
181
        private void select(SelectionInfo selectionInfo) {
176
            Caret caret = target.getCaret();
182
            Caret caret = target.getCaret();
177
            markIgnoreNextCaretUpdate();
183
            markIgnoreNextCaretUpdate();
178
            caret.setDot(selectionInfo.getStartOffset());
184
            caret.setDot(selectionInfo.getEndOffset());
179
            markIgnoreNextCaretUpdate();
185
            markIgnoreNextCaretUpdate();
180
            caret.moveDot(selectionInfo.getEndOffset());
186
            caret.moveDot(selectionInfo.getStartOffset());
181
        }
187
        }
182
        
188
        
183
        private void markIgnoreNextCaretUpdate() {
189
        private void markIgnoreNextCaretUpdate() {
Lines 215-227 Link Here
215
            int caretPos = target.getCaretPosition();
221
            int caretPos = target.getCaretPosition();
216
            positions.add(new SelectionInfo(caretPos, caretPos));
222
            positions.add(new SelectionInfo(caretPos, caretPos));
217
            SourcePositions sp = ci.getTrees().getSourcePositions();
223
            SourcePositions sp = ci.getTrees().getSourcePositions();
218
            TreePath tp = ci.getTreeUtilities().pathFor(caretPos);
224
	    final TreeUtilities treeUtilities = ci.getTreeUtilities();
225
            TreePath tp = treeUtilities.pathFor(caretPos);
219
            for (Tree tree: tp) {
226
            for (Tree tree: tp) {
220
                int startPos = (int)sp.getStartPosition(tp.getCompilationUnit(), tree);
227
                int startPos = (int)sp.getStartPosition(tp.getCompilationUnit(), tree);
221
                int endPos = (int)sp.getEndPosition(tp.getCompilationUnit(), tree);
228
                int endPos = (int)sp.getEndPosition(tp.getCompilationUnit(), tree);
222
                positions.add(new SelectionInfo(startPos, endPos));
229
                positions.add(new SelectionInfo(startPos, endPos));
230
		
231
		//Support selection of JavaDoc
232
		int docBegin = Integer.MAX_VALUE;
233
		{
234
		    for (Comment comment : treeUtilities.getComments(tree, true)) {
235
			docBegin = Math.min(comment.pos(), docBegin);
236
		    }
237
		}
238
		int docEnd = Integer.MIN_VALUE;
239
		{
240
		    for (Comment comment : treeUtilities.getComments(tree, false)) {
241
			docEnd = Math.max(comment.endPos(), docEnd);
242
		    }
243
		}
244
		if (docBegin != Integer.MAX_VALUE && docEnd != Integer.MIN_VALUE) {
245
		    positions.add(new SelectionInfo(docBegin, docEnd));
246
		} else if (docBegin == Integer.MAX_VALUE && docEnd != Integer.MIN_VALUE) {
247
		    positions.add(new SelectionInfo(startPos, docEnd));
248
		} else if (docBegin != Integer.MAX_VALUE && docEnd == Integer.MIN_VALUE) {
249
		    positions.add(new SelectionInfo(docBegin, endPos));
250
		}
223
            }
251
            }
224
            return positions.toArray(new SelectionInfo[positions.size()]);
252
	    //sort selectioninfo by their start
253
	    SortedSet<SelectionInfo> orderedPositions = new TreeSet<SelectionInfo>(new Comparator<SelectionInfo>() {
254
		@Override
255
		public int compare(SelectionInfo o1, SelectionInfo o2) {
256
		    return o2.getStartOffset() - o1.getStartOffset();
257
		}
258
	    });
259
	    orderedPositions.addAll(positions);
260
	    //for each selectioninfo add its line selection
261
	    if (target.getDocument() instanceof StyledDocument) {
262
		StyledDocument doc = (StyledDocument) target.getDocument();
263
264
		for (SelectionInfo selectionInfo : positions) {
265
		    int startOffset = NbDocument.findLineOffset(doc, NbDocument.findLineNumber(doc, selectionInfo.getStartOffset()));
266
		    int endOffset = NbDocument.findLineOffset(doc, NbDocument.findLineNumber(doc, selectionInfo.getEndOffset()) + 1);
267
		    orderedPositions.add(new SelectionInfo(startOffset, endOffset));
268
		}
269
	    }
270
	    
271
	    return orderedPositions.toArray(new SelectionInfo[orderedPositions.size()]);
225
        }
272
        }
226
273
227
        public void run() {
274
        public void run() {

Return to bug 215552