From 117172e1446316f0a223525c9ba785d6e4b8f677 Mon Sep 17 00:00:00 2001 From: Shiwani Gupta Date: Fri, 27 Sep 2024 06:44:49 +0530 Subject: [PATCH] Revert "Code generation improvement with strict check" --- pom.xml | 2 +- ...vider.java => JavaCompletionProvider.java} | 257 ++++++++---------- .../github/jeddict/ai/JeddictChatModel.java | 43 +-- .../java/io/github/jeddict/ai/Snippet.java | 4 - .../github/jeddict/ai/fix/JavaDocFixImpl.java | 3 +- 5 files changed, 116 insertions(+), 193 deletions(-) rename src/main/java/io/github/jeddict/ai/{JeddictCompletionProvider.java => JavaCompletionProvider.java} (73%) diff --git a/pom.xml b/pom.xml index 3ce4143..22f0753 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.jeddict jeddict-ai - 1.7 + 1.6 nbm Jeddict AI Assistant Jeddict AI Assistant is a powerful and intuitive plugin designed for Apache NetBeans IDE. diff --git a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java b/src/main/java/io/github/jeddict/ai/JavaCompletionProvider.java similarity index 73% rename from src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java rename to src/main/java/io/github/jeddict/ai/JavaCompletionProvider.java index 3dd830b..9332387 100644 --- a/src/main/java/io/github/jeddict/ai/JeddictCompletionProvider.java +++ b/src/main/java/io/github/jeddict/ai/JavaCompletionProvider.java @@ -85,11 +85,16 @@ import org.netbeans.api.editor.completion.Completion; import static org.netbeans.spi.editor.completion.CompletionProvider.COMPLETION_QUERY_TYPE; -@MimeRegistration(mimeType = "", service = CompletionProvider.class, position = 100) -public class JeddictCompletionProvider implements CompletionProvider { - +@MimeRegistration(mimeType = "", service = CompletionProvider.class, position = 100) //NOI18N +public class JavaCompletionProvider implements CompletionProvider { + private static final PreferencesManager prefsManager = PreferencesManager.getInstance(); + @Override + public int getAutoQueryTypes(JTextComponent component, String typedText) { + return 0; + } + @Override public CompletionTask createTask(int type, JTextComponent component) { if (!prefsManager.isAiAssistantActivated()) { @@ -99,23 +104,18 @@ public CompletionTask createTask(int type, JTextComponent component) { return null; } if (type == COMPLETION_QUERY_TYPE) { - return new AsyncCompletionTask(new JeddictCompletionQuery(type, component.getSelectionStart()), component); + return new AsyncCompletionTask(new JavaCompletionQuery(type, component.getSelectionStart()), component); } return null; } - @Override - public int getAutoQueryTypes(JTextComponent component, String typedText) { - return 0; - } - - static final class JeddictCompletionQuery extends AsyncCompletionQuery { + static final class JavaCompletionQuery extends AsyncCompletionQuery { private JTextComponent component; private final int queryType; private int caretOffset; - private JeddictCompletionQuery(int queryType, int caretOffset) { + private JavaCompletionQuery(int queryType, int caretOffset) { this.queryType = queryType; this.caretOffset = caretOffset; } @@ -259,61 +259,6 @@ private Set findReferencedClasses(CompilationUnitTree compilationUnit) { return referencedClasses; } - private JeddictItem createItem(Snippet varName, String line, String lineTextBeforeCaret, JavaToken javaToken, Tree.Kind kind, Document doc) throws BadLocationException { - int newcaretOffset = caretOffset; - if(javaToken.getId() == STRING_LITERAL && kind == Tree.Kind.STRING_LITERAL) { - lineTextBeforeCaret = doc.getText(javaToken.getOffset(), newcaretOffset - javaToken.getOffset()); - if(lineTextBeforeCaret.startsWith("\"")) { - lineTextBeforeCaret = lineTextBeforeCaret.substring(1); - } - } - - - String snippetWOSpace = removeAllSpaces(varName.getSnippet()); - String tlLine = trimLeadingSpaces(line); - String tlLineTextBeforeCaret = trimLeadingSpaces(lineTextBeforeCaret); - - // Handle line and snippet first words safely - String firstWordLine = ""; - if (lineTextBeforeCaret != null && !lineTextBeforeCaret.trim().isEmpty()) { - String[] textSegments = lineTextBeforeCaret.trim().split("[^a-zA-Z0-9]+"); - if(textSegments.length > 0) { - firstWordLine = textSegments[0]; // Split by any non-alphanumeric character - } - } - - String firstWordSnippet = ""; - if (varName.getSnippet() != null && !varName.getSnippet().trim().isEmpty()) { - String[] textSegments = varName.getSnippet().trim().split("[^a-zA-Z0-9]+"); - if(textSegments.length > 0) { - firstWordSnippet = textSegments[0]; // Split by any non-alphanumeric character - } - } - - if (firstWordLine.equalsIgnoreCase(firstWordSnippet)) { - newcaretOffset = newcaretOffset - tlLineTextBeforeCaret.length(); - } else if (snippetWOSpace.startsWith(removeAllSpaces(line))) { - newcaretOffset = newcaretOffset - tlLine.length(); - } else if (snippetWOSpace.startsWith(removeAllSpaces(lineTextBeforeCaret))) { - newcaretOffset = newcaretOffset - tlLineTextBeforeCaret.length(); - } - if (tlLine != null && !tlLine.isEmpty() && tlLine.charAt(0) == '@' - && snippetWOSpace != null && !snippetWOSpace.isEmpty() && snippetWOSpace.charAt(0) == '@') { - newcaretOffset = newcaretOffset - tlLine.length(); - } - int caretToEndLength = -1; - if (javaToken.getId() == STRING_LITERAL && kind == Tree.Kind.STRING_LITERAL) { - - } else if (newcaretOffset != caretOffset) { - caretToEndLength = line.length() - lineTextBeforeCaret.length(); - String textAfterCaret = doc.getText(caretOffset, caretToEndLength); - caretToEndLength = trimTrailingSpaces(textAfterCaret).length(); - } - System.out.println("varName.getSnippet(), "+ varName.getSnippet() + caretOffset + " " + newcaretOffset); - JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), varName.getImports(), newcaretOffset, caretToEndLength, true, false, -1); - return var; - } - boolean done = false; @Override @@ -325,12 +270,8 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse done = true; this.caretOffset = caretOffset; String mimeType = (String) doc.getProperty("mimeType"); - JavaToken javaToken = isJavaContext(component.getDocument(), caretOffset, true); - if(javaToken != null) { - System.out.println("javaToken " +javaToken.getId()); - } if (COMPLETION_QUERY_TYPE == queryType && JAVA_MIME.equals(mimeType) - && javaToken.isJavaContext()) { + && isJavaContext(component.getDocument(), caretOffset, true)) { JavacTask task = getJavacTask(doc); Iterable ast = task.parse(); task.analyze(); @@ -351,55 +292,103 @@ protected void query(CompletionResultSet resultSet, Document doc, int caretOffse System.out.println("path.getLeaf().getKind() " + path.getLeaf().getKind()); if (path.getParentPath() != null) { System.out.println("path.getParentPath().getLeaf().getKind() " + path.getParentPath().getLeaf().getKind()); - if (path.getParentPath().getParentPath() != null) { - System.out.println("path.getParentPath().getParentPath().getLeaf().getKind() " + path.getParentPath().getParentPath().getLeaf().getKind()); - } } } - Tree.Kind kind = path.getLeaf().getKind(); if (path == null - || kind == Tree.Kind.ERRONEOUS) { + || path.getLeaf().getKind() == Tree.Kind.ERRONEOUS) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); - List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(classDataContent, updateddoc, line, path); + List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(classDataContent, updateddoc, line, null); for (Snippet varName : sugs) { - resultSet.addItem(createItem(varName, line, lineTextBeforeCaret, javaToken, kind, doc)); + int newcaretOffset = caretOffset; + String snippetWOSpace = removeAllSpaces(varName.getSnippet()); + if (snippetWOSpace.startsWith(removeAllSpaces(line))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(line).length(); + } else if (snippetWOSpace.startsWith(removeAllSpaces(lineTextBeforeCaret))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(lineTextBeforeCaret).length(); + } + int caretToEndLength = -1; + if (newcaretOffset != caretOffset) { + caretToEndLength = line.length() - lineTextBeforeCaret.length(); + String textAfterCaret = doc.getText(caretOffset, caretToEndLength); + caretToEndLength = trimTrailingSpaces(textAfterCaret).length(); + } + + JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), varName.getImports(), newcaretOffset, caretToEndLength, true, false, -1); + resultSet.addItem(var); + } - } else if (kind == Tree.Kind.COMPILATION_UNIT) { + } else if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(classDataContent, updateddoc, line, path); for (Snippet varName : sugs) { - resultSet.addItem(createItem(varName, line, lineTextBeforeCaret, javaToken, kind, doc)); - } - } else if ((trimLeadingSpaces(line).length() > 0 - && trimLeadingSpaces(line).charAt(0) == '@') || kind == Tree.Kind.ANNOTATION) { - String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); - List annotationSuggestions = getJeddictChatModel(fileObject).suggestAnnotations(classDataContent, updateddoc, line); - for (Snippet annotationSuggestion : annotationSuggestions) { - resultSet.addItem(createItem(annotationSuggestion, line, lineTextBeforeCaret,javaToken, kind, doc)); + int newcaretOffset = caretOffset; + String snippetWOSpace = removeAllSpaces(varName.getSnippet()); + if (snippetWOSpace.startsWith(removeAllSpaces(line))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(line).length(); + } else if (snippetWOSpace.startsWith(removeAllSpaces(lineTextBeforeCaret))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(lineTextBeforeCaret).length(); + } + int caretToEndLength = -1; + if (newcaretOffset != caretOffset) { + caretToEndLength = line.length() - lineTextBeforeCaret.length(); + String textAfterCaret = doc.getText(caretOffset, caretToEndLength); + caretToEndLength = trimTrailingSpaces(textAfterCaret).length(); + } + + JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), varName.getImports(), newcaretOffset, caretToEndLength, true, false, -1); + resultSet.addItem(var); + } - } else if (kind == Tree.Kind.MODIFIERS - || kind == Tree.Kind.IDENTIFIER) { + } else if (path.getLeaf().getKind() == Tree.Kind.MODIFIERS + || path.getLeaf().getKind() == Tree.Kind.IDENTIFIER) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(classDataContent, updateddoc, line, path); for (Snippet varName : sugs) { - resultSet.addItem(createItem(varName, line, lineTextBeforeCaret, javaToken, kind, doc)); + + int newcaretOffset = caretOffset; + String snippetWOSpace = removeAllSpaces(varName.getSnippet()); + if (snippetWOSpace.startsWith(removeAllSpaces(line))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(line).length(); + } else if (snippetWOSpace.startsWith(removeAllSpaces(lineTextBeforeCaret))) { + newcaretOffset = newcaretOffset - trimLeadingSpaces(lineTextBeforeCaret).length(); + } + int caretToEndLength = -1; + if (newcaretOffset != caretOffset) { + caretToEndLength = line.length() - lineTextBeforeCaret.length(); + String textAfterCaret = doc.getText(caretOffset, caretToEndLength); + caretToEndLength = trimTrailingSpaces(textAfterCaret).length(); + } + JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), varName.getImports(), newcaretOffset, caretToEndLength, true, false, -1); + resultSet.addItem(var); + + } + } else if (path.getLeaf().getKind() == Tree.Kind.ANNOTATION) { + String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); + List sugs = getJeddictChatModel(fileObject).suggestAnnotations(classDataContent, updateddoc, line); + for (String varName : sugs) { + JeddictItem var = new JeddictItem(null, null, varName, Collections.emptyList(), caretOffset - 1, true, false, -1); + resultSet.addItem(var); + } - } else if (kind == Tree.Kind.CLASS) { + } else if (path.getLeaf().getKind() == Tree.Kind.CLASS) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); 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 if (kind == Tree.Kind.BLOCK) { + } else if (path.getLeaf().getKind() == Tree.Kind.BLOCK) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); 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 if (kind == Tree.Kind.VARIABLE) { + } else if (path.getLeaf().getKind() == Tree.Kind.VARIABLE) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_VAR_NAMES_LIST}"); String currentVarName = getVariableNameAtCaret(doc, caretOffset); List sugs = getJeddictChatModel(fileObject).suggestVariableNames(classDataContent, updateddoc, line); @@ -408,15 +397,16 @@ && trimLeadingSpaces(line).charAt(0) == '@') || kind == Tree.Kind.ANNOTATION) { resultSet.addItem(var); } - } else if (kind == Tree.Kind.METHOD) { + } else if (path.getLeaf().getKind() == Tree.Kind.METHOD) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_METHOD_NAMES_LIST}"); String currentVarName = getVariableNameAtCaret(doc, caretOffset); List sugs = getJeddictChatModel(fileObject).suggestMethodNames(classDataContent, updateddoc, line); for (String varName : sugs) { JeddictItem var = new JeddictItem(null, null, varName, Collections.emptyList(), caretOffset - currentVarName.length(), true, false, -1); resultSet.addItem(var); + } - } else if (kind == Tree.Kind.METHOD_INVOCATION) { + } else if (path.getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION) { String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_METHOD_INVOCATION}"); String currentVarName = getVariableNameAtCaret(doc, caretOffset); List sugs = getJeddictChatModel(fileObject).suggestMethodInvocations(classDataContent, updateddoc, line); @@ -426,21 +416,18 @@ && trimLeadingSpaces(line).charAt(0) == '@') || kind == Tree.Kind.ANNOTATION) { resultSet.addItem(var); } - } else if (kind == Tree.Kind.STRING_LITERAL) { + } else if (path.getLeaf().getKind() == 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)); + JeddictItem var = new JeddictItem(null, null, varName, Collections.emptyList(), caretOffset, true, false, -1); + resultSet.addItem(var); + } } else { - System.out.println(kind + " " + path.getLeaf().toString()); + System.out.println(path.getLeaf().getKind() + " " + path.getLeaf().toString()); } - } else if (COMPLETION_QUERY_TYPE == queryType && JAVA_MIME.equals(mimeType)) { + } else if(COMPLETION_QUERY_TYPE == queryType && JAVA_MIME.equals(mimeType)) { String line = getLineText(doc, caretOffset); JavacTask task = getJavacTask(doc); Iterable ast = task.parse(); @@ -480,16 +467,16 @@ && trimLeadingSpaces(line).charAt(0) == '@') || kind == Tree.Kind.ANNOTATION) { JeddictItem var = new JeddictItem(null, null, varName, Collections.emptyList(), newcaretOffset, true, false, -1); resultSet.addItem(var); } - } + } } else { String currentLine = getLineText(doc, caretOffset); FileObject fileObject = getFileObjectFromEditor(doc); - String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); - List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(updateddoc, currentLine, mimeType); - for (Snippet varName : sugs) { - JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), Collections.emptyList(), caretOffset, true, false, -1); - resultSet.addItem(var); - } + String updateddoc = insertPlaceholderAtCaret(doc, caretOffset, "${SUGGEST_CODE_LIST}"); + List sugs = getJeddictChatModel(fileObject).suggestNextLineCode(updateddoc, currentLine, mimeType); + for (Snippet varName : sugs) { + JeddictItem var = new JeddictItem(null, null, varName.getSnippet(), Collections.emptyList(), caretOffset, true, false, -1); + resultSet.addItem(var); + } } } catch (Exception e) { Exceptions.printStackTrace(e); @@ -508,35 +495,7 @@ 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) { + public static boolean isJavaContext(final Document doc, final int offset, final boolean allowInStrings) { if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).readLock(); } @@ -546,22 +505,21 @@ public static JavaToken isJavaContext(final Document doc, final int offset, fina LanguagePath path = LanguagePath.get(MimeLookup.getLookup("text/x-dialog-binding").lookup(Language.class)); //NOI18N Document d = (Document) attributes.getValue(path, "dialogBinding.document"); //NOI18N if (d != null) { - return new JavaToken(true);//"text/x-java".equals(NbEditorUtilities.getMimeType(d)); //NOI18N + return true;//"text/x-java".equals(NbEditorUtilities.getMimeType(d)); //NOI18N } FileObject fo = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N - return new JavaToken("text/x-java".equals(fo.getMIMEType())); //NOI18N + return "text/x-java".equals(fo.getMIMEType()); //NOI18N } TokenSequence ts = SourceUtils.getJavaTokenSequence(TokenHierarchy.get(doc), offset); if (ts == null) { - return new JavaToken(false); + return false; } if (!ts.moveNext() && !ts.movePrevious()) { - return new JavaToken(true, ts.token().id(), ts.offset()); + return true; } if (offset == ts.offset()) { - return new JavaToken(true, ts.token().id(), ts.offset()); + return true; } - switch (ts.token().id()) { case DOUBLE_LITERAL: case FLOAT_LITERAL: @@ -576,16 +534,15 @@ public static JavaToken isJavaContext(final Document doc, final int offset, fina case JAVADOC_COMMENT: case LINE_COMMENT: case BLOCK_COMMENT: - return new JavaToken(false, ts.token().id(), ts.offset()); + return false; case STRING_LITERAL: - return new JavaToken(allowInStrings, ts.token().id(), ts.offset()); + return allowInStrings; } - return new JavaToken(true, ts.token().id(), ts.offset()); + return true; } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).readUnlock(); } } } - } diff --git a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java index 0e0ada3..1924aad 100644 --- a/src/main/java/io/github/jeddict/ai/JeddictChatModel.java +++ b/src/main/java/io/github/jeddict/ai/JeddictChatModel.java @@ -417,19 +417,7 @@ 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 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 + } 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}. " @@ -440,17 +428,6 @@ 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 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. " @@ -501,21 +478,18 @@ public List suggestJavadocOrComment(String classDatas, String classConte return comments; } - public List 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; + public List 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; // Generate the list of suggested annotations String jsonResponse = generate(prompt); System.out.println("jsonResponse " + jsonResponse); // Parse the JSON response into a List - List annotations = parseJsonToSnippets(jsonResponse); + List annotations = parseJsonToList(jsonResponse); return annotations; } @@ -580,9 +554,6 @@ private List parseJsonToList(String json) { private List parseJsonToListWithSplit(String json) { List variableNames = new ArrayList<>(); - if(json == null || json.isEmpty()){ - return variableNames; - } json = removeCodeBlockMarkers(json).trim(); String newjson = json; diff --git a/src/main/java/io/github/jeddict/ai/Snippet.java b/src/main/java/io/github/jeddict/ai/Snippet.java index 425ce4f..74223ae 100644 --- a/src/main/java/io/github/jeddict/ai/Snippet.java +++ b/src/main/java/io/github/jeddict/ai/Snippet.java @@ -22,10 +22,6 @@ public Snippet(String snippet, List imports) { this.imports = imports; } - public Snippet(String snippet) { - this.snippet = snippet; - } - public List getImports() { return imports; } diff --git a/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java b/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java index acebe0b..4acfdf7 100644 --- a/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java +++ b/src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java @@ -133,7 +133,7 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception int startPos = (int) start; int endPos = (int) end; - + try { // Search for '*/' after the end position of the current comment String content = document.getText(endPos, document.getLength() - endPos); @@ -164,7 +164,6 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception String lastLine = geIndentaion(wc, tree); if (lastLine.isBlank() && lastLine.length() <= 12) { StringBuilder indentedContent = new StringBuilder(); - boolean ignore = true; for (String line : javadocContent.split("\n")) { if (ignore) {