Skip to content

Commit

Permalink
Support for SUGGEST_IF_CONDITIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
jGauravGupta committed Sep 29, 2024
1 parent 6b55164 commit 2453ff8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 37 deletions.
41 changes: 41 additions & 0 deletions src/main/java/io/github/jeddict/ai/JavaToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 org.netbeans.api.java.lexer.JavaTokenId;

/**
*
* @author Gaurav Gupta
*/
public class JavaToken {

boolean javaContext;
JavaTokenId id;
int offset;

public JavaToken(boolean javaContext, JavaTokenId id, int offset) {
this.javaContext = javaContext;
this.id = id;
this.offset = offset;
}

public JavaToken(boolean javaContext) {
this.javaContext = javaContext;
}

public boolean isJavaContext() {
return javaContext;
}

public JavaTokenId getId() {
return id;
}

public int getOffset() {
return offset;
}

}
15 changes: 13 additions & 2 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,18 @@ 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 {
} else if (path.getLeaf().getKind() == Tree.Kind.PARENTHESIZED
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.IF) {
prompt = "You are an API server that suggests Java code to enhance an if-statement. "
+ "At the placeholder location ${SUGGEST_IF_CONDITIONS}, suggest additional conditional checks or actions within the if-statement. "
+ "Ensure that the suggestions are contextually appropriate for the condition. "
+ "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 If Statement 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. "
+ "Ensure that the suggestions are relevant to the context. "
Expand All @@ -470,7 +481,7 @@ public List<Snippet> suggestNextLineCode(String classDatas, String classContent,
List<Snippet> nextLines = parseJsonToSnippets(jsonResponse);
return nextLines;
}

public List<String> suggestJavaComment(String classDatas, String classContent, String lineText) {
String prompt = "You are an API server that suggests appropriate Java comments for a specific context in a given Java class at the placeholder location ${SUGGEST_JAVA_COMMENT}. "
+ "Based on the provided Java class content and the line of comment: \"" + lineText + " ${SUGGEST_JAVA_COMMENT} \", suggest relevant Java comment as appropriate for the context represented by the placeholder ${SUGGEST_JAVA_COMMENT} in the Java Class. "
Expand Down
49 changes: 14 additions & 35 deletions src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import org.netbeans.api.editor.completion.Completion;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import static org.netbeans.spi.editor.completion.CompletionProvider.COMPLETION_QUERY_TYPE;

@MimeRegistration(mimeType = "", service = CompletionProvider.class, position = 100)
Expand Down Expand Up @@ -344,9 +346,9 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse
Set<String> findReferencedClasses = findReferencedClasses(compilationUnit);
List<ClassData> classDatas = getClassData(fileObject, findReferencedClasses, activeClassContext);
String classDataContent = classDatas.stream()
// .peek(s -> System.out.println("Sending: " + s.getClassSignature()))
.map(cd -> cd.toString())
.collect(Collectors.joining("\n--------------------\n"));

if (path != null) {
System.out.println("path.getLeaf().getKind() " + path.getLeaf().getKind());
if (path.getParentPath() != null) {
Expand All @@ -358,6 +360,7 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse
}

Tree.Kind kind = path.getLeaf().getKind();
Tree.Kind parentKind = path.getParentPath().getLeaf().getKind();
if (path == null
|| kind == Tree.Kind.ERRONEOUS) {
String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}");
Expand Down Expand Up @@ -429,16 +432,20 @@ && trimLeadingSpaces(line).charAt(0) == '@') || kind == Tree.Kind.ANNOTATION) {
} else if (kind == Tree.Kind.STRING_LITERAL) {
String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_STRING_LITERAL_LIST}");
List<String> sugs = getJeddictChatModel(fileObject).suggestStringLiterals(classDataContent, updateddoc, line);
// for (String varName : sugs) {
// JeddictItem var = new JeddictItem(null, null, varName, Collections.emptyList(), caretOffset, true, false, -1);
// resultSet.addItem(var);
//
// }
for (String varName : sugs) {
resultSet.addItem(createItem(new Snippet(varName), line, lineTextBeforeCaret, javaToken, kind, doc));
}
} else if (kind == Tree.Kind.PARENTHESIZED
&& parentKind != null
&& parentKind == Tree.Kind.IF) {
String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_IF_CONDITIONS}");
List<Snippet> sugs = getJeddictChatModel(fileObject).suggestNextLineCode(classDataContent, updateddoc, line, path);
for (Snippet varName : sugs) {
JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), varName.getImports(), caretOffset, true, false, -1);
resultSet.addItem(var);
}
} else {
System.out.println(kind + " " + path.getLeaf().toString());
System.out.println("Skipped : " +kind + " " + path.getLeaf().toString());
}
} else if (COMPLETION_QUERY_TYPE == queryType && JAVA_MIME.equals(mimeType)) {
String line = getLineText(doc, caretOffset);
Expand Down Expand Up @@ -508,34 +515,6 @@ private static boolean isJavaIdentifierPart(String text, boolean allowForDor) {
}
}

static class JavaToken {
boolean javaContext;
JavaTokenId id;
int offset;

public JavaToken(boolean javaContext, JavaTokenId id, int offset) {
this.javaContext = javaContext;
this.id = id;
this.offset = offset;
}

public JavaToken(boolean javaContext) {
this.javaContext = javaContext;
}

public boolean isJavaContext() {
return javaContext;
}

public JavaTokenId getId() {
return id;
}

public int getOffset() {
return offset;
}

}
public static JavaToken isJavaContext(final Document doc, final int offset, final boolean allowInStrings) {
if (doc instanceof AbstractDocument) {
((AbstractDocument) doc).readLock();
Expand Down

0 comments on commit 2453ff8

Please sign in to comment.