Skip to content

Commit

Permalink
gitextensions#5348 Hotkey F4 to open file in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
mstv committed Sep 6, 2018
1 parent 58f3b6c commit 56f720d
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 112 deletions.
114 changes: 66 additions & 48 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,15 @@ private void InternalInitialize(bool hard)
void SetShortcutKeyDisplayStringsFromHotkeySettings()
{
// Add shortcuts to the menu items
gitBashToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.GitBash).ToShortcutKeyDisplayString();
commitToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.Commit).ToShortcutKeyDisplayString();
stashChangesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.Stash).ToShortcutKeyDisplayString();
stashPopToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.StashPop).ToShortcutKeyDisplayString();
closeToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.CloseRepository).ToShortcutKeyDisplayString();
gitGUIToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.GitGui).ToShortcutKeyDisplayString();
kGitToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.GitGitK).ToShortcutKeyDisplayString();
checkoutBranchToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.CheckoutBranch).ToShortcutKeyDisplayString();
settingsToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Commands.OpenSettings).ToShortcutKeyDisplayString();
gitBashToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.GitBash).ToShortcutKeyDisplayString();
commitToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.Commit).ToShortcutKeyDisplayString();
stashChangesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.Stash).ToShortcutKeyDisplayString();
stashPopToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.StashPop).ToShortcutKeyDisplayString();
closeToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.CloseRepository).ToShortcutKeyDisplayString();
gitGUIToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.GitGui).ToShortcutKeyDisplayString();
kGitToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.GitGitK).ToShortcutKeyDisplayString();
checkoutBranchToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.CheckoutBranch).ToShortcutKeyDisplayString();
settingsToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys(Command.OpenSettings).ToShortcutKeyDisplayString();

// TODO: add more
}
Expand Down Expand Up @@ -2028,7 +2028,7 @@ private void _createPullRequestToolStripMenuItem_Click(object sender, EventArgs

public static readonly string HotkeySettingsName = "Browse";

internal enum Commands
internal enum Command
{
GitBash = 0,
GitGui = 1,
Expand All @@ -2052,10 +2052,13 @@ internal enum Commands
FocusFilter = 18,
OpenWithDifftool = 19,
OpenSettings = 20,
ToggleBranchTreePanel = 21
ToggleBranchTreePanel = 21,
EditFile = 22,
OpenAsTempFile = 23,
OpenAsTempFileWith = 24,
}

internal Keys GetShortcutKeys(Commands cmd)
internal Keys GetShortcutKeys(Command cmd)
{
return GetShortcutKeys((int)cmd);
}
Expand Down Expand Up @@ -2088,50 +2091,65 @@ private void QuickFetch()
UICommands.RepoChangedNotifier.Notify();
}

private void OpenWithDifftool()
{
if (revisionDiff.Visible)
{
revisionDiff.ExecuteCommand(RevisionDiffControl.Command.OpenWithDifftool);
}
else if (fileTree.Visible)
{
fileTree.ExecuteCommand(RevisionFileTreeControl.Command.OpenWithDifftool);
}
}

