[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/correctionassist PythonCorrectionProcessor.
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-09-24 16:41:30
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/correctionassist In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15928/src/org/python/pydev/editor/correctionassist Modified Files: PythonCorrectionProcessor.java Log Message: Decoration support / Making correction assistant on Ctrl+1 Index: PythonCorrectionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PythonCorrectionProcessor.java 23 Sep 2004 18:06:31 -0000 1.2 --- PythonCorrectionProcessor.java 24 Sep 2004 16:41:12 -0000 1.3 *************** *** 18,22 **** --- 18,27 ---- import org.python.pydev.editor.PyEdit; import org.python.pydev.editor.actions.PyAction; + import org.python.pydev.editor.actions.PyBackspace; import org.python.pydev.editor.actions.PySelection; + import org.python.pydev.editor.model.AbstractNode; + import org.python.pydev.editor.model.ClassNode; + import org.python.pydev.editor.model.FunctionNode; + import org.python.pydev.editor.model.ModelUtils; /** *************** *** 65,70 **** PySelection ps = new PySelection(edit, false); ! List results = getAssignToResults(ps); ! results.addAll(getCreations(ps)); return (ICompletionProposal[]) results.toArray(new ICompletionProposal[0]); --- 70,83 ---- PySelection ps = new PySelection(edit, false); ! List results = new ArrayList(); ! try { ! results.addAll(getAssignToResults(ps)); ! } catch (BadLocationException e) { ! } ! ! try { ! results.addAll(getCreations(ps)); ! } catch (BadLocationException e1) { ! } return (ICompletionProposal[]) results.toArray(new ICompletionProposal[0]); *************** *** 74,79 **** * @param ps * @return */ ! private List getCreations(PySelection ps) { List l = new ArrayList(); --- 87,93 ---- * @param ps * @return + * @throws BadLocationException */ ! private List getCreations(PySelection ps) throws BadLocationException { List l = new ArrayList(); *************** *** 83,96 **** //here, any callable can be used to create a new method or class. ! //we have to parse it, because we have to discover the parameters for the new class. return l; } /** * @param ps */ ! private List getAssignToResults(PySelection ps) { List l = new ArrayList(); --- 97,198 ---- //here, any callable can be used to create a new method or class. ! //we have to parse it, because we have to discover the parameters for the new class or method. + String callName = getBeforeParentesisTok(ps); + + if(callName.length() == 0){ + return l; + } + + String params = "("+getInsideParentesisTok(ps)+")"; + int firstCharPosition = PyAction.getFirstCharRelativePosition(ps.doc, ps.absoluteCursorOffset); + String indentation = PyBackspace.getStaticIndentationString(); + + + String delim = PyAction.getDelimiter(ps.doc, 0); + String cls = "class "+callName+":"+delim+delim; + cls += indentation+"def __init__"+params+":"+delim; + cls += indentation+indentation+"pass"+delim; + + String method = "def "+callName+params+":"+delim+indentation+"pass"+delim; + + if (firstCharPosition == 0){ //we are in the global context + + int newPos = 0; + int lineOfOffset = ps.doc.getLineOfOffset(ps.absoluteCursorOffset); + + if(lineOfOffset > 0){ + newPos = ps.doc.getLineInformation(lineOfOffset - 1).getOffset(); + } + + l.add(new CompletionProposal(cls, newPos, 0, cls.length()+1, null, + "Create new class (global context)", null, null)); + + l.add(new CompletionProposal(method, newPos, 0, method.length()+1, null, + "Create new method (global context)", null, null)); + + }else{ //we are in a method or class context + + AbstractNode root = edit.getPythonModel(); + if (root == null){ + return l; + } + + //now, discover in which node we are right now... + AbstractNode current = ModelUtils.getLessOrEqualNode(root, ps.absoluteCursorOffset, ps.doc); + while (current != null) { + if (current instanceof FunctionNode + || current instanceof ClassNode) { + break; + } + current = ModelUtils.getPreviousNode(current); + } + + + if(ps.selection.indexOf("self.") != -1){ //we are going for a class method. + + if (current instanceof FunctionNode) { //if we are in a class, here we are within a method. + FunctionNode node = (FunctionNode) current; + + int newPos = 0; + int lineOfOffset = node.getStart().line; + + if(lineOfOffset > 0){ + newPos = ps.doc.getLineInformation(lineOfOffset).getOffset(); + } + + int col = node.getStart().column; + String newIndent = indentation; + + while(newIndent.length() < col){ + newIndent += indentation; + } + String atStart = newIndent.replaceFirst(indentation, ""); + method = method.replaceAll(indentation, newIndent); + method = atStart+method+delim; + + l.add(new CompletionProposal(method, newPos, 0, method.length()-4, null, + "Create new method (in class)", null, null)); + } + + }else{ //we are going for a class or a global method. + //TODO: End this. + // l.add(new CompletionProposal(callName, 0, 0, 0, null, + // "Create new class", null, null)); + // + // l.add(new CompletionProposal(callName, 0, 0, 0, null, + // "Create new method", null, null)); + } + } return l; } + /** * @param ps + * @throws BadLocationException */ ! private List getAssignToResults(PySelection ps) throws BadLocationException { List l = new ArrayList(); *************** *** 122,159 **** // self.|result| = 1+1 ! String string = ps.selection.replaceAll("\\(*\\)", "()"); ! try { ! int firstCharPosition = PyAction.getFirstCharPosition(ps.doc, ps.absoluteCursorOffset); ! int i; ! String callName = "result"; ! if ((i = string.indexOf("()")) != -1) { ! callName = ""; ! for (int j = i-1; j >= 0 && stillInTok(string, j); j--) { ! callName = string.charAt(j) + callName; ! } ! ! if(callName.length()>0){ ! //all that just to change first char to lower case. ! char[] ds = callName.toCharArray(); ! ds[0] = (""+ds[0]).toLowerCase().charAt(0); ! callName = new String(ds); ! } ! } ! callName += " = "; ! l.add(new CompletionProposal(callName, firstCharPosition, 0, 0, null, ! "Assign to new local variable", null, null)); ! ! l.add(new CompletionProposal("self." + callName, firstCharPosition, 0, 0, null, ! "Assign to new field", null, null)); ! ! } catch (BadLocationException e) { ! e.printStackTrace(); } } ! return l; } --- 224,278 ---- // self.|result| = 1+1 ! String callName = getBeforeParentesisTok(ps); ! if(callName.length() > 0){ ! //all that just to change first char to lower case. ! char[] ds = callName.toCharArray(); ! ds[0] = (""+ds[0]).toLowerCase().charAt(0); ! callName = new String(ds); ! }else{ ! callName = "result"; ! } ! int firstCharPosition = PyAction.getFirstCharPosition(ps.doc, ps.absoluteCursorOffset); ! callName += " = "; ! l.add(new CompletionProposal(callName, firstCharPosition, 0, 0, null, ! "Assign to new local variable", null, null)); ! ! l.add(new CompletionProposal("self." + callName, firstCharPosition, 0, 5, null, ! "Assign to new field", null, null)); ! } ! return l; ! } ! /** ! * @param ps ! * @return ! */ ! private String getInsideParentesisTok(PySelection ps) { ! int beg = ps.selection.indexOf('(')+1; ! int end = ps.selection.indexOf(')'); ! return ps.selection.substring(beg, end); ! } ! /** ! * @param ps ! * @return string with the token or empty token if not found. ! */ ! private String getBeforeParentesisTok(PySelection ps) { ! String string = ps.selection.replaceAll("\\(.*\\)", "()"); ! ! int i; ! ! String callName = ""; ! if ((i = string.indexOf("()")) != -1) { ! callName = ""; ! ! for (int j = i-1; j >= 0 && stillInTok(string, j); j--) { ! callName = string.charAt(j) + callName; } + } ! return callName; } *************** *** 166,170 **** char c = string.charAt(j); ! return c != '\n' && c != '\r' && c != ' ' && c != '.' && c != '(' && c != ')'; } --- 285,289 ---- char c = string.charAt(j); ! return c != '\n' && c != '\r' && c != ' ' && c != '.' && c != '(' && c != ')' && c != ',' && c != ']' && c != '['; } |