Search in sources :

Example 1 with IPySourceViewer

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

Example 2 with IPySourceViewer

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;
}
Also used : IPySourceViewer(org.python.pydev.core.IPySourceViewer) IScriptConsoleViewer(org.python.pydev.core.interactive_console.IScriptConsoleViewer) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature)

Example 3 with IPySourceViewer

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);
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IPySourceViewer(org.python.pydev.core.IPySourceViewer) IPyEdit(org.python.pydev.core.IPyEdit) IDocument(org.eclipse.jface.text.IDocument) DefaultIndentPrefs(org.python.pydev.core.autoedit.DefaultIndentPrefs)

Example 4 with IPySourceViewer

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 "";
}
Also used : IPySourceViewer(org.python.pydev.core.IPySourceViewer) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPyEdit(org.python.pydev.core.IPyEdit) IPythonNature(org.python.pydev.core.IPythonNature)

Aggregations

IPySourceViewer (org.python.pydev.core.IPySourceViewer)4 IPyEdit (org.python.pydev.core.IPyEdit)3 MisconfigurationException (org.python.pydev.core.MisconfigurationException)3 IPythonNature (org.python.pydev.core.IPythonNature)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 Point (org.eclipse.swt.graphics.Point)1 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)1 IIndentPrefs (org.python.pydev.core.IIndentPrefs)1 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)1 DefaultIndentPrefs (org.python.pydev.core.autoedit.DefaultIndentPrefs)1 PySelection (org.python.pydev.core.docutils.PySelection)1 IScriptConsoleViewer (org.python.pydev.core.interactive_console.IScriptConsoleViewer)1 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)1 Pass (org.python.pydev.parser.jython.ast.Pass)1 AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)1 PyAstFactory (org.python.pydev.parser.jython.ast.factory.PyAstFactory)1 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)1