cleanup and removing dead code in completions

This commit is contained in:
Ben Fry
2022-08-03 06:13:37 -04:00
parent 5853010098
commit 5c8c03a0a4
4 changed files with 24 additions and 287 deletions

View File

@@ -171,13 +171,6 @@ public class CompletionCandidate implements Comparable<CompletionCandidate> {
}
/*
Object getWrappedObject() {
return wrappedObject;
}
*/
public String getElementName() {
return elementName;
}
@@ -198,33 +191,6 @@ public class CompletionCandidate implements Comparable<CompletionCandidate> {
}
// TODO this is gross [fry 180326]
/*
private String getNoHtmlLabel(){
if (!label.contains("<html>")) {
return label;
} else {
StringBuilder ans = new StringBuilder(label);
while (ans.indexOf("<") > -1) {
int a = ans.indexOf("<"), b = ans.indexOf(">");
if (a > b) break;
ans.replace(a, b+1, "");
}
return ans.toString();
}
}
*/
boolean startsWith(String newWord) {
// System.out.println("checking " + newWord);
// return getNoHtmlLabel().toLowerCase().startsWith(newWord);
// this seems to be elementName in all cases [fry 180326]
return elementName.startsWith(newWord);
}
CompletionCandidate withLabelAndCompString(String withLabel,
String withCompletion) {
return new CompletionCandidate(elementName,
@@ -289,10 +255,8 @@ public class CompletionCandidate implements Comparable<CompletionCandidate> {
}
}
labelBuilder.append(')');
if (method.getReturnType() != null) {
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
}
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
labelBuilder.append(" - <font color=#777777>");
labelBuilder.append(method.getDeclaringClass().getSimpleName());
@@ -335,10 +299,8 @@ public class CompletionCandidate implements Comparable<CompletionCandidate> {
}
}
labelBuilder.append(")");
if (method.getReturnType() != null) {
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
}
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
labelBuilder.append(" - <font color=#777777>");
labelBuilder.append(method.getDeclaringClass().getSimpleName());
labelBuilder.append("</font>");

View File

