Search in sources :

Example 16 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PySetNextTarget method setNextToLine.

@Override
public boolean setNextToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException {
    // System.out.println("Run to line:"+target);
    PyStackFrame stack = null;
    if (target instanceof PyStackFrame) {
        stack = (PyStackFrame) target;
        target = stack.getThread();
    }
    if (!(part instanceof PyEdit)) {
        return false;
    }
    PyEdit pyEdit = (PyEdit) part;
    SimpleNode ast = pyEdit.getAST();
    if (ast == null) {
        IDocument doc = pyEdit.getDocument();
        SourceModule sourceModule;
        IPythonNature nature = null;
        try {
            nature = pyEdit.getPythonNature();
        } catch (MisconfigurationException e) {
            // Let's try to find a suitable nature
            File editorFile = pyEdit.getEditorFile();
            if (editorFile == null || !editorFile.exists()) {
                Log.log(e);
                return false;
            }
            nature = InterpreterManagersAPI.getInfoForFile(editorFile).o1;
        }
        if (nature == null) {
            Log.log("Unable to determine nature!");
            return false;
        }
        try {
            sourceModule = AbstractModule.createModuleFromDoc("", pyEdit.getEditorFile(), doc, nature, true);
        } catch (MisconfigurationException e) {
            Log.log(e);
            return false;
        }
        ast = sourceModule.getAst();
    }
    if (ast == null) {
        Log.log("Cannot determine context to run to.");
        return false;
    }
    if (target instanceof PyThread && selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        PyThread pyThread = (PyThread) target;
        if (!pyThread.isPydevThread()) {
            int sourceLine = stack.getLineNumber();
            int targetLine = textSelection.getStartLine();
            if (!NodeUtils.isValidContextForSetNext(ast, sourceLine, targetLine)) {
                return false;
            }
            String functionName = NodeUtils.getContextName(targetLine, ast);
            if (functionName == null) {
                // global context
                functionName = "";
            } else {
                functionName = FullRepIterable.getLastPart(functionName).trim();
            }
            pyThread.setNextStatement(targetLine + 1, functionName);
            return true;
        }
    }
    return true;
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) File(java.io.File) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 17 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PyParser method reparseDocument.

/**
 * Parses the document, generates error annotations
 *
 * @param argsToReparse: will be passed to fireParserError / fireParserChanged so that the IParserObserver2
 * can check it. This is useful when the reparse was done with some specific thing in mind, so that its requestor
 * can pass some specific thing to the parser observers
 *
 * @return a tuple with the SimpleNode root(if parsed) and the error (if any).
 *         if we are able to recover from a reparse, we have both, the root and the error.
 */
@Override
public ParseOutput reparseDocument(Object... argsToReparse) {
    // get the document ast and error in object
    int version;
    AdditionalGrammarVersionsToCheck additionalGrammarsToCheck = null;
    try {
        version = grammarVersionProvider.getGrammarVersion();
        additionalGrammarsToCheck = grammarVersionProvider.getAdditionalGrammarVersions();
    } catch (MisconfigurationException e1) {
        // Ok, we cannot get it... let's put on the default
        version = IGrammarVersionProvider.LATEST_GRAMMAR_PY3_VERSION;
    }
    long documentTime = System.currentTimeMillis();
    ParseOutput obj = reparseDocument(new ParserInfo(document, version, true, additionalGrammarsToCheck));
    IFile original = null;
    IAdaptable adaptable = null;
    if (input == null) {
        return obj;
    }
    original = (input instanceof IAdaptable) ? ((IAdaptable) input).getAdapter(IFile.class) : null;
    if (original != null) {
        adaptable = original;
    } else {
        // probably an external file, may have some location provider mechanism
        // it may be org.eclipse.ui.internal.editors.text.JavaFileEditorInput
        adaptable = (IAdaptable) input;
    }
    // delete the markers
    if (original != null) {
        try {
            deleteErrorMarkers(original);
        } catch (ResourceException e) {
            // so, there is no need to log this failure
            if (original.exists()) {
                Log.log(e);
            }
        } catch (CoreException e) {
            Log.log(e);
        }
    } else if (adaptable == null) {
        // ok, we have nothing... maybe we are in tests...
        if (!PyParser.ACCEPT_NULL_INPUT_EDITOR) {
            throw new RuntimeException("Null input editor received in parser!");
        }
    }
    if (disposed) {
        // if it was disposed in this time, don't fire any notification nor return anything valid.
        return new ParseOutput();
    }
    ErrorParserInfoForObservers errorInfo = null;
    if (obj.error instanceof ParseException || obj.error instanceof TokenMgrError) {
        errorInfo = new ErrorParserInfoForObservers(obj.error, adaptable, document, argsToReparse);
    }
    if (obj.ast != null) {
        // Ok, reparse successful, lets erase the markers that are in the editor we just parsed
        // Note: we may get the ast even if errors happen (and we'll notify in that case too).
        ChangedParserInfoForObservers info = new ChangedParserInfoForObservers(obj.ast, obj.modificationStamp, adaptable, document, documentTime, errorInfo, argsToReparse);
        fireParserChanged(info);
    }
    if (errorInfo != null) {
        fireParserError(errorInfo);
    }
    if (postParserListeners.size() > 0) {
        ArrayList<IPostParserListener> tempList = new ArrayList<>(postParserListeners);
        for (IPostParserListener iParserObserver : tempList) {
            iParserObserver.participantsNotified(argsToReparse);
        }
    }
    return obj;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) ErrorParserInfoForObservers(org.python.pydev.shared_core.parsing.ErrorParserInfoForObservers) IFile(org.eclipse.core.resources.IFile) MisconfigurationException(org.python.pydev.core.MisconfigurationException) LowMemoryArrayList(org.python.pydev.shared_core.structure.LowMemoryArrayList) ArrayList(java.util.ArrayList) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ChangedParserInfoForObservers(org.python.pydev.shared_core.parsing.ChangedParserInfoForObservers) CoreException(org.eclipse.core.runtime.CoreException) AdditionalGrammarVersionsToCheck(org.python.pydev.core.IGrammarVersionProvider.AdditionalGrammarVersionsToCheck) ResourceException(org.eclipse.core.internal.resources.ResourceException) ParseException(org.python.pydev.parser.jython.ParseException)

