Skip to content

Commit

Permalink
Merge pull request #25 from jeddict/dev
Browse files Browse the repository at this point in the history
Java Editor Kit in Chat Window
  • Loading branch information
jShiwaniGupta authored Oct 19, 2024
2 parents 5ae1f00 + e8efda0 commit b7da35d
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 130 deletions.
51 changes: 0 additions & 51 deletions src/main/java/io/github/jeddict/ai/AssistantTopComponent.java

This file was deleted.

36 changes: 24 additions & 12 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pre> 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;

Expand All @@ -728,20 +725,37 @@ 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 <code type=\"full\" class=\"java\">. \n";
} else {
promptExtend = "Orignal Java Class Content:\n" + classContent + "\n\n"
+ "If Full Java Class is in response then wrap it in <code type=\"full\" class=\"java\">. "
+ "If partial snippet of Java Class are in response then wrap it in <code type=\"snippet\" class=\"java\">. ";
}
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. "
+ "Ensure the HTML content is well-structured, clearly answers the query. "
+ "Use Bootstrap CSS for overall styling and highlight.js for code examples in the response. "
+ "Wrap the code blocks in <pre> 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 <code type=\"full\" class=\"java\">. "
+ "If partial snippet of Java Class are in response then wrap it in <code type=\"snippet\" class=\"java\">. "
+ "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. "
Expand All @@ -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 <pre> 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 <code type=\"full\" class=\"java\">. "
+ "If partial snippet of Java Class are in response then wrap it in <code type=\"snippet\" class=\"java\">. "
+ "Orignal Java Class Content:\n" + classContent + "\n\n"
+ "Previous Chat Response:\n" + previousChatResponse + "\n\n"
+ promptExtend
+ "User Query:\n" + userQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading

0 comments on commit b7da35d

Please sign in to comment.