From b02da0a48015d2d8925da8ca5fa5394d6d1ae5e2 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Fri, 13 Mar 2015 16:31:43 +0300 Subject: [PATCH] IDEA-105705: select word at caret: expand selection document wide for non-physical documents - fix for CamelHumps enabled --- .../actions/SelectWordAtCaretAction.java | 22 +++++----- .../LightPlatformCodeInsightTestCase.java | 42 +++++++++++++++---- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectWordAtCaretAction.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectWordAtCaretAction.java index d495132b085d..2d84961be490 100644 --- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectWordAtCaretAction.java +++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectWordAtCaretAction.java @@ -72,21 +72,19 @@ public class SelectWordAtCaretAction extends TextComponentEditorAction implement if (ranges.isEmpty()) return; - int startWordOffset = Math.max(0, ranges.get(0).getStartOffset()); - int endWordOffset = Math.min(ranges.get(0).getEndOffset(), document.getTextLength()); + SelectionModel selectionModel = editor.getSelectionModel(); + final TextRange selectionRange = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); - final SelectionModel selectionModel = editor.getSelectionModel(); - if (camel && ranges.size() == 2 && selectionModel.getSelectionStart() == startWordOffset && - selectionModel.getSelectionEnd() == endWordOffset) { - startWordOffset = Math.max(0, ranges.get(1).getStartOffset()); - endWordOffset = Math.min(ranges.get(1).getEndOffset(), document.getTextLength()); + TextRange minimumRange = new TextRange(0, editor.getDocument().getTextLength()); + for (TextRange range : ranges) { + if (range.contains(selectionRange) && !range.equals(selectionRange)) { + if (minimumRange.contains(range)) { + minimumRange = range; + } + } } - if (startWordOffset >= selectionModel.getSelectionStart() && selectionModel.getSelectionEnd() >= endWordOffset && ranges.size() == 1) { - startWordOffset = 0; - endWordOffset = document.getTextLength(); - } - selectionModel.setSelection(startWordOffset, endWordOffset); + selectionModel.setSelection(minimumRange.getStartOffset(), minimumRange.getEndOffset()); } } diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index 040748d2056d..e184530ad771 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -181,6 +181,25 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest }.execute().getResultObject(); } + @NotNull + protected static Editor configureFromFileTextWithoutPSI(@NonNls @NotNull final String fileText) { + return new WriteCommandAction(null) { + @Override + protected void run(@NotNull Result result) throws Throwable { + final Document fakeDocument = EditorFactory.getInstance().createDocument(fileText); + EditorTestUtil.CaretAndSelectionState caretsState = EditorTestUtil.extractCaretAndSelectionMarkers(fakeDocument); + + String newFileText = fakeDocument.getText(); + Document document = EditorFactory.getInstance().createDocument(newFileText); + final Editor editor = EditorFactory.getInstance().createEditor(document); + ((EditorImpl)editor).setCaretActive(); + + EditorTestUtil.setCaretsAndSelection(editor, caretsState); + result.setResult(editor); + } + }.execute().getResultObject(); + } + protected static Editor createEditor(@NotNull VirtualFile file) { PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); Editor editor = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), file, 0), false); @@ -325,29 +344,34 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest protected void checkResultByText(final String message, @NotNull final String fileText, final boolean ignoreTrailingSpaces, final String filePath) { bringRealEditorBack(); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); + checkResultByText(message, myEditor, fileText, ignoreTrailingSpaces, filePath); + } + + protected static void checkResultByText(final String message, + @NotNull final Editor editor, + @NotNull final String fileText, + final boolean ignoreTrailingSpaces, + final String filePath) { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { - final Document document = EditorFactory.getInstance().createDocument(fileText); + final Document fakeDocument = EditorFactory.getInstance().createDocument(fileText); if (ignoreTrailingSpaces) { - ((DocumentImpl)document).stripTrailingSpaces(getProject()); + ((DocumentImpl)fakeDocument).stripTrailingSpaces(getProject()); } - EditorTestUtil.CaretAndSelectionState carets = EditorTestUtil.extractCaretAndSelectionMarkers(document); + EditorTestUtil.CaretAndSelectionState carets = EditorTestUtil.extractCaretAndSelectionMarkers(fakeDocument); - PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting(); - String newFileText = document.getText(); - - PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); - String fileText = myFile.getText(); + String newFileText = fakeDocument.getText(); + String fileText = editor.getDocument().getText(); String failMessage = getMessage("Text mismatch", message); if (filePath != null && !newFileText.equals(fileText)) { throw new FileComparisonFailure(failMessage, newFileText, fileText, filePath); } assertEquals(failMessage, newFileText, fileText); - EditorTestUtil.verifyCaretAndSelectionState(myEditor, carets, message); + EditorTestUtil.verifyCaretAndSelectionState(editor, carets, message); } }); }