|
1 |
| -using Community.VisualStudio.Toolkit;using JeffPires.VisualChatGPTStudio.ToolWindows.Turbo; |
2 |
| -using JeffPires.VisualChatGPTStudio.Utils;using Microsoft.VisualStudio.Shell;using Microsoft.VisualStudio.Text;using System;using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Text; |
5 |
| -using System.Threading; |
| 1 | +using Community.VisualStudio.Toolkit;using JeffPires.VisualChatGPTStudio.Utils;using Microsoft.VisualStudio.Shell;using Microsoft.VisualStudio.Text;using System;using System.Threading; |
6 | 2 | using System.Windows.Input;using Constants = JeffPires.VisualChatGPTStudio.Utils.Constants;using Span = Microsoft.VisualStudio.Text.Span;namespace JeffPires.VisualChatGPTStudio.Commands{
|
7 | 3 | /// <summary>
|
8 | 4 | /// Base abstract class for generic commands
|
|
44 | 40 | /// Requests a response from ChatGPT and handles the result.
|
45 | 41 | /// </summary>
|
46 | 42 | /// <param name="selectedText">The selected text.</param>
|
47 |
| - private async System.Threading.Tasks.Task RequestAsync(string selectedText) { string command = GetCommand(selectedText); if (typeof(TCommand) != typeof(AskAnything) && string.IsNullOrWhiteSpace(command)) { await VS.MessageBox.ShowAsync(Constants.EXTENSION_NAME, string.Format(Constants.MESSAGE_SET_COMMAND, typeof(TCommand).Name), buttons: Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_OK); return; } if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { await TerminalWindowCommand.Instance.RequestToWindowAsync(command, selectedText); return; } await VS.StatusBar.ShowProgressAsync(Constants.MESSAGE_WAITING_CHATGPT, 1, 2); string[] stopSequences = OptionsGeneral.StopSequences.Split(','); if (typeof(TCommand) == typeof(AddSummary)) { stopSequences = new[] { "public", "private", "internal" }; } CancellationTokenSource = new CancellationTokenSource(); if (OptionsGeneral.SingleResponse) { string result = await ChatGPT.GetResponseAsync(OptionsGeneral, command, selectedText, stopSequences, CancellationTokenSource.Token); ResultHandler(result); } else { await ChatGPT.GetResponseAsync(OptionsGeneral, command, selectedText, stopSequences, ResultHandler, CancellationTokenSource.Token); } await FormatDocumentAsync(); } |
| 43 | + private async System.Threading.Tasks.Task RequestAsync(string selectedText) { string command = GetCommand(selectedText); if (typeof(TCommand) != typeof(AskAnything) && string.IsNullOrWhiteSpace(command)) { await VS.MessageBox.ShowAsync(Constants.EXTENSION_NAME, string.Format(Constants.MESSAGE_SET_COMMAND, typeof(TCommand).Name), buttons: Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_OK); return; } if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { |
| 44 | + await TerminalWindowCommand.Instance.RequestToWindowAsync(command, selectedText, IsCodeCommand()); return; } await VS.StatusBar.ShowProgressAsync(Constants.MESSAGE_WAITING_CHATGPT, 1, 2); string[] stopSequences = OptionsGeneral.StopSequences.Split(','); if (typeof(TCommand) == typeof(AddSummary)) { stopSequences = new[] { "public", "private", "internal" }; } CancellationTokenSource = new CancellationTokenSource(); if (OptionsGeneral.SingleResponse) { string result = await ChatGPT.GetResponseAsync(OptionsGeneral, command, selectedText, stopSequences, CancellationTokenSource.Token); ResultHandler(result); } else { await ChatGPT.GetResponseAsync(OptionsGeneral, command, selectedText, stopSequences, ResultHandler, CancellationTokenSource.Token); } await FormatDocumentAsync(); } |
48 | 45 |
|
49 | 46 | /// <summary>
|
50 | 47 | /// Handles the result of a command sent to ChatGPT.
|
|
59 | 56 | _ = docView.TextBuffer?.Replace(new Span(position, docView.TextView.Selection.StreamSelectionSpan.GetText().Length), String.Empty); } else if (commandType == CommandType.InsertBefore) { position = positionStart; InsertANewLine(false); } else { position = positionEnd; InsertANewLine(true); } if (typeof(TCommand) == typeof(Explain) || typeof(TCommand) == typeof(FindBugs)) { AddCommentChars(); } firstIteration = false; } if (OptionsGeneral.SingleResponse) { result = RemoveBlankLinesFromResult(result); } else if (!responseStarted && (result.Equals("\n") || result.Equals("\r") || result.Equals(Environment.NewLine))) {
|
60 | 57 | //Do nothing when API send only break lines on response begin
|
61 | 58 | return; } responseStarted = true; if (typeof(TCommand) == typeof(AddSummary) && (result.Contains("{") || result.Contains("}"))) { return; } else if (typeof(TCommand) == typeof(Explain) || typeof(TCommand) == typeof(FindBugs)) { result = FormatResultToAddCommentsCharForEachLine(result); }
|
62 |
| - else if (typeof(TCommand) == typeof(AddTests) || typeof(TCommand) == typeof(Complete) || typeof(TCommand) == typeof(Optimize)) { result = FormatResultForCodeCommands(result); } docView.TextBuffer?.Insert(position, result); position += result.Length; |
| 59 | + else if (IsCodeCommand()) { result = TextFormat.RemoveCodeTagsFromOpenAIResponses(OptionsGeneral.SingleResponse, result); } docView.TextBuffer?.Insert(position, result); position += result.Length; |
63 | 60 | } catch (Exception ex) { Logger.Log(ex); } }
|
64 | 61 |
|
65 | 62 | /// <summary>
|
|
105 | 102 | return result; }
|
106 | 103 |
|
107 | 104 | /// <summary>
|
108 |
| - /// Formats the result for code commands by extracting the code segments avoiding show additional comments. |
109 |
| - /// </summary> |
110 |
| - /// <param name="result">The result.</param> |
111 |
| - /// <returns>The formatted result for code commands.</returns> |
112 |
| - private string FormatResultForCodeCommands(string result) |
| 105 | + /// Determines if the generic type TCommand is one of the specified command types. |
| 106 | + /// </summary> |
| 107 | + private bool IsCodeCommand() |
113 | 108 | {
|
114 |
| - if (!OptionsGeneral.SingleResponse) |
115 |
| - { |
116 |
| - if (result.StartsWith("`")) |
117 |
| - { |
118 |
| - return string.Empty; |
119 |
| - } |
120 |
| - |
121 |
| - return result; |
122 |
| - } |
123 |
| - |
124 |
| - List<ChatMessageSegment> segments = TextFormat.GetChatTurboResponseSegments(result); |
125 |
| - |
126 |
| - if (!segments.Any(s => s.Author == AuthorEnum.ChatGPTCode)) |
127 |
| - { |
128 |
| - return result; |
129 |
| - } |
130 |
| - |
131 |
| - StringBuilder content = new(); |
132 |
| - |
133 |
| - foreach (ChatMessageSegment segment in segments) |
134 |
| - { |
135 |
| - if (segment.Author == AuthorEnum.ChatGPTCode) |
136 |
| - { |
137 |
| - content.AppendLine(segment.Content); |
138 |
| - } |
139 |
| - } |
140 |
| - |
141 |
| - return content.ToString(); |
| 109 | + return typeof(TCommand) == typeof(AddSummary) || typeof(TCommand) == typeof(AddTests) || typeof(TCommand) == typeof(Complete) || typeof(TCommand) == typeof(Optimize); |
142 | 110 | } }
|
143 | 111 |
|
144 | 112 | /// <summary>
|
|
0 commit comments