[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/codecompletion/revisited/modules SourceModu
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2006-03-18 17:11:54
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/revisited/modules In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30488/src/org/python/pydev/editor/codecompletion/revisited/modules Modified Files: SourceModule.java Log Message: Ok, the nested modules thing still needs to be better checked, but some 'basic' support has been added (check SourceModule.getGlobalTokens) Index: SourceModule.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/revisited/modules/SourceModule.java,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** SourceModule.java 2 Mar 2006 17:11:43 -0000 1.36 --- SourceModule.java 18 Mar 2006 17:11:50 -0000 1.37 *************** *** 13,16 **** --- 13,17 ---- import org.python.parser.SimpleNode; + import org.python.parser.ast.Assign; import org.python.parser.ast.Attribute; import org.python.parser.ast.ClassDef; *************** *** 148,154 **** /** ! * @see org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule#getGlobalTokens(java.lang.String) */ ! public IToken[] getGlobalTokens(ICompletionState state, ICodeCompletionASTManager manager) { IToken[] t = getTokens(GlobalModelVisitor.GLOBAL_TOKENS); --- 149,155 ---- /** ! * @see org.python.pydev.core.IModule#getGlobalTokens(org.python.pydev.core.ICompletionState, org.python.pydev.core.ICodeCompletionASTManager) */ ! public IToken[] getGlobalTokens(ICompletionState initialState, ICodeCompletionASTManager manager) { IToken[] t = getTokens(GlobalModelVisitor.GLOBAL_TOKENS); *************** *** 156,210 **** SourceToken[] tokens = (SourceToken[]) t; for (int i = 0; i < tokens.length; i++) { ! if(tokens[i].getRepresentation().equals(state.getActivationToken())){ ! ! SimpleNode a = tokens[i].getAst(); ! ! //COMPLETION: get the completions for the whole hierarchy if this is a class!! ! List modToks = new ArrayList(Arrays.asList(GlobalModelVisitor.getTokens(a, GlobalModelVisitor.INNER_DEFS, name))); ! ! try { ! if (a instanceof ClassDef) { ! ClassDef c = (ClassDef) a; ! for (int j = 0; j < c.bases.length; j++) { ! if (c.bases[j] instanceof Name) { ! Name n = (Name) c.bases[j]; ! String base = n.id; ! //An error in the programming might result in an error. ! // ! //e.g. The case below results in a loop. ! // ! //class A(B): ! // ! // def a(self): ! // pass ! // ! //class B(A): ! // ! // def b(self): ! // pass ! state = state.getCopy(); ! state.setActivationToken(base); ! ! state.checkMemory(this, base); ! ! final IToken[] comps = manager.getCompletionsForModule(this, state); ! modToks.addAll(Arrays.asList(comps)); ! } else if (c.bases[j] instanceof Attribute) { ! Attribute attr = (Attribute) c.bases[j]; ! String s = NodeUtils.getFullRepresentationString(attr); ! ! state = state.getCopy(); ! state.setActivationToken(s); ! final IToken[] comps = manager.getCompletionsForModule(this, state); ! modToks.addAll(Arrays.asList(comps)); } } ! } - } catch (CompletionRecursionException e) { - // let's return what we have so far... } ! ! return (IToken[]) modToks.toArray(new IToken[0]); } } --- 157,226 ---- SourceToken[] tokens = (SourceToken[]) t; for (int i = 0; i < tokens.length; i++) { ! String activationToken = initialState.getActivationToken(); ! SourceToken token = tokens[i]; ! String rep = token.getRepresentation(); ! ! SimpleNode ast = token.getAst(); ! ! if(activationToken.length() > rep.length() && activationToken.startsWith(rep)){ ! int iActTok = 0; ! String[] actToks = activationToken.split("\\."); ! if(actToks[iActTok].equals(rep)){ ! String act = actToks[iActTok]; ! //System.out.println("Now we have to find act..."+act+"(which is a definition of:"+rep+")"); ! try { ! Definition[] definitions; ! String value = activationToken; ! while(true){ ! if(iActTok > actToks.length){ ! break; //unable to find it ! } ! definitions = findDefinition(value, token.getLineDefinition(), token.getColDefinition(), manager.getNature(), new ArrayList<FindInfo>()); ! if(definitions.length == 1){ ! Definition d = definitions[0]; ! if(d.ast instanceof Assign){ ! Assign assign = (Assign) d.ast; ! value = NodeUtils.getRepresentationString(assign.value); ! System.out.println(value); ! definitions = findDefinition(value, d.line, d.col, manager.getNature(), new ArrayList<FindInfo>()); ! ! }else if(d.ast instanceof ClassDef){ ! IToken[] toks = (IToken[]) getToks(initialState, manager, d.ast).toArray(new IToken[0]); ! if(iActTok == actToks.length-1){ ! return toks; ! } ! value = d.value; ! ! }else if (d.ast instanceof Name){ ! FindDefinitionModelVisitor visitor = new FindDefinitionModelVisitor(actToks[actToks.length-1], d.line, d.col, d.module); ! ClassDef classDef = d.scope.getClassDef(); ! classDef.accept(visitor); ! d = visitor.definitions.get(0); ! value = d.value; ! ! }else if (d.ast == null && d.module != null && d.value.length() == 0){ ! ICompletionState copy = initialState.getCopy(); ! copy.setActivationToken(value); ! IToken[] completionsForModule = manager.getCompletionsForModule(this, copy); ! return completionsForModule; ! ! }else{ ! //System.out.println("breaking"); ! break; ! } ! }else{ ! ICompletionState copy = initialState.getCopy(); ! copy.setActivationToken(value); ! IToken[] completionsForModule = manager.getCompletionsForModule(this, copy); ! return completionsForModule; } + iActTok++; } ! } catch (Exception e) { ! e.printStackTrace(); } } ! } else if(rep.equals(activationToken)){ ! return (IToken[]) getToks(initialState, manager, ast).toArray(new IToken[0]); } } *************** *** 215,218 **** --- 231,290 ---- } + /** + * @param initialState + * @param manager + * @param ast + * @return + */ + private List<IToken> getToks(ICompletionState initialState, ICodeCompletionASTManager manager, SimpleNode ast) { + List<IToken> modToks = modToks = new ArrayList<IToken>(Arrays.asList(GlobalModelVisitor.getTokens(ast, GlobalModelVisitor.INNER_DEFS, name)));//name = moduleName + + try { + //COMPLETION: get the completions for the whole hierarchy if this is a class!! + ICompletionState state; + if (ast instanceof ClassDef) { + ClassDef c = (ClassDef) ast; + for (int j = 0; j < c.bases.length; j++) { + if (c.bases[j] instanceof Name) { + Name n = (Name) c.bases[j]; + String base = n.id; + //An error in the programming might result in an error. + // + //e.g. The case below results in a loop. + // + //class A(B): + // + // def a(self): + // pass + // + //class B(A): + // + // def b(self): + // pass + state = initialState.getCopy(); + state.setActivationToken(base); + + state.checkMemory(this, base); + + final IToken[] comps = manager.getCompletionsForModule(this, state); + modToks.addAll(Arrays.asList(comps)); + } else if (c.bases[j] instanceof Attribute) { + Attribute attr = (Attribute) c.bases[j]; + String s = NodeUtils.getFullRepresentationString(attr); + + state = initialState.getCopy(); + state.setActivationToken(s); + final IToken[] comps = manager.getCompletionsForModule(this, state); + modToks.addAll(Arrays.asList(comps)); + } + } + + } + } catch (CompletionRecursionException e) { + // let's return what we have so far... + } + return modToks; + } + public Definition[] findDefinition(String rep, int line, int col, IPythonNature nature, List<FindInfo> lFindInfo) throws Exception{ //the line passed in starts at 1 and the lines for the visitor start at 0 |