From e79a56e67c09abd0e894806117b95676a5d65d37 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Tue, 17 Jan 2017 15:02:10 +0300 Subject: [PATCH] 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' --- .../MethodOrClassSelectioner.java | 9 ++++---- .../editorActions/UnSelectWordHandler.java | 22 +++++++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/wordSelection/MethodOrClassSelectioner.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/wordSelection/MethodOrClassSelectioner.java index 60cdcc1caef3..55d53c47d241 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/wordSelection/MethodOrClassSelectioner.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/wordSelection/MethodOrClassSelectioner.java @@ -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++; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/UnSelectWordHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/UnSelectWordHandler.java index d46e20930b31..ca141c665faf 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/UnSelectWordHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/UnSelectWordHandler.java @@ -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() { @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); } });