Search in sources :

Example 1 with INodeAdapter

use of org.python.pydev.refactoring.ast.adapters.INodeAdapter in project Pydev by fabioz.

the class ConstructorFieldRequestProcessor method getFields.

private List<INodeAdapter> getFields(Iterator<ITreeNode> iter) {
    List<INodeAdapter> fields = new ArrayList<INodeAdapter>();
    ITreeNode field = iter.next();
    while (field instanceof TreeNodeField) {
        fields.add(field.getAdapter());
        if (iter.hasNext()) {
            field = iter.next();
        } else {
            break;
        }
    }
    return fields;
}
Also used : INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) ITreeNode(org.python.pydev.refactoring.core.model.tree.ITreeNode) TreeNodeField(org.python.pydev.refactoring.core.model.constructorfield.TreeNodeField) ArrayList(java.util.ArrayList)

Example 2 with INodeAdapter

use of org.python.pydev.refactoring.ast.adapters.INodeAdapter in project Pydev by fabioz.

the class ConstructorFieldRequestProcessor method generateRequests.

private List<ConstructorFieldRequest> generateRequests() {
    List<ConstructorFieldRequest> requests = new ArrayList<ConstructorFieldRequest>();
    List<ITreeNode> nodes = new ArrayList<ITreeNode>();
    for (Object o : checked) {
        nodes.add((ITreeNode) o);
    }
    Iterator<ITreeNode> iter = nodes.iterator();
    while (iter.hasNext()) {
        ITreeNode node = iter.next();
        if (node instanceof TreeNodeClassField) {
            List<INodeAdapter> fields = getFields(iter);
            if (!fields.isEmpty()) {
                ClassDefAdapter clazz = (ClassDefAdapter) node.getAdapter();
                ConstructorFieldRequest request = new ConstructorFieldRequest(clazz, fields, offsetStrategy, adapterPrefs);
                requests.add(request);
            }
        }
    }
    return requests;
}
Also used : ConstructorFieldRequest(org.python.pydev.refactoring.codegenerator.constructorfield.request.ConstructorFieldRequest) INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) ITreeNode(org.python.pydev.refactoring.core.model.tree.ITreeNode) TreeNodeClassField(org.python.pydev.refactoring.core.model.constructorfield.TreeNodeClassField) ArrayList(java.util.ArrayList) ClassDefAdapter(org.python.pydev.refactoring.ast.adapters.ClassDefAdapter)

Example 3 with INodeAdapter

use of org.python.pydev.refactoring.ast.adapters.INodeAdapter in project Pydev by fabioz.

the class MockupConstructorFieldRequestProcessor method getRefactoringRequests.

@Override
public List<ConstructorFieldRequest> getRefactoringRequests() {
    IClassDefAdapter clazz = module.getClasses().get(classSelection);
    List<INodeAdapter> attributes = new ArrayList<INodeAdapter>();
    for (int index : attributeSelection) {
        attributes.add(clazz.getAttributes().get(index));
    }
    ConstructorFieldRequest req = new ConstructorFieldRequest(clazz, attributes, this.offsetStrategy, new AdapterPrefs("\n", new IGrammarVersionProvider() {

        @Override
        public int getGrammarVersion() throws MisconfigurationException {
            return IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_2_7;
        }

        @Override
        public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
            return null;
        }
    }));
    List<ConstructorFieldRequest> requests = new ArrayList<ConstructorFieldRequest>();
    requests.add(req);
    return requests;
}
Also used : ConstructorFieldRequest(org.python.pydev.refactoring.codegenerator.constructorfield.request.ConstructorFieldRequest) INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) ArrayList(java.util.ArrayList) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) IClassDefAdapter(org.python.pydev.refactoring.ast.adapters.IClassDefAdapter)

Example 4 with INodeAdapter

use of org.python.pydev.refactoring.ast.adapters.INodeAdapter in project Pydev by fabioz.

the class ConstructorMethodEdit method getEditNode.

/**
 * Tricky :)
 * @throws MisconfigurationException
 */
