Thread: [Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/correctionassist FixCompletionProposal.java
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-10-19 18:58:37
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/correctionassist In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24845/src/org/python/pydev/editor/correctionassist Modified Files: PythonCorrectionProcessor.java Added Files: FixCompletionProposal.java Log Message: New proposals on quick fix. Index: PythonCorrectionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/correctionassist/PythonCorrectionProcessor.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PythonCorrectionProcessor.java 8 Oct 2004 16:38:43 -0000 1.5 --- PythonCorrectionProcessor.java 19 Oct 2004 18:58:25 -0000 1.6 *************** *** 83,100 **** 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]); } /** * @param ps --- 83,189 ---- PySelection ps = new PySelection(edit, false); ! List results = new ArrayList(); ! String sel = getLine(ps); ! ! //at least some text must be selected ! if(ps.textSelection.getLength() > 0){ ! ! try { ! results.addAll(getTryProps(ps)); ! } catch (BadLocationException e) { ! } ! } else if(sel.indexOf("import") == -1){ ! ! try { ! results.addAll(getAssignToResults(ps)); ! } catch (BadLocationException e) { ! } ! ! try { ! results.addAll(getCreations(ps)); ! } catch (BadLocationException e1) { ! } ! }else{ ! ! try { ! results.addAll(getMoveImports(ps)); ! } catch (BadLocationException e1) { ! } } + return (ICompletionProposal[]) results.toArray(new ICompletionProposal[0]); } + + + /** + * @param ps + * @return + */ + private List getTryProps(PySelection ps) throws BadLocationException { + ArrayList l = new ArrayList(); + String indentation = PyBackspace.getStaticIndentationString(); + + int start = ps.startLine.getOffset(); + int end = ps.endLine.getOffset()+ps.endLine.getLength(); + + String string = ps.doc.get(start, end-start); + String delimiter = PyAction.getDelimiter(ps.doc, 0); + + int firstCharPosition = PyAction.getFirstCharRelativePosition(ps.doc, start); + String startIndent = ""; + int i = 0; + while(i < firstCharPosition){ + startIndent += " "; + i++; + } + + int finRelNewPos; + int excRelNewPos; + string = indentation+ string.replaceAll(delimiter, delimiter+indentation); + String except = startIndent+"try:"+delimiter+string+delimiter; + except += startIndent+"except:"+delimiter; + excRelNewPos = except.length() - delimiter.length() -1; + except += startIndent+indentation+"raise"; + + String finall = startIndent+"try:"+delimiter+string+delimiter; + finall += startIndent+"finally:"+delimiter; + finall += startIndent+indentation; + finRelNewPos = finall.length(); + finall += "pass"; + + l.add(new CompletionProposal(except, start, end-start, excRelNewPos, null, + "Surround with try..except", null, null)); + + l.add(new CompletionProposal(finall, start, end-start, finRelNewPos, null, + "Surround with try..finally", null, null)); + + return l; + } + + /** + * @param ps + * @return + */ + private List getMoveImports(PySelection ps) throws BadLocationException { + ArrayList l = new ArrayList(); + String sel = getLine(ps).trim(); + + int i = sel.indexOf("import"); + if(ps.startLineIndex != ps.endLineIndex) + return l; + + + String delimiter = PyAction.getDelimiter(ps.doc, 0); + + if(i != -1){ + l.add(new FixCompletionProposal(sel+delimiter, 0, 0, ps.startLine.getOffset(), null, + "Move import to global scope", null, null, ps.startLineIndex+1)); + } + return l; + } + /** * @param ps *************** *** 104,109 **** private List getCreations(PySelection ps) throws BadLocationException { List l = new ArrayList(); ! if (ps.selection.trim().length() == 0) { return l; } --- 193,200 ---- private List getCreations(PySelection ps) throws BadLocationException { List l = new ArrayList(); + String sel = getLine(ps); ! ! if (sel.trim().length() == 0) { return l; } *************** *** 125,129 **** String delim = PyAction.getDelimiter(ps.doc, 0); ! String cls = "class "+callName+":"+delim+delim; String self = "self"; --- 216,220 ---- String delim = PyAction.getDelimiter(ps.doc, 0); ! String cls = "class "+callName+"(object):"+delim+delim; String self = "self"; *************** *** 143,155 **** 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)); method = method.replaceFirst("%s", ""); ! 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 --- 234,246 ---- if(lineOfOffset > 0){ ! newPos = ps.doc.getLineInformation(lineOfOffset).getOffset(); } ! l.add(new FixCompletionProposal(cls, newPos, 0, cls.length()+1, null, ! "Make this a new class", null, null, ps.startLineIndex+4)); method = method.replaceFirst("%s", ""); ! l.add(new FixCompletionProposal(method, newPos, 0, method.length()+1, null, ! "Make this a new method", null, null, ps.startLineIndex+2)); }else{ //we are in a method or class context *************** *** 171,175 **** ! 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. --- 262,266 ---- ! if(sel.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. *************** *** 200,209 **** }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)); } } --- 291,322 ---- }else{ //we are going for a class or a global method. ! ! while (current != null) { ! if(current instanceof ClassNode) { ! break; ! } ! current = ModelUtils.getPreviousNode(current); ! } ! if(current instanceof ClassNode){ ! ClassNode node = (ClassNode) current; ! ! int newPos = 0; ! int lineOfOffset = node.getStart().line; ! ! if(lineOfOffset > 0){ ! newPos = ps.doc.getLineInformation(lineOfOffset).getOffset(); ! } ! method = method+delim; ! ! method = method.replaceFirst("%s", ""); ! ! l.add(new CompletionProposal(method, newPos, 0, method.length(), null, ! "Create new method (in global context)", null, null)); ! ! cls = cls+delim; ! ! l.add(new CompletionProposal(cls, newPos, 0, cls.length(), null, ! "Create new class (in global context)", null, null)); ! } } } *************** *** 213,216 **** --- 326,336 ---- /** + * + */ + private String getLine(PySelection ps) { + return ps.selection.replaceAll("#.*", ""); + } + + /** * @param ps * @throws BadLocationException *************** *** 218,223 **** private List getAssignToResults(PySelection ps) throws BadLocationException { List l = new ArrayList(); ! ! if (ps.selection.trim().length() == 0) { return l; } --- 338,343 ---- private List getAssignToResults(PySelection ps) throws BadLocationException { List l = new ArrayList(); ! String sel = getLine(ps); ! if (sel.trim().length() == 0) { return l; } *************** *** 225,229 **** //first thing: check were we are and check that no single '=' exists. // '==' may happen. ! if (ps.selection.replaceAll("==", "").indexOf("=") == -1) { //second: go on and make the suggestion. --- 345,349 ---- //first thing: check were we are and check that no single '=' exists. // '==' may happen. ! if (sel.replaceAll("==", "").indexOf("=") == -1) { //second: go on and make the suggestion. *************** *** 246,250 **** // self.|result| = 1+1 ! String callName = getBeforeParentesisTok(ps); if(callName.length() > 0){ --- 366,370 ---- // self.|result| = 1+1 ! String callName = getTokToAssign(ps); if(callName.length() > 0){ *************** *** 272,279 **** * @return */ private String getInsideParentesisTok(PySelection ps) { ! int beg = ps.selection.indexOf('(')+1; ! int end = ps.selection.indexOf(')'); ! return ps.selection.substring(beg, end); } --- 392,426 ---- * @return */ + private String getTokToAssign(PySelection ps) { + String beforeParentesisTok = getBeforeParentesisTok(ps); + if(beforeParentesisTok.length() > 0){ + return beforeParentesisTok; + } + //otherwise, try to find . (ignore code after #) + String string = getLine(ps); + String callName = ""; + //get parentesis position and go backwards + + int i; + if ((i = string.lastIndexOf(".")) != -1) { + callName = ""; + + for (int j = i+1; j < string.length() && stillInTok(string, j); j++) { + callName += string.charAt(j); + } + } + return callName; + } + + /** + * @param ps + * @return + */ private String getInsideParentesisTok(PySelection ps) { ! String sel = getLine(ps); ! ! int beg = sel.indexOf('(')+1; ! int end = sel.indexOf(')'); ! return sel.substring(beg, end); } *************** *** 283,292 **** */ private String getBeforeParentesisTok(PySelection ps) { ! String string = ps.selection.replaceAll("\\(.*\\)", "()"); int i; String callName = ""; ! if ((i = string.indexOf("()")) != -1) { callName = ""; --- 430,440 ---- */ private String getBeforeParentesisTok(PySelection ps) { ! String string = getLine(ps); int i; String callName = ""; ! //get parentesis position and go backwards ! if ((i = string.indexOf("(")) != -1) { callName = ""; *************** *** 307,311 **** char c = string.charAt(j); ! return c != '\n' && c != '\r' && c != ' ' && c != '.' && c != '(' && c != ')' && c != ',' && c != ']' && c != '['; } --- 455,459 ---- char c = string.charAt(j); ! return c != '\n' && c != '\r' && c != ' ' && c != '.' && c != '(' && c != ')' && c != ',' && c != ']' && c != '[' && c != '#'; } --- NEW FILE: FixCompletionProposal.java --- /* * Created on Oct 19, 2004 * * @author Fabio Zadrozny */ package org.python.pydev.editor.correctionassist; import org.eclipse.jface.text.Assert; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; /** * A new Completion proposal because the default is final. * This differs because we can specify a line to be deleted after the completion is processed. * * @author Fabio Zadrozny */ public class FixCompletionProposal implements ICompletionProposal { /** The string to be displayed in the completion proposal popup. */ private String fDisplayString; /** The replacement string. */ private String fReplacementString; /** The replacement offset. */ private int fReplacementOffset; /** The replacement length. */ private int fReplacementLength; /** The cursor position after this proposal has been applied. */ private int fCursorPosition; /** The image to be displayed in the completion proposal popup. */ private Image fImage; /** The context information of this proposal. */ private IContextInformation fContextInformation; /** The additional info of this proposal. */ private String fAdditionalProposalInfo; private int lineToRemove; /** * Creates a new completion proposal based on the provided information. The replacement string is * considered being the display string too. All remaining fields are set to <code>null</code>. * * @param replacementString the actual string to be inserted into the document * @param replacementOffset the offset of the text to be replaced * @param replacementLength the length of the text to be replaced * @param cursorPosition the position of the cursor following the insert relative to replacementOffset */ public FixCompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, int lineToRemove) { this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null, lineToRemove); } /** * Creates a new completion proposal. All fields are initialized based on the provided information. * * @param replacementString the actual string to be inserted into the document * @param replacementOffset the offset of the text to be replaced * @param replacementLength the length of the text to be replaced * @param cursorPosition the position of the cursor following the insert relative to replacementOffset * @param image the image to display for this proposal * @param displayString the string to be displayed for the proposal * @param contextInformation the context information associated with this proposal * @param additionalProposalInfo the additional information associated with this proposal */ public FixCompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo, int lineToRemove) { Assert.isNotNull(replacementString); Assert.isTrue(replacementOffset >= 0); Assert.isTrue(replacementLength >= 0); Assert.isTrue(cursorPosition >= 0); fReplacementString= replacementString; fReplacementOffset= replacementOffset; fReplacementLength= replacementLength; fCursorPosition= cursorPosition; fImage= image; fDisplayString= displayString; fContextInformation= contextInformation; fAdditionalProposalInfo= additionalProposalInfo; this.lineToRemove = lineToRemove; } /* * @see ICompletionProposal#apply(IDocument) */ public void apply(IDocument document) { try { document.replace(fReplacementOffset, fReplacementLength, fReplacementString); if(lineToRemove >=0 && lineToRemove <= document.getNumberOfLines()){ IRegion lineInformation = document.getLineInformation(lineToRemove); document.replace(lineInformation.getOffset(), lineInformation.getLength(), ""); } } catch (BadLocationException x) { // ignore } } /* * @see ICompletionProposal#getSelection(IDocument) */ public Point getSelection(IDocument document) { if(lineToRemove >=0 && lineToRemove <= document.getNumberOfLines()){ try { IRegion lineInformation = document.getLineInformation(lineToRemove); int pos = lineInformation.getOffset(); return new Point(pos, 0); } catch (BadLocationException e) { return new Point(fReplacementOffset + fCursorPosition, 0); } }else{ return new Point(fReplacementOffset + fCursorPosition, 0); } } /* * @see ICompletionProposal#getContextInformation() */ public IContextInformation getContextInformation() { return fContextInformation; } /* * @see ICompletionProposal#getImage() */ public Image getImage() { return fImage; } /* * @see ICompletionProposal#getDisplayString() */ public String getDisplayString() { if (fDisplayString != null) return fDisplayString; return fReplacementString; } /* * @see ICompletionProposal#getAdditionalProposalInfo() */ public String getAdditionalProposalInfo() { return fAdditionalProposalInfo; } } |