protected override bool ExecuteCommand(int cmd)
{
switch ((Commands)cmd)
{
case Commands.GitBash: Module.RunBash(); break;
case Commands.GitGui: Module.RunGui(); break;
case Commands.GitGitK: Module.RunGitK(); break;
case Commands.FocusRevisionGrid: RevisionGrid.Focus(); break;
case Commands.FocusCommitInfo: CommitInfoTabControl.SelectedTab = CommitInfoTabPage; break;
case Commands.FocusFileTree: CommitInfoTabControl.SelectedTab = TreeTabPage; fileTree.Focus(); break;
case Commands.FocusDiff: CommitInfoTabControl.SelectedTab = DiffTabPage; revisionDiff.Focus(); break;
case Commands.FocusFilter: FocusFilter(); break;
case Commands.Commit: CommitToolStripMenuItemClick(null, null); break;
case Commands.AddNotes: AddNotes(); break;
case Commands.FindFileInSelectedCommit: FindFileInSelectedCommit(); break;
case Commands.CheckoutBranch: CheckoutBranchToolStripMenuItemClick(null, null); break;
case Commands.QuickFetch: QuickFetch(); break;
case Commands.QuickPull: UICommands.StartPullDialogAndPullImmediately(this); break;
case Commands.QuickPush: UICommands.StartPushDialog(this, true); break;
case Commands.CloseRepository: CloseToolStripMenuItemClick(null, null); break;
case Commands.Stash: UICommands.StashSave(this, AppSettings.IncludeUntrackedFilesInManualStash); break;
case Commands.StashPop: UICommands.StashPop(this); break;
case Commands.OpenWithDifftool: OpenWithDifftool(); break;
case Commands.OpenSettings: OnShowSettingsClick(null, null); break;
case Commands.ToggleBranchTreePanel: toggleBranchTreePanel_Click(null, null); break;
switch ((Command)cmd)
{
case Command.GitBash: Module.RunBash(); break;
case Command.GitGui: Module.RunGui(); break;
case Command.GitGitK: Module.RunGitK(); break;
case Command.FocusRevisionGrid: RevisionGrid.Focus(); break;
case Command.FocusCommitInfo: CommitInfoTabControl.SelectedTab = CommitInfoTabPage; break;
case Command.FocusFileTree: CommitInfoTabControl.SelectedTab = TreeTabPage; fileTree.Focus(); break;
case Command.FocusDiff: CommitInfoTabControl.SelectedTab = DiffTabPage; revisionDiff.Focus(); break;
case Command.FocusFilter: FocusFilter(); break;
case Command.Commit: CommitToolStripMenuItemClick(null, null); break;
case Command.AddNotes: AddNotes(); break;
case Command.FindFileInSelectedCommit: FindFileInSelectedCommit(); break;
case Command.CheckoutBranch: CheckoutBranchToolStripMenuItemClick(null, null); break;
case Command.QuickFetch: QuickFetch(); break;
case Command.QuickPull: UICommands.StartPullDialogAndPullImmediately(this); break;
case Command.QuickPush: UICommands.StartPushDialog(this, true); break;
case Command.CloseRepository: CloseToolStripMenuItemClick(null, null); break;
case Command.Stash: UICommands.StashSave(this, AppSettings.IncludeUntrackedFilesInManualStash); break;
case Command.StashPop: UICommands.StashPop(this); break;
case Command.OpenWithDifftool: OpenWithDifftool(); break;
case Command.OpenSettings: OnShowSettingsClick(null, null); break;
case Command.ToggleBranchTreePanel: toggleBranchTreePanel_Click(null, null); break;
case Command.EditFile: EditFile(); break;
case Command.OpenAsTempFile when fileTree.Visible: fileTree.ExecuteCommand(RevisionFileTreeControl.Command.OpenAsTempFile); break;
case Command.OpenAsTempFileWith when fileTree.Visible: fileTree.ExecuteCommand(RevisionFileTreeControl.Command.OpenAsTempFileWith); break;
default: return base.ExecuteCommand(cmd);
}

return true;

void OpenWithDifftool()
{
if (revisionDiff.Visible)
{
revisionDiff.ExecuteCommand(RevisionDiffControl.Command.OpenWithDifftool);
}
else if (fileTree.Visible)
{
fileTree.ExecuteCommand(RevisionFileTreeControl.Command.OpenWithDifftool);
}
}

void EditFile()
{
if (revisionDiff.Visible)
{
revisionDiff.ExecuteCommand(RevisionDiffControl.Command.EditFile);
}
else if (fileTree.Visible)
{
fileTree.ExecuteCommand(RevisionFileTreeControl.Command.EditFile);
}
}
}

internal bool ExecuteCommand(Commands cmd)
internal bool ExecuteCommand(Command cmd)
{
return ExecuteCommand((int)cmd);
}
Expand Down
72 changes: 48 additions & 24 deletions GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,24 @@ public FormCommit([NotNull] GitUICommands commands, CommitKind commitKind = Comm
HotkeysEnabled = true;
Hotkeys = HotkeySettingsManager.LoadHotkeys(HotkeySettingsName);

openToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.OpenFile);
openWithToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.OpenFileWith);
editFileToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.EditFile);
stagedOpenToolStripMenuItem7.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.OpenFile);
stagedOpenWithToolStripMenuItem8.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.OpenFileWith);
stagedEditFileToolStripMenuItem11.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.EditFile);