@Override
protected SimpleNode getEditNode() throws MisconfigurationException {
    List<IClassDefAdapter> bases = classAdapter.getBaseClasses();
    // extractArguments
    NameTokType varArg = null;
    NameTokType kwArg = null;
    Set<String> argsNames = new LinkedHashSet<String>();
    for (IClassDefAdapter baseClass : bases) {
        FunctionDefAdapter init = baseClass.getFirstInit();
        if (init != null) {
            if (!init.getArguments().hasOnlySelf()) {
                argsNames.addAll(init.getArguments().getSelfFilteredArgNames());
            }
            if (varArg == null && init.getArguments().hasVarArg()) {
                varArg = new NameTok(ARGS, NameTok.VarArg);
            }
            if (kwArg == null && init.getArguments().hasKwArg()) {
                kwArg = new NameTok(KWARGS, NameTok.KwArg);
            }
        }
    }
    // addOwnArguments
    for (INodeAdapter adapter1 : attributes) {
        argsNames.add(nodeHelper.getPublicAttr(adapter1.getName()));
    }
    List<exprType> argsExprList = new ArrayList<exprType>();
    Name selfArg = new Name(NodeHelper.KEYWORD_SELF, Name.Param, false);
    argsExprList.add(selfArg);
    for (String parameter : argsNames) {
        argsExprList.add(new Name(parameter.trim(), Name.Param, false));
    }
    exprType[] argsExpr = argsExprList.toArray(new exprType[0]);
    argumentsType args = new argumentsType(argsExpr, varArg, kwArg, null, null, null, null, null, null, null);
    // constructorCalls
    List<stmtType> body = new ArrayList<stmtType>();
    for (IClassDefAdapter base : bases) {
        Expr init = extractConstructorInit(base);
        if (init != null) {
            body.add(init);
        }
    }
    // initAttributes
    for (INodeAdapter adapter : attributes) {
        exprType target = new Attribute(new Name(NodeHelper.KEYWORD_SELF, Name.Load, false), new NameTok(adapter.getName(), NameTok.Attrib), Attribute.Store);
        Assign initParam1 = new Assign(new exprType[] { target }, new Name(nodeHelper.getPublicAttr(adapter.getName()), Name.Load, false), null);
        Assign initParam = initParam1;
        body.add(initParam);
    }
    // create function def
    return new FunctionDef(new NameTok(NodeHelper.KEYWORD_INIT, NameTok.FunctionName), args, body.toArray(new stmtType[0]), null, null, false);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) NameTokType(org.python.pydev.parser.jython.ast.NameTokType) Attribute(org.python.pydev.parser.jython.ast.Attribute) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Name(org.python.pydev.parser.jython.ast.Name) Expr(org.python.pydev.parser.jython.ast.Expr) FunctionDefAdapter(org.python.pydev.refactoring.ast.adapters.FunctionDefAdapter) Assign(org.python.pydev.parser.jython.ast.Assign) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) IClassDefAdapter(org.python.pydev.refactoring.ast.adapters.IClassDefAdapter) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 5 with INodeAdapter

use of org.python.pydev.refactoring.ast.adapters.INodeAdapter in project Pydev by fabioz.

the class MockupGeneratePropertiesRequestProcessor method getRefactoringRequests.

@Override
public List<GeneratePropertiesRequest> getRefactoringRequests() {
    IClassDefAdapter clazz = module.getClasses().get(classSelection);
    List<INodeAdapter> attributes = new ArrayList<INodeAdapter>();
    for (int index : attributeSelection) {
        attributes.add(clazz.getAttributes().get(index));
    }
    List<PropertyTextAdapter> properties = new ArrayList<PropertyTextAdapter>();
    for (int elem : methodSelection) {
        properties.add(new PropertyTextAdapter(elem, ""));
    }
    List<GeneratePropertiesRequest> requests = new ArrayList<GeneratePropertiesRequest>();
    GeneratePropertiesRequest req;
    for (INodeAdapter elem : attributes) {
        req = new GeneratePropertiesRequest(clazz, elem, properties, methodOffsetStrategy, propertyOffsetStrategy, accessModifier, new AdapterPrefs("\n", new IGrammarVersionProvider() {

            @Override
            public int getGrammarVersion() throws MisconfigurationException {
                return IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_2_7;
            }

            @Override
            public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
                return null;
            }
        }));
        requests.add(req);
    }
    return requests;
}
Also used : INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) PropertyTextAdapter(org.python.pydev.refactoring.ast.adapters.PropertyTextAdapter) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) IClassDefAdapter(org.python.pydev.refactoring.ast.adapters.IClassDefAdapter) GeneratePropertiesRequest(org.python.pydev.refactoring.codegenerator.generateproperties.request.GeneratePropertiesRequest)

Aggregations

ArrayList (java.util.ArrayList)5 INodeAdapter (org.python.pydev.refactoring.ast.adapters.INodeAdapter)5 IClassDefAdapter (org.python.pydev.refactoring.ast.adapters.IClassDefAdapter)3 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)2 AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)2 ConstructorFieldRequest (org.python.pydev.refactoring.codegenerator.constructorfield.request.ConstructorFieldRequest)2 ITreeNode (org.python.pydev.refactoring.core.model.tree.ITreeNode)2 LinkedHashSet (java.util.LinkedHashSet)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 Assign (org.python.pydev.parser.jython.ast.Assign)1 Attribute (org.python.pydev.parser.jython.ast.Attribute)1 Expr (org.python.pydev.parser.jython.ast.Expr)1 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)1 Name (org.python.pydev.parser.jython.ast.Name)1 NameTok (org.python.pydev.parser.jython.ast.NameTok)1 NameTokType (org.python.pydev.parser.jython.ast.NameTokType)1 org.python.pydev.parser.jython.ast.argumentsType (org.python.pydev.parser.jython.ast.argumentsType)1 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)1 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)1 ClassDefAdapter (org.python.pydev.refactoring.ast.adapters.ClassDefAdapter)1