Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update method using Document API instead of Java Source Tree #61

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions src/main/java/io/github/jeddict/ai/hints/MethodFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
import org.netbeans.spi.java.hints.JavaFix;
import org.openide.util.NbBundle;
import static io.github.jeddict.ai.util.UIUtil.queryToEnhance;
import java.io.IOException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.netbeans.modules.editor.indent.api.Reformat;
import org.openide.util.Exceptions;

/**
*
Expand Down Expand Up @@ -116,45 +121,33 @@ protected void performRewrite(JavaFix.TransformationContext tc) throws Exception
String methodContent = json.getString("methodContent");
SourceUtil.addImports(copy, imports);

// remove comment by recreaing method
if (leaf instanceof MethodTree) {
MethodTree methodTree = (MethodTree) leaf;
TreeMaker treeMaker = copy.getTreeMaker();

MethodTree newMethodTree = treeMaker.Method(
methodTree.getModifiers(),
methodTree.getName(),
methodTree.getReturnType(),
methodTree.getTypeParameters(),
methodTree.getParameters(),
methodTree.getThrows(),
methodTree.getBody(),
methodTree.getDefaultValue() != null
? (ExpressionTree) methodTree.getDefaultValue()
: null
);

// Rewrite the method with the new tree
copy.rewrite(methodTree, newMethodTree);
}

// Formating
String lastLine = geIndentaion(copy, leaf);
if (lastLine.isBlank() && lastLine.length() <= 12) {
StringBuilder indentedContent = new StringBuilder();
boolean ignore = true;
for (String line : methodContent.split("\n")) {
if (ignore) {
ignore = false;
indentedContent.append(line).append("\n");
} else {
indentedContent.append(lastLine).append(line).append("\n");
}
if (leaf instanceof MethodTree methodTree) {
long startPos = copy.getTrees().getSourcePositions().getStartPosition(copy.getCompilationUnit(), methodTree);
long endPos = copy.getTrees().getSourcePositions().getEndPosition(copy.getCompilationUnit(), methodTree);
try {
insertAndReformat(copy.getDocument(), methodContent, (int) startPos, (int) endPos - (int) startPos);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
methodContent = indentedContent.toString();
}
copy.rewrite(leaf, copy.getTreeMaker().QualIdent(methodContent));

}

private void insertAndReformat(Document document, String content, int startPosition, int lengthToRemove) {
try {
if (lengthToRemove > 0) {
document.remove(startPosition, lengthToRemove);
}
document.insertString(startPosition, content, null);
Reformat reformat = Reformat.get(document);
reformat.lock();
try {
reformat.reformat(startPosition, startPosition + content.length());
} finally {
reformat.unlock();
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}

}
Loading