SelectedDiff.AddContextMenuSeparator();
_stageSelectedLinesToolStripMenuItem = SelectedDiff.AddContextMenuEntry(_stageSelectedLines.Text, StageSelectedLinesToolStripMenuItemClick);
_stageSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.StageSelectedFile).ToShortcutKeyDisplayString();
_stageSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.StageSelectedFile);
_resetSelectedLinesToolStripMenuItem = SelectedDiff.AddContextMenuEntry(_resetSelectedLines.Text, ResetSelectedLinesToolStripMenuItemClick);
_resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ResetSelectedFiles).ToShortcutKeyDisplayString();
_resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.ResetSelectedFiles);
_resetSelectedLinesToolStripMenuItem.Image = Reset.Image;
resetChanges.ShortcutKeyDisplayString = _resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString;
stagedResetChanges.ShortcutKeyDisplayString = _resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString;
deleteFileToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.DeleteSelectedFiles).ToShortcutKeyDisplayString();
viewFileHistoryToolStripItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ShowHistory).ToShortcutKeyDisplayString();
stagedFileHistoryToolStripMenuItem6.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ShowHistory).ToShortcutKeyDisplayString();
deleteFileToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.DeleteSelectedFiles);
viewFileHistoryToolStripItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.ShowHistory);
stagedFileHistoryToolStripMenuItem6.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.ShowHistory);
commitAuthorStatus.ToolTipText = _commitCommitterToolTip.Text;
skipWorktreeToolStripMenuItem.ToolTipText = _skipWorktreeToolTip.Text;
assumeUnchangedToolStripMenuItem.ToolTipText = _assumeUnchangedToolTip.Text;
Expand Down Expand Up @@ -516,7 +523,7 @@ private void SelectedDiff_ContextMenuOpening(object sender, System.ComponentMode

public static readonly string HotkeySettingsName = "Commit";

internal enum Commands
internal enum Command
{
AddToGitIgnore = 0,
DeleteSelectedFiles = 1,
Expand All @@ -530,7 +537,15 @@ internal enum Commands
ShowHistory = 9,
ToggleSelectionFilter = 10,
StageAll = 11,
OpenWithDifftool = 12
OpenWithDifftool = 12,
OpenFile = 13,
OpenFileWith = 14,
EditFile = 15
}

private string GetShortcutKeyDisplayString(Command cmd)
{
return GetShortcutKeys((int)cmd).ToShortcutKeyDisplayString();
}

private bool AddToGitIgnore()
Expand Down Expand Up @@ -676,21 +691,24 @@ private bool ToggleSelectionFilter()

protected override bool ExecuteCommand(int cmd)
{
switch ((Commands)cmd)
{
case Commands.AddToGitIgnore: return AddToGitIgnore();
case Commands.DeleteSelectedFiles: return DeleteSelectedFiles();
case Commands.FocusStagedFiles: return FocusStagedFiles();
case Commands.FocusUnstagedFiles: return FocusUnstagedFiles();
case Commands.FocusSelectedDiff: return FocusSelectedDiff();
case Commands.FocusCommitMessage: return FocusCommitMessage();
case Commands.ResetSelectedFiles: return ResetSelectedFiles();
case Commands.StageSelectedFile: return StageSelectedFile();
case Commands.UnStageSelectedFile: return UnStageSelectedFile();
case Commands.ShowHistory: return StartFileHistoryDialog();
case Commands.ToggleSelectionFilter: return ToggleSelectionFilter();
case Commands.StageAll: return StageAllFiles();
case Commands.OpenWithDifftool: SelectedDiff.OpenWithDifftool?.Invoke(); return true;
switch ((Command)cmd)
{
case Command.AddToGitIgnore: return AddToGitIgnore();
case Command.DeleteSelectedFiles: return DeleteSelectedFiles();
case Command.FocusStagedFiles: return FocusStagedFiles();
case Command.FocusUnstagedFiles: return FocusUnstagedFiles();
case Command.FocusSelectedDiff: return FocusSelectedDiff();
case Command.FocusCommitMessage: return FocusCommitMessage();
case Command.ResetSelectedFiles: return ResetSelectedFiles();
case Command.StageSelectedFile: return StageSelectedFile();
case Command.UnStageSelectedFile: return UnStageSelectedFile();
case Command.ShowHistory: return StartFileHistoryDialog();
case Command.ToggleSelectionFilter: return ToggleSelectionFilter();
case Command.StageAll: return StageAllFiles();
case Command.OpenWithDifftool: SelectedDiff.OpenWithDifftool?.Invoke(); return true;
case Command.OpenFile: openToolStripMenuItem.PerformClick(); return true;
case Command.OpenFileWith: openWithToolStripMenuItem.PerformClick(); return true;
case Command.EditFile: editFileToolStripMenuItem.PerformClick(); return true;
default: return base.ExecuteCommand(cmd);
}
}
Expand Down Expand Up @@ -1105,7 +1123,7 @@ private void ShowChanges(GitItemStatus item, bool staged)
_stageSelectedLinesToolStripMenuItem.Text = staged ? _unstageSelectedLines.Text : _stageSelectedLines.Text;
_stageSelectedLinesToolStripMenuItem.Image = staged ? toolUnstageItem.Image : toolStageItem.Image;
_stageSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString =
GetShortcutKeys((int)(staged ? Commands.UnStageSelectedFile : Commands.StageSelectedFile)).ToShortcutKeyDisplayString();
GetShortcutKeyDisplayString(staged ? Command.UnStageSelectedFile : Command.StageSelectedFile);

return;
}
Expand Down Expand Up @@ -2516,13 +2534,19 @@ private void UpdateAuthorInfo()
: $"{committer} {_commitAuthorInfo.Text} {toolAuthor.Text}";
}

