Skip to content

Commit

Permalink
Get caret state from the editor using reflection. Fixes #211
Browse files Browse the repository at this point in the history
  • Loading branch information
nosami committed Apr 18, 2018
1 parent 3b3dc27 commit e0c723c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
9 changes: 7 additions & 2 deletions XSVim/Addin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ type XSVim() =

initConfig()
if not (Vim.editorStates.ContainsKey x.FileName) then
Vim.editorStates.Add(x.FileName, VimState.Default)
let editor = x.Editor
let state =
match Vim.getCaretMode editor with
| Insert -> { VimState.Default with mode = InsertMode }
| Block -> VimState.Default

let state = Vim.switchToNormalMode editor state
Vim.editorStates.Add(x.FileName, state)
editor.GrabFocus()
EditActions.SwitchCaretMode editor
let caretChanged =
editor.CaretPositionChanged.Subscribe
(fun _e ->
Expand Down
9 changes: 1 addition & 8 deletions XSVim/Properties/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ open System.Runtime.CompilerServices
[<AutoOpen>]
module AddinVersion =
[<Literal>]
let version = "0.53.0"
let version = "0.53.1"

[<assembly: AssemblyTitle("XSVim")>]
[<assembly: AssemblyDescription("")>]
[<assembly: AssemblyConfiguration("")>]
[<assembly: AssemblyCompany("")>]
[<assembly: AssemblyProduct("")>]
[<assembly: AssemblyCopyright("${AuthorCopyright}")>]
[<assembly: AssemblyTrademark("")>]

// The assembly version has the format {Major}.{Minor}.{Build}.{Revision}

[<assembly: AssemblyVersion(version)>]
Expand Down
22 changes: 15 additions & 7 deletions XSVim/XSVim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,18 @@ module Vim =
let linewise = isLineWise vimState command
{ linewise=linewise; content=editor.SelectedText }

let setCaretMode editor state caretMode =
match state.mode, caretMode with
| NotInsertMode, Insert -> EditActions.SwitchCaretMode editor
| InsertMode, Block -> EditActions.SwitchCaretMode editor
let getCaretMode (editor:TextEditor) =
let caret = editor.Carets.[0]
let caretMode:bool = caret?IsInInsertMode
match caretMode with
| true -> Insert
| false -> Block

let setCaretMode (editor:TextEditor) caretMode =
let currentMode = getCaretMode editor
match currentMode, caretMode with
| Block, Insert -> EditActions.SwitchCaretMode editor
| Insert, Block -> EditActions.SwitchCaretMode editor
| _ -> ()

let setAutoCompleteOnKeystroke value =
Expand All @@ -729,7 +737,7 @@ module Vim =
state.undoGroup

setAutoCompleteOnKeystroke true
setCaretMode editor state Insert
setCaretMode editor Insert
{ state with mode = InsertMode; statusMessage = "-- INSERT --" |> Some; keys = []; undoGroup = group }

let switchToNormalMode (editor:TextEditor) vimState =
Expand All @@ -740,7 +748,7 @@ module Vim =
| _ -> vimState.lastSelection
editor.ClearSelection()
setAutoCompleteOnKeystroke false
setCaretMode editor vimState Block
setCaretMode editor Block
// stupid hack to prevent intellisense in normal mode
// https://github.com/mono/monodevelop/blob/fdbfbe89529bd9076e1906e7b70fdb51a9ae6b99/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Extension/CompletionTextEditorExtension.cs#L153
if editor.SelectionMode = SelectionMode.Normal then EditActions.ToggleBlockSelectionMode editor
Expand Down Expand Up @@ -1073,7 +1081,7 @@ module Vim =
else
state
| VisualMode | VisualLineMode | VisualBlockMode ->
setCaretMode editor vimState Block
setCaretMode editor Block
let start, finish = VimHelpers.getRange vimState editor command
let statusMessage =
match mode with
Expand Down

0 comments on commit e0c723c

Please sign in to comment.