mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add default implementations to remaining delegated-to-caret methods in SelectionModel
GitOrigin-RevId: f75a4b07e634d24e14125fc678d512ee2ce27c0a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b584986070
commit
90ca9a6360
@@ -41,7 +41,9 @@ public interface SelectionModel {
|
||||
* @return object that encapsulates information about visual position of selected text start if any
|
||||
*/
|
||||
@Nullable
|
||||
VisualPosition getSelectionStartPosition();
|
||||
default VisualPosition getSelectionStartPosition() {
|
||||
return getEditor().getCaretModel().getCurrentCaret().getSelectionStartPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the end offset in the document of the selected text range, or the caret
|
||||
@@ -57,7 +59,9 @@ public interface SelectionModel {
|
||||
* @return object that encapsulates information about visual position of selected text end if any;
|
||||
*/
|
||||
@Nullable
|
||||
VisualPosition getSelectionEndPosition();
|
||||
default VisualPosition getSelectionEndPosition() {
|
||||
return getEditor().getCaretModel().getCurrentCaret().getSelectionEndPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text selected in the editor.
|
||||
@@ -65,14 +69,33 @@ public interface SelectionModel {
|
||||
* @return the selected text, or {@code null} if there is currently no selection.
|
||||
*/
|
||||
@Nullable
|
||||
String getSelectedText();
|
||||
default String getSelectedText() {
|
||||
return getSelectedText(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* If {@code allCarets} is {@code true}, returns the concatenation of selections for all carets, or {@code null} if there
|
||||
* are no selections. If {@code allCarets} is {@code false}, works just like {@link #getSelectedText}.
|
||||
*/
|
||||
@Nullable
|
||||
String getSelectedText(boolean allCarets);
|
||||
default String getSelectedText(boolean allCarets) {
|
||||
if (allCarets && getEditor().getCaretModel().supportsMultipleCarets()) {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
String separator = "";
|
||||
for (Caret caret : getEditor().getCaretModel().getAllCarets()) {
|
||||
buf.append(separator);
|
||||
String caretSelectedText = caret.getSelectedText();
|
||||
if (caretSelectedText != null) {
|
||||
buf.append(caretSelectedText);
|
||||
}
|
||||
separator = "\n";
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
else {
|
||||
return getEditor().getCaretModel().getCurrentCaret().getSelectedText();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset from which the user started to extend the selection (the selection start
|
||||
@@ -82,13 +105,17 @@ public interface SelectionModel {
|
||||
* @return the offset from which the selection was started, or the caret offset if there is
|
||||
* currently no selection.
|
||||
*/
|
||||
int getLeadSelectionOffset();
|
||||
default int getLeadSelectionOffset() {
|
||||
return getEditor().getCaretModel().getCurrentCaret().getLeadSelectionOffset();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return object that encapsulates information about visual position from which the user started to extend the selection if any
|
||||
*/
|
||||
@Nullable
|
||||
VisualPosition getLeadSelectionPosition();
|
||||
default VisualPosition getLeadSelectionPosition() {
|
||||
return getEditor().getCaretModel().getCurrentCaret().getLeadSelectionPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a range of text is currently selected.
|
||||
@@ -169,7 +196,16 @@ public interface SelectionModel {
|
||||
* Removes the selection in the editor. If {@code allCarets} is {@code true}, removes selections from all carets in the
|
||||
* editor, otherwise, does this just for the current caret.
|
||||
*/
|
||||
void removeSelection(boolean allCarets);
|
||||
default void removeSelection(boolean allCarets) {
|
||||
if (!allCarets) {
|
||||
getEditor().getCaretModel().getCurrentCaret().removeSelection();
|
||||
}
|
||||
else {
|
||||
for (Caret caret : getEditor().getCaretModel().getAllCarets()) {
|
||||
caret.removeSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener for receiving information about selection changes.
|
||||
|
||||
-36
@@ -4,7 +4,6 @@ package com.intellij.openapi.editor.impl;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.LogicalPosition;
|
||||
import com.intellij.openapi.editor.SelectionModel;
|
||||
import com.intellij.openapi.editor.VisualPosition;
|
||||
import com.intellij.openapi.editor.event.SelectionListener;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -22,47 +21,12 @@ class ImaginarySelectionModel implements SelectionModel {
|
||||
return myEditor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getSelectedText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getSelectedText(boolean allCarets) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getSelectionStartPosition() {
|
||||
throw myEditor.notImplemented();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getSelectionEndPosition() {
|
||||
throw myEditor.notImplemented();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeadSelectionOffset() {
|
||||
throw myEditor.notImplemented();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getLeadSelectionPosition() {
|
||||
throw myEditor.notImplemented();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSelection(boolean allCarets) {
|
||||
throw myEditor.notImplemented();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSelectionListener(@NotNull SelectionListener listener) {
|
||||
throw myEditor.notImplemented();
|
||||
|
||||
-27
@@ -42,38 +42,11 @@ class SelectionModelWindow implements SelectionModel {
|
||||
return myInjectedEditor.offsetToVisualPosition(getSelectionEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSelectedText() {
|
||||
return myHostModel.getSelectedText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getSelectedText(boolean allCarets) {
|
||||
return myHostModel.getSelectedText(allCarets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeadSelectionOffset() {
|
||||
return myDocument.hostToInjected(myHostModel.getLeadSelectionOffset());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getLeadSelectionPosition() {
|
||||
return myHostModel.getLeadSelectionPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSelection(boolean anyCaret) {
|
||||
return myHostModel.hasSelection(anyCaret);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSelection(boolean allCarets) {
|
||||
myHostModel.removeSelection(allCarets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSelectionListener(@NotNull final SelectionListener listener) {
|
||||
myHostModel.addSelectionListener(listener);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
package com.intellij.openapi.editor.impl;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.*;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
@@ -50,18 +49,6 @@ public class SelectionModelImpl implements SelectionModel {
|
||||
return myEditor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getSelectionStartPosition() {
|
||||
return myEditor.getCaretModel().getCurrentCaret().getSelectionStartPosition();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getSelectionEndPosition() {
|
||||
return myEditor.getCaretModel().getCurrentCaret().getSelectionEndPosition();
|
||||
}
|
||||
|
||||
void fireSelectionChanged(SelectionEvent event) {
|
||||
TextRange[] oldRanges = event.getOldRanges();
|
||||
TextRange[] newRanges = event.getNewRanges();
|
||||
@@ -96,18 +83,6 @@ public class SelectionModelImpl implements SelectionModel {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSelection(boolean allCarets) {
|
||||
if (!allCarets) {
|
||||
myEditor.getCaretModel().getCurrentCaret().removeSelection();
|
||||
}
|
||||
else {
|
||||
for (Caret caret : myEditor.getCaretModel().getAllCarets()) {
|
||||
caret.removeSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlockSelection(@NotNull LogicalPosition blockStart, @NotNull LogicalPosition blockEnd) {
|
||||
List<CaretState> caretStates = EditorModificationUtil.calcBlockSelectionState(myEditor, blockStart, blockEnd);
|
||||
@@ -147,33 +122,6 @@ public class SelectionModelImpl implements SelectionModel {
|
||||
LOG.assertTrue(success);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSelectedText() {
|
||||
return getSelectedText(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSelectedText(boolean allCarets) {
|
||||
ApplicationManager.getApplication().assertReadAccessAllowed();
|
||||
|
||||
if (myEditor.getCaretModel().supportsMultipleCarets() && allCarets) {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
String separator = "";
|
||||
for (Caret caret : myEditor.getCaretModel().getAllCarets()) {
|
||||
buf.append(separator);
|
||||
String caretSelectedText = caret.getSelectedText();
|
||||
if (caretSelectedText != null) {
|
||||
buf.append(caretSelectedText);
|
||||
}
|
||||
separator = "\n";
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
else {
|
||||
return myEditor.getCaretModel().getCurrentCaret().getSelectedText();
|
||||
}
|
||||
}
|
||||
|
||||
public static void doSelectLineAtCaret(Caret caret) {
|
||||
Editor editor = caret.getEditor();
|
||||
int lineNumber = caret.getLogicalPosition().line;
|
||||
@@ -194,17 +142,6 @@ public class SelectionModelImpl implements SelectionModel {
|
||||
caret.setSelection(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeadSelectionOffset() {
|
||||
return myEditor.getCaretModel().getCurrentCaret().getLeadSelectionOffset();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getLeadSelectionPosition() {
|
||||
return myEditor.getCaretModel().getCurrentCaret().getLeadSelectionPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copySelectionToClipboard() {
|
||||
EditorCopyPasteHelper.getInstance().copySelectionToClipboard(myEditor);
|
||||
|
||||
+2
-2
@@ -21,10 +21,10 @@ import javax.swing.text.BadLocationException;
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class TextAreaDocument extends TextComponentDocument {
|
||||
class TextAreaDocument extends TextComponentDocument {
|
||||
private final JTextArea myTextArea;
|
||||
|
||||
public TextAreaDocument(final JTextArea textComponent) {
|
||||
TextAreaDocument(final JTextArea textComponent) {
|
||||
super(textComponent);
|
||||
myTextArea = textComponent;
|
||||
}
|
||||
|
||||
+89
-30
@@ -15,15 +15,24 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.textarea;
|
||||
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.openapi.editor.*;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.openapi.editor.impl.SelectionModelImpl;
|
||||
import com.intellij.openapi.util.UserDataHolderBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class TextComponentCaret extends UserDataHolderBase implements Caret {
|
||||
private final Editor myEditor;
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.JTextComponent;
|
||||
|
||||
public TextComponentCaret(Editor editor) {
|
||||
class TextComponentCaret extends UserDataHolderBase implements Caret {
|
||||
private final TextComponentEditorImpl myEditor;
|
||||
|
||||
TextComponentCaret(TextComponentEditorImpl editor) {
|
||||
myEditor = editor;
|
||||
}
|
||||
|
||||
@@ -51,103 +60,147 @@ public class TextComponentCaret extends UserDataHolderBase implements Caret {
|
||||
|
||||
@Override
|
||||
public void moveToLogicalPosition(@NotNull LogicalPosition pos) {
|
||||
getCaretModel().moveToLogicalPosition(pos);
|
||||
moveToOffset(myEditor.logicalPositionToOffset(pos), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToVisualPosition(@NotNull VisualPosition pos) {
|
||||
getCaretModel().moveToVisualPosition(pos);
|
||||
moveToLogicalPosition(myEditor.visualToLogicalPosition(pos));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToOffset(int offset) {
|
||||
getCaretModel().moveToOffset(offset);
|
||||
moveToOffset(offset, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToOffset(int offset, boolean locateBeforeSoftWrap) {
|
||||
getCaretModel().moveToOffset(offset, locateBeforeSoftWrap);
|
||||
JTextComponent textComponent = getTextComponent();
|
||||
int targetOffset = Math.min(offset, textComponent.getText().length());
|
||||
int currentPosition = textComponent.getCaretPosition();
|
||||
// We try to preserve selection, to match EditorImpl behaviour.
|
||||
// It's only possible though, if target offset is located at either end of existing selection.
|
||||
if (targetOffset != currentPosition) {
|
||||
if (targetOffset == textComponent.getCaret().getMark()) {
|
||||
textComponent.setCaretPosition(currentPosition);
|
||||
textComponent.moveCaretPosition(targetOffset);
|
||||
}
|
||||
else {
|
||||
textComponent.setCaretPosition(targetOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUpToDate() {
|
||||
return getCaretModel().isUpToDate();
|
||||
return true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public LogicalPosition getLogicalPosition() {
|
||||
return getCaretModel().getLogicalPosition();
|
||||
JTextComponent textComponent = getTextComponent();
|
||||
int caretPos = textComponent.getCaretPosition();
|
||||
int line;
|
||||
int lineStart;
|
||||
if (textComponent instanceof JTextArea) {
|
||||
final JTextArea textArea = (JTextArea)textComponent;
|
||||
try {
|
||||
line = textArea.getLineOfOffset(caretPos);
|
||||
lineStart = textArea.getLineStartOffset(line);
|
||||
}
|
||||
catch (BadLocationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
line = 0;
|
||||
lineStart = 0;
|
||||
}
|
||||
return new LogicalPosition(line, caretPos - lineStart);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getVisualPosition() {
|
||||
return getCaretModel().getVisualPosition();
|
||||
LogicalPosition pos = getLogicalPosition();
|
||||
return new VisualPosition(pos.line, pos.column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOffset() {
|
||||
return getCaretModel().getOffset();
|
||||
return getTextComponent().getCaretPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisualLineStart() {
|
||||
return getCaretModel().getVisualLineStart();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisualLineEnd() {
|
||||
return getCaretModel().getVisualLineEnd();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelectionStart() {
|
||||
return getSelectionModel().getSelectionStart();
|
||||
return getTextComponent().getSelectionStart();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getSelectionStartPosition() {
|
||||
return myEditor.offsetToVisualPosition(getSelectionModel().getSelectionStart());
|
||||
return myEditor.offsetToVisualPosition(getSelectionStart());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelectionEnd() {
|
||||
return getSelectionModel().getSelectionEnd();
|
||||
return getTextComponent().getSelectionEnd();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getSelectionEndPosition() {
|
||||
return myEditor.offsetToVisualPosition(getSelectionModel().getSelectionEnd());
|
||||
return myEditor.offsetToVisualPosition(getSelectionEnd());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getSelectedText() {
|
||||
return getSelectionModel().getSelectedText();
|
||||
return getTextComponent().getSelectedText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeadSelectionOffset() {
|
||||
return getSelectionModel().getLeadSelectionOffset();
|
||||
JTextComponent textComponent = getTextComponent();
|
||||
final int caretPosition = textComponent.getCaretPosition();
|
||||
final int start = textComponent.getSelectionStart();
|
||||
final int end = textComponent.getSelectionEnd();
|
||||
return caretPosition == start ? end : start;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public VisualPosition getLeadSelectionPosition() {
|
||||
return myEditor.offsetToVisualPosition(getSelectionModel().getLeadSelectionOffset());
|
||||
return myEditor.offsetToVisualPosition(getLeadSelectionOffset());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSelection() {
|
||||
return getSelectionModel().hasSelection();
|
||||
return getSelectionStart() != getSelectionEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(int startOffset, int endOffset) {
|
||||
getSelectionModel().setSelection(startOffset, endOffset);
|
||||
JTextComponent textComponent = getTextComponent();
|
||||
if (textComponent.getCaretPosition() == startOffset) { // avoid moving caret (required for correct Ctrl-W operation)
|
||||
textComponent.setCaretPosition(endOffset);
|
||||
textComponent.moveCaretPosition(startOffset);
|
||||
}
|
||||
else {
|
||||
textComponent.setCaretPosition(startOffset);
|
||||
textComponent.moveCaretPosition(endOffset);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -158,34 +211,40 @@ public class TextComponentCaret extends UserDataHolderBase implements Caret {
|
||||
|
||||
@Override
|
||||
public void setSelection(int startOffset, @Nullable VisualPosition endPosition, int endOffset) {
|
||||
getSelectionModel().setSelection(startOffset, endPosition, endOffset);
|
||||
setSelection(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(@Nullable VisualPosition startPosition, int startOffset, @Nullable VisualPosition endPosition, int endOffset) {
|
||||
getSelectionModel().setSelection(startPosition, startOffset, endPosition, endOffset);
|
||||
setSelection(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(@Nullable VisualPosition startPosition, int startOffset, @Nullable VisualPosition endPosition, int endOffset,
|
||||
boolean updateSystemSelection) {
|
||||
// updating system selection is not supported currently for TextComponentEditor
|
||||
setSelection(startPosition, startOffset, endPosition, endOffset);
|
||||
setSelection(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSelection() {
|
||||
getSelectionModel().removeSelection();
|
||||
JTextComponent textComponent = getTextComponent();
|
||||
final int position = textComponent.getCaretPosition();
|
||||
textComponent.select(position, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectLineAtCaret() {
|
||||
getSelectionModel().selectLineAtCaret();
|
||||
SelectionModelImpl.doSelectLineAtCaret(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectWordAtCaret(boolean honorCamelWordsSettings) {
|
||||
getSelectionModel().selectWordAtCaret(honorCamelWordsSettings);
|
||||
removeSelection();
|
||||
|
||||
EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(
|
||||
IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET);
|
||||
handler.execute(myEditor, null, DataManager.getInstance().getDataContext(myEditor.getComponent()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -218,7 +277,7 @@ public class TextComponentCaret extends UserDataHolderBase implements Caret {
|
||||
public void setVisualAttributes(@NotNull CaretVisualAttributes attributes) {
|
||||
}
|
||||
|
||||
private SelectionModel getSelectionModel() {
|
||||
return myEditor.getSelectionModel();
|
||||
private JTextComponent getTextComponent() {
|
||||
return myEditor.getContentComponent();
|
||||
}
|
||||
}
|
||||
|
||||
+2
-89
@@ -9,23 +9,18 @@ import com.intellij.util.EventDispatcher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class TextComponentCaretModel implements CaretModel {
|
||||
private final JTextComponent myTextComponent;
|
||||
class TextComponentCaretModel implements CaretModel {
|
||||
private final TextComponentEditor myEditor;
|
||||
private final Caret myCaret;
|
||||
private final EventDispatcher<CaretActionListener> myCaretActionListeners = EventDispatcher.create(CaretActionListener.class);
|
||||
|
||||
public TextComponentCaretModel(@NotNull JTextComponent textComponent, @NotNull TextComponentEditor editor) {
|
||||
myTextComponent = textComponent;
|
||||
TextComponentCaretModel(@NotNull TextComponentEditorImpl editor) {
|
||||
myEditor = editor;
|
||||
myCaret = new TextComponentCaret(editor);
|
||||
}
|
||||
@@ -41,78 +36,6 @@ public class TextComponentCaretModel implements CaretModel {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToLogicalPosition(@NotNull final LogicalPosition pos) {
|
||||
moveToOffset(myEditor.logicalPositionToOffset(pos), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToVisualPosition(@NotNull final VisualPosition pos) {
|
||||
moveToLogicalPosition(myEditor.visualToLogicalPosition(pos));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToOffset(int offset) {
|
||||
moveToOffset(offset, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveToOffset(final int offset, boolean locateBeforeSoftWrap) {
|
||||
int targetOffset = Math.min(offset, myTextComponent.getText().length());
|
||||
int currentPosition = myTextComponent.getCaretPosition();
|
||||
// We try to preserve selection, to match EditorImpl behaviour.
|
||||
// It's only possible though, if target offset is located at either end of existing selection.
|
||||
if (targetOffset != currentPosition) {
|
||||
if (targetOffset == myTextComponent.getCaret().getMark()) {
|
||||
myTextComponent.setCaretPosition(currentPosition);
|
||||
myTextComponent.moveCaretPosition(targetOffset);
|
||||
}
|
||||
else {
|
||||
myTextComponent.setCaretPosition(targetOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUpToDate() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public LogicalPosition getLogicalPosition() {
|
||||
int caretPos = myTextComponent.getCaretPosition();
|
||||
int line;
|
||||
int lineStart;
|
||||
if (myTextComponent instanceof JTextArea) {
|
||||
final JTextArea textArea = (JTextArea)myTextComponent;
|
||||
try {
|
||||
line = textArea.getLineOfOffset(caretPos);
|
||||
lineStart = textArea.getLineStartOffset(line);
|
||||
}
|
||||
catch (BadLocationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
line = 0;
|
||||
lineStart = 0;
|
||||
}
|
||||
return new LogicalPosition(line, caretPos - lineStart);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public VisualPosition getVisualPosition() {
|
||||
LogicalPosition pos = getLogicalPosition();
|
||||
return new VisualPosition(pos.line, pos.column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOffset() {
|
||||
return myTextComponent.getCaretPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCaretListener(@NotNull final CaretListener listener) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
@@ -123,16 +46,6 @@ public class TextComponentCaretModel implements CaretModel {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisualLineStart() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisualLineEnd() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextAttributes getTextAttributes() {
|
||||
return null;
|
||||
|
||||
+2
-2
@@ -27,10 +27,10 @@ import javax.swing.text.JTextComponent;
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class TextComponentDocument extends UserDataHolderBase implements com.intellij.openapi.editor.Document {
|
||||
class TextComponentDocument extends UserDataHolderBase implements com.intellij.openapi.editor.Document {
|
||||
private final JTextComponent myTextComponent;
|
||||
|
||||
public TextComponentDocument(final JTextComponent textComponent) {
|
||||
TextComponentDocument(final JTextComponent textComponent) {
|
||||
myTextComponent = textComponent;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -59,8 +59,8 @@ public class TextComponentEditorImpl extends UserDataHolderBase implements TextC
|
||||
else {
|
||||
myDocument = new TextComponentDocument(textComponent);
|
||||
}
|
||||
myCaretModel = new TextComponentCaretModel(textComponent, this);
|
||||
mySelectionModel = new TextComponentSelectionModel(textComponent, this);
|
||||
myCaretModel = new TextComponentCaretModel(this);
|
||||
mySelectionModel = new TextComponentSelectionModel(this);
|
||||
myScrollingModel = new TextComponentScrollingModel(textComponent);
|
||||
mySoftWrapModel = new TextComponentSoftWrapModel();
|
||||
myFoldingModel = new TextComponentFoldingModel();
|
||||
@@ -85,7 +85,7 @@ public class TextComponentEditorImpl extends UserDataHolderBase implements TextC
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public JComponent getContentComponent() {
|
||||
public JTextComponent getContentComponent() {
|
||||
return myTextComponent;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* @author Denis Zhdanov
|
||||
*/
|
||||
public class TextComponentFoldingModel implements FoldingModel {
|
||||
class TextComponentFoldingModel implements FoldingModel {
|
||||
|
||||
@Override
|
||||
public FoldRegion addFoldRegion(int startOffset, int endOffset, @NotNull String placeholderText) {
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ import java.awt.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class TextComponentInlayModel implements InlayModel {
|
||||
class TextComponentInlayModel implements InlayModel {
|
||||
@Nullable
|
||||
@Override
|
||||
public <T extends EditorCustomElementRenderer> Inlay<T> addInlineElement(int offset,
|
||||
|
||||
+2
-2
@@ -28,10 +28,10 @@ import java.awt.*;
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class TextComponentScrollingModel implements ScrollingModel {
|
||||
class TextComponentScrollingModel implements ScrollingModel {
|
||||
private final JTextComponent myTextComponent;
|
||||
|
||||
public TextComponentScrollingModel(@NotNull JTextComponent textComponent) {
|
||||
TextComponentScrollingModel(@NotNull JTextComponent textComponent) {
|
||||
myTextComponent = textComponent;
|
||||
}
|
||||
|
||||
|
||||
+3
-88
@@ -15,29 +15,21 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.textarea;
|
||||
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.openapi.editor.*;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.openapi.editor.event.SelectionListener;
|
||||
import com.intellij.openapi.editor.impl.SelectionModelImpl;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.JTextComponent;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class TextComponentSelectionModel implements SelectionModel {
|
||||
private final JTextComponent myTextComponent;
|
||||
class TextComponentSelectionModel implements SelectionModel {
|
||||
private final TextComponentEditor myEditor;
|
||||
|
||||
public TextComponentSelectionModel(@NotNull JTextComponent textComponent, @NotNull TextComponentEditor textComponentEditor) {
|
||||
myTextComponent = textComponent;
|
||||
TextComponentSelectionModel(@NotNull TextComponentEditorImpl textComponentEditor) {
|
||||
myEditor = textComponentEditor;
|
||||
}
|
||||
|
||||
@@ -46,87 +38,24 @@ public class TextComponentSelectionModel implements SelectionModel {
|
||||
return myEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelectionStart() {
|
||||
return myTextComponent.getSelectionStart();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getSelectionStartPosition() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelectionEnd() {
|
||||
return myTextComponent.getSelectionEnd();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getSelectionEndPosition() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getSelectedText() {
|
||||
return myTextComponent.getSelectedText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getSelectedText(boolean allCarets) {
|
||||
return getSelectedText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeadSelectionOffset() {
|
||||
final int caretPosition = myTextComponent.getCaretPosition();
|
||||
final int start = myTextComponent.getSelectionStart();
|
||||
final int end = myTextComponent.getSelectionEnd();
|
||||
return caretPosition == start ? end : start;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public VisualPosition getLeadSelectionPosition() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSelection(boolean anyCaret) {
|
||||
return myTextComponent.getSelectionStart() != myTextComponent.getSelectionEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(final int startOffset, final int endOffset) {
|
||||
if (myTextComponent.getCaretPosition() == startOffset) { // avoid moving caret (required for correct Ctrl-W operation)
|
||||
myTextComponent.setCaretPosition(endOffset);
|
||||
myTextComponent.moveCaretPosition(startOffset);
|
||||
}
|
||||
else {
|
||||
myTextComponent.setCaretPosition(startOffset);
|
||||
myTextComponent.moveCaretPosition(endOffset);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(int startOffset, @Nullable VisualPosition endPosition, int endOffset) {
|
||||
setSelection(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(@Nullable VisualPosition startPosition, int startOffset, @Nullable VisualPosition endPosition, int endOffset) {
|
||||
setSelection(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSelection(boolean allCarets) {
|
||||
final int position = myTextComponent.getCaretPosition();
|
||||
myTextComponent.select(position, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSelectionListener(@NotNull final SelectionListener listener) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
@@ -137,23 +66,9 @@ public class TextComponentSelectionModel implements SelectionModel {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectLineAtCaret() {
|
||||
SelectionModelImpl.doSelectLineAtCaret(myEditor.getCaretModel().getPrimaryCaret());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectWordAtCaret(final boolean honorCamelWordsSettings) {
|
||||
removeSelection();
|
||||
|
||||
EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(
|
||||
IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET);
|
||||
handler.execute(myEditor, null, DataManager.getInstance().getDataContext(myEditor.getComponent()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copySelectionToClipboard() {
|
||||
if (! (myTextComponent instanceof JPasswordField)) {
|
||||
if (! (myEditor.getContentComponent() instanceof JPasswordField)) {
|
||||
EditorCopyPasteHelper.getInstance().copySelectionToClipboard(myEditor);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ import java.util.List;
|
||||
/**
|
||||
* @author Denis Zhdanov
|
||||
*/
|
||||
public class TextComponentSoftWrapModel implements SoftWrapModel {
|
||||
class TextComponentSoftWrapModel implements SoftWrapModel {
|
||||
|
||||
@Override
|
||||
public boolean isSoftWrappingEnabled() {
|
||||
|
||||
Reference in New Issue
Block a user