Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guicursor! #337

Merged
merged 36 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
646a89d
Refactor showMode update
citizenmatt Jun 4, 2021
2be0f5c
Reset caret visual position after mode changes
citizenmatt Jun 4, 2021
b50281f
Extract caret shape methods to helper
citizenmatt Jun 4, 2021
6c4bd9c
Refactor caret shape functions
citizenmatt Jun 4, 2021
e859b1c
Remove resetCaret
citizenmatt Jun 4, 2021
9c71b44
Update caret visual attributes when mode changes
citizenmatt Jun 5, 2021
55dedb4
Remove resetShape method
citizenmatt Jun 10, 2021
dfbec1f
Rename "bound" options to "bounded"
citizenmatt Jun 10, 2021
287ba70
Simplify BoundedListOption
citizenmatt Jun 10, 2021
e93a619
Make ListOption generic + create StringListOption
citizenmatt Jun 10, 2021
9f46e19
Fix typo
citizenmatt Jun 10, 2021
43620c2
Add guicursor option
citizenmatt Jun 10, 2021
9cf0a1a
Report errors while parsing guicursor option
citizenmatt Jun 11, 2021
8a55199
Add guicursor to dictionary
citizenmatt Jun 11, 2021
ccd792b
Provide hook for resetting cached values
citizenmatt Jun 28, 2021
d19c776
Minor refactor
citizenmatt Jun 28, 2021
ad19dc0
Use guicursor options to draw caret
citizenmatt Jun 28, 2021
b4d40fa
Rename assert method
citizenmatt Jun 28, 2021
a6087dd
Use replace mode caret for change character action
citizenmatt Jun 28, 2021
2f73dac
Force the caret visible when updating attributes
citizenmatt Jun 28, 2021
f051231
Use guicursor instead of editor cursor settings
citizenmatt Jun 28, 2021
64be751
Use guicursor options to draw ex caret
citizenmatt Jun 29, 2021
1caf380
Improve caret painting in command line
citizenmatt Jun 29, 2021
fe7dc49
Make ExShortcutKeyAction dumb aware
citizenmatt Jun 30, 2021
0288a0f
Use insert caret for select mode
citizenmatt Jun 30, 2021
0d840b2
Merge branch 'master' into feature/guicursor
citizenmatt Jun 30, 2021
d6a99d4
Update to latest EAP
citizenmatt Jun 30, 2021
91585e1
Run linters
citizenmatt Jun 30, 2021
d63e3da
Merge branch 'master' into feature/guicursor
citizenmatt Jul 28, 2021
70a45f3
Treat use block caret option as caret override
citizenmatt Jul 29, 2021
6ab5380
Add tests for caret attributes in nested modes
citizenmatt Aug 10, 2021
ae21727
Merge branch 'master' into feature/guicursor
citizenmatt Sep 27, 2021
74b0e3b
Merge branch 'master' into feature/guicursor
AlexPl292 Sep 29, 2021
c268407
Merge branch 'master' into feature/guicursor
AlexPl292 Sep 29, 2021
3fae2fd
isBlockCursor method doesn't work for UI tests at the moment
AlexPl292 Sep 29, 2021
ee74367
Merge branch 'master' into feature/guicursor
AlexPl292 Oct 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/com/maddyhome/idea/vim/KeyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
import com.maddyhome.idea.vim.action.change.insert.InsertCompletedLiteralAction;
import com.maddyhome.idea.vim.action.macro.ToggleRecordingAction;
import com.maddyhome.idea.vim.command.*;
import com.maddyhome.idea.vim.group.ChangeGroup;
import com.maddyhome.idea.vim.group.RegisterGroup;
import com.maddyhome.idea.vim.group.visual.VisualGroupKt;
import com.maddyhome.idea.vim.handler.ActionBeanClass;
import com.maddyhome.idea.vim.handler.EditorActionHandlerBase;
import com.maddyhome.idea.vim.helper.*;
Expand All @@ -67,6 +65,7 @@

