Skip to content

Commit

Permalink
Goto current line when opening internal text editor (#11191)
Browse files Browse the repository at this point in the history
Goto currently selected line in diff/tree/commit editor,
when opening the internal editor (e.g. with F4).
  • Loading branch information
gerhardol committed Sep 5, 2023
1 parent ee050a5 commit 6a64d95
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
3 changes: 1 addition & 2 deletions GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,8 +2717,7 @@ private void editFileToolStripMenuItem_Click(object sender, EventArgs e)
return;
}

UICommands.StartFileEditorDialog(fileName);

UICommands.StartFileEditorDialog(fileName, lineNumber: SelectedDiff.CurrentFileLine);
UnstagedSelectionChanged(this, EventArgs.Empty);
}

Expand Down
8 changes: 4 additions & 4 deletions GitUI/CommandsDialogs/FormEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private FormEditor()
InitializeComponent();
}

public FormEditor(GitUICommands commands, string? fileName, bool showWarning, bool readOnly = false)
public FormEditor(GitUICommands commands, string? fileName, bool showWarning, bool readOnly = false, int? lineNumber = null)
: base(commands)
{
_fileName = fileName;
Expand All @@ -34,7 +34,7 @@ public FormEditor(GitUICommands commands, string? fileName, bool showWarning, bo
// for translation form
if (_fileName is not null)
{
OpenFile(_fileName);
OpenFile(_fileName, lineNumber);
}

fileViewer.TextChanged += (s, e) => HasChanges = true;
Expand All @@ -54,11 +54,11 @@ private bool HasChanges
}
}

private void OpenFile(string fileName)
private void OpenFile(string fileName, int? line = null)
{
try
{
fileViewer.ViewFileAsync(fileName);
fileViewer.ViewFileAsync(fileName, line: line);
fileViewer.IsReadOnly = false;
Text = fileName;

Expand Down
5 changes: 3 additions & 2 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,8 +993,9 @@ private void diffEditWorkingDirectoryFileToolStripMenuItem_Click(object sender,
return;
}

var fileName = _fullPathResolver.Resolve(DiffFiles.SelectedItem.Item.Name);
UICommands.StartFileEditorDialog(fileName);
string? fileName = _fullPathResolver.Resolve(DiffFiles.SelectedItem.Item.Name);
int? lineNumber = BlameControl.Visible ? BlameControl.CurrentFileLine : DiffText.CurrentFileLine;
UICommands.StartFileEditorDialog(fileName, lineNumber: lineNumber);
RequestRefresh();
}

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

var fileName = _fullPathResolver.Resolve(gitItem.FileName);
Validates.NotNull(fileName);
UICommands.StartFileEditorDialog(fileName);
int? lineNumber = BlameControl.Visible ? BlameControl.CurrentFileLine : FileText.CurrentFileLine;
UICommands.StartFileEditorDialog(fileName, lineNumber: lineNumber);
_refreshGitStatus?.Invoke();
}

Expand Down
4 changes: 2 additions & 2 deletions GitUI/GitUICommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,9 +1684,9 @@ private bool RunRebaseCommand(IReadOnlyDictionary<string, string?> arguments)
return StartRebaseDialog(owner: null, onto: branch);
}

public bool StartFileEditorDialog(string? filename, bool showWarning = false)
public bool StartFileEditorDialog(string? filename, bool showWarning = false, int? lineNumber = null)
{
using FormEditor formEditor = new(this, filename, showWarning);
using FormEditor formEditor = new(this, filename, showWarning, lineNumber: lineNumber);
return formEditor.ShowDialog() != DialogResult.Cancel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ public void HasChanges_updated_correctly()
}
}

[Test]
public void Should_set_linenumber_correctly()
{
string filePath = Path.Combine(_referenceRepository.Module.WorkingDir, Path.GetRandomFileName());

try
{
const int lineNumerDefault = 2;
File.WriteAllText(filePath, "Hello↔world\nline2\nline3\n\n\n", _commands.Module.FilesEncoding);

UITest.RunForm<FormEditor>(
() =>
{
using FormEditor formEditor = new(_commands, filePath, showWarning: false, lineNumber: lineNumerDefault);
formEditor.ShowDialog();
},
form =>
{
Assert.False(form.GetTestAccessor().HasChanges);
var fileViewerInternal = form.GetTestAccessor().FileViewer.GetTestAccessor().FileViewerInternal;
fileViewerInternal.SetText(fileViewerInternal.GetText() + "!", openWithDifftool: null, isDiff: false);
Assert.True(form.GetTestAccessor().HasChanges);
form.GetTestAccessor().SaveChanges();
Assert.False(form.GetTestAccessor().HasChanges);
Assert.AreEqual(lineNumerDefault, fileViewerInternal.CurrentFileLine(false));
return Task.CompletedTask;
});
}
finally
{
File.Delete(filePath);
}
}

[Ignore("Unstable UTF8EncodingSealed result")]
[Test]
public void Should_preserve_encoding_utf8()
Expand Down

0 comments on commit 6a64d95

Please sign in to comment.