Skip to content

Commit

Permalink
com.github.javaparser dependency removed (#5)
Browse files Browse the repository at this point in the history
* com.github.javaparser dependency removed

* polish
  • Loading branch information
jShiwaniGupta authored Sep 26, 2024
1 parent a961972 commit 68082ea
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 247 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@
<artifactId>langchain4j</artifactId>
<version>0.34.0</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.24.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package io.github.jeddict.ai;

import static com.github.javaparser.utils.Utils.trimTrailingSpaces;
import io.github.jeddict.ai.scanner.MyTreePathScanner;
import com.sun.source.tree.ClassTree;
import java.util.*;
Expand Down Expand Up @@ -80,6 +79,7 @@
import static io.github.jeddict.ai.util.MimeUtil.JAVA_MIME;
import static io.github.jeddict.ai.util.StringUtil.removeAllSpaces;
import static io.github.jeddict.ai.util.StringUtil.trimLeadingSpaces;
import static io.github.jeddict.ai.util.StringUtil.trimTrailingSpaces;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.netbeans.api.editor.completion.Completion;
Expand Down
170 changes: 65 additions & 105 deletions src/main/java/io/github/jeddict/ai/fix/JavaDocFixImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@
package io.github.jeddict.ai.fix;

import io.github.jeddict.ai.util.StringUtil;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.CLASS;
Expand All @@ -40,16 +34,15 @@
import io.github.jeddict.ai.Action;
import io.github.jeddict.ai.JeddictChatModel;
import static io.github.jeddict.ai.Action.ENHANCE;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
import static io.github.jeddict.ai.util.SourceUtil.geIndentaion;
import static io.github.jeddict.ai.util.StringUtil.removeCodeBlockMarkers;
import static io.github.jeddict.ai.util.StringUtil.trimLeadingSpaces;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.TreeMaker;
Expand Down Expand Up @@ -86,140 +79,107 @@ protected String getText() {

@Override
protected void performRewrite(JavaFix.TransformationContext tc) throws Exception {
WorkingCopy copy = tc.getWorkingCopy();
if (copy.toPhase(JavaSource.Phase.RESOLVED).compareTo(JavaSource.Phase.RESOLVED) < 0) {
WorkingCopy wc = tc.getWorkingCopy();
if (wc.toPhase(JavaSource.Phase.RESOLVED).compareTo(JavaSource.Phase.RESOLVED) < 0) {
return;
}
saveOpenEditor();

TreePath treePath = tc.getPath();
Tree leaf = treePath.getLeaf();
Element elm = copy.getTrees().getElement(treePath);
Tree tree = treePath.getLeaf();
Element elm = wc.getTrees().getElement(treePath);
if (elm == null) {
return;
}

Document document = wc.getDocument();

String javadocContent;
DocCommentTree oldDocCommentTree = ((DocTrees) copy.getTrees()).getDocCommentTree(treePath);
DocCommentTree oldDocCommentTree = wc.getDocTrees().getDocCommentTree(treePath);

switch (leaf.getKind()) {
switch (tree.getKind()) {
case CLASS:
case INTERFACE:
if (action == ENHANCE) {
javadocContent = new JeddictChatModel().enhanceJavadocForClass(oldDocCommentTree.toString(), leaf.toString());
javadocContent = new JeddictChatModel().enhanceJavadocForClass(oldDocCommentTree.toString(), tree.toString());
} else {
javadocContent = new JeddictChatModel().generateJavadocForClass(leaf.toString());
javadocContent = new JeddictChatModel().generateJavadocForClass(tree.toString());
}
break;
case METHOD:
if (action == ENHANCE) {
javadocContent = new JeddictChatModel().enhanceJavadocForMethod(oldDocCommentTree.toString(), ((MethodTree) leaf).getName().toString());
javadocContent = new JeddictChatModel().enhanceJavadocForMethod(oldDocCommentTree.toString(), ((MethodTree) tree).getName().toString());
} else {
javadocContent = new JeddictChatModel().generateJavadocForMethod(((MethodTree) leaf).getName().toString());
javadocContent = new JeddictChatModel().generateJavadocForMethod(((MethodTree) tree).getName().toString());
}
break;
case VARIABLE:
if (action == ENHANCE) {
javadocContent = new JeddictChatModel().enhanceJavadocForField(oldDocCommentTree.toString(), ((VariableTree) leaf).getName().toString());
javadocContent = new JeddictChatModel().enhanceJavadocForField(oldDocCommentTree.toString(), ((VariableTree) tree).getName().toString());
} else {
javadocContent = new JeddictChatModel().generateJavadocForField(((VariableTree) leaf).getName().toString());
javadocContent = new JeddictChatModel().generateJavadocForField(((VariableTree) tree).getName().toString());
}
break;
default:
return;
}
javadocContent = removeCodeBlockMarkers(javadocContent);

Path filePath = Paths.get(copy.getFileObject().toURI());
if (action == ENHANCE && oldDocCommentTree != null && document != null) {
DocTrees docTrees = wc.getDocTrees();
CompilationUnitTree cuTree = wc.getCompilationUnit();

String sourceCode = new String(Files.readAllBytes(filePath));
JavaParser javaParser = new JavaParser();
CompilationUnit cu = javaParser.parse(sourceCode).getResult().orElse(null);
long start = docTrees.getSourcePositions().getStartPosition(cuTree, oldDocCommentTree, oldDocCommentTree);
long end = docTrees.getSourcePositions().getEndPosition(cuTree, oldDocCommentTree, oldDocCommentTree);

if (cu == null) {
return;
}
int startPos = (int) start;
int endPos = (int) end;

// Add the Javadoc comment to the appropriate location
switch (leaf.getKind()) {
case CLASS:
case INTERFACE:
String className = ((ClassTree) leaf).getSimpleName().toString();
cu.findFirst(ClassOrInterfaceDeclaration.class)
.filter(decl -> decl.getNameAsString().equals(className))
.ifPresent(classDecl -> {
JavadocComment comment = new JavadocComment(removeCodeBlockMarkers(javadocContent));
classDecl.setComment(comment);
});
break;
case METHOD:
MethodTree methodTree = (MethodTree) leaf;
String methodName = methodTree.getName().toString();

cu.findFirst(ClassOrInterfaceDeclaration.class).ifPresent(classDecl -> {
classDecl.findAll(MethodDeclaration.class).stream()
.filter(methodDecl -> methodDecl.getNameAsString().equals(methodName))
.findFirst()
.ifPresent(methodDecl -> {
JavadocComment comment = new JavadocComment(removeCodeBlockMarkers(javadocContent));
methodDecl.setComment(comment);
});
});
break;
case VARIABLE:
cu.findFirst(FieldDeclaration.class).ifPresent(fieldDecl -> {
fieldDecl.getVariables().forEach(varDecl -> {
if (varDecl.getNameAsString().equals(((VariableTree) leaf).getName().toString())) {
JavadocComment comment = new JavadocComment(removeCodeBlockMarkers(javadocContent));
fieldDecl.setComment(comment);
}
});
});
break;
default:
return;
}
try {
// Search for '*/' after the end position of the current comment
String content = document.getText(endPos, document.getLength() - endPos);
int afterEndPos = content.indexOf("*/") + endPos + 2; // Position after the '*/'

// Write the modified code back to the file
String modifiedCode = cu.toString();
Files.write(filePath, modifiedCode.getBytes());
}
// Search for '/**' before the start position of the current comment
content = document.getText(0, startPos);
int beforeStartPos = content.lastIndexOf("/**");

private void updateJavadoc(WorkingCopy copy, TreePath treePath, TreeMaker make, String javadocContent, ElementKind elementKind) {
List<DocTree> firstSentence = new LinkedList<>();
List<DocTree> tags = new LinkedList<>();
List<DocTree> body = new LinkedList<>();
boolean ci = treePath.getLeaf().getKind() == Tree.Kind.CLASS || treePath.getLeaf().getKind() == Tree.Kind.INTERFACE;

// Split the content into lines for simulation
String[] lines = javadocContent.split("\n");
for (String line : lines) {
line = trimLeadingSpaces(line);
if (line.startsWith("/**") || line.startsWith("*/")) {
} else if (line.startsWith("* @")) {
tags.add(make.Text(line.substring(1)));
} else if (line.startsWith("*") || line.isEmpty()) {
if (line.startsWith("*")) {
firstSentence.add(make.Text(line.substring(1)));
if (ci) {
firstSentence.add(make.Text("\n * "));
// Remove all space until a newline character is found before '/**'
while (beforeStartPos > 0 && content.charAt(beforeStartPos) != '\n') {
beforeStartPos--; // Move backward to include spaces before the newline
}

// Remove the entire Javadoc, from 'beforeStartPos' to 'afterEndPos'
if (beforeStartPos >= 0 && afterEndPos > beforeStartPos) { // Ensure valid positions
document.remove(beforeStartPos, afterEndPos - beforeStartPos);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}

int startOffset = (int) wc.getTrees().getSourcePositions()
.getStartPosition(wc.getCompilationUnit(), tree);

if (document != null) {
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) {
ignore = false;
indentedContent.append(line).append("\n").append(lastLine);
} else {
firstSentence.add(make.Text("\n * "));
indentedContent.append(line).append("\n").append(lastLine);
}
} else if (line.isEmpty()) {
firstSentence.add(make.Text(""));
}
javadocContent = indentedContent.toString();
} else {
javadocContent = javadocContent + '\n';
}
document.insertString(startOffset, javadocContent, null);
}
if (!firstSentence.isEmpty() && firstSentence.get(firstSentence.size() - 1).toString().equals("\n * ")) {
firstSentence.remove(firstSentence.size() - 1);
}
if (!firstSentence.isEmpty() && firstSentence.get(firstSentence.size() - 1).toString().equals("\n * ")) {
firstSentence.remove(firstSentence.size() - 1);
}

DocCommentTree oldDocCommentTree = ((DocTrees) copy.getTrees()).getDocCommentTree(treePath);
DocCommentTree newDocCommentTree = make.DocComment(firstSentence, body, tags);
copy.rewrite(treePath.getLeaf(), oldDocCommentTree, newDocCommentTree);

}

}
21 changes: 5 additions & 16 deletions src/main/java/io/github/jeddict/ai/fix/MethodFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package io.github.jeddict.ai.fix;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
Expand All @@ -15,11 +13,9 @@
import io.github.jeddict.ai.JeddictChatModel;
import io.github.jeddict.ai.util.SourceUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
import static io.github.jeddict.ai.util.SourceUtil.geIndentaion;
import static io.github.jeddict.ai.util.StringUtil.removeCodeBlockMarkers;
import static io.github.jeddict.ai.util.UIUtil.askQuery;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.lang.model.element.Element;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -28,7 +24,6 @@
import org.netbeans.api.java.source.TreeMaker;
import org.netbeans.api.java.source.TreePathHandle;
import org.netbeans.api.java.source.WorkingCopy;
import org.netbeans.modules.editor.indent.api.Reformat;
import org.netbeans.spi.java.hints.JavaFix;
import org.openide.util.NbBundle;

Expand Down Expand Up @@ -99,13 +94,7 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
}
}

Path filePath = Paths.get(copy.getFileObject().toURI());

String sourceCode = new String(Files.readAllBytes(filePath));
JavaParser javaParser = new JavaParser();
CompilationUnit cu = javaParser.parse(sourceCode).getResult().orElse(null);

if (cu == null || content == null) {
if (content == null) {
return;
}

Expand Down Expand Up @@ -137,9 +126,7 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
}

// Formating
int startPos = (int) copy.getTrees().getSourcePositions().getStartPosition(copy.getCompilationUnit(), leaf);
String[] lines = sourceCode.substring(0, startPos).split("\n"); // Zero-based index
String lastLine = lines[lines.length - 1];
String lastLine = geIndentaion(copy, leaf);
if (lastLine.isBlank() && lastLine.length() <= 12) {
StringBuilder indentedContent = new StringBuilder();
boolean ignore = true;
Expand All @@ -156,5 +143,7 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
copy.rewrite(leaf, copy.getTreeMaker().QualIdent(methodContent));

}



}
9 changes: 0 additions & 9 deletions src/main/java/io/github/jeddict/ai/fix/RestEndpointFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
*/
package io.github.jeddict.ai.fix;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.Tree;
import static com.sun.source.tree.Tree.Kind.CLASS;
Expand All @@ -18,11 +14,6 @@
import io.github.jeddict.ai.util.SourceUtil;
import static io.github.jeddict.ai.util.FileUtil.saveOpenEditor;
import static io.github.jeddict.ai.util.StringUtil.removeCodeBlockMarkers;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Formatter;
import javax.lang.model.element.Element;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down
Loading

0 comments on commit 68082ea

Please sign in to comment.