From 3a0ae1c6e08d362d415959bbcf9e02a473a3f27e Mon Sep 17 00:00:00 2001 From: Douglas Cunha Date: Sun, 27 Oct 2024 14:57:57 -0300 Subject: [PATCH] feat: support language for commit message generation - Extend the GenerateCommitMessage constructor to include a language parameter and use it in the selected language prompt logic. - Add property for commit message language with default value "English". - Add integration related configuration strings for commit message language. - Add property for CommitMessageLanguage in RepositoryConfigure. - Add language parameter to AIAssistant constructor and propagate it to GenerateCommitMessage. - Add integration configuration options in the repository settings view. --- src/Commands/GenerateCommitMessage.cs | 10 +++++--- src/Models/OpenAI.cs | 6 ----- src/Models/RepositorySettings.cs | 6 +++++ src/Resources/Icons.axaml | 1 - src/Resources/Locales/en_US.axaml | 3 ++- src/Resources/Locales/pt_BR.axaml | 3 ++- src/ViewModels/Preference.cs | 16 ------------ src/ViewModels/RepositoryConfigure.cs | 6 +++++ src/ViewModels/WorkingCopy.cs | 35 ++------------------------- src/Views/AIAssistant.axaml.cs | 6 +++-- src/Views/RepositoryConfigure.axaml | 33 +++++++++++++++++++++++++ src/Views/WorkingCopy.axaml | 28 ++++++--------------- src/Views/WorkingCopy.axaml.cs | 11 --------- 13 files changed, 69 insertions(+), 95 deletions(-) diff --git a/src/Commands/GenerateCommitMessage.cs b/src/Commands/GenerateCommitMessage.cs index cbf36b71..925b0806 100644 --- a/src/Commands/GenerateCommitMessage.cs +++ b/src/Commands/GenerateCommitMessage.cs @@ -20,9 +20,10 @@ namespace SourceGit.Commands } } - public GenerateCommitMessage(string repo, List changes, CancellationToken cancelToken, Action onProgress) + public GenerateCommitMessage(string repo, string language, List changes, CancellationToken cancelToken, Action onProgress) { _repo = repo; + _language = language ?? "English"; _changes = changes; _cancelToken = cancelToken; _onProgress = onProgress; @@ -70,11 +71,11 @@ namespace SourceGit.Commands } } - private static string GetSelectedLanguagePrompt() + private string GetSelectedLanguagePrompt() { - var selectedLanguage = Models.OpenAI.SelectedLanguage == "English" + var selectedLanguage = _language == "English" ? string.Empty - : $"{Environment.NewLine}Always write in {Models.OpenAI.SelectedLanguage}"; + : $"{Environment.NewLine}Always write in {_language}"; return selectedLanguage; } @@ -106,5 +107,6 @@ namespace SourceGit.Commands private List _changes; private CancellationToken _cancelToken; private Action _onProgress; + private readonly string _language; } } diff --git a/src/Models/OpenAI.cs b/src/Models/OpenAI.cs index e04682db..b1bb9465 100644 --- a/src/Models/OpenAI.cs +++ b/src/Models/OpenAI.cs @@ -94,12 +94,6 @@ namespace SourceGit.Models set; } - public static string SelectedLanguage - { - get; - set; - } - public static string AnalyzeDiffPrompt { get; diff --git a/src/Models/RepositorySettings.cs b/src/Models/RepositorySettings.cs index 40c00bc9..99f3dcff 100644 --- a/src/Models/RepositorySettings.cs +++ b/src/Models/RepositorySettings.cs @@ -112,6 +112,12 @@ namespace SourceGit.Models set; } = false; + public string CommitMessageLanguage + { + get; + set; + } = "English"; + public void PushCommitMessage(string message) { var existIdx = CommitMessages.IndexOf(message); diff --git a/src/Resources/Icons.axaml b/src/Resources/Icons.axaml index e280fba4..1a84597e 100644 --- a/src/Resources/Icons.axaml +++ b/src/Resources/Icons.axaml @@ -61,7 +61,6 @@ M412 66C326 132 271 233 271 347c0 17 1 34 4 50-41-48-98-79-162-83a444 444 0 00-46 196c0 207 142 382 337 439h2c19 0 34 15 34 33 0 11-6 21-14 26l1 14C183 973 0 763 0 511 0 272 166 70 393 7A35 35 0 01414 0c19 0 34 15 34 33a33 33 0 01-36 33zm200 893c86-66 141-168 141-282 0-17-1-34-4-50 41 48 98 79 162 83a444 444 0 0046-196c0-207-142-382-337-439h-2a33 33 0 01-34-33c0-11 6-21 14-26L596 0C841 51 1024 261 1024 513c0 239-166 441-393 504A35 35 0 01610 1024a33 33 0 01-34-33 33 33 0 0136-33zM512 704a192 192 0 110-384 192 192 0 010 384z M512 64A447 447 0 0064 512c0 248 200 448 448 448s448-200 448-448S760 64 512 64zM218 295h31c54 0 105 19 145 55 13 12 13 31 3 43a35 35 0 01-22 10 36 36 0 01-21-7 155 155 0 00-103-39h-31a32 32 0 01-31-31c0-18 13-31 30-31zm31 433h-31a32 32 0 01-31-31c0-16 13-31 31-31h31A154 154 0 00403 512 217 217 0 01620 295h75l-93-67a33 33 0 01-7-43 33 33 0 0143-7l205 148-205 148a29 29 0 01-18 6 32 32 0 01-31-31c0-10 4-19 13-25l93-67H620a154 154 0 00-154 154c0 122-97 220-217 220zm390 118a29 29 0 01-18 6 32 32 0 01-31-31c0-10 4-19 13-25l93-67h-75c-52 0-103-19-143-54-12-12-13-31-1-43a30 30 0 0142-3 151 151 0 00102 39h75L602 599a33 33 0 01-7-43 33 33 0 0143-7l205 148-203 151z M922 39H102A65 65 0 0039 106v609a65 65 0 0063 68h94v168a34 34 0 0019 31 30 30 0 0012 3 30 30 0 0022-10l182-192H922a65 65 0 0063-68V106A65 65 0 00922 39zM288 378h479a34 34 0 010 68H288a34 34 0 010-68zm0-135h479a34 34 0 010 68H288a34 34 0 010-68zm0 270h310a34 34 0 010 68H288a34 34 0 010-68z - M480-120q-74.31 0-140-28.42-65.69-28.43-114.42-77.16-48.73-48.73-77.16-114.42Q120-405.69 120-480q0-74.54 28.42-140.12 28.43-65.57 77.16-114.3 48.73-48.73 114.42-77.16Q405.69-840 480-840q74.54 0 140.12 28.42 65.57 28.43 114.3 77.16 48.73 48.73 77.16 114.3Q840-554.54 840-480q0 74.31-28.42 140-28.43 65.69-77.16 114.42-48.73 48.73-114.3 77.16Q554.54-120 480-120Zm0-39.69q35.23-45.23 58.08-88.85 22.84-43.61 37.15-97.61H384.77q15.85 57.07 37.92 100.69 22.08 43.61 57.31 85.77Zm-50.92-6q-28-33-51.12-81.58-23.11-48.58-34.42-98.88H190.15q34.39 74.61 97.5 122.38 63.12 47.77 141.43 58.08Zm101.84 0q78.31-10.31 141.43-58.08 63.11-47.77 97.5-122.38H616.46q-15.15 51.07-38.27 99.65-23.11 48.58-47.27 80.81ZM173.85-386.15h161.38q-4.54-24.62-6.42-47.97-1.89-23.34-1.89-45.88 0-22.54 1.89-45.88 1.88-23.35 6.42-47.97H173.85q-6.54 20.77-10.2 45.27Q160-504.08 160-480t3.65 48.58q3.66 24.5 10.2 45.27Zm201.38 0h209.54q4.54-24.62 6.42-47.2 1.89-22.57 1.89-46.65t-1.89-46.65q-1.88-22.58-6.42-47.2H375.23q-4.54 24.62-6.42 47.2-1.89 22.57-1.89 46.65t1.89 46.65q1.88 22.58 6.42 47.2Zm249.54 0h161.38q6.54-20.77 10.2-45.27Q800-455.92 800-480t-3.65-48.58q-3.66-24.5-10.2-45.27H624.77q4.54 24.62 6.42 47.97 1.89 23.34 1.89 45.88 0 22.54-1.89 45.88-1.88 23.35-6.42 47.97Zm-8.31-227.7h153.39Q734.69-690 673.5-736.23q-61.19-46.23-142.58-58.85 28 36.85 50.35 84.27 22.35 47.43 35.19 96.96Zm-231.69 0h190.46q-15.85-56.3-39.08-101.84-23.23-45.54-56.15-84.62-32.92 39.08-56.15 84.62-23.23 45.54-39.08 101.84Zm-194.62 0h153.39q12.84-49.53 35.19-96.96 22.35-47.42 50.35-84.27-82.16 12.62-142.96 59.23-60.81 46.62-95.97 122Z M875 117H149C109 117 75 151 75 192v640c0 41 34 75 75 75h725c41 0 75-34 75-75V192c0-41-34-75-75-75zM139 832V192c0-6 4-11 11-11h331v661H149c-6 0-11-4-11-11zm747 0c0 6-4 11-11 11H544v-661H875c6 0 11 4 11 11v640z M875 117H149C109 117 75 151 75 192v640c0 41 34 75 75 75h725c41 0 75-34 75-75V192c0-41-34-75-75-75zm-725 64h725c6 0 11 4 11 11v288h-747V192c0-6 4-11 11-11zm725 661H149c-6 0-11-4-11-11V544h747V832c0 6-4 11-11 11z M40 9 15 23 15 31 9 28 9 20 34 5 24 0 0 14 0 34 25 48 25 28 49 14zM26 29 26 48 49 34 49 15z diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 184837e8..6cbec9c6 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -19,7 +19,6 @@ Track Branch: Tracking remote branch OpenAI Assistant - Language used to generate commit message Use OpenAI to generate commit message Patch Error @@ -145,6 +144,8 @@ Minute(s) Default Remote Enable --signoff for commit + INTEGRATION + Commit Message Language ISSUE TRACKER Add Sample Github Rule Add Sample Jira Rule diff --git a/src/Resources/Locales/pt_BR.axaml b/src/Resources/Locales/pt_BR.axaml index c944cea5..0e6766fb 100644 --- a/src/Resources/Locales/pt_BR.axaml +++ b/src/Resources/Locales/pt_BR.axaml @@ -21,7 +21,6 @@ O que Checar: Adicionar Worktree Assistente do OpenAI - Idioma usado a gerar a mensagem de commit Usar OpenAI para gerar mensagem de commit Erros levantados e se recusa a aplicar o patch Erro @@ -136,6 +135,8 @@ Buscar remotos automaticamente Minuto(s) GIT + INTEGRAÇÃO + Idioma para mensagem de commit Adicionar Regra de Exemplo do Github Adicionar Regra de Exemplo do Jira Nova Regra diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 13f28f17..72667f54 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -316,19 +316,6 @@ namespace SourceGit.ViewModels } } - public string SelectedLanguage - { - get => Models.OpenAI.SelectedLanguage; - set - { - if (value != Models.OpenAI.SelectedLanguage) - { - Models.OpenAI.SelectedLanguage = value; - OnPropertyChanged(); - } - } - } - public string OpenAIAnalyzeDiffPrompt { get => Models.OpenAI.AnalyzeDiffPrompt; @@ -604,9 +591,6 @@ namespace SourceGit.ViewModels - Be as concise as possible, keep the message under 50 characters. """; } - - if (string.IsNullOrEmpty(Models.OpenAI.SelectedLanguage)) - Models.OpenAI.SelectedLanguage = "English"; } private RepositoryNode FindNodeRecursive(string id, List collection) diff --git a/src/ViewModels/RepositoryConfigure.cs b/src/ViewModels/RepositoryConfigure.cs index 3c969c4d..ee4b6cb5 100644 --- a/src/ViewModels/RepositoryConfigure.cs +++ b/src/ViewModels/RepositoryConfigure.cs @@ -86,6 +86,12 @@ namespace SourceGit.ViewModels } } + public string CommitMessageLanguage + { + get => _repo.Settings.CommitMessageLanguage; + set => _repo.Settings.CommitMessageLanguage = value; + } + public AvaloniaList CommitTemplates { get => _repo.Settings.CommitTemplates; diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index 282f4cc4..911c00d7 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -414,7 +414,7 @@ namespace SourceGit.ViewModels if (_staged is { Count: > 0 }) { - var dialog = new Views.AIAssistant(_repo.FullPath, _staged, generated => CommitMessage = generated); + var dialog = new Views.AIAssistant(_repo.FullPath, _repo.Settings.CommitMessageLanguage, _staged, generated => CommitMessage = generated); App.OpenDialog(dialog); } else @@ -1148,38 +1148,7 @@ namespace SourceGit.ViewModels return menu; } - public ContextMenu CreateContextMenuForSelectedLanguage() - { - var menu = new ContextMenu(); - var selectedLanguage = string.IsNullOrEmpty(Preference.Instance.SelectedLanguage) - ? "English" - : Preference.Instance.SelectedLanguage; - - foreach (var locale in Locale.Supported) - { - var language = locale.Name; - - var item = new MenuItem - { - Header = language, - Icon = selectedLanguage.Equals(language, StringComparison.OrdinalIgnoreCase) - ? App.CreateMenuIcon("Icons.Check") - : null - }; - - item.Click += (_, e) => - { - Preference.Instance.SelectedLanguage = language; - e.Handled = true; - }; - - menu.Items.Add(item); - } - - return menu; - } - - public ContextMenu CreateContextMenuForCommitMessages() + public ContextMenu CreateContextMenuForCommitMessages() { var menu = new ContextMenu(); diff --git a/src/Views/AIAssistant.axaml.cs b/src/Views/AIAssistant.axaml.cs index 6ceb5610..0b71d9a1 100644 --- a/src/Views/AIAssistant.axaml.cs +++ b/src/Views/AIAssistant.axaml.cs @@ -17,9 +17,10 @@ namespace SourceGit.Views InitializeComponent(); } - public AIAssistant(string repo, List changes, Action onDone) + public AIAssistant(string repo, string language, List changes, Action onDone) { _repo = repo; + _language = language; _changes = changes; _onDone = onDone; _cancel = new CancellationTokenSource(); @@ -35,7 +36,7 @@ namespace SourceGit.Views Task.Run(() => { - var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result(); + var message = new Commands.GenerateCommitMessage(_repo, _language, _changes, _cancel.Token, SetDescription).Result(); if (_cancel.IsCancellationRequested) return; @@ -67,5 +68,6 @@ namespace SourceGit.Views private List _changes; private Action _onDone; private CancellationTokenSource _cancel; + private readonly string _language; } } diff --git a/src/Views/RepositoryConfigure.axaml b/src/Views/RepositoryConfigure.axaml index 6383d904..644015c2 100644 --- a/src/Views/RepositoryConfigure.axaml +++ b/src/Views/RepositoryConfigure.axaml @@ -5,6 +5,7 @@ xmlns:m="using:SourceGit.Models" xmlns:vm="using:SourceGit.ViewModels" xmlns:v="using:SourceGit.Views" + xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.RepositoryConfigure" x:DataType="vm:RepositoryConfigure" @@ -340,6 +341,38 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Views/WorkingCopy.axaml b/src/Views/WorkingCopy.axaml index 0ef3c033..3d080fc6 100644 --- a/src/Views/WorkingCopy.axaml +++ b/src/Views/WorkingCopy.axaml @@ -55,7 +55,7 @@ Classes="icon_button" Width="26" Height="14" Padding="0" - ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.StageAll}" + ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.StageAll}" Command="{Binding StageAll}"> @@ -99,7 +99,7 @@ - @@ -135,7 +135,7 @@ - + @@ -165,7 +165,7 @@ - + @@ -185,7 +185,7 @@ - + - - - - - - - +