import static com.intellij.openapi.actionSystem.CommonDataKeys.*;
import static com.intellij.openapi.actionSystem.PlatformDataKeys.PROJECT_FILE_DIRECTORY;
import static com.maddyhome.idea.vim.helper.CaretVisualAttributesHelperKt.*;

/**
* This handles every keystroke that the user can argType except those that are still valid hotkeys for various Idea
Expand Down Expand Up @@ -357,7 +356,7 @@ private void handleEditorReset(@NotNull Editor editor, @NotNull KeyStroke key, f
}
}
reset(editor);
ChangeGroup.resetCaret(editor, false);
resetShape(CommandState.Mode.COMMAND, editor);
}

private boolean handleKeyMapping(final @NotNull Editor editor,
Expand Down Expand Up @@ -883,7 +882,7 @@ public void fullReset(@NotNull Editor editor) {
if (registerGroup != null) {
registerGroup.resetRegister();
}
VisualGroupKt.updateCaretState(editor);
updateCaretState(editor);
editor.getSelectionModel().removeSelection();
}

Expand Down Expand Up @@ -978,7 +977,7 @@ public void run() {
if (editorState.getSubMode() == CommandState.SubMode.SINGLE_COMMAND &&
(!cmd.getFlags().contains(CommandFlags.FLAG_EXPECT_MORE))) {
editorState.popModes();
VisualGroupKt.resetShape(CommandStateHelper.getMode(editor), editor);
resetShape(CommandStateHelper.getMode(editor), editor);
}

if (editorState.getCommandBuilder().isDone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.group.visual.updateCaretState
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.moveToInlayAwareOffset
import com.maddyhome.idea.vim.helper.updateCaretState

/**
* @author Alex Plate
Expand Down
38 changes: 25 additions & 13 deletions src/com/maddyhome/idea/vim/command/CommandState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.maddyhome.idea.vim.helper.DigraphSequence
import com.maddyhome.idea.vim.helper.MessageHelper
import com.maddyhome.idea.vim.helper.VimNlsSafe
import com.maddyhome.idea.vim.helper.noneOfEnum
import com.maddyhome.idea.vim.helper.updateCaretsVisualPosition
import com.maddyhome.idea.vim.helper.vimCommandState
import com.maddyhome.idea.vim.key.CommandPartNode
import com.maddyhome.idea.vim.option.OptionsManager.showmode
Expand All @@ -35,17 +36,17 @@ import java.util.*
import javax.swing.KeyStroke

/**
* Used to maintain state while entering a Vim command (operator, motion, text object, etc.)
* Used to maintain state before and while entering a Vim command (operator, motion, text object, etc.)
*/
class CommandState private constructor() {
class CommandState private constructor(private val editor: Editor) {
val commandBuilder = CommandBuilder(getKeyRootNode(MappingMode.NORMAL))
private val modeStates = Stack<ModeState>()
val mappingState = MappingState()
private val digraphSequence = DigraphSequence()
var isRecording = false
set(value) {
field = value
updateStatus()
doShowMode()
}
var isDotRepeatInProgress = false

Expand Down Expand Up @@ -78,17 +79,26 @@ class CommandState private constructor() {

fun pushModes(mode: Mode, submode: SubMode) {
val newModeState = ModeState(mode, submode)

logger.debug("Push new mode state: ${newModeState.toSimpleString()}")
logger.debug { "Stack of mode states before push: ${toSimpleString()}" }

val previousMode = currentModeState()
modeStates.push(newModeState)
setMappingMode()
updateStatus()

if (previousMode.mode != newModeState.mode) {
onModeChanged()
}
}

fun popModes() {
val popped = modeStates.pop()
setMappingMode()
updateStatus()
if (popped.mode != mode) {
onModeChanged()
}

logger.debug("Popped mode state: ${popped.toSimpleString()}")
logger.debug { "Stack of mode states after pop: ${toSimpleString()}" }
}
Expand All @@ -107,13 +117,17 @@ class CommandState private constructor() {

private fun resetModes() {
modeStates.clear()
onModeChanged()
setMappingMode()
}

private fun onModeChanged() {
AlexPl292 marked this conversation as resolved.
Show resolved Hide resolved
editor.updateCaretsVisualPosition()
doShowMode()
}

private fun setMappingMode() {
val modeState = currentModeState()
val newMappingMode = if (modeState.mode == Mode.OP_PENDING) MappingMode.OP_PENDING else modeToMappingMode(mode)
mappingState.mappingMode = newMappingMode
mappingState.mappingMode = modeToMappingMode(mode)
}

@Contract(pure = true)
Expand All @@ -124,7 +138,7 @@ class CommandState private constructor() {
Mode.VISUAL -> MappingMode.VISUAL
Mode.SELECT -> MappingMode.SELECT
Mode.CMD_LINE -> MappingMode.CMD_LINE
else -> error("Unexpected mode: $mode")
Mode.OP_PENDING -> MappingMode.OP_PENDING
}
}

Expand All @@ -137,7 +151,6 @@ class CommandState private constructor() {
val modeState = currentModeState()
popModes()
pushModes(modeState.mode, submode)
updateStatus()
}

fun startDigraphSequence() {
Expand Down Expand Up @@ -183,7 +196,6 @@ class CommandState private constructor() {
resetModes()
commandBuilder.resetInProgressCommandPart(getKeyRootNode(mappingState.mappingMode))
digraphSequence.reset()
updateStatus()
}

fun toSimpleString(): String = modeStates.joinToString { it.toSimpleString() }
Expand Down Expand Up @@ -262,7 +274,7 @@ class CommandState private constructor() {
return if (modeStates.size > 0) modeStates.peek() else defaultModeState
}

private fun updateStatus() {
private fun doShowMode() {
val msg = StringBuilder()
if (showmode.isSet) {
msg.append(getStatusString(modeStates.size - 1))
Expand Down Expand Up @@ -334,7 +346,7 @@ class CommandState private constructor() {
fun getInstance(editor: Editor): CommandState {
var res = editor.vimCommandState
if (res == null) {
res = CommandState()
res = CommandState(editor)
editor.vimCommandState = res
}
return res
Expand Down
10 changes: 4 additions & 6 deletions src/com/maddyhome/idea/vim/group/ChangeGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
import java.math.BigInteger;
import java.util.*;

import static com.maddyhome.idea.vim.helper.CaretVisualAttributesHelperKt.updateCaretState;

/**
* Provides all the insert/replace related functionality
*/
Expand Down Expand Up @@ -447,7 +449,7 @@ private void initInsert(@NotNull Editor editor, @NotNull DataContext context, @N
setInsertEditorState(editor, mode == CommandState.Mode.INSERT);
state.pushModes(mode, CommandState.SubMode.NONE);

VisualGroupKt.updateCaretState(editor);
updateCaretState(editor);
}

notifyListeners(editor);
Expand Down Expand Up @@ -551,7 +553,7 @@ public void processEscape(@NotNull Editor editor, @Nullable DataContext context)
CommandState.getInstance(editor).popModes();
exitAllSingleCommandInsertModes(editor);

VisualGroupKt.updateCaretState(editor);
updateCaretState(editor);
}

/**
Expand Down Expand Up @@ -1829,10 +1831,6 @@ private boolean deleteText(final @NotNull Editor editor,
return false;
}

public static void resetCaret(@NotNull Editor editor, boolean insert) {
editor.getSettings().setBlockCursor(!insert);
}

/**
* Sort range of text with a given comparator
*
Expand Down
10 changes: 7 additions & 3 deletions src/com/maddyhome/idea/vim/group/EditorGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorGutter;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.LineNumberConverter;
import com.intellij.openapi.editor.event.CaretEvent;
import com.intellij.openapi.editor.event.CaretListener;
import com.intellij.openapi.editor.ex.EditorGutterComponentEx;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.project.Project;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.group.visual.VisualGroupKt;
import com.maddyhome.idea.vim.helper.*;
import com.maddyhome.idea.vim.option.OptionChangeListener;
import com.maddyhome.idea.vim.option.OptionsManager;
Expand All @@ -41,6 +43,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.maddyhome.idea.vim.helper.CaretVisualAttributesHelperKt.resetShape;

/**
* @author vlan
*/
Expand Down Expand Up @@ -224,7 +228,7 @@ public void editorCreated(@NotNull Editor editor) {
VimPlugin.getChange().insertBeforeCursor(editor, EditorDataContext.init(editor, null));
KeyHandler.getInstance().reset(editor);
}
VisualGroupKt.resetShape(CommandStateHelper.getMode(editor), editor);
resetShape(CommandStateHelper.getMode(editor), editor);
editor.getSettings().setRefrainFromScrolling(REFRAIN_FROM_SCROLLING_VIM_VALUE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.maddyhome.idea.vim.helper.isIdeaVimDisabledHere
import com.maddyhome.idea.vim.helper.isTemplateActive
import com.maddyhome.idea.vim.helper.mode
import com.maddyhome.idea.vim.helper.popAllModes
import com.maddyhome.idea.vim.helper.updateCaretState
import com.maddyhome.idea.vim.listener.VimListenerManager
import com.maddyhome.idea.vim.option.IdeaRefactorMode
import com.maddyhome.idea.vim.option.OptionsManager
Expand Down
42 changes: 2 additions & 40 deletions src/com/maddyhome/idea/vim/group/visual/VisualGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
package com.maddyhome.idea.vim.group.visual

import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.CaretVisualAttributes
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.VisualPosition
import com.intellij.openapi.editor.colors.EditorColors
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.group.ChangeGroup
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.fileSize
Expand All @@ -36,8 +33,10 @@ import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.isEndAllowed
import com.maddyhome.idea.vim.helper.mode
import com.maddyhome.idea.vim.helper.moveToInlayAwareOffset
import com.maddyhome.idea.vim.helper.resetShape
import com.maddyhome.idea.vim.helper.sort
import com.maddyhome.idea.vim.helper.subMode
import com.maddyhome.idea.vim.helper.updateCaretState
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.helper.vimSelectionStart

Expand Down Expand Up @@ -136,43 +135,6 @@ val Caret.vimLeadSelectionOffset: Int
return caretOffset
}

/**
* Update caret's colour according to the current state
*
* Secondary carets became invisible colour in visual block mode
*/
fun updateCaretState(editor: Editor) {
// Update colour
if (editor.inBlockSubMode) {
editor.caretModel.allCarets.forEach {
if (it != editor.caretModel.primaryCaret) {
// Set background color for non-primary carets as selection background color
// to make them invisible
val color = editor.colorsScheme.getColor(EditorColors.SELECTION_BACKGROUND_COLOR)
val visualAttributes = it.visualAttributes
it.visualAttributes = CaretVisualAttributes(color, visualAttributes.weight)
}
}
} else {
editor.caretModel.allCarets.forEach { it.visualAttributes = CaretVisualAttributes.DEFAULT }
}

// Update shape
editor.mode.resetShape(editor)
}

fun CommandState.Mode.resetShape(editor: Editor) = when (this) {
CommandState.Mode.COMMAND, CommandState.Mode.VISUAL, CommandState.Mode.REPLACE -> ChangeGroup.resetCaret(
editor,
false
)
CommandState.Mode.SELECT, CommandState.Mode.INSERT -> ChangeGroup.resetCaret(
editor,
VimPlugin.getEditor().isBarCursorSettings
)
CommandState.Mode.CMD_LINE, CommandState.Mode.OP_PENDING -> Unit
}

fun charToNativeSelection(editor: Editor, start: Int, end: Int, mode: CommandState.Mode): Pair<Int, Int> {
val (nativeStart, nativeEnd) = sort(start, end)
val lineEnd = EditorHelper.getLineEndForOffset(editor, nativeEnd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.subMode
import com.maddyhome.idea.vim.helper.updateCaretState
import com.maddyhome.idea.vim.helper.vimForEachCaret
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.helper.vimLastSelectionType
Expand Down
Loading