private static bool SenderToFileStatusList(object sender, out FileStatusList list)
private bool SenderToFileStatusList(object sender, out FileStatusList list)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
ContextMenuStrip menu = item?.Owner as ContextMenuStrip;
ListView lv = menu?.SourceControl as ListView;

list = lv?.Parent as FileStatusList;
if (list == null /* menu action triggered directly by hotkey */)
{
// The inactive list's selection has been cleared.
list = Staged.SelectedItems.Any() ? Staged : Unstaged.SelectedItems.Any() ? list = Unstaged : null;
}

return list != null;
}

Expand Down
7 changes: 5 additions & 2 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public enum Command
DeleteSelectedFiles = 0,
ShowHistory = 1,
Blame = 2,
OpenWithDifftool = 3
OpenWithDifftool = 3,
EditFile = 4
}

public bool ExecuteCommand(Command cmd)
Expand All @@ -112,6 +113,7 @@ protected override bool ExecuteCommand(int cmd)
case Command.ShowHistory: fileHistoryDiffToolstripMenuItem.PerformClick(); break;
case Command.Blame: blameToolStripMenuItem.PerformClick(); break;
case Command.OpenWithDifftool: firstToSelectedToolStripMenuItem.PerformClick(); break;
case Command.EditFile: diffEditFileToolStripMenuItem.PerformClick(); break;
default: return base.ExecuteCommand(cmd);
}

Expand All @@ -125,6 +127,7 @@ public void ReloadHotkeys()
fileHistoryDiffToolstripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.ShowHistory);
blameToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.Blame);
firstToSelectedToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.OpenWithDifftool);
diffEditFileToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeyDisplayString(Command.EditFile);
DiffText.ReloadHotkeys();
}

Expand Down Expand Up @@ -410,7 +413,7 @@ private async void DiffText_ExtraDiffArgumentsChanged(object sender, EventArgs e
private void diffShowInFileTreeToolStripMenuItem_Click(object sender, EventArgs e)
{
// switch to view (and fills the first level of file tree data model if not already done)
(FindForm() as FormBrowse)?.ExecuteCommand(FormBrowse.Commands.FocusFileTree);
(FindForm() as FormBrowse)?.ExecuteCommand(FormBrowse.Command.FocusFileTree);
_revisionFileTree.ExpandToFile(DiffFiles.SelectedItems.First().Name);
}

Expand Down
Loading

0 comments on commit 56f720d

Please sign in to comment.