style: add .editorconfig for code formatting. see issu #25

This commit is contained in:
leo 2024-03-18 09:37:06 +08:00
parent a8eeea4f78
commit 18aaa0a143
225 changed files with 7781 additions and 3911 deletions

View file

@ -2,64 +2,89 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SourceGit.Commands {
public partial class Diff : Command {
namespace SourceGit.Commands
{
public partial class Diff : Command
{
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
private static partial Regex REG_INDICATOR();
private static readonly string PREFIX_LFS_NEW = "+version https://git-lfs.github.com/spec/";
private static readonly string PREFIX_LFS_DEL = "-version https://git-lfs.github.com/spec/";
private static readonly string PREFIX_LFS_MODIFY = " version https://git-lfs.github.com/spec/";
public Diff(string repo, Models.DiffOption opt) {
public Diff(string repo, Models.DiffOption opt)
{
WorkingDirectory = repo;
Context = repo;
Args = $"diff --ignore-cr-at-eol --unified=4 {opt}";
}
public Models.DiffResult Result() {
public Models.DiffResult Result()
{
Exec();
if (_result.IsBinary || _result.IsLFS) {
if (_result.IsBinary || _result.IsLFS)
{
_result.TextDiff = null;
} else {
}
else
{
ProcessInlineHighlights();
if (_result.TextDiff.Lines.Count == 0) {
if (_result.TextDiff.Lines.Count == 0)
{
_result.TextDiff = null;
} else {
}
else
{
_result.TextDiff.MaxLineNumber = Math.Max(_newLine, _oldLine);
}
}
}
return _result;
}
protected override void OnReadline(string line) {
protected override void OnReadline(string line)
{
if (_result.IsBinary) return;
if (_result.IsLFS) {
if (_result.IsLFS)
{
var ch = line[0];
if (ch == '-') {
if (line.StartsWith("-oid sha256:", StringComparison.Ordinal)) {
if (ch == '-')
{
if (line.StartsWith("-oid sha256:", StringComparison.Ordinal))
{
_result.LFSDiff.Old.Oid = line.Substring(12);
} else if (line.StartsWith("-size ", StringComparison.Ordinal)) {
}
else if (line.StartsWith("-size ", StringComparison.Ordinal))
{
_result.LFSDiff.Old.Size = long.Parse(line.Substring(6));
}
} else if (ch == '+') {
if (line.StartsWith("+oid sha256:", StringComparison.Ordinal)) {
}
else if (ch == '+')
{
if (line.StartsWith("+oid sha256:", StringComparison.Ordinal))
{
_result.LFSDiff.New.Oid = line.Substring(12);
} else if (line.StartsWith("+size ", StringComparison.Ordinal)) {
}
else if (line.StartsWith("+size ", StringComparison.Ordinal))
{
_result.LFSDiff.New.Size = long.Parse(line.Substring(6));
}
} else if (line.StartsWith(" size ", StringComparison.Ordinal)) {
}
else if (line.StartsWith(" size ", StringComparison.Ordinal))
{
_result.LFSDiff.New.Size = _result.LFSDiff.Old.Size = long.Parse(line.Substring(6));
}
return;
}
if (_result.TextDiff.Lines.Count == 0) {
if (_result.TextDiff.Lines.Count == 0)
{
var match = REG_INDICATOR().Match(line);
if (!match.Success) {
if (!match.Success)
{
if (line.StartsWith("Binary", StringComparison.Ordinal)) _result.IsBinary = true;
return;
}
@ -67,8 +92,11 @@ namespace SourceGit.Commands {
_oldLine = int.Parse(match.Groups[1].Value);
_newLine = int.Parse(match.Groups[2].Value);
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
} else {
if (line.Length == 0) {
}
else
{
if (line.Length == 0)
{
ProcessInlineHighlights();
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Normal, "", _oldLine, _newLine));
_oldLine++;
@ -77,8 +105,10 @@ namespace SourceGit.Commands {
}
var ch = line[0];
if (ch == '-') {
if (_oldLine == 1 && _newLine == 0 && line.StartsWith(PREFIX_LFS_DEL, StringComparison.Ordinal)) {
if (ch == '-')
{
if (_oldLine == 1 && _newLine == 0 && line.StartsWith(PREFIX_LFS_DEL, StringComparison.Ordinal))
{
_result.IsLFS = true;
_result.LFSDiff = new Models.LFSDiff();
return;
@ -86,8 +116,11 @@ namespace SourceGit.Commands {
_deleted.Add(new Models.TextDiffLine(Models.TextDiffLineType.Deleted, line.Substring(1), _oldLine, 0));
_oldLine++;
} else if (ch == '+') {
if (_oldLine == 0 && _newLine == 1 && line.StartsWith(PREFIX_LFS_NEW, StringComparison.Ordinal)) {
}
else if (ch == '+')
{
if (_oldLine == 0 && _newLine == 1 && line.StartsWith(PREFIX_LFS_NEW, StringComparison.Ordinal))
{
_result.IsLFS = true;
_result.LFSDiff = new Models.LFSDiff();
return;
@ -95,15 +128,21 @@ namespace SourceGit.Commands {
_added.Add(new Models.TextDiffLine(Models.TextDiffLineType.Added, line.Substring(1), 0, _newLine));
_newLine++;
} else if (ch != '\\') {
}
else if (ch != '\\')
{
ProcessInlineHighlights();
var match = REG_INDICATOR().Match(line);
if (match.Success) {
if (match.Success)
{
_oldLine = int.Parse(match.Groups[1].Value);
_newLine = int.Parse(match.Groups[2].Value);
_result.TextDiff.Lines.Add(new Models.TextDiffLine(Models.TextDiffLineType.Indicator, line, 0, 0));
} else {
if (_oldLine == 1 && _newLine == 1 && line.StartsWith(PREFIX_LFS_MODIFY, StringComparison.Ordinal)) {
}
else
{
if (_oldLine == 1 && _newLine == 1 && line.StartsWith(PREFIX_LFS_MODIFY, StringComparison.Ordinal))
{
_result.IsLFS = true;
_result.LFSDiff = new Models.LFSDiff();
return;
@ -117,10 +156,14 @@ namespace SourceGit.Commands {
}
}
private void ProcessInlineHighlights() {
if (_deleted.Count > 0) {
if (_added.Count == _deleted.Count) {
for (int i = _added.Count - 1; i >= 0; i--) {
private void ProcessInlineHighlights()
{
if (_deleted.Count > 0)
{
if (_added.Count == _deleted.Count)
{
for (int i = _added.Count - 1; i >= 0; i--)
{
var left = _deleted[i];
var right = _added[i];
@ -129,12 +172,15 @@ namespace SourceGit.Commands {
var chunks = Models.TextInlineChange.Compare(left.Content, right.Content);
if (chunks.Count > 4) continue;
foreach (var chunk in chunks) {
if (chunk.DeletedCount > 0) {
foreach (var chunk in chunks)
{
if (chunk.DeletedCount > 0)
{
left.Highlights.Add(new Models.TextInlineRange(chunk.DeletedStart, chunk.DeletedCount));
}
if (chunk.AddedCount > 0) {
if (chunk.AddedCount > 0)
{
right.Highlights.Add(new Models.TextInlineRange(chunk.AddedStart, chunk.AddedCount));
}
}
@ -145,16 +191,17 @@ namespace SourceGit.Commands {
_deleted.Clear();
}
if (_added.Count > 0) {
if (_added.Count > 0)
{
_result.TextDiff.Lines.AddRange(_added);
_added.Clear();
}
}
private Models.DiffResult _result = new Models.DiffResult() { TextDiff = new Models.TextDiff() };
private List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>();
private List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>();
private readonly Models.DiffResult _result = new Models.DiffResult() { TextDiff = new Models.TextDiff() };
private readonly List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>();
private readonly List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>();
private int _oldLine = 0;
private int _newLine = 0;
}
}
}