IDEA-80056 Column selection mode improvement

updating editor fixtures to work for multiple carets
This commit is contained in:
Dmitry Batrak
2014-02-13 13:21:26 +04:00
parent a91e90e770
commit 8c3d81d77b
13 changed files with 406 additions and 335 deletions
@@ -3,6 +3,6 @@ import static java.io.FileInputStream<caret>
class Main {
public static void main(String[] args) {
equals(FileInputStrea<caret>aaa);
equals(FileInputStreaaaa);
}
}
@@ -3,6 +3,6 @@ import static FileInpStre<caret>
class Main {
public static void main(String[] args) {
equals(FileInputStrea<caret>aaa);
equals(FileInputStreaaaa);
}
}
@@ -9,6 +9,6 @@ public abstract class Foo {
};
protected Foo() {
IBar bar = anonymou<caret>s;
IBar bar = anonymous;
}
}
@@ -39,6 +39,7 @@ import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
@@ -51,10 +52,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.search.ProjectScope;
import com.intellij.testFramework.PsiTestCase;
import com.intellij.testFramework.PsiTestData;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.VfsTestUtil;
import com.intellij.testFramework.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
@@ -102,10 +100,6 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
return new CodeInsightTestData();
}
public static final String CARET_MARKER = "<caret>";
@NonNls public static final String SELECTION_START_MARKER = "<selection>";
@NonNls public static final String SELECTION_END_MARKER = "</selection>";
protected void configureByFile(@NonNls String filePath) throws Exception {
configureByFile(filePath, null);
}
@@ -433,37 +427,37 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
@Override
public void run() {
Document document = editor.getDocument();
final String text = document.getText();
int caretIndex = text.indexOf(CARET_MARKER);
int selStartIndex = text.indexOf(SELECTION_START_MARKER);
int selEndIndex = text.indexOf(SELECTION_END_MARKER);
final RangeMarker caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex) : null;
final RangeMarker selStartMarker = selStartIndex >= 0 ? document.createRangeMarker(selStartIndex, selStartIndex) : null;
final RangeMarker selEndMarker = selEndIndex >= 0 ? document.createRangeMarker(selEndIndex, selEndIndex) : null;
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
}
EditorTestUtil.CaretsState caretState = EditorTestUtil.extractCaretAndSelectionMarkers(document);
final String newText = document.getText();
if (caretMarker != null) {
int caretLine = StringUtil.offsetToLineNumber(newText, caretMarker.getStartOffset());
int caretCol = caretMarker.getStartOffset() - StringUtil.lineColToOffset(newText, caretLine, 0);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
editor.getCaretModel().moveToLogicalPosition(pos);
if (editor.getCaretModel().supportsMultipleCarets()) {
List<LogicalPosition> caretPositions = new ArrayList<LogicalPosition>();
List<Segment> selections = new ArrayList<Segment>();
for (EditorTestUtil.Caret caret : caretState.carets) {
LogicalPosition pos = null;
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newText, caret.offset);
int caretCol = caret.offset - StringUtil.lineColToOffset(newText, caretLine, 0);
pos = new LogicalPosition(caretLine, caretCol);
}
caretPositions.add(pos);
selections.add(caret.selection == null ? null : caret.selection);
}
editor.getCaretModel().setCarets(caretPositions, selections);
}
if (selStartMarker != null) {
editor.getSelectionModel().setSelection(selStartMarker.getStartOffset(), selEndMarker.getStartOffset());
else {
assert caretState.carets.size() == 1 : "Multiple carets are not supported by the model";
EditorTestUtil.Caret caret = caretState.carets.get(0);
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newText, caret.offset);
int caretCol = caret.offset - StringUtil.lineColToOffset(newText, caretLine, 0);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
editor.getCaretModel().moveToLogicalPosition(pos);
}
if (caret.selection != null) {
editor.getSelectionModel().setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
}
}
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
@@ -499,6 +493,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
protected void checkResultByFile(@NonNls @NotNull final String filePath, final boolean stripTrailingSpaces) throws Exception {
new WriteCommandAction<Document>(getProject()) {
@SuppressWarnings("ConstantConditions")
@Override
protected void run(@NotNull Result<Document> result) throws Throwable {
getProject().getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
@@ -512,7 +507,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(fullPath.replace(File.separatorChar, '/'));
assertNotNull("Cannot find file " + fullPath, vFile);
String ft = null;
String ft;
try {
ft = VfsUtilCore.loadText(vFile);
}
@@ -523,23 +518,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
String fileText = StringUtil.convertLineSeparators(ft);
Document document = EditorFactory.getInstance().createDocument(fileText);
int caretIndex = fileText.indexOf(CARET_MARKER);
int selStartIndex = fileText.indexOf(SELECTION_START_MARKER);
int selEndIndex = fileText.indexOf(SELECTION_END_MARKER);
final RangeMarker caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex) : null;
final RangeMarker selStartMarker = selStartIndex >= 0 ? document.createRangeMarker(selStartIndex, selStartIndex) : null;
final RangeMarker selEndMarker = selEndIndex >= 0 ? document.createRangeMarker(selEndIndex, selEndIndex) : null;
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
}
EditorTestUtil.CaretsState caretState = EditorTestUtil.extractCaretAndSelectionMarkers(document);
String newFileText = document.getText();
String newFileText1 = newFileText;
@@ -559,36 +538,52 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
assertEquals("Text mismatch in file " + filePath, newFileText1, text);
if (caretMarker != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, caretMarker.getStartOffset());
int caretCol = caretMarker.getStartOffset() - StringUtil.lineColToOffset(newFileText, caretLine, 0);
CaretModel caretModel = myEditor.getCaretModel();
List<Caret> allCarets = caretModel.supportsMultipleCarets() ? new ArrayList<Caret>(caretModel.getAllCarets()) : null;
assertEquals("Unexpected number of carets", caretState.carets.size(), caretModel.supportsMultipleCarets() ? allCarets.size() : 1);
for (int i = 0; i < caretState.carets.size(); i++) {
String caretDescription = caretState.carets.size() == 1 ? "" : "caret " + i + "/" + caretState.carets.size() + " ";
Caret currentCaret = caretModel.supportsMultipleCarets() ? allCarets.get(i) : null;
LogicalPosition actualCaretPosition;
if (caretModel.supportsMultipleCarets()) {
actualCaretPosition = currentCaret.getLogicalPosition();
}
else {
actualCaretPosition = caretModel.getLogicalPosition();
}
EditorTestUtil.Caret expected = caretState.carets.get(i);
if (expected.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, expected.offset);
int caretCol = expected.offset - StringUtil.lineColToOffset(newFileText, caretLine, 0);
assertEquals("caretLine", caretLine + 1, myEditor.getCaretModel().getLogicalPosition().line + 1);
assertEquals("caretColumn", caretCol + 1, myEditor.getCaretModel().getLogicalPosition().column + 1);
}
assertEquals(caretDescription + "caretLine", caretLine + 1, actualCaretPosition.line + 1);
assertEquals(caretDescription + "caretColumn", caretCol + 1, actualCaretPosition.column + 1);
}
int actualSelectionStart = caretModel.supportsMultipleCarets() ? currentCaret.getSelectionStart() : myEditor.getSelectionModel().getSelectionStart();
int actualSelectionEnd = caretModel.supportsMultipleCarets() ? currentCaret.getSelectionEnd() : myEditor.getSelectionModel().getSelectionEnd();
if (expected.selection != null) {
int selStartLine = StringUtil.offsetToLineNumber(newFileText, expected.selection.getStartOffset());
int selStartCol = expected.selection.getStartOffset() - StringUtil.lineColToOffset(newFileText, selStartLine, 0);
if (selStartMarker != null && selEndMarker != null) {
int selStartLine = StringUtil.offsetToLineNumber(newFileText, selStartMarker.getStartOffset());
int selStartCol = selStartMarker.getStartOffset() - StringUtil.lineColToOffset(newFileText, selStartLine, 0);
int selEndLine = StringUtil.offsetToLineNumber(newFileText, expected.selection.getEndOffset());
int selEndCol = expected.selection.getEndOffset() - StringUtil.lineColToOffset(newFileText, selEndLine, 0);
int selEndLine = StringUtil.offsetToLineNumber(newFileText, selEndMarker.getEndOffset());
int selEndCol = selEndMarker.getEndOffset() - StringUtil.lineColToOffset(newFileText, selEndLine, 0);
assertEquals(caretDescription + "selectionStartLine", selStartLine + 1,
StringUtil.offsetToLineNumber(newFileText, actualSelectionStart) + 1);
assertEquals("selectionStartLine", selStartLine + 1,
StringUtil.offsetToLineNumber(newFileText, myEditor.getSelectionModel().getSelectionStart()) + 1);
assertEquals(caretDescription + "selectionStartCol", selStartCol + 1,
actualSelectionStart - StringUtil.lineColToOffset(newFileText, selStartLine, 0) + 1);
assertEquals("selectionStartCol", selStartCol + 1,
myEditor.getSelectionModel().getSelectionStart() - StringUtil.lineColToOffset(newFileText, selStartLine, 0) + 1);
assertEquals(caretDescription + "selectionEndLine", selEndLine + 1,
StringUtil.offsetToLineNumber(newFileText, actualSelectionEnd) + 1);
assertEquals("selectionEndLine", selEndLine + 1,
StringUtil.offsetToLineNumber(newFileText, myEditor.getSelectionModel().getSelectionEnd()) + 1);
assertEquals("selectionEndCol", selEndCol + 1,
myEditor.getSelectionModel().getSelectionEnd() - StringUtil.lineColToOffset(newFileText, selEndLine, 0) + 1);
}
else {
assertTrue("should has no selection, but was: ("+myEditor.getSelectionModel().getSelectionStart()+", "+myEditor.getSelectionModel().getSelectionEnd()+")",
!myEditor.getSelectionModel().hasSelection());
assertEquals(caretDescription + "selectionEndCol", selEndCol + 1,
actualSelectionEnd - StringUtil.lineColToOffset(newFileText, selEndLine, 0) + 1);
}
else {
assertFalse(caretDescription + "should has no selection, but was: (" + actualSelectionStart + ", " + actualSelectionEnd + ")",
caretModel.supportsMultipleCarets() ? currentCaret.hasSelection() : myEditor.getSelectionModel().hasSelection());
}
}
}
}.execute();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -18,54 +18,33 @@ package com.intellij.codeInsight;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NonNls;
import com.intellij.testFramework.EditorTestUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* @author cdr
*/
public class EditorInfo {
@NonNls public static final String CARET_MARKER = "<caret>";
@NonNls public static final String SELECTION_START_MARKER = "<selection>";
@NonNls public static final String SELECTION_END_MARKER = "</selection>";
String newFileText = null;
public RangeMarker caretMarker = null;
RangeMarker selStartMarker = null;
RangeMarker selEndMarker = null;
public EditorTestUtil.CaretsState caretState;
public EditorInfo(final String fileText) {
new WriteCommandAction(null){
@Override
protected void run(Result result) throws Throwable {
protected void run(@NotNull Result result) throws Throwable {
updateCaretAndSelection(EditorFactory.getInstance().createDocument(fileText));
}
}.execute();
}
private boolean updateCaretAndSelection(final Document document) {
private void updateCaretAndSelection(final Document document) {
caretState = EditorTestUtil.extractCaretAndSelectionMarkers(document, false);
newFileText = document.getText();
int caretIndex = newFileText.indexOf(CARET_MARKER);
int selStartIndex = newFileText.indexOf(SELECTION_START_MARKER);
int selEndIndex = newFileText.indexOf(SELECTION_END_MARKER);
caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex) : null;
selStartMarker = selStartIndex >= 0 ? document.createRangeMarker(selStartIndex, selStartIndex) : null;
selEndMarker = selEndIndex >= 0 ? document.createRangeMarker(selEndIndex, selEndIndex) : null;
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
}
newFileText = document.getText();
return caretMarker != null || selStartMarker != null || selEndMarker != null;
}
public String getNewFileText() {
@@ -73,15 +52,33 @@ public class EditorInfo {
}
public void applyToEditor(Editor editor) {
if (caretMarker != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, caretMarker.getStartOffset());
int caretCol = caretMarker.getStartOffset() - StringUtil.lineColToOffset(newFileText, caretLine, 0);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
editor.getCaretModel().moveToLogicalPosition(pos);
if (editor.getCaretModel().supportsMultipleCarets()) {
List<LogicalPosition> caretPositions = new ArrayList<LogicalPosition>();
List<Segment> selections = new ArrayList<Segment>();
for (EditorTestUtil.Caret caret : caretState.carets) {
LogicalPosition pos = null;
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, caret.offset);
int caretCol = caret.offset - StringUtil.lineColToOffset(newFileText, caretLine, 0);
pos = new LogicalPosition(caretLine, caretCol);
}
caretPositions.add(pos);
selections.add(caret.selection == null ? null : caret.selection);
}
editor.getCaretModel().setCarets(caretPositions, selections);
}
if (selStartMarker != null) {
editor.getSelectionModel().setSelection(selStartMarker.getStartOffset(), selEndMarker.getStartOffset());
else {
assert caretState.carets.size() == 1 : "Multiple carets are not supported by the model";
EditorTestUtil.Caret caret = caretState.carets.get(0);
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, caret.offset);
int caretCol = caret.offset - StringUtil.lineColToOffset(newFileText, caretLine, 0);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
editor.getCaretModel().moveToLogicalPosition(pos);
}
if (caret.selection != null) {
editor.getSelectionModel().setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
}
}
}
}
@@ -17,7 +17,9 @@ package com.intellij.testFramework;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
@@ -26,10 +28,13 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.impl.DefaultEditorTextRepresentationHelper;
import com.intellij.openapi.editor.impl.SoftWrapModelImpl;
import com.intellij.openapi.editor.impl.softwrap.mapping.SoftWrapApplianceManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.tree.IElementType;
import junit.framework.Assert;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -45,6 +50,8 @@ public class EditorTestUtil {
public static final String SELECTION_START_TAG = "<selection>";
public static final String SELECTION_END_TAG = "</selection>";
public static final String BLOCK_SELECTION_START_TAG = "<block>";
public static final String BLOCK_SELECTION_END_TAG = "</block>";
public static final char BACKSPACE_FAKE_CHAR = '\uFFFF';
public static final char SMART_ENTER_FAKE_CHAR = '\uFFFE';
@@ -175,6 +182,108 @@ public class EditorTestUtil {
return !model.getRegisteredSoftWraps().isEmpty();
}
/**
* Equivalent to <code>extractCaretAndSelectionMarkers(document, true)</code>.
*
* @see #extractCaretAndSelectionMarkers(com.intellij.openapi.editor.Document, boolean)
*/
public static CaretsState extractCaretAndSelectionMarkers(Document document) {
return extractCaretAndSelectionMarkers(document, true);
}
/**
* Removes &lt;caret&gt;, &lt;selection&gt; and &lt;/selection&gt; tags from document and returns a list of caret positions and selection
* ranges for each caret. Both caret positions and selection ranges can be null in the returned data.
*
* Should be invoked in write action, as it modifies the document!
*
* @param processBlockSelection if <code>true</code>, &lt;block&gt; and &lt;/block&gt; tags describing a block selection state will also be extracted.
*/
public static CaretsState extractCaretAndSelectionMarkers(Document document, boolean processBlockSelection) {
CaretsState result = new CaretsState();
String fileText = document.getText();
RangeMarker blockSelectionStartMarker = null;
RangeMarker blockSelectionEndMarker = null;
if (processBlockSelection) {
int blockSelectionStart = fileText.indexOf(BLOCK_SELECTION_START_TAG);
int blockSelectionEnd = fileText.indexOf(BLOCK_SELECTION_END_TAG);
if ((blockSelectionStart ^ blockSelectionEnd) < 0) {
throw new IllegalArgumentException("Both block selection opening and closing tag must be present");
}
if (blockSelectionStart >= 0) {
blockSelectionStartMarker = document.createRangeMarker(blockSelectionStart, blockSelectionStart);
blockSelectionEndMarker = document.createRangeMarker(blockSelectionEnd, blockSelectionEnd);
document.deleteString(blockSelectionStartMarker.getStartOffset(), blockSelectionStartMarker.getStartOffset() + BLOCK_SELECTION_START_TAG.length());
document.deleteString(blockSelectionEndMarker.getStartOffset(), blockSelectionEndMarker.getStartOffset() + BLOCK_SELECTION_END_TAG.length());
}
}
boolean multiCaret = StringUtil.getOccurrenceCount(document.getText(), CARET_TAG) > 1
|| StringUtil.getOccurrenceCount(document.getText(), SELECTION_START_TAG) > 1;
int pos = 0;
while (pos < document.getTextLength()) {
fileText = document.getText();
int caretIndex = fileText.indexOf(CARET_TAG, pos);
int selStartIndex = fileText.indexOf(SELECTION_START_TAG, pos);
int selEndIndex = fileText.indexOf(SELECTION_END_TAG, pos);
if ((selStartIndex ^ selEndIndex) < 0) {
selStartIndex = -1;
selEndIndex = -1;
}
if (0 <= selEndIndex && selEndIndex < selStartIndex) {
throw new IllegalArgumentException("Wrong order of selection opening and closing tags");
}
if (caretIndex < 0 && selStartIndex < 0 && selEndIndex < 0) {
break;
}
if (multiCaret && 0 <= caretIndex && caretIndex < selStartIndex) {
selStartIndex = -1;
selEndIndex = -1;
}
if (multiCaret && caretIndex > selEndIndex && selEndIndex >= 0) {
caretIndex = -1;
}
final RangeMarker caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex) : null;
final RangeMarker selStartMarker = selStartIndex >= 0
? document.createRangeMarker(selStartIndex, selStartIndex)
: null;
final RangeMarker selEndMarker = selEndIndex >= 0
? document.createRangeMarker(selEndIndex, selEndIndex)
: null;
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_TAG.length());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(),
selStartMarker.getStartOffset() + SELECTION_START_TAG.length());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(),
selEndMarker.getStartOffset() + SELECTION_END_TAG.length());
}
result.carets.add(new Caret(caretMarker == null ? null : caretMarker.getStartOffset(),
selStartMarker == null || selEndMarker == null
? null
: new TextRange(selStartMarker.getStartOffset(), selEndMarker.getEndOffset())));
pos = Math.max(caretMarker == null ? -1 : caretMarker.getStartOffset(), selEndMarker == null ? -1 : selEndMarker.getEndOffset());
}
if (result.carets.isEmpty()) {
result.carets.add(new Caret(null, null));
}
if (blockSelectionStartMarker != null) {
result.blockSelection = new TextRange(blockSelectionStartMarker.getStartOffset(), blockSelectionEndMarker.getStartOffset());
}
return result;
}
public static void enableMultipleCarets() {
Registry.get("editor.allow.multiple.carets").setValue(true);
}
@@ -182,4 +291,23 @@ public class EditorTestUtil {
public static void disableMultipleCarets() {
Registry.get("editor.allow.multiple.carets").setValue(false);
}
public static class CaretsState {
@NotNull
public final List<Caret> carets = new ArrayList<Caret>();
@Nullable
public TextRange blockSelection;
}
public static class Caret {
@Nullable
public final Integer offset;
@Nullable
public final TextRange selection;
public Caret(Integer offset, TextRange selection) {
this.offset = offset;
this.selection = selection;
}
}
}
@@ -41,8 +41,7 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileEditor.impl.TrailingSpacesStripper;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
@@ -77,10 +76,6 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
protected static PsiFile myFile;
protected static VirtualFile myVFile;
private static final String CARET_MARKER = "<caret>";
@NonNls private static final String SELECTION_START_MARKER = "<selection>";
@NonNls private static final String SELECTION_END_MARKER = "</selection>";
@Override
protected void runTest() throws Throwable {
final Throwable[] throwable = {null};
@@ -166,7 +161,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
protected static Document configureFromFileText(@NonNls @NotNull final String fileName, @NonNls @NotNull final String fileText) throws IOException {
return new WriteCommandAction<Document>(null) {
@Override
protected void run(Result<Document> result) throws Throwable {
protected void run(@NotNull Result<Document> result) throws Throwable {
if (myVFile != null) {
// avoid messing with invalid files, in case someone calls configureXXX() several times
PsiDocumentManager.getInstance(ourProject).commitAllDocuments();
@@ -181,25 +176,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
final Document fakeDocument = new DocumentImpl(fileText);
int caretIndex = fileText.indexOf(CARET_MARKER);
int selStartIndex = fileText.indexOf(SELECTION_START_MARKER);
int selEndIndex = fileText.indexOf(SELECTION_END_MARKER);
final RangeMarker caretMarker = caretIndex >= 0 ? fakeDocument.createRangeMarker(caretIndex, caretIndex) : null;
final RangeMarker selStartMarker = selStartIndex >= 0 ? fakeDocument.createRangeMarker(selStartIndex, selStartIndex) : null;
final RangeMarker selEndMarker = selEndIndex >= 0 ? fakeDocument.createRangeMarker(selEndIndex, selEndIndex) : null;
if (caretMarker != null) {
fakeDocument.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
}
if (selStartMarker != null) {
fakeDocument.deleteString(selStartMarker.getStartOffset(),
selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
}
if (selEndMarker != null) {
fakeDocument.deleteString(selEndMarker.getStartOffset(),
selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
}
EditorTestUtil.CaretsState caretsState = EditorTestUtil.extractCaretAndSelectionMarkers(fakeDocument);
String newFileText = fakeDocument.getText();
Document document;
@@ -209,28 +186,46 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
catch (IOException e) {
throw new RuntimeException(e);
}
setupCaret(caretMarker, newFileText);
setupSelection(selStartMarker, selEndMarker);
setupCaretAndSelection(caretsState, newFileText);
setupEditorForInjectedLanguage();
result.setResult(document);
}
}.execute().getResultObject();
}
private static void setupSelection(final RangeMarker selStartMarker, final RangeMarker selEndMarker) {
if (selStartMarker != null) {
myEditor.getSelectionModel().setSelection(selStartMarker.getStartOffset(), selEndMarker.getStartOffset());
private static void setupCaretAndSelection(EditorTestUtil.CaretsState caretsState, String fileText) {
List<EditorTestUtil.Caret> carets = caretsState.carets;
if (myEditor.getCaretModel().supportsMultipleCarets()) {
List<LogicalPosition> caretPositions = new ArrayList<LogicalPosition>();
List<Segment> selections = new ArrayList<Segment>();
for (EditorTestUtil.Caret caret : carets) {
LogicalPosition pos = null;
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(fileText, caret.offset);
int caretCol = EditorUtil.calcColumnNumber(null, myEditor.getDocument().getText(),
myEditor.getDocument().getLineStartOffset(caretLine), caret.offset,
CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE);
pos = new LogicalPosition(caretLine, caretCol);
}
caretPositions.add(pos);
selections.add(caret.selection == null ? null : caret.selection);
}
myEditor.getCaretModel().setCarets(caretPositions, selections);
}
}
private static void setupCaret(final RangeMarker caretMarker, String fileText) {
if (caretMarker != null) {
int caretLine = StringUtil.offsetToLineNumber(fileText, caretMarker.getStartOffset());
int caretCol = EditorUtil.calcColumnNumber(null, myEditor.getDocument().getText(),
myEditor.getDocument().getLineStartOffset(caretLine), caretMarker.getStartOffset(),
CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
myEditor.getCaretModel().moveToLogicalPosition(pos);
else {
assertEquals("Caret model doesn't support multiple carets", 1, carets.size());
EditorTestUtil.Caret caret = carets.get(0);
if (caret.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(fileText, caret.offset);
int caretCol = EditorUtil.calcColumnNumber(null, myEditor.getDocument().getText(),
myEditor.getDocument().getLineStartOffset(caretLine), caret.offset,
CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE);
LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
myEditor.getCaretModel().moveToLogicalPosition(pos);
}
if (caret.selection != null) {
myEditor.getSelectionModel().setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
}
}
}
@@ -381,7 +376,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
((DocumentImpl)document).stripTrailingSpaces(getProject());
}
List<Pair<Integer, TextRange>> carets = extractCaretAndSelectionMarkers(document);
EditorTestUtil.CaretsState carets = EditorTestUtil.extractCaretAndSelectionMarkers(document);
PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
String newFileText = document.getText();
@@ -399,65 +394,6 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
});
}
private static List<Pair<Integer, TextRange>> extractCaretAndSelectionMarkers(Document document) {
List<Pair<Integer, TextRange>> carets = new ArrayList<Pair<Integer, TextRange>>();
int pos = 0;
while (pos < document.getTextLength()) {
String fileText = document.getText();
int caretIndex = fileText.indexOf(CARET_MARKER, pos);
int selStartIndex = fileText.indexOf(SELECTION_START_MARKER, pos);
int selEndIndex = fileText.indexOf(SELECTION_END_MARKER, pos);
if (caretIndex < 0 && selStartIndex < 0 && selEndIndex < 0) {
break;
}
if ((selStartIndex ^ selEndIndex) < 0) {
throw new IllegalArgumentException("Both selection opening and closing tag must be present");
}
if (0 <= selEndIndex && selEndIndex < selStartIndex) {
throw new IllegalArgumentException("Wrong order of selection opening and closing tags");
}
if (caretIndex < selStartIndex) {
selStartIndex = -1;
selEndIndex = -1;
}
if (caretIndex > selEndIndex && selEndIndex >= 0) {
caretIndex = -1;
}
final RangeMarker caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex) : null;
final RangeMarker selStartMarker = selStartIndex >= 0
? document.createRangeMarker(selStartIndex, selStartIndex)
: null;
final RangeMarker selEndMarker = selEndIndex >= 0
? document.createRangeMarker(selEndIndex, selEndIndex)
: null;
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(),
selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(),
selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
}
carets.add(new Pair<Integer, TextRange>(caretMarker == null ? null : caretMarker.getStartOffset(),
selStartMarker == null || selEndMarker == null ? null : new TextRange(selStartMarker.getStartOffset(), selEndMarker.getEndOffset())));
pos = Math.max(caretMarker == null ? -1 : caretMarker.getStartOffset(), selEndMarker == null ? -1 : selEndMarker.getEndOffset()) + 1;
}
if (carets.isEmpty()) {
carets.add(new Pair<Integer, TextRange>(null, null));
}
return carets;
}
private static String getMessage(@NonNls String engineMessage, String userMessage) {
if (userMessage == null) return engineMessage;
return userMessage + " [" + engineMessage + "]";
@@ -468,12 +404,12 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
@SuppressWarnings("ConstantConditions")
private static void checkCaretAndSelectionPositions(List<Pair<Integer, TextRange>> carets, String newFileText, String message) {
private static void checkCaretAndSelectionPositions(EditorTestUtil.CaretsState caretState, String newFileText, String message) {
CaretModel caretModel = myEditor.getCaretModel();
List<Caret> allCarets = caretModel.supportsMultipleCarets() ? new ArrayList<Caret>(caretModel.getAllCarets()) : null;
assertEquals("Unexpected number of carets", carets.size(), caretModel.supportsMultipleCarets() ? allCarets.size() : 1);
for (int i = 0; i < carets.size(); i++) {
String caretDescription = getCaretDescription(i, carets.size());
assertEquals("Unexpected number of carets", caretState.carets.size(), caretModel.supportsMultipleCarets() ? allCarets.size() : 1);
for (int i = 0; i < caretState.carets.size(); i++) {
String caretDescription = getCaretDescription(i, caretState.carets.size());
Caret currentCaret = caretModel.supportsMultipleCarets() ? allCarets.get(i) : null;
LogicalPosition actualCaretPosition;
if (caretModel.supportsMultipleCarets()) {
@@ -482,23 +418,23 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
else {
actualCaretPosition = caretModel.getLogicalPosition();
}
Pair<Integer, TextRange> expected = carets.get(i);
if (expected.first != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, expected.first);
EditorTestUtil.Caret expected = caretState.carets.get(i);
if (expected.offset != null) {
int caretLine = StringUtil.offsetToLineNumber(newFileText, expected.offset);
int caretCol = EditorUtil.calcColumnNumber(null, newFileText,
StringUtil.lineColToOffset(newFileText, caretLine, 0),
expected.first,
expected.offset,
CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE);
assertEquals(getMessage("caretLine" + caretDescription, message), caretLine, actualCaretPosition.line);
assertEquals(getMessage("caretColumn" + caretDescription, message), caretCol, actualCaretPosition.column);
}
if (expected.second != null) {
int selStartLine = StringUtil.offsetToLineNumber(newFileText, expected.second.getStartOffset());
int selStartCol = expected.second.getStartOffset() - StringUtil.lineColToOffset(newFileText, selStartLine, 0);
if (expected.selection != null) {
int selStartLine = StringUtil.offsetToLineNumber(newFileText, expected.selection.getStartOffset());
int selStartCol = expected.selection.getStartOffset() - StringUtil.lineColToOffset(newFileText, selStartLine, 0);
int selEndLine = StringUtil.offsetToLineNumber(newFileText, expected.second.getEndOffset());
int selEndCol = expected.second.getEndOffset() - StringUtil.lineColToOffset(newFileText, selEndLine, 0);
int selEndLine = StringUtil.offsetToLineNumber(newFileText, expected.selection.getEndOffset());
int selEndCol = expected.selection.getEndOffset() - StringUtil.lineColToOffset(newFileText, selEndLine, 0);
assertEquals(
getMessage("selectionStartLine" + caretDescription, message),
@@ -62,10 +62,6 @@ import java.util.List;
public interface CodeInsightTestFixture extends IdeaProjectTestFixture {
@NonNls String CARET_MARKER = "<caret>";
@NonNls String SELECTION_START_MARKER = "<selection>";
@NonNls String SELECTION_END_MARKER = "</selection>";
@NonNls String BLOCK_START_MARKER = "<block>";
@NonNls String BLOCK_END_MARKER = "</block>";
@NonNls String ERROR_MARKER = "error";
@NonNls String WARNING_MARKER = "warning";
@@ -123,6 +123,10 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dmitry Avdeev
*/
@@ -1355,21 +1359,32 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
assert myEditor != null : "Editor couldn't be created for file: " +
copy.getPath() +
", use copyFileToProject(..) method for this file instead of configureByFile(..)";
int offset = loader.caretMarker != null ? loader.caretMarker.getStartOffset() : 0;
myEditor.getCaretModel().moveToOffset(offset);
if (loader.selStartMarker != null && loader.selEndMarker != null) {
int start = loader.selStartMarker.getStartOffset();
int end = loader.selEndMarker.getStartOffset();
if (loader.blockSelection) {
myEditor.getSelectionModel().setBlockSelection(myEditor.offsetToLogicalPosition(start), myEditor.offsetToLogicalPosition(end));
}
else {
myEditor.getSelectionModel().setSelection(start, end);
if (myEditor.getCaretModel().supportsMultipleCarets()) {
List<LogicalPosition> caretPositions = new ArrayList<LogicalPosition>();
List<Segment> selections = new ArrayList<Segment>();
for (EditorTestUtil.Caret caret : loader.caretState.carets) {
caretPositions.add(caret.offset == null ? null : myEditor.offsetToLogicalPosition(caret.offset));
selections.add(caret.selection == null ? null : caret.selection);
}
myEditor.getCaretModel().setCarets(caretPositions, selections);
}
else {
myEditor.getSelectionModel().removeSelection();
assert loader.caretState.carets.size() == 1 : "Multiple carets are not supported by the model";
EditorTestUtil.Caret caret = loader.caretState.carets.get(0);
int offset = caret.offset != null ? caret.offset : 0;
myEditor.getCaretModel().moveToOffset(offset);
if (caret.selection != null) {
myEditor.getSelectionModel().setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
}
else {
myEditor.getSelectionModel().removeSelection();
}
}
if (loader.caretState.blockSelection != null) {
myEditor.getSelectionModel().setBlockSelection(myEditor.offsetToLogicalPosition(loader.caretState.blockSelection.getStartOffset()),
myEditor.offsetToLogicalPosition(loader.caretState.blockSelection.getEndOffset()));
}
Module module = getModule();
@@ -1632,10 +1647,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
static class SelectionAndCaretMarkupLoader {
final String filePath;
final String newFileText;
final RangeMarker caretMarker;
final RangeMarker selStartMarker;
final RangeMarker selEndMarker;
final boolean blockSelection;
final EditorTestUtil.CaretsState caretState;
static SelectionAndCaretMarkupLoader fromFile(String path, Project project, String charset) throws IOException {
return new SelectionAndCaretMarkupLoader(
@@ -1661,38 +1673,12 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
this.filePath = filePath;
final Document document = EditorFactory.getInstance().createDocument(fileText);
int caretIndex = fileText.indexOf(CARET_MARKER);
int selStartIndex = fileText.indexOf(SELECTION_START_MARKER);
int selEndIndex = fileText.indexOf(SELECTION_END_MARKER);
int blockStartIndex = fileText.indexOf(BLOCK_START_MARKER);
int blockEndIndex = fileText.indexOf(BLOCK_END_MARKER);
caretMarker = caretIndex >= 0 ? document.createRangeMarker(caretIndex, caretIndex + CARET_MARKER.length()) : null;
if (selStartIndex >= 0 || selEndIndex >= 0) {
blockSelection = false;
selStartMarker = selStartIndex >= 0? document.createRangeMarker(selStartIndex, selStartIndex + SELECTION_START_MARKER.length()) : null;
selEndMarker = selEndIndex >= 0? document.createRangeMarker(selEndIndex, selEndIndex + SELECTION_END_MARKER.length()) : null;
}
else {
selStartMarker = blockStartIndex >= 0 ? document.createRangeMarker(blockStartIndex, blockStartIndex + BLOCK_START_MARKER.length()) : null;
selEndMarker = blockEndIndex >= 0 ? document.createRangeMarker(blockEndIndex, blockEndIndex + BLOCK_END_MARKER.length()) : null;
blockSelection = selStartMarker != null || selEndMarker != null;
}
new WriteCommandAction(project) {
caretState = new WriteCommandAction<EditorTestUtil.CaretsState>(project) {
@Override
protected void run(Result result) throws Exception {
if (caretMarker != null) {
document.deleteString(caretMarker.getStartOffset(), caretMarker.getEndOffset());
}
if (selStartMarker != null) {
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getEndOffset());
}
if (selEndMarker != null) {
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getEndOffset());
}
protected void run(@NotNull Result<EditorTestUtil.CaretsState> result) throws Exception {
result.setResult(EditorTestUtil.extractCaretAndSelectionMarkers(document));
}
}.execute();
}.execute().getResultObject();
newFileText = document.getText();
}
@@ -1720,6 +1706,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
}
@SuppressWarnings("ConstantConditions")
private void checkResult(final String expectedFile,
final boolean stripTrailingSpaces,
final SelectionAndCaretMarkupLoader loader,
@@ -1756,57 +1743,74 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
}
}
if (loader.caretMarker != null) {
final int tabSize = CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE;
int caretLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.caretMarker.getStartOffset());
int caretCol = EditorUtil.calcColumnNumber(null, loader.newFileText, StringUtil.lineColToOffset(loader.newFileText, caretLine, 0),
loader.caretMarker.getStartOffset(), tabSize);
final int actualLine = editor.getCaretModel().getLogicalPosition().line;
final int actualCol = editor.getCaretModel().getLogicalPosition().column;
boolean caretPositionEquals = caretLine == actualLine && caretCol == actualCol;
Assert.assertTrue("Caret position in " + expectedFile + " differs. Expected " + genCaretPositionPresentation(caretLine, caretCol) +
". Actual " + genCaretPositionPresentation(actualLine, actualCol), caretPositionEquals);
}
if (loader.selStartMarker != null && loader.selEndMarker != null) {
int selStartLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.selStartMarker.getStartOffset());
int selStartCol = loader.selStartMarker.getStartOffset() - StringUtil.lineColToOffset(loader.newFileText, selStartLine, 0);
int selEndLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.selEndMarker.getEndOffset());
int selEndCol = loader.selEndMarker.getEndOffset() - StringUtil.lineColToOffset(loader.newFileText, selEndLine, 0);
int selectionStart;
int selectionEnd;
if (editor.getSelectionModel().hasBlockSelection()) {
int[] starts = editor.getSelectionModel().getBlockSelectionStarts();
int[] ends = editor.getSelectionModel().getBlockSelectionEnds();
selectionStart = starts[starts.length-1];
selectionEnd = ends[ends.length-1];
boolean hasChecks = false;
for (int i = 0; i < loader.caretState.carets.size(); i++) {
EditorTestUtil.Caret expected = loader.caretState.carets.get(i);
if (expected.offset != null || expected.selection != null) {
hasChecks = true;
break;
}
else {
selectionStart = editor.getSelectionModel().getSelectionStart();
selectionEnd = editor.getSelectionModel().getSelectionEnd();
}
if (!hasChecks) {
return; // nothing to check, so we skip caret/selection assertions
}
CaretModel caretModel = editor.getCaretModel();
List<Caret> allCarets = caretModel.supportsMultipleCarets() ? new ArrayList<Caret>(caretModel.getAllCarets()) : null;
assertEquals("Unexpected number of carets", loader.caretState.carets.size(), caretModel.supportsMultipleCarets() ? allCarets.size() : 1);
for (int i = 0; i < loader.caretState.carets.size(); i++) {
EditorTestUtil.Caret expected = loader.caretState.carets.get(i);
String caretDescription = loader.caretState.carets.size() == 1 ? "" : "(" + i + "/" + loader.caretState.carets.size() + ") ";
if (expected.offset != null) {
final int tabSize = CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(StdFileTypes.JAVA).TAB_SIZE;
int caretLine = StringUtil.offsetToLineNumber(loader.newFileText, expected.offset);
int caretCol = EditorUtil.calcColumnNumber(null, loader.newFileText, StringUtil.lineColToOffset(loader.newFileText, caretLine, 0), expected.offset, tabSize);
final int actualLine = caretModel.supportsMultipleCarets() ? allCarets.get(i).getLogicalPosition().line : caretModel.getLogicalPosition().line;
final int actualCol = caretModel.supportsMultipleCarets() ? allCarets.get(i).getLogicalPosition().column : caretModel.getLogicalPosition().column;
boolean caretPositionEquals = caretLine == actualLine && caretCol == actualCol;
assertTrue("Caret" + caretDescription + " position in " + expectedFile + " differs. Expected " + genCaretPositionPresentation(caretLine, caretCol)
+ ". Actual " + genCaretPositionPresentation(actualLine, actualCol), caretPositionEquals);
}
final int selStartLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionStart);
final int selStartColActual = selectionStart - StringUtil.lineColToOffset(loader.newFileText, selStartLineActual, 0);
if (expected.selection != null) {
int selStartLine = StringUtil.offsetToLineNumber(loader.newFileText, expected.selection.getStartOffset());
int selStartCol = expected.selection.getStartOffset() - StringUtil.lineColToOffset(loader.newFileText, selStartLine, 0);
final int selEndLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionEnd);
final int selEndColActual = selectionEnd - StringUtil.lineColToOffset(loader.newFileText, selEndLineActual, 0);
int selEndLine = StringUtil.offsetToLineNumber(loader.newFileText, expected.selection.getEndOffset());
int selEndCol = expected.selection.getEndOffset() - StringUtil.lineColToOffset(loader.newFileText, selEndLine, 0);
final boolean selectionEquals = selStartCol == selStartColActual &&
selStartLine == selStartLineActual &&
selEndCol == selEndColActual &&
selEndLine == selEndLineActual;
Assert.assertTrue("selection in " + expectedFile +
" differs. Expected " + genSelectionPresentation(selStartLine, selStartCol, selEndLine, selEndCol) +
". Actual " + genSelectionPresentation(selStartLineActual, selStartColActual, selEndLineActual, selEndColActual),
selectionEquals);
}
else if (editor != null) {
Assert.assertTrue("has no selection in " + expectedFile, !editor.getSelectionModel().hasSelection());
int selectionStart;
int selectionEnd;
if (editor.getSelectionModel().hasBlockSelection()) {
int[] starts = editor.getSelectionModel().getBlockSelectionStarts();
int[] ends = editor.getSelectionModel().getBlockSelectionEnds();
selectionStart = starts[starts.length-1];
selectionEnd = ends[ends.length-1];
}
else {
selectionStart = caretModel.supportsMultipleCarets() ? allCarets.get(i).getSelectionStart() : editor.getSelectionModel().getSelectionStart();
selectionEnd = caretModel.supportsMultipleCarets() ? allCarets.get(i).getSelectionEnd() : editor.getSelectionModel().getSelectionEnd();
}
final int selStartLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionStart);
final int selStartColActual = selectionStart - StringUtil.lineColToOffset(loader.newFileText, selStartLineActual, 0);
final int selEndLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionEnd);
final int selEndColActual = selectionEnd - StringUtil.lineColToOffset(loader.newFileText, selEndLineActual, 0);
final boolean selectionEquals = selStartCol == selStartColActual &&
selStartLine == selStartLineActual &&
selEndCol == selEndColActual &&
selEndLine == selEndLineActual;
assertTrue(caretDescription + "selection in " + expectedFile + " differs. Expected " + genSelectionPresentation(selStartLine, selStartCol, selEndLine, selEndCol) +
". Actual " + genSelectionPresentation(selStartLineActual, selStartColActual, selEndLineActual, selEndColActual),
selectionEquals
);
}
else if (editor != null) {
assertFalse(caretDescription + "has no selection in " + expectedFile, editor.getSelectionModel().hasSelection());
}
}
}
@@ -1,9 +1,24 @@
/*
* Copyright 2000-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.parentheses;
class ComparisonParenthese {
void foo(Object a, boolean b) {
final boolean c = b == (a <caret>!= null);
boolean d = c == (1 < 3<caret>);
boolean d = c == (1 < 3);
}
}
@@ -3,11 +3,11 @@ if (true) {
}
pri<caret>ntln 'b'
pri<caret>ntln 'c'
println 'c'
-----
if (true) {
println 'a'
pri<caret>ntln 'b'
}
pri<caret>ntln 'c'
println 'c'
@@ -2,10 +2,10 @@ if (true) {
}
pri<caret>ntln 'b'
pri<caret>ntln 'c'
println 'c'
-----
if (true) {
pri<caret>ntln 'b'
}
pri<caret>ntln 'c'
println 'c'
@@ -3,7 +3,7 @@ class A{
* @param x<caret> abc
* dfgdsfgdjkg
* @param y sdgfhjsd
* @param <caret>
* @param
*/
def abc(def xx, def xy) {