use of org.python.pydev.core.IPySourceViewer 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;
}
use of org.python.pydev.core.IPySourceViewer in project Pydev by fabioz.
the class PyDocumentTemplateContext method getGrammarVersion.
public int getGrammarVersion() {
if (this.viewer instanceof IPySourceViewer) {
try {
IPythonNature nature = ((IPySourceViewer) this.viewer).getEdit().getPythonNature();
if (nature != null) {
return nature.getGrammarVersion();
}
} catch (MisconfigurationException e) {
}
}
if (this.viewer instanceof IScriptConsoleViewer) {
// interactive console
IScriptConsoleViewer v = (IScriptConsoleViewer) this.viewer;
IInterpreterInfo interpreterInfo = (IInterpreterInfo) v.getInterpreterInfo();
if (interpreterInfo != null) {
return interpreterInfo.getGrammarVersion();
}
}
return IGrammarVersionProvider.LATEST_GRAMMAR_PY3_VERSION;
}
use of org.python.pydev.core.IPySourceViewer in project Pydev by fabioz.
the class CtxInsensitiveImportComplProposal method apply.
/**
* This is the apply that should actually be called!
*/
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
IAdaptable projectAdaptable;
if (viewer instanceof IPySourceViewer) {
IPySourceViewer pySourceViewer = (IPySourceViewer) viewer;
IPyEdit pyEdit = pySourceViewer.getEdit();
this.indentString = pyEdit.getIndentPrefs().getIndentationString();
projectAdaptable = pyEdit;
} else {
// happens on compare editor
this.indentString = new DefaultIndentPrefs(null).getIndentationString();
projectAdaptable = null;
}
// of having the focus on the editor and just pressing up/down).
if ((stateMask & SWT.SHIFT) != 0) {
this.setAddLocalImport(true);
}
apply(document, trigger, stateMask, offset, projectAdaptable);
}
use of org.python.pydev.core.IPySourceViewer in project Pydev by fabioz.
the class PyDocumentTemplateContext method getModuleName.
public String getModuleName() {
if (this.viewer instanceof IPySourceViewer) {
try {
IPySourceViewer pyViewer = (IPySourceViewer) this.viewer;
IPyEdit edit = pyViewer.getEdit();
IPythonNature nature = edit.getPythonNature();
if (nature != null) {
return nature.resolveModule(edit.getEditorFile());
}
} catch (MisconfigurationException e) {
}
}
return "";
}
Aggregations