IDEA-105705: select word at caret: expand selection document wide for non-physical documents - fix for CamelHumps enabled

This commit is contained in:
Aleksey Pivovarov
2015-04-03 12:27:53 +03:00
parent 89c9276480
commit b02da0a480
2 changed files with 43 additions and 21 deletions
@@ -72,21 +72,19 @@ public class SelectWordAtCaretAction extends TextComponentEditorAction implement
if (ranges.isEmpty()) return; if (ranges.isEmpty()) return;
int startWordOffset = Math.max(0, ranges.get(0).getStartOffset()); SelectionModel selectionModel = editor.getSelectionModel();
int endWordOffset = Math.min(ranges.get(0).getEndOffset(), document.getTextLength()); final TextRange selectionRange = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
final SelectionModel selectionModel = editor.getSelectionModel(); TextRange minimumRange = new TextRange(0, editor.getDocument().getTextLength());
if (camel && ranges.size() == 2 && selectionModel.getSelectionStart() == startWordOffset && for (TextRange range : ranges) {
selectionModel.getSelectionEnd() == endWordOffset) { if (range.contains(selectionRange) && !range.equals(selectionRange)) {
startWordOffset = Math.max(0, ranges.get(1).getStartOffset()); if (minimumRange.contains(range)) {
endWordOffset = Math.min(ranges.get(1).getEndOffset(), document.getTextLength()); minimumRange = range;
}
}
} }
if (startWordOffset >= selectionModel.getSelectionStart() && selectionModel.getSelectionEnd() >= endWordOffset && ranges.size() == 1) { selectionModel.setSelection(minimumRange.getStartOffset(), minimumRange.getEndOffset());
startWordOffset = 0;
endWordOffset = document.getTextLength();
}
selectionModel.setSelection(startWordOffset, endWordOffset);
} }
} }
@@ -181,6 +181,25 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}.execute().getResultObject(); }.execute().getResultObject();
} }
@NotNull
protected static Editor configureFromFileTextWithoutPSI(@NonNls @NotNull final String fileText) {
return new WriteCommandAction<Editor>(null) {
@Override
protected void run(@NotNull Result<Editor> 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) { protected static Editor createEditor(@NotNull VirtualFile file) {
PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
Editor editor = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), file, 0), false); 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) { protected void checkResultByText(final String message, @NotNull final String fileText, final boolean ignoreTrailingSpaces, final String filePath) {
bringRealEditorBack(); bringRealEditorBack();
PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); 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() { ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override @Override
public void run() { public void run() {
final Document document = EditorFactory.getInstance().createDocument(fileText); final Document fakeDocument = EditorFactory.getInstance().createDocument(fileText);
if (ignoreTrailingSpaces) { 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 = fakeDocument.getText();
String newFileText = document.getText(); String fileText = editor.getDocument().getText();
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
String fileText = myFile.getText();
String failMessage = getMessage("Text mismatch", message); String failMessage = getMessage("Text mismatch", message);
if (filePath != null && !newFileText.equals(fileText)) { if (filePath != null && !newFileText.equals(fileText)) {
throw new FileComparisonFailure(failMessage, newFileText, fileText, filePath); throw new FileComparisonFailure(failMessage, newFileText, fileText, filePath);
} }
assertEquals(failMessage, newFileText, fileText); assertEquals(failMessage, newFileText, fileText);
EditorTestUtil.verifyCaretAndSelectionState(myEditor, carets, message); EditorTestUtil.verifyCaretAndSelectionState(editor, carets, message);
} }
}); });
} }