Example 18 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class DjangoNewProjectPage method getNextPage.

@Override
public IWizardPage getNextPage() {
    String projectType = this.getProjectType();
    IInterpreterManager interpreterManager;
    if (IPythonNature.Versions.ALL_JYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getJythonInterpreterManager();
    } else if (IPythonNature.Versions.ALL_IRONPYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getIronpythonInterpreterManager();
    } else {
        // if others fail, consider it python
        interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
    }
    try {
        String projectInterpreter = this.getProjectInterpreter();
        IInterpreterInfo interpreterInfo;
        if (projectInterpreter.toLowerCase().equals("default")) {
            interpreterInfo = interpreterManager.getDefaultInterpreterInfo(false);
        } else {
            interpreterInfo = interpreterManager.getInterpreterInfo(projectInterpreter, new NullProgressMonitor());
        }
        IModule module = interpreterInfo.getModulesManager().getModuleWithoutBuiltins("django.core.__init__", null, false, new BaseModuleRequest(false));
        if (module == null) {
            DjangoNotAvailableWizardPage page = new DjangoNotAvailableWizardPage("Django not available", interpreterInfo);
            page.setWizard(this.getWizard());
            return page;
        }
    } catch (MisconfigurationException e) {
        ErrorWizardPage page = new ErrorWizardPage("Unexpected error.", "An unexpected error happened:\n" + e.getMessage());
        page.setWizard(this.getWizard());
        return page;
    }
    return super.getNextPage();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 19 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PrettyPrinterV2 method createDefaultPrefs.

public static PrettyPrinterPrefsV2 createDefaultPrefs(IGrammarVersionProvider versionProvider, IIndentPrefs indentPrefs, String endLineDelim) {
    if (versionProvider == null) {
        versionProvider = new IGrammarVersionProvider() {

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

            @Override
            public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
                return null;
            }
        };
    }
    PrettyPrinterPrefsV2 prettyPrinterPrefs = new PrettyPrinterPrefsV2(endLineDelim, indentPrefs.getIndentationString(), versionProvider);
    prettyPrinterPrefs.setSpacesAfterComma(1);
    prettyPrinterPrefs.setSpacesBeforeComment(1);
    prettyPrinterPrefs.setLinesAfterMethod(1);
    prettyPrinterPrefs.setLinesAfterClass(2);
    prettyPrinterPrefs.setLinesAfterSuite(1);
    return prettyPrinterPrefs;
}
Also used : IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 20 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class ModuleAdapter method resolveClass.

/**
 * Get a class adapter for a given class contained in this module.
 *
 * @param name the name of the class we want to resolve.
 *
 * @return an adapter to the class.
 */
public IClassDefAdapter resolveClass(String name) {
    CompletionCache completionCache = new CompletionCache();
    HashSet<String> toResolve = new HashSet<String>();
    toResolve.add(name);
    Set<IClassDefAdapter> resolved;
    try {
        resolved = resolveImportedClass(toResolve, completionCache);
    } catch (MisconfigurationException e) {
        throw new RuntimeException(e);
    }
    if (toResolve.size() == 1) {
        return resolved.iterator().next();
    }
    return null;
}
Also used : CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) MisconfigurationException(org.python.pydev.core.MisconfigurationException) HashSet(java.util.HashSet)

Aggregations

MisconfigurationException (org.python.pydev.core.MisconfigurationException)99 IPythonNature (org.python.pydev.core.IPythonNature)28 ArrayList (java.util.ArrayList)23 File (java.io.File)21 CoreException (org.eclipse.core.runtime.CoreException)18 IOException (java.io.IOException)14 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)14 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 IDocument (org.eclipse.jface.text.IDocument)13 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)13 IInterpreterManager (org.python.pydev.core.IInterpreterManager)12 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)12 HashSet (java.util.HashSet)11 IModule (org.python.pydev.core.IModule)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 SimpleNode (org.python.pydev.parser.jython.SimpleNode)10 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)9 IFile (org.eclipse.core.resources.IFile)9 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)9 TokensList (org.python.pydev.core.TokensList)9