diff --git a/src/main/java/io/github/jeddict/ai/JavaToken.java b/src/main/java/io/github/jeddict/ai/JavaToken.java new file mode 100644 index 0000000..5adaaf9 --- /dev/null +++ b/src/main/java/io/github/jeddict/ai/JavaToken.java @@ -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; + } + +} diff --git a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java index 0e0ada3..44c8f69 100644 --- a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java +++ b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java @@ -451,7 +451,18 @@ public List 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. " @@ -471,7 +482,7 @@ public List suggestNextLineCode(String classDatas, String classContent, List nextLines = parseJsonToSnippets(jsonResponse); return nextLines; } - + public List 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. " diff --git a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java b/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java index 3dd830b..af2de56 100644 --- a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java +++ b/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java @@ -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) @@ -344,9 +346,9 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse Set findReferencedClasses = findReferencedClasses(compilationUnit); List 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) { @@ -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}"); @@ -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 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 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); @@ -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();