diff --git a/src/main/java/io/github/jeddict/ai/AssistantTopComponent.java b/src/main/java/io/github/jeddict/ai/AssistantTopComponent.java deleted file mode 100644 index 59a45fa..0000000 --- a/src/main/java/io/github/jeddict/ai/AssistantTopComponent.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package io.github.jeddict.ai; - -import java.awt.BorderLayout; -import java.util.prefs.Preferences; -import javax.swing.JEditorPane; -import org.openide.windows.TopComponent; - -/** - * - * @author Shiwani Gupta - */ -public class AssistantTopComponent extends TopComponent { - - public static final String PREFERENCE_KEY = "AssistantTopComponentOpen"; - private final JEditorPane editorPane; - - public AssistantTopComponent(String name) { - setName(name); - setLayout(new BorderLayout()); - - editorPane = new JEditorPane(); - editorPane.setContentType("text/html"); - editorPane.setEditable(false); - add(editorPane, BorderLayout.CENTER); - } - - @Override - public void componentOpened() { - super.componentOpened(); - Preferences prefs = Preferences.userNodeForPackage(this.getClass()); - boolean shouldOpen = prefs.getBoolean(PREFERENCE_KEY, true); - if (!shouldOpen) { - this.close(); - } - } - - @Override - public void componentClosed() { - super.componentClosed(); - Preferences prefs = Preferences.userNodeForPackage(this.getClass()); - prefs.putBoolean(PREFERENCE_KEY, false); - } - - public JEditorPane getEditorPane() { - return editorPane; - } -} diff --git a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java index f49fdeb..7527913 100644 --- a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java +++ b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java @@ -716,10 +716,7 @@ public String enhanceExpressionStatement(String classContent, String parentConte } public String generateHtmlDescriptionForClass(String classContent) { - String prompt = "You are an API server that provides a detailed and interactive HTML-formatted description of a Java class. " - + "Given the following Java class content, generate an HTML document that includes a clear and engaging description of the class. " - + "Ensure that the HTML content is visually appealing by utilizing Bootstrap CSS for overall styling and use highlight.js for code examples in the response. " - + "Wrap the code blocks in
tags to preserve formatting and indentation. "
+ String prompt = "You are an API server that provides description of following class in HTML. "
+ "Do not include additional text or explanations outside of the HTML content.\n\n"
+ "Java Class Content:\n" + classContent;
@@ -728,10 +725,29 @@ public String generateHtmlDescriptionForClass(String classContent) {
System.out.println(answer);
return answer;
}
+
+ public String generateHtmlDescriptionForMethod(String methodContent) {
+ String prompt = "You are an API server that provides description of following Method in HTML. "
+ + "Do not include additional text or explanations outside of the HTML content.\n\n"
+ + "Java Method Content:\n" + methodContent;
- public String generateHtmlDescriptionForClass(String classContent, String previousChatResponse, String userQuery) {
- String prompt;
+ // Generate the HTML description
+ String answer = generate(prompt);
+ System.out.println(answer);
+ return answer;
+ }
+ public String generateHtmlDescription(String classContent, String methodContent, String previousChatResponse, String userQuery) {
+ String prompt;
+ String promptExtend;
+ if (methodContent != null) {
+ promptExtend = "Method Content:\n" + methodContent + "\n\n"
+ + "Do not return complete Java Class, return only Method and wrap it in . \n";
+ } else {
+ promptExtend = "Orignal Java Class Content:\n" + classContent + "\n\n"
+ + "If Full Java Class is in response then wrap it in . "
+ + "If partial snippet of Java Class are in response then wrap it in . ";
+ }
if (previousChatResponse == null) {
prompt = "You are an API server that provides an interactive HTML-formatted answer to a user's query based on Orignal Java class content. "
+ "Given the following Java class content, and the user's query, generate an HTML document that directly addresses the specific query. "
@@ -739,9 +755,7 @@ public String generateHtmlDescriptionForClass(String classContent, String previo
+ "Use Bootstrap CSS for overall styling and highlight.js for code examples in the response. "
+ "Wrap the code blocks in tags to preserve formatting and indentation. "
+ "Do not include additional text or explanations outside of the HTML content.\n\n"
- + "If Full Java Class is in response then wrap it in . "
- + "If partial snippet of Java Class are in response then wrap it in . "
- + "Orignal Java Class Content:\n" + classContent + "\n\n"
+ + promptExtend
+ "User Query:\n" + userQuery;
} else {
prompt = "You are an API server that provides an interactive HTML-formatted answer to a user's query based on Orignal Java class content and Previous Chat Content. "
@@ -750,10 +764,8 @@ public String generateHtmlDescriptionForClass(String classContent, String previo
+ "Use Bootstrap CSS for overall styling and highlight.js for code examples in the response. "
+ "Wrap the code blocks in tags to preserve formatting and indentation. "
+ "Do not include additional text or explanations outside of the HTML content.\n\n"
- + "If Full Java Class is in response then wrap it in . "
- + "If partial snippet of Java Class are in response then wrap it in . "
- + "Orignal Java Class Content:\n" + classContent + "\n\n"
+ "Previous Chat Response:\n" + previousChatResponse + "\n\n"
+ + promptExtend
+ "User Query:\n" + userQuery;
}
diff --git a/src/main/java/io/github/jeddict/ai/Action.java b/src/main/java/io/github/jeddict/ai/completion/Action.java
similarity index 95%
rename from src/main/java/io/github/jeddict/ai/Action.java
rename to src/main/java/io/github/jeddict/ai/completion/Action.java
index fc561b9..8470b61 100644
--- a/src/main/java/io/github/jeddict/ai/Action.java
+++ b/src/main/java/io/github/jeddict/ai/completion/Action.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package io.github.jeddict.ai;
+package io.github.jeddict.ai.completion;
public enum Action {
diff --git a/src/main/java/io/github/jeddict/ai/JavaToken.java b/src/main/java/io/github/jeddict/ai/completion/JavaToken.java
similarity index 95%
rename from src/main/java/io/github/jeddict/ai/JavaToken.java
rename to src/main/java/io/github/jeddict/ai/completion/JavaToken.java
index 5adaaf9..f258718 100644
--- a/src/main/java/io/github/jeddict/ai/JavaToken.java
+++ b/src/main/java/io/github/jeddict/ai/completion/JavaToken.java
@@ -2,7 +2,7 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai;
+package io.github.jeddict.ai.completion;
import org.netbeans.api.java.lexer.JavaTokenId;
diff --git a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java b/src/main/java/io/github/jeddict/ai/completion/JeddictCompletionProvider.java
similarity index 99%
rename from src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java
rename to src/main/java/io/github/jeddict/ai/completion/JeddictCompletionProvider.java
index 95fed2f..cb4a888 100644
--- a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java
+++ b/src/main/java/io/github/jeddict/ai/completion/JeddictCompletionProvider.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package io.github.jeddict.ai;
+package io.github.jeddict.ai.completion;
import io.github.jeddict.ai.scanner.MyTreePathScanner;
import com.sun.source.tree.ClassTree;
@@ -48,6 +48,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.DocTrees;
+import io.github.jeddict.ai.Snippet;
import static io.github.jeddict.ai.scanner.ProjectClassScanner.getFileObjectFromEditor;
import io.github.jeddict.ai.scanner.ClassData;
import java.util.stream.Collectors;
diff --git a/src/main/java/io/github/jeddict/ai/JeddictItem.java b/src/main/java/io/github/jeddict/ai/completion/JeddictItem.java
similarity index 98%
rename from src/main/java/io/github/jeddict/ai/JeddictItem.java
rename to src/main/java/io/github/jeddict/ai/completion/JeddictItem.java
index 88e53ef..1906666 100644
--- a/src/main/java/io/github/jeddict/ai/JeddictItem.java
+++ b/src/main/java/io/github/jeddict/ai/completion/JeddictItem.java
@@ -2,9 +2,10 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai;
+package io.github.jeddict.ai.completion;
-import static io.github.jeddict.ai.Utilities.getHTMLColor;
+import io.github.jeddict.ai.util.Utilities;
+import static io.github.jeddict.ai.util.Utilities.getHTMLColor;
import io.github.jeddict.ai.util.SourceUtil;
import java.net.URL;
import java.util.List;
diff --git a/src/main/java/io/github/jeddict/ai/components/AssistantTopComponent.java b/src/main/java/io/github/jeddict/ai/components/AssistantTopComponent.java
new file mode 100644
index 0000000..b8564dd
--- /dev/null
+++ b/src/main/java/io/github/jeddict/ai/components/AssistantTopComponent.java
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package io.github.jeddict.ai.components;
+
+import java.awt.BorderLayout;
+import java.awt.Desktop;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.prefs.Preferences;
+import javax.swing.BoxLayout;
+import javax.swing.JEditorPane;
+import javax.swing.JPanel;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkListener;
+import javax.swing.text.EditorKit;
+import javax.swing.text.html.HTMLEditorKit;
+import javax.swing.text.html.StyleSheet;
+import org.netbeans.api.editor.mimelookup.MimeLookup;
+import org.netbeans.api.editor.mimelookup.MimePath;
+import org.openide.windows.TopComponent;
+
+/**
+ *
+ * @author Shiwani Gupta
+ */
+public class AssistantTopComponent extends TopComponent {
+
+ public static final String PREFERENCE_KEY = "AssistantTopComponentOpen";
+ private final JPanel parentPanel;
+ private HTMLEditorKit editorKit;
+
+ public AssistantTopComponent(String name) {
+ setName(name);
+ setLayout(new BorderLayout());
+
+ parentPanel = new JPanel();
+ parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.Y_AXIS));
+
+ add(parentPanel, BorderLayout.CENTER);
+ }
+
+ public void clear() {
+ parentPanel.removeAll();
+ }
+
+ public JEditorPane createHtmlPane(String content) {
+ JEditorPane editorPane = new JEditorPane();
+ editorPane.setContentType("text/html");
+ editorPane.setEditorKit(getHTMLEditorKit());
+ editorPane.addHyperlinkListener(e -> {
+ if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
+ try {
+ Desktop.getDesktop().browse(e.getURL().toURI());
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ });
+ editorPane.setEditable(false);
+ editorPane.setText(content);
+ parentPanel.add(editorPane);
+ return editorPane;
+ }
+
+ public JEditorPane createCodePane(String content) {
+ JEditorPane editorPane = new JEditorPane();
+ editorPane.setEditorKit(createEditorKit("text/x-java"));
+ editorPane.setEditable(false);
+ editorPane.setText(content);
+ parentPanel.add(editorPane);
+ return editorPane;
+ }
+
+ public static EditorKit createEditorKit(String mimeType) {
+ return MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(EditorKit.class);
+ }
+
+ @Override
+ public void componentOpened() {
+ super.componentOpened();
+ Preferences prefs = Preferences.userNodeForPackage(this.getClass());
+ boolean shouldOpen = prefs.getBoolean(PREFERENCE_KEY, true);
+ if (!shouldOpen) {
+ this.close();
+ }
+ }
+
+ @Override
+ public void componentClosed() {
+ super.componentClosed();
+ Preferences prefs = Preferences.userNodeForPackage(this.getClass());
+ prefs.putBoolean(PREFERENCE_KEY, false);
+ }
+
+ public JPanel getParentPanel() {
+ return parentPanel;
+ }
+
+ private HTMLEditorKit getHTMLEditorKit() {
+ if (editorKit != null) {
+ return editorKit;
+ }
+ editorKit = new HTMLEditorKit();
+ StyleSheet styleSheet = editorKit.getStyleSheet();
+ styleSheet.addRule("html { font-family: sans-serif; line-height: 1.15; -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); }");
+ styleSheet.addRule("article, aside, figcaption, figure, footer, header, hgroup, main, nav, section { display: block; }");
+ styleSheet.addRule("body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; font-size: 1rem; font-weight: 400; line-height: 1.5; color: #212529; text-align: left; background-color: #fff; }");
+ styleSheet.addRule("hr { box-sizing: content-box; height: 0; overflow: visible; }");
+ styleSheet.addRule("h1, h2, h3, h4, h5, h6 { margin-top: 0; margin-bottom: 0.5rem; }");
+ styleSheet.addRule("p { margin-top: 0; margin-bottom: 1rem; }");
+ styleSheet.addRule("abbr[title], abbr[data-original-title] { text-decoration: underline; -webkit-text-decoration: underline dotted; text-decoration: underline dotted; cursor: help; border-bottom: 0; -webkit-text-decoration-skip-ink: none; text-decoration-skip-ink: none; }");
+ styleSheet.addRule("address { margin-bottom: 1rem; font-style: normal; line-height: inherit; }");
+ styleSheet.addRule("ol, ul, dl { margin-top: 0; margin-bottom: 1rem; }");
+ styleSheet.addRule("ol ol, ul ul, ol ul, ul ol { margin-bottom: 0; }");
+ styleSheet.addRule("dt { font-weight: 700; }");
+ styleSheet.addRule("dd { margin-bottom: .5rem; margin-left: 0; }");
+ styleSheet.addRule("blockquote { margin: 0 0 1rem; }");
+ styleSheet.addRule("b, strong { font-weight: bolder; }");
+ styleSheet.addRule("small { font-size: 80%; }");
+ styleSheet.addRule("sub, sup { position: relative; font-size: 75%; line-height: 0; vertical-align: baseline; }");
+ styleSheet.addRule("sub { bottom: -.25em; }");
+ styleSheet.addRule("sup { top: -.5em; }");
+ styleSheet.addRule("a { color: #007bff; text-decoration: none; background-color: transparent; }");
+ styleSheet.addRule("a:hover { color: #0056b3; text-decoration: underline; }");
+ styleSheet.addRule("a:not([href]) { color: inherit; text-decoration: none; }");
+ styleSheet.addRule("a:not([href]):hover { color: inherit; text-decoration: none; }");
+ styleSheet.addRule("pre, code, kbd, samp { font-family: SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; font-size: 1em; }");
+ styleSheet.addRule("pre { margin-top: 0; margin-bottom: 1rem; overflow: auto; }");
+ styleSheet.addRule("figure { margin: 0 0 1rem; }");
+ styleSheet.addRule("img { vertical-align: middle; border-style: none; }");
+ styleSheet.addRule("svg { overflow: hidden; vertical-align: middle; }");
+ styleSheet.addRule("table { border-collapse: collapse; }");
+ styleSheet.addRule("caption { padding-top: 0.75rem; padding-bottom: 0.75rem; color: #6c757d; text-align: left; caption-side: bottom; }");
+ styleSheet.addRule("th { text-align: inherit; }");
+ styleSheet.addRule("label { display: inline-block; margin-bottom: 0.5rem; }");
+ styleSheet.addRule("button { border-radius: 0; }");
+ styleSheet.addRule("button:focus { outline: 1px dotted; outline: 5px auto -webkit-focus-ring-color; }");
+ styleSheet.addRule("input, button, select, optgroup, textarea { margin: 0; font-family: inherit; font-size: inherit; line-height: inherit; }");
+ styleSheet.addRule("button, input { overflow: visible; }");
+ styleSheet.addRule("button, select { text-transform: none; }");
+ styleSheet.addRule("select { word-wrap: normal; }");
+ styleSheet.addRule("button, [type='button'], [type='reset'], [type='submit'] { -webkit-appearance: button; }");
+ styleSheet.addRule("button:not(:disabled), [type='button']:not(:disabled), [type='reset']:not(:disabled), [type='submit']:not(:disabled) { cursor: pointer; }");
+ styleSheet.addRule("button::-moz-focus-inner, [type='button']::-moz-focus-inner, [type='reset']::-moz-focus-inner, [type='submit']::-moz-focus-inner { padding: 0; border-style: none; }");
+ styleSheet.addRule("input[type='radio'], input[type='checkbox'] { box-sizing: border-box; padding: 0; }");
+ styleSheet.addRule("input[type='date'], input[type='time'], input[type='datetime-local'], input[type='month'] { -webkit-appearance: listbox; }");
+ styleSheet.addRule("textarea { overflow: auto; resize: vertical; }");
+ styleSheet.addRule("fieldset { min-width: 0; padding: 0; margin: 0; border: 0; }");
+ styleSheet.addRule("legend { display: block; width: 100%; max-width: 100%; padding: 0; margin-bottom: .5rem; font-size: 1.5rem; line-height: inherit; color: inherit; white-space: normal; }");
+ styleSheet.addRule("progress { vertical-align: baseline; }");
+ styleSheet.addRule("[type='number']::-webkit-inner-spin-button, [type='number']::-webkit-outer-spin-button { height: auto; }");
+ styleSheet.addRule("[type='search'] { outline-offset: -2px; -webkit-appearance: none; }");
+ styleSheet.addRule("[type='search']::-webkit-search-decoration { -webkit-appearance: none; }");
+ styleSheet.addRule("::-webkit-file-upload-button { font: inherit; -webkit-appearance: button; }");
+ styleSheet.addRule("output { display: inline-block; }");
+ styleSheet.addRule("summary { display: list-item; cursor: pointer; }");
+ styleSheet.addRule("template { display: none; }");
+ styleSheet.addRule("[hidden] { display: none !important; }");
+ styleSheet.addRule("h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 { margin-bottom: 0.5rem; font-weight: 500; line-height: 1.2; }");
+ styleSheet.addRule("h1, .h1 { font-size: 2.5rem; }");
+ styleSheet.addRule("h2, .h2 { font-size: 2rem; }");
+ styleSheet.addRule("h3, .h3 { font-size: 1.75rem; }");
+ styleSheet.addRule("h4, .h4 { font-size: 1.5rem; }");
+ styleSheet.addRule("h5, .h5 { font-size: 1.25rem; }");
+ styleSheet.addRule("h6, .h6 { font-size: 1rem; }");
+ styleSheet.addRule(".lead { font-size: 1.25rem; font-weight: 300; }");
+ styleSheet.addRule(".display-1 { font-size: 6rem; font-weight: 300; line-height: 1.2; }");
+ styleSheet.addRule(".display-2 { font-size: 5.5rem; font-weight: 300; line-height: 1.2; }");
+ styleSheet.addRule(".display-3 { font-size: 4.5rem; font-weight: 300; line-height: 1.2; }");
+ styleSheet.addRule(".display-4 { font-size: 3.5rem; font-weight: 300; line-height: 1.2; }");
+ styleSheet.addRule("hr { margin-top: 1rem; margin-bottom: 1rem; border: 0; border-top: 1px solid rgba(0, 0, 0, 0.1); }");
+ styleSheet.addRule("pre, code, kbd, samp { font-family: SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; font-size: 1em; }");
+ styleSheet.addRule("pre { margin-top: 0; margin-bottom: 1rem; overflow: auto; }");
+ styleSheet.addRule("code { font-size: 87.5%; color: #e83e8c; word-wrap: break-word; }");
+ styleSheet.addRule("pre { display: block; font-size: 87.5%; color: #212529; }");
+ styleSheet.addRule("pre code { font-size: inherit; color: inherit; word-break: normal; }");
+ return editorKit;
+ }
+
+}
diff --git a/src/main/java/io/github/jeddict/ai/fix/ExpressionFix.java b/src/main/java/io/github/jeddict/ai/hints/ExpressionFix.java
similarity index 97%
rename from src/main/java/io/github/jeddict/ai/fix/ExpressionFix.java
rename to src/main/java/io/github/jeddict/ai/hints/ExpressionFix.java
index 69cb635..fca7553 100644
--- a/src/main/java/io/github/jeddict/ai/fix/ExpressionFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/ExpressionFix.java
@@ -2,7 +2,7 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
@@ -13,7 +13,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import io.github.jeddict.ai.util.StringUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
diff --git a/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java b/src/main/java/io/github/jeddict/ai/hints/JavaDocFixImpl.java
similarity index 98%
rename from src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java
rename to src/main/java/io/github/jeddict/ai/hints/JavaDocFixImpl.java
index 80cfd5e..3d7d166 100644
--- a/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java
+++ b/src/main/java/io/github/jeddict/ai/hints/JavaDocFixImpl.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import io.github.jeddict.ai.util.StringUtil;
import com.sun.source.doctree.DocCommentTree;
@@ -31,9 +31,9 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.DocTrees;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
-import static io.github.jeddict.ai.Action.ENHANCE;
+import static io.github.jeddict.ai.completion.Action.ENHANCE;
import static io.github.jeddict.ai.util.SourceUtil.geIndentaion;
import static io.github.jeddict.ai.util.StringUtil.removeCodeBlockMarkers;
import static io.github.jeddict.ai.util.StringUtil.trimLeadingSpaces;
diff --git a/src/main/java/io/github/jeddict/ai/JeddictHint.java b/src/main/java/io/github/jeddict/ai/hints/JeddictHint.java
similarity index 94%
rename from src/main/java/io/github/jeddict/ai/JeddictHint.java
rename to src/main/java/io/github/jeddict/ai/hints/JeddictHint.java
index 6371fec..497e69e 100644
--- a/src/main/java/io/github/jeddict/ai/JeddictHint.java
+++ b/src/main/java/io/github/jeddict/ai/hints/JeddictHint.java
@@ -16,15 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
-package io.github.jeddict.ai;
-
-import io.github.jeddict.ai.fix.JavaDocFixImpl;
-import io.github.jeddict.ai.fix.ExpressionFix;
-import io.github.jeddict.ai.fix.RestEndpointFix;
-import io.github.jeddict.ai.fix.MethodFix;
-import io.github.jeddict.ai.fix.TextFix;
-import io.github.jeddict.ai.fix.VariableFix;
-import io.github.jeddict.ai.fix.VariableNameFix;
+package io.github.jeddict.ai.hints;
+
+import io.github.jeddict.ai.hints.JavaDocFixImpl;
+import io.github.jeddict.ai.hints.ExpressionFix;
+import io.github.jeddict.ai.hints.RestEndpointFix;
+import io.github.jeddict.ai.hints.MethodFix;
+import io.github.jeddict.ai.hints.TextFix;
+import io.github.jeddict.ai.hints.VariableFix;
+import io.github.jeddict.ai.hints.VariableNameFix;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.CLASS;
@@ -38,7 +38,8 @@
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
-import io.github.jeddict.ai.fix.LearnFix;
+import io.github.jeddict.ai.completion.Action;
+import io.github.jeddict.ai.hints.LearnFix;
import io.github.jeddict.ai.settings.PreferencesManager;
import java.util.Locale;
import java.util.logging.Logger;
@@ -164,8 +165,8 @@ public static ErrorDescription run(HintContext ctx) {
}
}
}
-// fixes[i++] = new LearnFix(tpHandle, Action.LEARN, treePath).toEditorFix();
-// fixes[i++] = new LearnFix(tpHandle, Action.QUERY, treePath).toEditorFix();
+ fixes[i++] = new LearnFix(tpHandle, Action.LEARN, treePath).toEditorFix();
+ fixes[i++] = new LearnFix(tpHandle, Action.QUERY, treePath).toEditorFix();
}
break;
diff --git a/src/main/java/io/github/jeddict/ai/fix/LearnFix.java b/src/main/java/io/github/jeddict/ai/hints/LearnFix.java
similarity index 74%
rename from src/main/java/io/github/jeddict/ai/fix/LearnFix.java
rename to src/main/java/io/github/jeddict/ai/hints/LearnFix.java
index e3f5e56..449c30b 100644
--- a/src/main/java/io/github/jeddict/ai/fix/LearnFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/LearnFix.java
@@ -1,17 +1,34 @@
/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.CLASS;
+import static com.sun.source.tree.Tree.Kind.ENUM;
import static com.sun.source.tree.Tree.Kind.INTERFACE;
+import static com.sun.source.tree.Tree.Kind.METHOD;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
-import io.github.jeddict.ai.AssistantTopComponent;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
+import io.github.jeddict.ai.components.AssistantTopComponent;
import io.github.jeddict.ai.util.StringUtil;
import static io.github.jeddict.ai.util.StringUtil.removeCodeBlockMarkers;
import static io.github.jeddict.ai.util.UIUtil.askQueryAboutClass;
@@ -32,8 +49,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
@@ -82,20 +97,33 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
}
Tree leaf = tc.getPath().getLeaf();
- if (leaf.getKind() == CLASS || leaf.getKind() == INTERFACE) {
+ if (leaf.getKind() == CLASS
+ || leaf.getKind() == INTERFACE
+ || leaf.getKind() == ENUM
+ || leaf.getKind() == METHOD) {
String response = null;
if (action == Action.QUERY) {
String query = askQueryAboutClass();
if (query == null) {
return;
}
- response = new JeddictChatModel().generateHtmlDescriptionForClass(treePath.getCompilationUnit().toString(), null, query);
+ response = new JeddictChatModel().generateHtmlDescription(treePath.getCompilationUnit().toString(), null, null, query);
+ } else {
+ if (leaf instanceof MethodTree) {
+ response = new JeddictChatModel().generateHtmlDescriptionForMethod(leaf.toString());
+ } else {
+ response = new JeddictChatModel().generateHtmlDescriptionForClass(treePath.getCompilationUnit().toString());
+ }
+ }
+ String name;
+ if (leaf instanceof MethodTree) {
+ name = ((MethodTree) leaf).getName().toString();
} else {
- response = new JeddictChatModel().generateHtmlDescriptionForClass(treePath.getCompilationUnit().toString());
+ name = ((ClassTree) leaf).getSimpleName().toString();
}
- displayHtmlContent(copy, tc.getPath(), removeCodeBlockMarkers(response), ((ClassTree) leaf).getSimpleName().toString());
+ displayHtmlContent(copy, tc.getPath(), removeCodeBlockMarkers(response), name);
}
}
@@ -105,20 +133,19 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
private int currentResponseIndex = -1;
String javaCode = null;
- private void displayHtmlContent(WorkingCopy copy, TreePath tp, String htmlContent, String title) {
+ private void displayHtmlContent(WorkingCopy copy, TreePath tp, final String response, String title) {
SwingUtilities.invokeLater(() -> {
try {
File tempFile = File.createTempFile("tempHtml", ".html");
tempFile.deleteOnExit();
try (FileWriter writer = new FileWriter(tempFile)) {
- writer.write(htmlContent);
+ writer.write(response);
}
Preferences prefs = Preferences.userNodeForPackage(AssistantTopComponent.class);
prefs.putBoolean(AssistantTopComponent.PREFERENCE_KEY, true);
topComponent = new AssistantTopComponent(title + " AI Assistance");
- topComponent.getEditorPane().setPage(tempFile.toURI().toURL());
- topComponent.getEditorPane().addHyperlinkListener((HyperlinkEvent e) -> {
+ updateEditor(response).addHyperlinkListener((HyperlinkEvent e) -> {
if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
@@ -128,7 +155,7 @@ private void displayHtmlContent(WorkingCopy copy, TreePath tp, String htmlConten
}
});
- JScrollPane scrollPane = new JScrollPane(topComponent.getEditorPane());
+ JScrollPane scrollPane = new JScrollPane(topComponent.getParentPanel());
topComponent.add(scrollPane, BorderLayout.CENTER);
// Create a panel for the text field and buttons
@@ -213,27 +240,29 @@ private void displayHtmlContent(WorkingCopy copy, TreePath tp, String htmlConten
if (!question.isEmpty()) {
submitButton.setText("Loading...");
submitButton.setEnabled(false);
- handleQuestion(question, topComponent.getEditorPane(), submitButton);
+ handleQuestion(question, submitButton);
}
};
submitButton.addActionListener(submitActionListener);
questionField.addActionListener(submitActionListener);
- responseHistory.add(htmlContent);
+ responseHistory.add(response);
currentResponseIndex = responseHistory.size() - 1;
prevButton.addActionListener(e -> {
if (currentResponseIndex > 0) {
currentResponseIndex--;
- topComponent.getEditorPane().setText(responseHistory.get(currentResponseIndex));
+ String historyResponse = responseHistory.get(currentResponseIndex);
+ updateEditor(historyResponse);
updateNavigationButtons(prevButton, nextButton);
}
});
nextButton.addActionListener(e -> {
if (currentResponseIndex < responseHistory.size() - 1) {
currentResponseIndex++;
- topComponent.getEditorPane().setText(responseHistory.get(currentResponseIndex));
+ String historyResponse = responseHistory.get(currentResponseIndex);
+ updateEditor(historyResponse);
updateNavigationButtons(prevButton, nextButton);
}
});
@@ -255,7 +284,26 @@ private void displayHtmlContent(WorkingCopy copy, TreePath tp, String htmlConten
});
}
- private void handleQuestion(String question, JEditorPane editorPane, JButton submitButton) {
+ private JEditorPane updateEditor(String response) {
+ JEditorPane editorPane = null;
+ String[] parts = response.split("||||
");
+// String[] parts = response.split("|||
");
+ topComponent.clear();
+ for (int i = 0; i < parts.length; i++) {
+ if (i % 2 == 1) {
+ editorPane = topComponent.createCodePane(parts[i]);
+ javaCode = parts[i];
+ } else {
+ editorPane = topComponent.createHtmlPane(parts[i]);
+ }
+ }
+ if (editorPane != null) {
+ editorPane.setCaretPosition(editorPane.getDocument().getLength());
+ }
+ return editorPane;
+ }
+
+ private void handleQuestion(String question, JButton submitButton) {
SwingUtilities.invokeLater(() -> {
try {
String prevChat = responseHistory.get(responseHistory.size() - 1);
@@ -264,22 +312,14 @@ private void handleQuestion(String question, JEditorPane editorPane, JButton sub
prevChat = null;
}
}
- String response = new JeddictChatModel().generateHtmlDescriptionForClass(treePath.getCompilationUnit().toString(), prevChat, question);
+
+ String response = new JeddictChatModel().generateHtmlDescription(treePath.getCompilationUnit().toString(), treePath.getLeaf() instanceof MethodTree ? treePath.getLeaf().toString() : null, prevChat, question);
response = removeCodeBlockMarkers(response);
if (responseHistory.isEmpty() || !response.equals(responseHistory.get(responseHistory.size() - 1))) {
responseHistory.add(response);
currentResponseIndex = responseHistory.size() - 1;
}
-
- Pattern pattern = Pattern.compile("]*type=\"full\"[^>]*class=\"java\"[^>]*>(.*?)
", Pattern.DOTALL);
- Matcher matcher = pattern.matcher(response);
- if (matcher.find()) {
- javaCode = matcher.group(1);
- } else {
- javaCode = null;
- }
- editorPane.setText(response);
- editorPane.setCaretPosition(editorPane.getDocument().getLength()); // Scroll to the bottom
+ updateEditor(response);
submitButton.setText("Ask");
submitButton.setEnabled(true);
saveButton.setEnabled(javaCode != null);
diff --git a/src/main/java/io/github/jeddict/ai/fix/MethodFix.java b/src/main/java/io/github/jeddict/ai/hints/MethodFix.java
similarity index 98%
rename from src/main/java/io/github/jeddict/ai/fix/MethodFix.java
rename to src/main/java/io/github/jeddict/ai/hints/MethodFix.java
index c2e3fea..9b02bd6 100644
--- a/src/main/java/io/github/jeddict/ai/fix/MethodFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/MethodFix.java
@@ -2,14 +2,14 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.METHOD;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import io.github.jeddict.ai.util.SourceUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
diff --git a/src/main/java/io/github/jeddict/ai/fix/RestEndpointFix.java b/src/main/java/io/github/jeddict/ai/hints/RestEndpointFix.java
similarity index 97%
rename from src/main/java/io/github/jeddict/ai/fix/RestEndpointFix.java
rename to src/main/java/io/github/jeddict/ai/hints/RestEndpointFix.java
index fd8dbfd..53d1617 100644
--- a/src/main/java/io/github/jeddict/ai/fix/RestEndpointFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/RestEndpointFix.java
@@ -2,14 +2,14 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.CLASS;
import static com.sun.source.tree.Tree.Kind.INTERFACE;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import io.github.jeddict.ai.util.SourceUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
diff --git a/src/main/java/io/github/jeddict/ai/fix/TextFix.java b/src/main/java/io/github/jeddict/ai/hints/TextFix.java
similarity index 96%
rename from src/main/java/io/github/jeddict/ai/fix/TextFix.java
rename to src/main/java/io/github/jeddict/ai/hints/TextFix.java
index d4b08e7..5ed893e 100644
--- a/src/main/java/io/github/jeddict/ai/fix/TextFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/TextFix.java
@@ -2,13 +2,13 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
import org.netbeans.api.java.source.JavaSource;
diff --git a/src/main/java/io/github/jeddict/ai/fix/VariableFix.java b/src/main/java/io/github/jeddict/ai/hints/VariableFix.java
similarity index 97%
rename from src/main/java/io/github/jeddict/ai/fix/VariableFix.java
rename to src/main/java/io/github/jeddict/ai/hints/VariableFix.java
index 95d7a0c..482001e 100644
--- a/src/main/java/io/github/jeddict/ai/fix/VariableFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/VariableFix.java
@@ -2,14 +2,14 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import io.github.jeddict.ai.util.SourceUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
diff --git a/src/main/java/io/github/jeddict/ai/fix/VariableNameFix.java b/src/main/java/io/github/jeddict/ai/hints/VariableNameFix.java
similarity index 99%
rename from src/main/java/io/github/jeddict/ai/fix/VariableNameFix.java
rename to src/main/java/io/github/jeddict/ai/hints/VariableNameFix.java
index ea6b3f0..9ac8fea 100644
--- a/src/main/java/io/github/jeddict/ai/fix/VariableNameFix.java
+++ b/src/main/java/io/github/jeddict/ai/hints/VariableNameFix.java
@@ -2,7 +2,7 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
-package io.github.jeddict.ai.fix;
+package io.github.jeddict.ai.hints;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
@@ -14,7 +14,7 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreeScanner;
-import io.github.jeddict.ai.Action;
+import io.github.jeddict.ai.completion.Action;
import io.github.jeddict.ai.JeddictChatModel;
import java.util.List;
import java.util.stream.Collectors;
diff --git a/src/main/java/io/github/jeddict/ai/Utilities.java b/src/main/java/io/github/jeddict/ai/util/Utilities.java
similarity index 62%
rename from src/main/java/io/github/jeddict/ai/Utilities.java
rename to src/main/java/io/github/jeddict/ai/util/Utilities.java
index a5d0f1d..36013e1 100644
--- a/src/main/java/io/github/jeddict/ai/Utilities.java
+++ b/src/main/java/io/github/jeddict/ai/util/Utilities.java
@@ -1,8 +1,22 @@
/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
*/
-package io.github.jeddict.ai;
+package io.github.jeddict.ai.util;
import java.awt.Color;
import java.util.EnumSet;