Skip to content

Commit

Permalink
Allow configuring editor for local files
Browse files Browse the repository at this point in the history
  • Loading branch information
SlugFiller committed Aug 30, 2023
1 parent 0054eca commit a118e87
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
2 changes: 2 additions & 0 deletions GitCommands/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ public static int DiffVerticalRulerPosition
set => SetInt("diffverticalrulerposition", value);
}

public static ISetting<string> LocalFileEditor => Setting.Create(DetailedSettingsPath, nameof(LocalFileEditor), string.Empty);

[MaybeNull]
public static string RecentWorkingDir
{
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,7 @@ private void editFileToolStripMenuItem_Click(object sender, EventArgs e)
return;
}

UICommands.StartFileEditorDialog(fileName);
UICommands.StartLocalFileEditorDialog(fileName);

UnstagedSelectionChanged(this, EventArgs.Empty);
}
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ private void diffEditWorkingDirectoryFileToolStripMenuItem_Click(object sender,
}

var fileName = _fullPathResolver.Resolve(DiffFiles.SelectedItem.Item.Name);
UICommands.StartFileEditorDialog(fileName);
UICommands.StartLocalFileEditorDialog(fileName);
RequestRefresh();
}

Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/RevisionFileTreeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ private void editCheckedOutFileToolStripMenuItem_Click(object sender, EventArgs

var fileName = _fullPathResolver.Resolve(gitItem.FileName);
Validates.NotNull(fileName);
UICommands.StartFileEditorDialog(fileName);
UICommands.StartLocalFileEditorDialog(fileName);
_refreshGitStatus?.Invoke();
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public DiffViewerSettingsPage()
chkShowDiffForAllParents.Text = TranslatedStrings.ShowDiffForAllParentsText;
chkShowDiffForAllParents.ToolTipText = TranslatedStrings.ShowDiffForAllParentsTooltip;
chkContScrollToNextFileOnlyWithAlt.Text = TranslatedStrings.ContScrollToNextFileOnlyWithAlt;

cboEditor.Items.Add(string.Empty);
cboEditor.Items.AddRange(EditorHelper.GetEditors());
}

protected override void SettingsToPage()
Expand All @@ -28,6 +31,7 @@ protected override void SettingsToPage()
chkShowDiffForAllParents.Checked = AppSettings.ShowDiffForAllParents;
chkShowAllCustomDiffTools.Checked = AppSettings.ShowAvailableDiffTools;
VerticalRulerPosition.Value = AppSettings.DiffVerticalRulerPosition;
cboEditor.Text = AppSettings.LocalFileEditor.Value;

base.SettingsToPage();
}
Expand All @@ -45,6 +49,7 @@ protected override void PageToSettings()
AppSettings.ShowDiffForAllParents = chkShowDiffForAllParents.Checked;
AppSettings.ShowAvailableDiffTools = chkShowAllCustomDiffTools.Checked;
AppSettings.DiffVerticalRulerPosition = (int)VerticalRulerPosition.Value;
AppSettings.LocalFileEditor.Value = cboEditor.Text;

base.PageToSettings();
}
Expand Down
35 changes: 35 additions & 0 deletions GitUI/GitUICommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.RegularExpressions;
using GitCommands;
using GitCommands.Git;
using GitCommands.Git.Commands;
Expand All @@ -19,6 +20,9 @@ namespace GitUI
/// <summary>Contains methods to invoke GitEx forms, dialogs, etc.</summary>
public sealed class GitUICommands : IGitUICommands
{
private static readonly char[] _whiteSpaceChars = { ' ', '\r', '\n', '\t' };
private static readonly Regex SingleArgRegex = new("([^\" \r\n\t][^ \r\n\t]*)|(\"([^\"\\\\]|(\\\\.))+\")", RegexOptions.Compiled);

private const string BlameHistoryCommand = "blamehistory";
private const string FileHistoryCommand = "filehistory";

Expand Down Expand Up @@ -1690,6 +1694,37 @@ public bool StartFileEditorDialog(string? filename, bool showWarning = false)
return formEditor.ShowDialog() != DialogResult.Cancel;
}

public void StartLocalFileEditorDialog(string? filename)
{
string cmd = AppSettings.LocalFileEditor.Value;
if (string.IsNullOrWhiteSpace(cmd))
{
StartFileEditorDialog(filename);
return;
}

Match m = SingleArgRegex.Match(cmd);
if (!m.Success)
{
return;
}

StringBuilder args = new StringBuilder();
if (m.Length < cmd.Length)
{
if (m.Index + m.Length < cmd.Length)
{
args.Append(cmd.Substring(m.Index + m.Length).TrimStart(_whiteSpaceChars));
args.Append(" ");
}

cmd = cmd.Substring(m.Index, m.Length);
}

args.Append(filename.Quote());
new Executable(cmd).Start(args.ToString(), useShellExecute: true, throwOnErrorExit: false);
}

/// <summary>
/// Remove working directory from filename and convert to POSIX path.
/// This is to prevent filenames that are too long while there is room left when the workingdir was not in the path.
Expand Down
4 changes: 4 additions & 0 deletions GitUI/Translation/English.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,10 @@ the last selected commit.</source>
<source>Vertical ruler position [chars]</source>
<target />
</trans-unit>
<trans-unit id="lblEditor.Text">
<source>Local file editor (Leave blank to use built-in editor)</source>
<target />
</trans-unit>
</body>
</file>
<file datatype="plaintext" original="EditNetSpell" source-language="en">
Expand Down

0 comments on commit a118e87

Please sign in to comment.