IDEA-166208 Selection expansion skips a step: method without comment

this also redoes the fix for IDEA-116706 to make sure select/unselect actions are 'symmetric'
This commit is contained in:
Dmitry Batrak
2017-01-17 15:03:18 +03:00
parent 28f5ee6817
commit e79a56e67c
2 changed files with 19 additions and 12 deletions
@@ -45,10 +45,9 @@ public class MethodOrClassSelectioner extends BasicSelectioner {
PsiElement firstChild = e.getFirstChild();
PsiElement[] children = e.getChildren();
int i = 1;
if (firstChild instanceof PsiDocComment) {
int i = 1;
while (children[i] instanceof PsiWhiteSpace) {
i++;
}
@@ -59,10 +58,10 @@ public class MethodOrClassSelectioner extends BasicSelectioner {
range = TextRange.create(firstChild.getTextRange());
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
}
else if (firstChild instanceof PsiComment) {
int i = 1;
firstChild = children[i++];
}
if (firstChild instanceof PsiComment) {
while (children[i] instanceof PsiComment || children[i] instanceof PsiWhiteSpace) {
i++;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,7 +27,10 @@ import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.util.Processor;
import org.jetbrains.annotations.Nullable;
@@ -108,9 +111,9 @@ public class UnSelectWordHandler extends EditorActionHandler {
SelectWordUtil.processRanges(element, text, cursorOffset, editor, new Processor<TextRange>() {
@Override
public boolean process(TextRange range) {
range = expandToFoldingBoundaries(range);
if (selectionRange.contains(range) && !range.equals(selectionRange) &&
(range.contains(finalCursorOffset) || finalCursorOffset == range.getEndOffset()) &&
!isOffsetCollapsed(range.getStartOffset()) && !isOffsetCollapsed(range.getEndOffset())) {
(range.contains(finalCursorOffset) || finalCursorOffset == range.getEndOffset())) {
if (maximumRange.get() == null || range.contains(maximumRange.get())) {
maximumRange.set(range);
}
@@ -119,9 +122,14 @@ public class UnSelectWordHandler extends EditorActionHandler {
return false;
}
private boolean isOffsetCollapsed(int offset) {
FoldRegion region = editor.getFoldingModel().getCollapsedRegionAtOffset(offset);
return region != null && region.getStartOffset() != offset && region.getEndOffset() != offset;
private TextRange expandToFoldingBoundaries(TextRange range) {
int startOffset = range.getStartOffset();
FoldRegion region = editor.getFoldingModel().getCollapsedRegionAtOffset(startOffset);
if (region != null) startOffset = region.getStartOffset();
int endOffset = range.getEndOffset();
region = editor.getFoldingModel().getCollapsedRegionAtOffset(endOffset);
if (region != null && endOffset > region.getStartOffset()) endOffset = region.getEndOffset();
return new TextRange(startOffset, endOffset);
}
});