Skip to content

Commit

Permalink
Merge pull request #6 from jeddict/dev
Browse files Browse the repository at this point in the history
Code generation improvement with strict check and Release v1.7
  • Loading branch information
jShiwaniGupta authored Sep 26, 2024
2 parents 68082ea + 260a9f6 commit f83a085
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 116 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jeddict</groupId>
<artifactId>jeddict-ai</artifactId>
<version>1.6</version>
<version>1.7</version>
<packaging>nbm</packaging>
<name>Jeddict AI Assistant</name>
<description>Jeddict AI Assistant is a powerful and intuitive plugin designed for Apache NetBeans IDE.
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,19 @@ public List<Snippet> suggestNextLineCode(String classDatas, String classContent,
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
} else if (path.getLeaf().getKind() == Tree.Kind.MODIFIERS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.METHOD) {
prompt = "You are an API server that suggests Java code modifications for a method. "
+ "At the placeholder location ${SUGGEST_CODE_LIST}, suggest method-level modifiers such as 'public', 'protected', 'private', 'abstract', 'static', 'final', 'synchronized', or relevant method-level annotations. "
+ "Additionally, you may suggest method-specific annotations like '@Override', '@Deprecated', '@Transactional', etc. "
+ "Ensure that the suggestions are appropriate for the method context provided. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Method Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.CLASS) {
prompt = "You are an API server that suggests Java code for an inner class at the placeholder location ${SUGGEST_CODE_LIST}. "
Expand All @@ -428,6 +440,17 @@ public List<Snippet> suggestNextLineCode(String classDatas, String classContent,
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
prompt = "You are an API server that suggests Java code for an class at the placeholder location ${SUGGEST_CODE_LIST}. "
+ "Based on the provided Java class content, suggest either relevant class level members, attributes, constants, methods or blocks. "
+ "Ensure that the suggestions are contextually appropriate for an class. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else {
prompt = "You are an API server that suggests Java code for a specific context in a given Java class at the placeholder location ${SUGGEST_CODE_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest a relevant single line of code or a multi-line code block as appropriate for the context represented by the placeholder ${SUGGEST_CODE_LIST} in the Java class. "
Expand Down Expand Up @@ -478,18 +501,21 @@ public List<String> suggestJavadocOrComment(String classDatas, String classConte
return comments;
}

public List<String> suggestAnnotations(String classDatas, String classContent, String lineText) {
String prompt = "You are an API server that suggests Java annotations for a specific context in a given Java class at the placeholder location ${SUGGEST_ANNOTATION_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest relevant annotations that can be applied at the placeholder location represented by ${SUGGEST_ANNOTATION_LIST} in the Java Class. "
+ "Return a JSON array where each element is a single annotation formatted as a string. "
+ "Ensure that the suggestions are appropriate for the given Java Class Content:\n\n" + classContent;
public List<Snippet> suggestAnnotations(String classDatas, String classContent, String lineText) {
String prompt = "You are an API server that suggests Java annotations for a specific context in a given Java class at the placeholder location ${SUGGEST_ANNOTATION_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest relevant annotations that can be applied at the placeholder location represented by ${SUGGEST_ANNOTATION_LIST} in the Java Class. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. "
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Ensure that the suggestions are appropriate for the given Java Class Content:\n\n" + classContent;

// Generate the list of suggested annotations
String jsonResponse = generate(prompt);
System.out.println("jsonResponse " + jsonResponse);

// Parse the JSON response into a List
List<String> annotations = parseJsonToList(jsonResponse);
List<Snippet> annotations = parseJsonToSnippets(jsonResponse);
return annotations;
}

Expand Down Expand Up @@ -554,6 +580,9 @@ private List<String> parseJsonToList(String json) {

private List<String> parseJsonToListWithSplit(String json) {
List<String> variableNames = new ArrayList<>();
if(json == null || json.isEmpty()){
return variableNames;
}

json = removeCodeBlockMarkers(json).trim();
String newjson = json;
Expand Down
Loading

0 comments on commit f83a085

Please sign in to comment.