@@ -67,8 +67,6 @@ public class CompletionGenerator {
public CompletionGenerator(JavaMode mode) {
this.mode = mode;
//addCompletionPopupListner();
//loadJavaDoc();
}
@@ -86,8 +84,6 @@ public class CompletionGenerator {
CompletionCandidate[] cand = new CompletionCandidate[params.size() + 1];
cand[0] = new CompletionCandidate(md);
for (int i = 0; i < params.size(); i++) {
// cand[i + 1] = new CompletionCandidate(params.get(i).toString(), "", "",
// CompletionCandidate.LOCAL_VAR);
cand[i + 1] = new CompletionCandidate((SingleVariableDeclaration) params.get(i));
}
return cand;
@@ -122,65 +118,6 @@ public class CompletionGenerator {
}
// /**
// * Find the parent of the expression in a().b, this would give me the return
// * type of a(), so that we can find all children of a() beginning with b
// */
// public static ASTNode resolveExpression(ASTNode nearestNode,
// ASTNode expression, boolean noCompare) {
// log("Resolving " + getNodeAsString(expression) + " noComp "
// + noCompare);
// if (expression instanceof SimpleName) {
// return findDeclaration2(((SimpleName) expression), nearestNode);
// } else if (expression instanceof MethodInvocation) {
// log("3. Method Invo "
// + ((MethodInvocation) expression).getName());
// return findDeclaration2(((MethodInvocation) expression).getName(),
// nearestNode);
// } else if (expression instanceof FieldAccess) {
// log("2. Field access "
// + getNodeAsString(((FieldAccess) expression).getExpression()) + "|||"
// + getNodeAsString(((FieldAccess) expression).getName()));
// if (noCompare) {
// /*
// * ASTNode ret = findDeclaration2(((FieldAccess) expression).getName(),
// * nearestNode); log("Found as ->"+getNodeAsString(ret));
// * return ret;
// */
// return findDeclaration2(((FieldAccess) expression).getName(),
// nearestNode);
// } else {
//
// /*
// * Note how for the next recursion, noCompare is reversed. Let's say
// * I've typed getABC().quark.nin where nin is incomplete(ninja being the
// * field), when execution first enters here, it calls resolveExpr again
// * for "getABC().quark" where we know that quark field must be complete,
// * so we toggle noCompare. And kaboom.
// */
// return resolveExpression(nearestNode,
// ((FieldAccess) expression).getExpression(),
// true);
// }
// //return findDeclaration2(((FieldAccess) expression).getExpression(), nearestNode);
// } else if (expression instanceof QualifiedName) {
// log("1. Resolving "
// + ((QualifiedName) expression).getQualifier() + " ||| "
// + ((QualifiedName) expression).getName());
// if (noCompare) { // no compare, as in "abc.hello." need to resolve hello here
// return findDeclaration2(((QualifiedName) expression).getName(),
// nearestNode);
// } else {
// //User typed "abc.hello.by" (bye being complete), so need to resolve "abc.hello." only
// return findDeclaration2(((QualifiedName) expression).getQualifier(),
// nearestNode);
// }
// }
//
// return null;
// }
/**
* Finds the type of the expression in foo.bar().a().b, this would give me the
* type of b if it exists in return type of a(). If noCompare is true,
@@ -888,23 +825,6 @@ public class CompletionGenerator {
}
/*
protected SketchOutline sketchOutline;
public void showSketchOutline() {
if (editor.hasJavaTabs()) return;
sketchOutline = new SketchOutline(editor, codeTree);
sketchOutline.show();
}
public void showTabOutline() {
new TabOutline(editor).show();
}
*/
/**
* Give this thing a {@link Name} instance - a {@link SimpleName} from the
* ASTNode for ex, and it tries its level best to locate its declaration in
@@ -960,7 +880,7 @@ public class CompletionGenerator {
}
}
} else {
parent = parent.getParent(); // Move one up the ast. V V IMP!!
parent = parent.getParent(); // Move one up the AST. Very important.
}
} else if (parent.getNodeType() == ASTNode.FIELD_ACCESS) {
FieldAccess fa = (FieldAccess) parent;
@@ -1476,7 +1396,7 @@ public class CompletionGenerator {
static private ASTNode definedIn(ASTNode node, String name,
ArrayList<Integer> constrains) {
List<Integer> constrains) {
if (node == null)
return null;
if (constrains != null) {
@@ -1617,43 +1537,6 @@ public class CompletionGenerator {
}
// public void jdocWindowVisible(boolean visible) {
// // frmJavaDoc.setVisible(visible);
// }
// public static String readFile2(String path) {
// BufferedReader reader = null;
// try {
// reader = new BufferedReader(
// new InputStreamReader(
// new FileInputStream(
// new File(
// path))));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// try {
// StringBuilder ret = new StringBuilder();
// // ret.append("package " + className + ";\n");
// String line;
// while ((line = reader.readLine()) != null) {
// ret.append(line);
// ret.append("\n");
// }
// return ret.toString();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// return null;
// }
static private void log(Object object) {
Messages.log(object == null ? "null" : object.toString());
}
@@ -1667,7 +1550,7 @@ public class CompletionGenerator {
newWord = newWord.toLowerCase();
for (CompletionCandidate comp : candidates) {
//if (comp.getNoHtmlLabel().toLowerCase().startsWith(newWord)) {
if (comp.startsWith(newWord)) {
if (comp.getElementName().startsWith(newWord)) {
newCandidate.add(comp);
}
}
@@ -1925,112 +1808,4 @@ public class CompletionGenerator {
}
return defListModel;
}
/// JavaDocs -----------------------------------------------------------------
//TODO: Work on this later.
/*
protected TreeMap<String, String> jdocMap;
protected void loadJavaDoc() {
jdocMap = new TreeMap<>();
// presently loading only p5 reference for PApplet
// TODO: use something like ExecutorService here [jv]
new Thread(new Runnable() {
@Override
public void run() {
try {
loadJavaDoc(jdocMap, editor.getMode().getReferenceFolder());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
static void loadJavaDoc(TreeMap<String, String> jdocMap,
File referenceFolder) throws IOException {
Document doc;
FileFilter fileFilter = new FileFilter() {
@Override
public boolean accept(File file) {
if(!file.getName().endsWith("_.html"))
return false;
int k = 0;
for (int i = 0; i < file.getName().length(); i++) {
if(file.getName().charAt(i)== '_')
k++;
if(k > 1)
return false;
}
return true;
}
};
for (File docFile : referenceFolder.listFiles(fileFilter)) {
doc = Jsoup.parse(docFile, null);
Elements elm = doc.getElementsByClass("ref-item");
String msg = "";
String methodName = docFile.getName().substring(0, docFile.getName().indexOf('_'));
//System.out.println(methodName);
for (org.jsoup.nodes.Element ele : elm) {
msg = "<html><body> <strong><div style=\"width: 300px; text-justification: justify;\"></strong><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"ref-item\">"
+ ele.html() + "</table></div></html></body></html>";
//mat.replaceAll("");
msg = msg.replaceAll("img src=\"", "img src=\""
+ referenceFolder.toURI().toURL().toString() + "/");
//System.out.println(ele.text());
}
jdocMap.put(methodName, msg);
}
//System.out.println("JDoc loaded " + jdocMap.size());
}
public void updateJavaDoc(final CompletionCandidate candidate) {
String methodmatch = candidate.toString();
if (methodmatch.indexOf('(') != -1) {
methodmatch = methodmatch.substring(0, methodmatch.indexOf('('));
}
//log("jdoc match " + methodmatch);
String temp = "<html> </html>";
for (final String key : jdocMap.keySet()) {
if (key.startsWith(methodmatch) && key.length() > 3) {
log("Matched jdoc " + key);
if (candidate.getWrappedObject() != null) {
String definingClass = "";
if (candidate.getWrappedObject() instanceof Field)
definingClass = ((Field) candidate.getWrappedObject())
.getDeclaringClass().getName();
else if (candidate.getWrappedObject() instanceof Method)
definingClass = ((Method) candidate.getWrappedObject())
.getDeclaringClass().getName();
if (definingClass.equals("processing.core.PApplet")) {
temp = (jdocMap.get(key));
break;
}
}
}
}
final String jdocString = temp;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
javadocPane.setText(jdocString);
scrollPane.getVerticalScrollBar().setValue(0);
//frmJavaDoc.setVisible(!jdocString.equals("<html> </html>"));
editor.toFront();
editor.ta.requestFocus();
}
});
}
*/
}

View File

@@ -56,9 +56,7 @@ public class CompletionPanel {
final private String subWord;
/** Position where the completion has to be inserted */
final private int insertionPosition;
final private JavaTextArea textArea;
final private int caretPos;
/** Scroll pane in which the completion list is displayed */
final private JScrollPane scrollPane;
@@ -80,18 +78,16 @@ public class CompletionPanel {
/**
* Triggers the completion popup
* @param position - insertion position(caret pos)
* @param subWord - Partial word which triggered the code completion and which needs to be completed
* @param caret - insertion caret position
* @param subWord - Partial word that triggered the code completion
* @param items - completion candidates
* @param location - Point location where popup list is to be displayed
*/
public CompletionPanel(final JEditTextArea textarea,
int position, String subWord,
public CompletionPanel(JavaEditor editor, int caret, String subWord,
DefaultListModel<CompletionCandidate> items,
final Point location, JavaEditor editor) {
this.textArea = (JavaTextArea) textarea;
Point location) {
this.editor = editor;
this.insertionPosition = position;
this.caretPos = caret;
if (subWord.indexOf('.') != -1 && subWord.indexOf('.') != subWord.length()-1) {
this.subWord = subWord.substring(subWord.lastIndexOf('.') + 1);
} else {
@@ -144,6 +140,7 @@ public class CompletionPanel {
popupMenu.setPopupSize(calcWidth(), calcHeight(items.getSize())); //TODO: Eradicate this evil
popupMenu.setFocusable(false);
// TODO: Update JavaDoc to completionList.getSelectedValue()
JavaTextArea textarea = editor.getJavaTextArea();
popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0) + location.y);
//log("Suggestion shown: " + System.currentTimeMillis());
}
@@ -164,7 +161,8 @@ public class CompletionPanel {
*/
private int calcHeight(int itemCount) {
int maxHeight = 250;
FontMetrics fm = textArea.getGraphics().getFontMetrics();
// TODO not the right metrics [fry 220802]
FontMetrics fm = editor.getTextArea().getGraphics().getFontMetrics();
float itemHeight = Math.max((fm.getHeight() + (fm.getDescent()) * 0.5f),
classIcon.getIconHeight() * 1.2f);
@@ -189,7 +187,8 @@ public class CompletionPanel {
private int calcWidth() {
int maxWidth = 300;
float min = 0;
FontMetrics fm = textArea.getGraphics().getFontMetrics();
// TODO these are definitely not the right metrics [fry 220802]
FontMetrics fm = editor.getTextArea().getGraphics().getFontMetrics();
for (int i = 0; i < completionList.getModel().getSize(); i++) {
float h = fm.stringWidth(completionList.getModel().getElementAt(i).getLabel());
min = Math.max(min, h);
@@ -243,14 +242,15 @@ public class CompletionPanel {
}
}
JavaTextArea textArea = editor.getJavaTextArea();
Messages.err(subWord + " <= sub word, Inserting suggestion=> " +
selectedSuggestion + " Current sub: " + currentSubWord);
if (currentSubWord.length() > 0) {
textArea.getDocument().remove(insertionPosition - currentSubWordLen,
textArea.getDocument().remove(caretPos - currentSubWordLen,
currentSubWordLen);
}
textArea.getDocument().insertString(insertionPosition - currentSubWordLen,
textArea.getDocument().insertString(caretPos - currentSubWordLen,
completionString, null);
if (selectedSuggestion.endsWith(")") && !selectedSuggestion.endsWith("()")) {
// place the caret between '( and first ','
@@ -259,7 +259,7 @@ public class CompletionPanel {
// the case of single param methods, containing no ','
textArea.setCaretPosition(textArea.getCaretPosition() - 1); // just before ')'
} else {
textArea.setCaretPosition(insertionPosition + x);
textArea.setCaretPosition(caretPos + x);
}
}

View File

@@ -517,8 +517,8 @@ public class JavaTextArea extends PdeTextArea {
new Point(offsetToX(getCaretLine(),
position - getLineStartOffset(getCaretLine())),
lineToY(getCaretLine()) + getPainter().getLineHeight());
suggestion = new CompletionPanel(this, position, subWord,
listModel, location, getJavaEditor());
suggestion = new CompletionPanel(getJavaEditor(), position, subWord,
listModel, location);
requestFocusInWindow();
} catch (Exception e) {