Search in sources :

Example 1 with AdapterPrefs

use of org.python.pydev.parser.jython.ast.factory.AdapterPrefs in project Pydev by fabioz.

the class AbstractAdditionalDependencyInfo method addAstForCompiledModule.

private void addAstForCompiledModule(IModule module, InterpreterInfo info, ModulesKey newKey, boolean removeFirst) {
    TokensList globalTokens = module.getGlobalTokens();
    PyAstFactory astFactory = new PyAstFactory(new AdapterPrefs("\n", info.getModulesManager().getNature()));
    List<stmtType> body = new ArrayList<>(globalTokens.size());
    for (IterTokenEntry entry : globalTokens) {
        IToken token = entry.getToken();
        switch(token.getType()) {
            case IToken.TYPE_CLASS:
                body.add(astFactory.createClassDef(token.getRepresentation()));
                break;
            case IToken.TYPE_FUNCTION:
                body.add(astFactory.createFunctionDef(token.getRepresentation()));
                break;
            default:
                Name attr = astFactory.createName(token.getRepresentation());
                // assign to itself just for generation purposes.
                body.add(astFactory.createAssign(attr, attr));
                break;
        }
    }
    // System.out.println("Creating info for: " + module.getName());
    if (removeFirst) {
        removeInfoFromModule(newKey.name, false);
    }
    addAstInfo(astFactory.createModule(body), newKey, false);
}
Also used : IToken(org.python.pydev.core.IToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) IterTokenEntry(org.python.pydev.core.IterTokenEntry) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Name(org.python.pydev.parser.jython.ast.Name)

Example 2 with AdapterPrefs

use of org.python.pydev.parser.jython.ast.factory.AdapterPrefs in project Pydev by fabioz.

the class FunctionArgAdapter method getSignature.

public String getSignature() {
    argumentsType astNode = this.getASTNode().createCopy();
    AdapterPrefs adapterPrefs = new AdapterPrefs(getModule().getEndLineDelimiter(), this.getModule().nature);
    String ret = StringUtils.replaceNewLines(Rewriter.createSourceFromAST(astNode, true, adapterPrefs), "");
    return ret;
}
Also used : AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType)

Example 3 with AdapterPrefs

use of org.python.pydev.parser.jython.ast.factory.AdapterPrefs in project Pydev by fabioz.

the class PyAstFactoryTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    astFactory = new PyAstFactory(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;
        }
    }));
}
Also used : IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory)

Example 4 with AdapterPrefs

use of org.python.pydev.parser.jython.ast.factory.AdapterPrefs 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 5 with AdapterPrefs

use of org.python.pydev.parser.jython.ast.factory.AdapterPrefs in project Pydev by fabioz.

the class OverrideMethodCompletionProposal method applyOnDocument.

public int applyOnDocument(ITextViewer viewer, IDocument document, char trigger, int stateMask, int offset) {
    IGrammarVersionProvider versionProvider = null;
    IPyEdit edit = null;
    if (viewer instanceof IPySourceViewer) {
        IPySourceViewer pySourceViewer = (IPySourceViewer) viewer;
        versionProvider = edit = pySourceViewer.getEdit();
    } else {
        versionProvider = new IGrammarVersionProvider() {

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

            @Override
            public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
                return null;
            }
        };
    }
    String delimiter = PySelection.getDelimiter(document);
    PyAstFactory factory = new PyAstFactory(new AdapterPrefs(delimiter, versionProvider));
    // Note that the copy won't have a parent.
    stmtType overrideBody = factory.createOverrideBody(this.functionDef, parentClassName, currentClassName);
    FunctionDef functionDef = this.functionDef.createCopy(false);
    functionDef.body = new stmtType[] { overrideBody != null ? overrideBody : new Pass() };
    try {
        MakeAstValidForPrettyPrintingVisitor.makeValid(functionDef);
    } catch (Exception e) {
        Log.log(e);
    }
    IIndentPrefs indentPrefs;
    if (edit != null) {
        indentPrefs = edit.getIndentPrefs();
    } else {
        indentPrefs = DefaultIndentPrefs.get(null);
    }
    String printed = NodeUtils.printAst(indentPrefs, edit, functionDef, delimiter);
    PySelection ps = new PySelection(document, offset);
    try {
        String lineContentsToCursor = ps.getLineContentsToCursor();
        int defIndex = lineContentsToCursor.indexOf("def");
        int defOffset = ps.getLineOffset() + defIndex;
        printed = StringUtils.indentTo(printed, lineContentsToCursor.substring(0, defIndex), false);
        printed = StringUtils.rightTrim(printed);
        this.fLen += offset - defOffset;
        document.replace(defOffset, this.fLen, printed);
        return defOffset + printed.length();
    } catch (BadLocationException x) {
    // ignore
    }
    return -1;
}
Also used : IPySourceViewer(org.python.pydev.core.IPySourceViewer) IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) MisconfigurationException(org.python.pydev.core.MisconfigurationException) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) IIndentPrefs(org.python.pydev.core.IIndentPrefs) IPyEdit(org.python.pydev.core.IPyEdit) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Point(org.eclipse.swt.graphics.Point) Pass(org.python.pydev.parser.jython.ast.Pass) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) PySelection(org.python.pydev.core.docutils.PySelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)20 PyAstFactory (org.python.pydev.parser.jython.ast.factory.PyAstFactory)15 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)12 ArrayList (java.util.ArrayList)6 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)6 MisconfigurationException (org.python.pydev.core.MisconfigurationException)4 TokensList (org.python.pydev.core.TokensList)3 List (java.util.List)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IModule (org.python.pydev.core.IModule)2 IToken (org.python.pydev.core.IToken)2 IterTokenEntry (org.python.pydev.core.IterTokenEntry)2 SimpleNode (org.python.pydev.parser.jython.SimpleNode)2 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)2 IClassDefAdapter (org.python.pydev.refactoring.ast.adapters.IClassDefAdapter)2 File (java.io.File)1 IOException (java.io.IOException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IDocument (org.eclipse.jface.text.IDocument)1 Point (org.eclipse.swt.graphics.Point)1