diff --git a/pdex/Todo, GSoC 2013.txt b/pdex/Todo, GSoC 2013.txt index 8a2d2fe73..70980f35b 100644 --- a/pdex/Todo, GSoC 2013.txt +++ b/pdex/Todo, GSoC 2013.txt @@ -42,6 +42,7 @@ Finer details * Diamond operator isn't supported for now. Bummer. * Icons for completions? Or overkill right now? +x 'Show Usage' menu item added x Show declaring class for completions x! Ignore String case while finding completion candidates x Multiple 3rd party classes found in various packages. Not a chance no more. @@ -69,7 +70,8 @@ Refactoring =========== * Undo misbehaves here, handle carefully. -* Add support for word select on right click and rename, mouse co-ordinates need to obtained carefully + +x Add support for word select on right click and rename, mouse co-ordinates need to obtained carefully Refactoring would work only when code is compiler error free. I plan to do a find replace type op on the compile ready code. 1. First identify the declaration of the variable in the AST. We'll then make a list of all its occurrences. diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index 1a81009c2..0d68ada2b 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -1805,17 +1805,19 @@ public class ASTGenerator { private void refactorIt(){ String newName = txtRenameField.getText(); + String selText = lastClickedWord == null ? editor.ta.getSelectedText() + : lastClickedWord; DefaultMutableTreeNode defCU = findAllOccurrences(); if(defCU == null){ - editor.statusError("Can't locate definition of " + editor.ta.getSelectedText()); + editor.statusError("Can't locate definition of " + selText); return; } treeRename.setModel(new DefaultTreeModel(defCU)); ((DefaultTreeModel) treeRename.getModel()).reload(); - frmOccurenceList.setTitle("Usage of " + editor.ta.getSelectedText()); + frmOccurenceList.setTitle("Usage of " + selText); frmOccurenceList.setVisible(true); int lineOffsetDisplacementConst = newName.length() - - editor.ta.getSelectedText().length(); + - selText.length(); HashMap lineOffsetDisplacement = new HashMap(); // I need to store the pde and java offsets beforehand because once @@ -1857,10 +1859,13 @@ public class ASTGenerator { errorCheckerService.runManualErrorCheck(); frmOccurenceList.setVisible(false); frmRename.setVisible(false); + lastClickedWord = null; + lastClickedWordNode = null; } public void handleShowUsage(){ - if(editor.ta.getSelectedText() == null){ + System.out.println("Last clicked word:" + lastClickedWord); + if(lastClickedWord == null && editor.ta.getSelectedText() == null){ editor.statusError("Highlight the class/function/variable name first"); return; } @@ -1870,20 +1875,39 @@ public class ASTGenerator { return; } DefaultMutableTreeNode defCU = findAllOccurrences(); + String selText = lastClickedWord == null ? editor.ta.getSelectedText() + : lastClickedWord; if(defCU == null){ - editor.statusError("Can't locate definition of " + editor.ta.getSelectedText()); + editor.statusError("Can't locate definition of " + selText); return; } treeRename.setModel(new DefaultTreeModel(defCU)); ((DefaultTreeModel) treeRename.getModel()).reload(); - frmOccurenceList.setTitle("Usage of " + editor.ta.getSelectedText()); + frmOccurenceList.setTitle("Usage of " + selText); frmOccurenceList.setVisible(true); + lastClickedWord = null; + lastClickedWordNode = null; } + protected String lastClickedWord = null; + protected ASTNodeWrapper lastClickedWordNode = null; + + public String getLastClickedWord() { + return lastClickedWord; + } + + public void setLastClickedWord(int lineNumber, String lastClickedWord, int offset) { + this.lastClickedWord = lastClickedWord; + lastClickedWordNode = getASTNodeAt(lineNumber, lastClickedWord, offset, false); + System.out.println("Last clicked node: " + lastClickedWordNode); + } + private DefaultMutableTreeNode findAllOccurrences(){ - String selText = editor.ta.getSelectedText(); + System.out.println("Last clicked word:" + lastClickedWord); + String selText = lastClickedWord == null ? editor.ta.getSelectedText() + : lastClickedWord; int line = editor.ta.getSelectionStartLine(); - System.out.println(editor.ta.getSelectedText() + System.out.println(selText + "<- offsets " + (line) + ", " @@ -1894,11 +1918,14 @@ public class ASTGenerator { .getLineStartOffset(line))); int offwhitespace = editor.ta .getLineStartNonWhiteSpaceOffset(line); - ASTNodeWrapper wnode = getASTNodeAt(line - + errorCheckerService.mainClassOffset, - selText, - editor.ta.getSelectionStart() - - offwhitespace, false); + ASTNodeWrapper wnode; + if (lastClickedWord == null) { + wnode = getASTNodeAt(line + errorCheckerService.mainClassOffset, selText, + editor.ta.getSelectionStart() - offwhitespace, false); + } + else{ + wnode = lastClickedWordNode; + } if(wnode.getNode() == null){ return null; } @@ -2080,7 +2107,8 @@ public class ASTGenerator { } public void handleRefactor(){ - if(editor.ta.getSelectedText() == null){ + System.out.println("Last clicked word:" + lastClickedWord); + if(lastClickedWord == null && editor.ta.getSelectedText() == null){ editor.statusError("Highlight the class/function/variable name first"); return; } @@ -2094,10 +2122,12 @@ public class ASTGenerator { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { + String selText = lastClickedWord == null ? editor.ta.getSelectedText() + : lastClickedWord; frmOccurenceList.setTitle("All occurrences of " - + editor.ta.getSelectedText()); + + selText); lblRefactorOldName.setText("Current name: " - + editor.ta.getSelectedText()); + + selText); txtRenameField.requestFocus(); } }); diff --git a/pdex/src/processing/mode/experimental/TextArea.java b/pdex/src/processing/mode/experimental/TextArea.java index 41d4115ec..40feea600 100644 --- a/pdex/src/processing/mode/experimental/TextArea.java +++ b/pdex/src/processing/mode/experimental/TextArea.java @@ -247,23 +247,85 @@ public class TextArea extends JEditTextArea { } - - private String fetchPhrase(KeyEvent evt) { - char keyChar = evt.getKeyChar(); - if (keyChar == KeyEvent.VK_ENTER) { - //System.out.println("Enter keypress."); + + + private String fetchPhrase(MouseEvent evt) { + System.out.println("--handle Mouse Right Click--"); + int off = xyToOffset(evt.getX(), evt.getY()); + if (off < 0) return null; + int line = getLineOfOffset(off); + if (line < 0) + return null; + String s = getLineText(line); + if (s == null) + return null; + else if (s.length() == 0) + return null; + else { + int x = xToOffset(line, evt.getX()), x2 = x + 1, x1 = x - 1; + int xLS = off - getLineStartNonWhiteSpaceOffset(line); + System.out.println("x=" + x); + if (x < 0 || x >= s.length()) + return null; + String word = s.charAt(x) + ""; + if (s.charAt(x) == ' ') + return null; + if (!(Character.isLetterOrDigit(s.charAt(x)) || s.charAt(x) == '_' || s + .charAt(x) == '$')) + return null; + int i = 0; + while (true) { + i++; + if (x1 >= 0 && x1 < s.length()) { + if (Character.isLetter(s.charAt(x1)) || s.charAt(x1) == '_') { + word = s.charAt(x1--) + word; + } else + x1 = -1; + } else + x1 = -1; + + if (x2 >= 0 && x2 < s.length()) { + if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_' + || s.charAt(x2) == '$') + word = word + s.charAt(x2++); + else + x2 = -1; + } else + x2 = -1; + + if (x1 < 0 && x2 < 0) + break; + if (i > 200) { + // time out! + System.err.println("Whoopsy! :P"); + break; + } + } + if (Character.isDigit(word.charAt(0))) + return null; + System.out.println("Mouse click, word: " + word.trim()); + errorCheckerService.astGenerator.setLastClickedWord(line + + errorCheckerService.mainClassOffset, word, xLS); + return word.trim(); } + } + private String fetchPhrase(KeyEvent evt) { + if (evt != null) { + char keyChar = evt.getKeyChar(); + if (keyChar == KeyEvent.VK_ENTER) { + //System.out.println("Enter keypress."); + return null; + } // if (keyChar == KeyEvent.VK_BACK_SPACE || keyChar == KeyEvent.VK_DELETE) // ; // accepted these keys - else if (keyChar == KeyEvent.VK_ESCAPE) { - //System.out.println("ESC keypress. fetchPhrase()"); - return null; + else if (keyChar == KeyEvent.VK_ESCAPE) { + //System.out.println("ESC keypress. fetchPhrase()"); + return null; + } else if (keyChar == KeyEvent.CHAR_UNDEFINED) { + return null; + } } - else if (keyChar == KeyEvent.CHAR_UNDEFINED) { - return null; - } - int off = getCaretPosition(); System.out.print("off " + off); if (off < 0) @@ -579,12 +641,18 @@ public class TextArea extends JEditTextArea { editor.gutterDblClicked(line); } } - } else { - // forward to standard listeners - for (MouseListener ml : mouseListeners) { - ml.mousePressed(me); - } + return; } + + if (me.getButton() == MouseEvent.BUTTON3) { + fetchPhrase(me); + } + + // forward to standard listeners + for (MouseListener ml : mouseListeners) { + ml.mousePressed(me); + } + } @Override diff --git a/pdex/src/processing/mode/experimental/TextAreaPainter.java b/pdex/src/processing/mode/experimental/TextAreaPainter.java index 6a06e4d9b..b1ab4a029 100644 --- a/pdex/src/processing/mode/experimental/TextAreaPainter.java +++ b/pdex/src/processing/mode/experimental/TextAreaPainter.java @@ -103,6 +103,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter { return; else { int x = ta.xToOffset(line, evt.getX()), x2 = x + 1, x1 = x - 1; + System.out.println("x="+x); int xLS = off - ta.getLineStartNonWhiteSpaceOffset(line); if (x < 0 || x >= s.length()) return;