@@ -75,9 +75,8 @@ public void run(@NotNull ProgressIndicator indicator) {
7575 if (response .statusCode () == 200 ) {
7676 var responseParsed = (new Gson ()).fromJson (response .body (), Response .class );
7777 var responseText = responseParsed .getChoices ()[0 ].getMessage ().getContent ();
78- String docstring = parseDocstring (responseText );
7978 var app = ApplicationManager .getApplication ();
80- app .invokeLater (() -> showEditor (e , docstring ));
79+ app .invokeLater (() -> showEditor (e , responseText ));
8180 } else {
8281 indicator .cancel ();
8382 showError (response .body ());
@@ -111,35 +110,45 @@ public void run(@NotNull ProgressIndicator indicator) {
111110 }
112111 }
113112
114- private static HttpRequest buildRequest (String apiKey , String model , String prompt , String code ) {
115- var messages = new Message []{new Message ("user" , prompt + "```" + code + "```" )};
113+ private HttpRequest buildRequest (String apiKey , String model , String prompt , String code ) {
114+ var endpointURL = getEndpointURL ();
115+ var isAzure = endpointURL .contains ("azure" );
116+
117+ if (isAzure ) {
118+ endpointURL = endpointURL + "openai/deployments/" + model + "/chat/completions?api-version=2024-02-01" ;
119+ }
120+
121+ var messages = new Message []{new Message (prompt + "```" + code + "```" )};
116122 var requestBody = new Request (model , messages , 0 );
117123 var requestJson = new Gson ().toJson (requestBody );
118124
119- return HttpRequest .newBuilder (URI .create ("https://api.openai.com/v1/chat/completions" ))
120- .header ("Authorization" , "Bearer " + apiKey )
121- .header ("Content-Type" , "application/json" )
125+ var requestBuilder = HttpRequest .newBuilder (URI .create (endpointURL ));
126+
127+ if (isAzure ) {
128+ requestBuilder .header ("api-key" , apiKey );
129+ } else {
130+ requestBuilder .header ("Authorization" , "Bearer " + apiKey );
131+ }
132+
133+ return requestBuilder .header ("Content-Type" , "application/json" )
122134 .POST (HttpRequest .BodyPublishers .ofString (requestJson ))
123135 .build ();
124136 }
125137
126- private String parseDocstring (String responseText ) {
127- Pattern pattern = Pattern .compile (":\n (\\ s*\" \" \" .*\" \" \" )" , Pattern .DOTALL );
128- Matcher matcher = pattern .matcher (responseText );
129- if (matcher .find ()) {
130- String docstring = matcher .group (1 );
131- String [] docstringSplit = docstring .split ("\" \" \" " );
132- docstring = docstringSplit [0 ] + "\" \" \" " + docstringSplit [1 ] + "\" \" \" " ;
133- return docstring ;
138+ private String getEndpointURL () {
139+ String endpointInput = PropertiesComponent .getInstance ().getValue (SettingsConfigurable .ENDPOINT_SETTINGS_KEY );
140+
141+ if (endpointInput == null || endpointInput .isEmpty ()) {
142+ return "https://api.openai.com/v1/chat/completions" ;
134143 } else {
135- return "" ;
144+ return endpointInput ;
136145 }
137146 }
138147
139148 private String getApiKey () throws IOException {
140149 String apiKeyInput = PropertiesComponent .getInstance ().getValue (SettingsConfigurable .API_KEY_SETTING_KEY );
141150
142- if (apiKeyInput == null || apiKeyInput .length () == 0 ) {
151+ if (apiKeyInput == null || apiKeyInput .isEmpty () ) {
143152 apiKeyInput = JOptionPane .showInputDialog ("Enter your Open AI API key:" );
144153 PropertiesComponent .getInstance ().setValue (SettingsConfigurable .API_KEY_SETTING_KEY , apiKeyInput );
145154 }
@@ -175,7 +184,7 @@ private void showError(String text) {
175184 JOptionPane .showMessageDialog (null , text , "Error" , JOptionPane .ERROR_MESSAGE );
176185 }
177186
178- private void showEditor (AnActionEvent e , String docstring ) {
187+ private void showEditor (AnActionEvent e , String responseText ) {
179188 var resultWindow = new JFrame ();
180189 var virtualFile = e .getData (PlatformDataKeys .VIRTUAL_FILE );
181190 var fileType = FileTypeManager .getInstance ().getFileTypeByFile (virtualFile );
@@ -185,7 +194,8 @@ private void showEditor(AnActionEvent e, String docstring) {
185194 return ;
186195 }
187196
188- var newDocument = EditorFactory .getInstance ().createDocument (applyIndentation (docstring , "" ));
197+ DocstringFormatter docstringFormatter = new DocstringFormatter (responseText );
198+ var newDocument = EditorFactory .getInstance ().createDocument (docstringFormatter .docstringWithIndentation ("" ));
189199 var editor = EditorFactory .getInstance ().createEditor (newDocument , null , fileType , false );
190200 editor .getContentComponent ().setPreferredSize (new Dimension (1000 , 800 ));
191201 var confirmBtn = new JButton ("Insert" );
@@ -196,7 +206,7 @@ private void showEditor(AnActionEvent e, String docstring) {
196206 int lineEndOffset = mainDocument .getLineEndOffset (caretPosition .line );
197207 int nextLineStartOffset = mainDocument .getLineStartOffset (caretPosition .line + 1 );
198208 int offset = Math .min (lineEndOffset + 1 , nextLineStartOffset );
199- String formattedDocstring = applyIndentation ( docstring , correctIndentation );
209+ String formattedDocstring = docstringFormatter . docstringWithIndentation ( correctIndentation );
200210
201211 confirmBtn .addActionListener (actionEvent -> {
202212 var app = ApplicationManager .getApplication ();
@@ -215,31 +225,6 @@ private void showEditor(AnActionEvent e, String docstring) {
215225 resultWindow .setLocationRelativeTo (null );
216226 }
217227
218- private String applyIndentation (String docstring , String indentation ) {
219- Pattern pattern = Pattern .compile ("^(\\ s+)" );
220- Matcher matcher = pattern .matcher (docstring );
221-
222- if (matcher .find ()) {
223- String leadingWhitespace = matcher .group (1 );
224- int minIndent = leadingWhitespace .length ();
225- String [] lines = docstring .split ("\n " );
226- StringBuilder unindented = new StringBuilder ();
227-
228- for (String line : lines ) {
229- unindented .append (indentation );
230-
231- if (line .length () >= minIndent ) {
232- unindented .append (line .substring (minIndent ));
233- }
234- unindented .append ("\n " );
235- }
236-
237- return unindented .toString ();
238- }
239-
240- return docstring ;
241- }
242-
243228 private String determineCorrectIndentation (String code ) {
244229 Pattern pattern = Pattern .compile ("\n (\\ s+)" );
245230 Matcher matcher = pattern .matcher (code );
@@ -250,4 +235,4 @@ private String determineCorrectIndentation(String code) {
250235
251236 return "" ;
252237 }
253- }
238+ }
0 commit comments