mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
better surrounder: document-based with column mode support
This commit is contained in:
+6
-4
@@ -39,6 +39,7 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.LogicalPosition;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.editor.SelectionModel;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.options.ShowSettingsUtil;
|
||||
@@ -86,11 +87,12 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
|
||||
|
||||
@Nullable
|
||||
public static List<AnAction> buildSurroundActions(final Project project, final Editor editor, PsiFile file, @Nullable Surrounder surrounder){
|
||||
if (!editor.getSelectionModel().hasSelection()) {
|
||||
editor.getSelectionModel().selectLineAtCaret();
|
||||
SelectionModel selectionModel = editor.getSelectionModel();
|
||||
if (!selectionModel.hasSelection() && !selectionModel.hasBlockSelection()) {
|
||||
selectionModel.selectLineAtCaret();
|
||||
}
|
||||
int startOffset = editor.getSelectionModel().getSelectionStart();
|
||||
int endOffset = editor.getSelectionModel().getSelectionEnd();
|
||||
int startOffset = selectionModel.getSelectionStart();
|
||||
int endOffset = selectionModel.getSelectionEnd();
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
|
||||
+2
@@ -59,6 +59,8 @@ 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";
|
||||
|
||||
+42
-11
@@ -1304,7 +1304,14 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
|
||||
myEditor.getCaretModel().moveToOffset(offset);
|
||||
}
|
||||
if (loader.selStartMarker != null && loader.selEndMarker != null) {
|
||||
myEditor.getSelectionModel().setSelection(loader.selStartMarker.getStartOffset(), loader.selEndMarker.getStartOffset());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Module module = getModule();
|
||||
@@ -1524,6 +1531,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
|
||||
final RangeMarker caretMarker;
|
||||
final RangeMarker selStartMarker;
|
||||
final RangeMarker selEndMarker;
|
||||
final boolean blockSelection;
|
||||
|
||||
static SelectionAndCaretMarkupLoader fromFile(String path, Project project) throws IOException {
|
||||
return new SelectionAndCaretMarkupLoader(StringUtil.convertLineSeparators(FileUtil.loadFile(new File(path))),
|
||||
@@ -1551,22 +1559,32 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
|
||||
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) : null;
|
||||
selStartMarker = selStartIndex >= 0 ? document.createRangeMarker(selStartIndex, selStartIndex) : null;
|
||||
selEndMarker = selEndIndex >= 0 ? document.createRangeMarker(selEndIndex, selEndIndex) : null;
|
||||
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) {
|
||||
@Override
|
||||
protected void run(Result result) throws Exception {
|
||||
if (caretMarker != null) {
|
||||
document.deleteString(caretMarker.getStartOffset(), caretMarker.getStartOffset() + CARET_MARKER.length());
|
||||
document.deleteString(caretMarker.getStartOffset(), caretMarker.getEndOffset());
|
||||
}
|
||||
if (selStartMarker != null) {
|
||||
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getStartOffset() + SELECTION_START_MARKER.length());
|
||||
document.deleteString(selStartMarker.getStartOffset(), selStartMarker.getEndOffset());
|
||||
}
|
||||
if (selEndMarker != null) {
|
||||
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getStartOffset() + SELECTION_END_MARKER.length());
|
||||
document.deleteString(selEndMarker.getStartOffset(), selEndMarker.getEndOffset());
|
||||
}
|
||||
}
|
||||
}.execute();
|
||||
@@ -1640,11 +1658,24 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
|
||||
int selEndLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.selEndMarker.getEndOffset());
|
||||
int selEndCol = loader.selEndMarker.getEndOffset() - StringUtil.lineColToOffset(loader.newFileText, selEndLine, 0);
|
||||
|
||||
final int selStartLineActual = StringUtil.offsetToLineNumber(loader.newFileText, myEditor.getSelectionModel().getSelectionStart());
|
||||
final int selStartColActual = myEditor.getSelectionModel().getSelectionStart() - StringUtil.lineColToOffset(loader.newFileText, selStartLineActual, 0);
|
||||
int selectionStart;
|
||||
int selectionEnd;
|
||||
if (myEditor.getSelectionModel().hasBlockSelection()) {
|
||||
int[] starts = myEditor.getSelectionModel().getBlockSelectionStarts();
|
||||
int[] ends = myEditor.getSelectionModel().getBlockSelectionEnds();
|
||||
selectionStart = starts[starts.length-1];
|
||||
selectionEnd = ends[ends.length-1];
|
||||
}
|
||||
else {
|
||||
selectionStart = myEditor.getSelectionModel().getSelectionStart();
|
||||
selectionEnd = myEditor.getSelectionModel().getSelectionEnd();
|
||||
}
|
||||
|
||||
final int selEndLineActual = StringUtil.offsetToLineNumber(loader.newFileText, myEditor.getSelectionModel().getSelectionEnd());
|
||||
final int selEndColActual = myEditor.getSelectionModel().getSelectionEnd() - StringUtil.lineColToOffset(loader.newFileText, selEndLineActual, 0);
|
||||
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 &&
|
||||
|
||||
Reference in New Issue
Block a user