Skip to content

Commit

Permalink
Allow tab key to complete snippets when in insert mode
Browse files Browse the repository at this point in the history
Fixes #2786
  • Loading branch information
nosami committed Mar 14, 2020
1 parent ab14d85 commit bd4165f
Showing 1 changed file with 54 additions and 55 deletions.
109 changes: 54 additions & 55 deletions Src/VimMac/VimKeyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ internal sealed class VimKeyProcessor : KeyProcessor
private readonly ISignatureHelpBroker _signatureHelpBroker;
private readonly InlineRenameListenerFactory _inlineRenameListenerFactory;

public ITextBuffer TextBuffer
{
get { return VimBuffer.TextBuffer; }
}

public ITextView TextView
{
get { return VimBuffer.TextView; }
}

public bool ModeChanged { get; private set; }

public VimKeyProcessor(
IVimBuffer vimBuffer,
IKeyUtil keyUtil,
Expand All @@ -55,25 +43,11 @@ public ITextView TextView
_inlineRenameListenerFactory = inlineRenameListenerFactory;
}

/// <summary>
/// Try and process the given KeyInput with the IVimBuffer. This is overridable by
/// derived classes in order for them to prevent any KeyInput from reaching the
/// IVimBuffer
/// </summary>
private bool TryProcess(KeyInput keyInput)
{
return VimBuffer.CanProcessAsCommand(keyInput) && VimBuffer.Process(keyInput).IsAnyHandled;
}
public ITextBuffer TextBuffer => VimBuffer.TextBuffer;

private bool KeyEventIsDeadChar(KeyEventArgs e)
{
return string.IsNullOrEmpty(e.Characters);
}
public ITextView TextView => VimBuffer.TextView;

private bool IsEscapeKey(KeyEventArgs e)
{
return (NSKey)e.Event.KeyCode == NSKey.Escape;
}
public bool ModeChanged { get; private set; }

/// <summary>
/// This handler is necessary to intercept keyboard input which maps to Vim
Expand All @@ -92,31 +66,8 @@ public override void KeyDown(KeyEventArgs e)
{
VimTrace.TraceInfo("VimKeyProcessor::KeyDown {0} {1}", e.Characters, e.CharactersIgnoringModifiers);

bool handled;
if (KeyEventIsDeadChar(e))
{
// When a dead key combination is pressed we will get the key down events in
// sequence after the combination is complete. The dead keys will come first
// and be followed the final key which produces the char. That final key
// is marked as DeadCharProcessed.
//
// All of these should be ignored. They will produce a TextInput value which
// we can process in the TextInput event
handled = false;
}
else if (_completionBroker.IsCompletionActive(TextView) && !IsEscapeKey(e))
{
handled = false;
}
else if (_signatureHelpBroker.IsSignatureHelpActive(TextView))
{
handled = false;
}
else if (_inlineRenameListenerFactory.InRename)
{
handled = false;
}
else
bool handled = false;
if (ShouldBeProcessedByVim(e))
{
var oldMode = VimBuffer.Mode.ModeKind;

Expand All @@ -143,13 +94,61 @@ public override void KeyDown(KeyEventArgs e)

var status = Mac.StatusBar.GetStatus(VimBuffer);
var text = status.Text;
if(VimBuffer.ModeKind == ModeKind.Command)
if (VimBuffer.ModeKind == ModeKind.Command)
{
// Add a fake 'caret'
text = text.Insert(status.CaretPosition, "|");
}
IdeApp.Workbench.StatusBar.ShowMessage(text);
e.Handled = handled;
}

/// <summary>
/// Try and process the given KeyInput with the IVimBuffer. This is overridable by
/// derived classes in order for them to prevent any KeyInput from reaching the
/// IVimBuffer
/// </summary>
private bool TryProcess(KeyInput keyInput)
{
return VimBuffer.CanProcessAsCommand(keyInput) && VimBuffer.Process(keyInput).IsAnyHandled;
}

private bool KeyEventIsDeadChar(KeyEventArgs e)
{
return string.IsNullOrEmpty(e.Characters);
}

private bool IsEscapeKey(KeyEventArgs e)
{
return (NSKey)e.Event.KeyCode == NSKey.Escape;
}

private bool ShouldBeProcessedByVim(KeyEventArgs e)
{
if (KeyEventIsDeadChar(e))
// When a dead key combination is pressed we will get the key down events in
// sequence after the combination is complete. The dead keys will come first
// and be followed the final key which produces the char. That final key
// is marked as DeadCharProcessed.
//
// All of these should be ignored. They will produce a TextInput value which
// we can process in the TextInput event
return false;

if (_completionBroker.IsCompletionActive(TextView) && !IsEscapeKey(e))
return false;

if (_signatureHelpBroker.IsSignatureHelpActive(TextView))
return false;

if (_inlineRenameListenerFactory.InRename)
return false;

if (VimBuffer.Mode.ModeKind == ModeKind.Insert && e.Characters == "\t")
// Allow tab key to work for snippet completion
return false;

return true;
}
}
}

0 comments on commit bd4165f

Please sign in to comment.