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);
}
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;
}
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;
}
}));
}
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;
}
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;
}
Aggregations