From 7ce2291b64f3cb6f9513baf22655db2bcffbcd17 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Sun, 21 Apr 2013 01:40:41 +0530 Subject: [PATCH] better everythang \m/ --- .../mode/experimental/ASTGenerator.java | 215 +++++++++--------- .../mode/experimental/CompletionPanel.java | 45 +++- .../experimental/ErrorCheckerService.java | 2 +- .../mode/experimental/JavadocHelper.java | 122 ++++++++++ .../mode/experimental/TextArea.java | 14 +- 5 files changed, 269 insertions(+), 129 deletions(-) create mode 100644 pdex/src/processing/mode/experimental/JavadocHelper.java diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index f20872177..f42c7b8ff 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -6,12 +6,14 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.BufferedReader; import java.io.File; +import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; @@ -21,6 +23,7 @@ import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; @@ -69,6 +72,7 @@ import processing.app.syntax.JEditTextArea; import com.google.classpath.ClassPath; import com.google.classpath.ClassPathFactory; +import com.google.classpath.DirectoryClassPath.FileFileFilter; import com.google.classpath.RegExpResourceFilter; import com.ibm.icu.util.StringTokenizer; @@ -90,7 +94,9 @@ public class ASTGenerator { private JTable tableAuto; - private JLabel jdocLabel; + private JEditorPane javadocPane; + + private JScrollPane scrollPane; public ASTGenerator(ErrorCheckerService ecs) { this.errorCheckerService = ecs; @@ -113,9 +119,15 @@ public class ASTGenerator { frameAutoComp.add(sp2); jdocWindow = new JFrame(); - jdocWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - jdocLabel = new JLabel(); - jdocWindow.add(jdocLabel); + jdocWindow.setTitle("P5 InstaHelp"); + //jdocWindow.setUndecorated(true); + jdocWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + javadocPane = new JEditorPane(); + javadocPane.setContentType("text/html"); + javadocPane.setEditable(false); + scrollPane = new JScrollPane(); + scrollPane.setViewportView(javadocPane); + jdocWindow.add(scrollPane); jdocMap = new TreeMap(); //loadJars(); @@ -164,23 +176,28 @@ public class ASTGenerator { } } - private DefaultMutableTreeNode buildAST2(String source) { - ASTParser parser = ASTParser.newParser(AST.JLS4); - parser.setSource(source.toCharArray()); - parser.setKind(ASTParser.K_COMPILATION_UNIT); + private DefaultMutableTreeNode buildAST2(String source, CompilationUnit cu) { + if (cu == null) { + ASTParser parser = ASTParser.newParser(AST.JLS4); + parser.setSource(source.toCharArray()); + parser.setKind(ASTParser.K_COMPILATION_UNIT); - Map options = JavaCore.getOptions(); + Map options = JavaCore.getOptions(); - JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); - options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); - parser.setCompilerOptions(options); - compilationUnit = (CompilationUnit) parser.createAST(null); + JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); + options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); + parser.setCompilerOptions(options); + compilationUnit = (CompilationUnit) parser.createAST(null); + } else { + compilationUnit = cu; + System.out.println("Other cu"); + } // OutlineVisitor visitor = new OutlineVisitor(); // compilationUnit.accept(visitor); - codeTree = new DefaultMutableTreeNode( - getNodeAsString((ASTNode) compilationUnit - .types().get(0))); - visitRecur((ASTNode) compilationUnit.types().get(0), codeTree); +// codeTree = new DefaultMutableTreeNode( +// getNodeAsString((ASTNode) compilationUnit +// .types().get(0))); +// visitRecur((ASTNode) compilationUnit.types().get(0), codeTree); SwingWorker worker = new SwingWorker() { @Override @@ -210,7 +227,7 @@ public class ASTGenerator { jdocWindow.setBounds(new Rectangle(errorCheckerService.getEditor() .getX() + errorCheckerService.getEditor().getWidth(), errorCheckerService.getEditor() - .getY(), 400, 400)); + .getY(), 450, 600)); jdocWindow.setVisible(true); } jtree.validate(); @@ -240,28 +257,32 @@ public class ASTGenerator { + File.pathSeparatorChar + System .getProperty("java.home") - + "/lib/rt.jar"); + + "/lib/rt.jar"+ File.pathSeparatorChar); if (errorCheckerService.classpathJars != null) { for (URL jarPath : errorCheckerService.classpathJars) { tehPath.append(jarPath.getPath() + File.pathSeparatorChar); } } - //String paths[] = tehPaths.split(File.separatorChar +""); -// StringTokenizer st = new StringTokenizer(tehPath.toString(), -// File.pathSeparatorChar + ""); +// String paths[] = tehPath.toString().split(File.separatorChar +""); + StringTokenizer st = new StringTokenizer(tehPath.toString(), + File.pathSeparatorChar + ""); + while (st.hasMoreElements()) { + String sstr = (String) st.nextElement(); + System.out.println(sstr); + } classPath = factory.createFromPath(tehPath.toString()); -// for (String packageName : classPath.listPackages("")) { -// System.out.println(packageName); -// } + for (String packageName : classPath.listPackages("")) { + System.out.println(packageName); + } RegExpResourceFilter regExpResourceFilter = new RegExpResourceFilter( ".*", - "Vec3D.class"); -// String[] resources = classPath.findResources("", regExpResourceFilter); -// for (String className : resources) { -// System.out.println("-> " + className); -// } + "ArrayList.class"); + String[] resources = classPath.findResources("", regExpResourceFilter); + for (String className : resources) { + System.out.println("-> " + className); + } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -272,84 +293,21 @@ public class ASTGenerator { private TreeMap jdocMap; private void loadJavaDoc() { - + // presently loading only p5 reference for PApplet Thread t = new Thread(new Runnable() { @Override public void run() { - Document doc; - - Pattern pat = Pattern.compile("\\w+"); - try { - File javaDocFile = new File( - "/home/quarkninja/Workspaces/processing-workspace/processing/build/javadoc/core/processing/core/PApplet.html"); - //SimpleOpenNI.SimpleOpenNI - doc = Jsoup.parse(javaDocFile, null); - - String msg = ""; - Elements elm = doc.getElementsByTag("pre"); -// Elements desc = doc.getElementsByTag("dl"); - //System.out.println(elm.toString()); - - for (Iterator iterator = elm.iterator(); iterator.hasNext();) { - Element element = (Element) iterator.next(); - - //System.out.println(element.text()); -// if (element.nextElementSibling() != null) -// System.out.println(element.nextElementSibling().text()); - //System.out.println("-------------------"); - msg = "
" - + element.html() - + element.nextElementSibling() - + "
"; - int k = 0; - Matcher matcher = pat.matcher(element.text()); - ArrayList parts = new ArrayList(); - while (matcher.find()) { -// System.out.print("Start index: " + matcher.start()); -// System.out.print(" End index: " + matcher.end() + " "); - if (k == 0 && !matcher.group().equals("public")) { - k = -1; - break; - } - // System.out.print(matcher.group() + " "); - parts.add(matcher.group()); - k++; - } - if (k <= 0 || parts.size() < 3) - continue; - int i = 0; - if (parts.get(i).equals("public")) - i++; - if (parts.get(i).equals("static") || parts.get(i).equals("final") - || parts.get(i).equals("class")) - i++; - if (parts.get(i).equals("static") || parts.get(i).equals("final")) - i++; -// System.out.println("Ret Type " + parts.get(i)); - - i++; // return type - - //System.out.println("Name " + parts.get(i)); - jdocMap.put(parts.get(i), msg); - } - System.out.println("JDoc loaded"); -// for (String key : jdocMap.keySet()) { -// System.out.println("Method: " + key); -// System.out.println("Method: " + jdocMap.get(key)); -// } - } catch (Exception e) { - e.printStackTrace(); - } + JavadocHelper.loadJavaDoc(jdocMap); } }); t.start(); } - public DefaultMutableTreeNode buildAST() { - return buildAST2(errorCheckerService.sourceCode); + public DefaultMutableTreeNode buildAST(CompilationUnit cu) { + return buildAST2(errorCheckerService.sourceCode,cu); } public String[] checkForTypes2(ASTNode node) { @@ -570,6 +528,9 @@ public class ASTGenerator { System.err.print("Typed: " + word2 + "|"); anode = findClosestNode(lineNumber, (ASTNode) compilationUnit.types() .get(0)); + if(anode == null) + //Make sure anode is not NULL if couldn't find a closeset node + anode = (ASTNode)compilationUnit.types().get(0); System.err.println(lineNumber + " Nearest ASTNode to PRED " + getNodeAsString(anode)); @@ -592,7 +553,7 @@ public class ASTGenerator { .getStructuralProperty(TypeDeclaration.SUPERCLASS_TYPE_PROPERTY); System.out.println("Superclass " + st.getName()); for (CompletionCandidate can : getMembersForType(st.getName() - .toString(), word2, noCompare)) { + .toString(), word2, noCompare,false)) { candidates.add(can); } //findDeclaration(st.getName()) @@ -696,11 +657,22 @@ public class ASTGenerator { } else { if (stp != null) { candidates = getMembersForType(stp.getName().toString(), - child.toString(), noCompare); + child.toString(), noCompare,false); } } } + else if(word.length() - word2.length() == 1){ +// int dC = 0; +// for (int i = 0; i < word.length(); i++) { +// if(word.charAt(i) == '.') +// dC++; +// } +// if(dC == 1 && word.charAt(word.length() - 1) == '.'){ + System.out.println("All members of " + word2); + candidates = getMembersForType(word2, "", true,true); +// } + } } @@ -729,9 +701,16 @@ public class ASTGenerator { } + /** + * Loads classes from .jar files in sketch classpath + * @param typeName + * @param child + * @param noCompare + * @return + */ public ArrayList getMembersForType(String typeName, String child, - boolean noCompare) { + boolean noCompare,boolean staticOnly) { ArrayList candidates = new ArrayList(); RegExpResourceFilter regExpResourceFilter; regExpResourceFilter = new RegExpResourceFilter(".*", typeName + ".class"); @@ -750,6 +729,8 @@ public class ASTGenerator { errorCheckerService.classLoader); for (Method method : probableClass.getMethods()) { + if(!Modifier.isStatic(method.getModifiers()) && staticOnly) + continue; StringBuffer label = new StringBuffer(method.getName() + "("); for (int i = 0; i < method.getParameterTypes().length; i++) { label.append(method.getParameterTypes()[i].getSimpleName()); @@ -768,6 +749,8 @@ public class ASTGenerator { .toString())); } for (Field field : probableClass.getFields()) { + if(!Modifier.isStatic(field.getModifiers()) && staticOnly) + continue; if (noCompare) candidates.add(new CompletionCandidate(field.getName(), @@ -804,14 +787,19 @@ public class ASTGenerator { System.out.println("Class: " + candidate.getDefiningClass()); if (candidate.getDefiningClass().equals("processing.core.PApplet")) - jdocLabel.setText(jdocMap.get(key)); + { javadocPane.setText(jdocMap.get(key)); + //jdocWindow.setVisible(true); + //editor.textArea().requestFocus() + } else - jdocLabel.setText(""); + javadocPane.setText(""); + javadocPane.setCaretPosition(0); } }); break; } } + //jdocWindow.setVisible(false); } @@ -876,15 +864,18 @@ public class ASTGenerator { return null; } - ASTNode retNode = nodes.get(0); - for (ASTNode cNode : nodes) { - if (getLineNumber(cNode) <= lineNumber) - retNode = cNode; - else - break; - } + if (nodes.size() > 0) { + ASTNode retNode = nodes.get(0); + for (ASTNode cNode : nodes) { + if (getLineNumber(cNode) <= lineNumber) + retNode = cNode; + else + break; + } - return retNode; + return retNode; + } + return null; } // static DefaultMutableTreeNode findNodeBS(DefaultMutableTreeNode tree, @@ -1800,6 +1791,10 @@ public class ASTGenerator { // .getStartPosition()); return value; } + + public void jdocWindowVisible(boolean visible){ + jdocWindow.setVisible(visible); + } public static String readFile(String path) { BufferedReader reader = null; diff --git a/pdex/src/processing/mode/experimental/CompletionPanel.java b/pdex/src/processing/mode/experimental/CompletionPanel.java index 03f0450dc..6d5eeb130 100644 --- a/pdex/src/processing/mode/experimental/CompletionPanel.java +++ b/pdex/src/processing/mode/experimental/CompletionPanel.java @@ -9,6 +9,7 @@ import java.awt.event.MouseEvent; import javax.swing.BorderFactory; import javax.swing.JList; import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.text.BadLocationException; @@ -25,6 +26,8 @@ public class CompletionPanel { private TextArea textarea; + private JScrollPane scrollPane; + public CompletionPanel(JEditTextArea textarea, int position, String subWord, CompletionCandidate[] items, Point location) { this.textarea = (TextArea) textarea; @@ -37,12 +40,14 @@ public class CompletionPanel { popupMenu.removeAll(); popupMenu.setOpaque(false); popupMenu.setBorder(null); - popupMenu.add(list = createSuggestionList(position, items), - BorderLayout.CENTER); + scrollPane = new JScrollPane(); + scrollPane.setViewportView(list = createSuggestionList(position, items)); + popupMenu.add(scrollPane, BorderLayout.CENTER); + this.textarea.errorCheckerService.astGenerator + .updateJavaDoc((CompletionCandidate) list.getSelectedValue()); popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0) + location.y); - this.textarea.errorCheckerService.astGenerator.updateJavaDoc((CompletionCandidate) list - .getSelectedValue()); + } public void hide() { @@ -53,7 +58,8 @@ public class CompletionPanel { return popupMenu.isVisible(); } - public JList createSuggestionList(final int position, final CompletionCandidate[] items) { + public JList createSuggestionList(final int position, + final CompletionCandidate[] items) { JList list = new JList(items); list.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); @@ -74,8 +80,8 @@ public class CompletionPanel { public boolean insertSelection() { if (list.getSelectedValue() != null) { try { - final String selectedSuggestion = ((CompletionCandidate) list.getSelectedValue()).toString() - .substring(subWord.length()); + final String selectedSuggestion = ((CompletionCandidate) list + .getSelectedValue()).toString().substring(subWord.length()); textarea.getDocument().insertString(insertionPosition, selectedSuggestion, null); textarea.setCaretPosition(insertionPosition @@ -91,30 +97,47 @@ public class CompletionPanel { public void hideSuggestion() { hide(); + //textarea.errorCheckerService.astGenerator.jdocWindowVisible(false); } public void moveUp() { if (list.getSelectedIndex() == 0) { + scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum()); selectIndex(list.getModel().getSize() - 1); + return; } else { int index = Math.max(list.getSelectedIndex() - 1, 0); selectIndex(index); } - textarea.errorCheckerService.astGenerator.updateJavaDoc((CompletionCandidate) list - .getSelectedValue()); + int step = scrollPane.getVerticalScrollBar().getMaximum() + / list.getModel().getSize(); + scrollPane.getVerticalScrollBar().setValue(scrollPane + .getVerticalScrollBar() + .getValue() + - step); + textarea.errorCheckerService.astGenerator + .updateJavaDoc((CompletionCandidate) list.getSelectedValue()); } public void moveDown() { if (list.getSelectedIndex() == list.getModel().getSize() - 1) { + scrollPane.getVerticalScrollBar().setValue(0); selectIndex(0); + return; } else { int index = Math.min(list.getSelectedIndex() + 1, list.getModel() .getSize() - 1); selectIndex(index); } - textarea.errorCheckerService.astGenerator.updateJavaDoc((CompletionCandidate) list - .getSelectedValue()); + textarea.errorCheckerService.astGenerator + .updateJavaDoc((CompletionCandidate) list.getSelectedValue()); + int step = scrollPane.getVerticalScrollBar().getMaximum() + / list.getModel().getSize(); + scrollPane.getVerticalScrollBar().setValue(scrollPane + .getVerticalScrollBar() + .getValue() + + step); } private void selectIndex(int index) { diff --git a/pdex/src/processing/mode/experimental/ErrorCheckerService.java b/pdex/src/processing/mode/experimental/ErrorCheckerService.java index 57d6ab3dd..fbdfbb953 100644 --- a/pdex/src/processing/mode/experimental/ErrorCheckerService.java +++ b/pdex/src/processing/mode/experimental/ErrorCheckerService.java @@ -270,7 +270,7 @@ public class ErrorCheckerService implements Runnable{ if (problems.length == 0 && editor.compilationCheckEnabled) { //mainClassOffset++; // just a hack. - astGenerator.buildAST(); + astGenerator.buildAST(cu); sourceCode = xqpreproc.doYourThing(sourceCode, programImports); prepareCompilerClasspath(); // mainClassOffset = xqpreproc.mainClassOffset; // tiny, but diff --git a/pdex/src/processing/mode/experimental/JavadocHelper.java b/pdex/src/processing/mode/experimental/JavadocHelper.java new file mode 100644 index 000000000..7f59da5ab --- /dev/null +++ b/pdex/src/processing/mode/experimental/JavadocHelper.java @@ -0,0 +1,122 @@ +package processing.mode.experimental; + +import java.io.File; +import java.io.FileFilter; +import java.util.Iterator; +import java.util.TreeMap; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +public class JavadocHelper { + + public static void loadJavaDoc(TreeMap jdocMap){ + Document doc; + + //Pattern pat = Pattern.compile("\\w+"); + try { + File p5Ref = new File( + "/home/quarkninja/Workspaces/processing-workspace/processing/build/linux/work/modes/java/reference"); + FileFilter fileFilter = new FileFilter() { + 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 : p5Ref.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 (Iterator it = elm.iterator(); it.hasNext();) { + Element ele = (Element) it.next(); + msg = "
" + + ele.html() + "
"; + //mat.replaceAll(""); + msg = msg.replaceAll("img src=\"", "img src=\"" + + p5Ref.toURI().toURL().toString() + "/"); + //System.out.println(ele.text()); + } + jdocMap.put(methodName, msg); + } + + /* File javaDocFile = new File( + "/home/quarkninja/Workspaces/processing-workspace/processing/build/javadoc/core/processing/core/PApplet.html"); + //SimpleOpenNI.SimpleOpenNI + doc = Jsoup.parse(javaDocFile, null); + + String msg = ""; + Elements elm = doc.getElementsByTag("pre"); +// Elements desc = doc.getElementsByTag("dl"); + //System.out.println(elm.toString()); + + for (Iterator iterator = elm.iterator(); iterator.hasNext();) { + Element element = (Element) iterator.next(); + + //System.out.println(element.text()); +// if (element.nextElementSibling() != null) +// System.out.println(element.nextElementSibling().text()); + //System.out.println("-------------------"); + msg = "
" + + element.html() + + element.nextElementSibling() + + "
"; + int k = 0; + Matcher matcher = pat.matcher(element.text()); + ArrayList parts = new ArrayList(); + while (matcher.find()) { +// System.out.print("Start index: " + matcher.start()); +// System.out.print(" End index: " + matcher.end() + " "); + if (k == 0 && !matcher.group().equals("public")) { + k = -1; + break; + } + // System.out.print(matcher.group() + " "); + parts.add(matcher.group()); + k++; + } + if (k <= 0 || parts.size() < 3) + continue; + int i = 0; + if (parts.get(i).equals("public")) + i++; + if (parts.get(i).equals("static") || parts.get(i).equals("final") + || parts.get(i).equals("class")) + i++; + if (parts.get(i).equals("static") || parts.get(i).equals("final")) + i++; +// System.out.println("Ret Type " + parts.get(i)); + + i++; // return type + + //System.out.println("Name " + parts.get(i)); + jdocMap.put(parts.get(i), msg); + } + System.out.println("JDoc loaded"); +// for (String key : jdocMap.keySet()) { +// System.out.println("Method: " + key); +// System.out.println("Method: " + jdocMap.get(key)); +// } + * + */ + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/pdex/src/processing/mode/experimental/TextArea.java b/pdex/src/processing/mode/experimental/TextArea.java index 0092ed36d..e07cda0b3 100644 --- a/pdex/src/processing/mode/experimental/TextArea.java +++ b/pdex/src/processing/mode/experimental/TextArea.java @@ -612,13 +612,13 @@ public class TextArea extends JEditTextArea { return; } suggestion = new CompletionPanel(this, position, subWord, items, location); - requestFocusInWindow(); -// SwingUtilities.invokeLater(new Runnable() { -// @Override -// public void run() { -// requestFocusInWindow(); -// } -// }); +// requestFocusInWindow(); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + requestFocusInWindow(); + } + }); } private void hideSuggestion() {