Search in sources :

Example 1 with ErrorDescription

use of org.python.pydev.shared_core.model.ErrorDescription in project Pydev by fabioz.

the class AbstractAdditionalInfoWithBuild method restoreInfoForModuleManager.

/**
 * Restores the info for a module manager
 *
 * @param monitor a monitor to keep track of the progress
 * @param m the module manager
 * @param nature the associated nature (may be null if there is no associated nature -- as is the case when
 * restoring system info).
 *
 * @return the info generated from the module manager
 */
public static AbstractAdditionalTokensInfo restoreInfoForModuleManager(IProgressMonitor monitor, IModulesManager m, String additionalFeedback, AbstractAdditionalTokensInfo info, IPythonNature nature, int grammarVersion) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    // TODO: Check if keeping a zip file open makes things faster...
    // Timer timer = new Timer();
    ModulesKey[] allModules = m.getOnlyDirectModules();
    int i = 0;
    FastStringBuffer msgBuffer = new FastStringBuffer();
    for (ModulesKey key : allModules) {
        if (monitor.isCanceled()) {
            return null;
        }
        i++;
        if (PythonPathHelper.canAddAstInfoForSourceModule(key)) {
            if (i % 17 == 0) {
                msgBuffer.clear();
                msgBuffer.append("Creating ");
                msgBuffer.append(additionalFeedback);
                msgBuffer.append(" additional info (");
                msgBuffer.append(i);
                msgBuffer.append(" of ");
                msgBuffer.append(allModules.length);
                msgBuffer.append(") for ");
                msgBuffer.append(key.file.getName());
                monitor.setTaskName(msgBuffer.toString());
                monitor.worked(1);
            }
            try {
                if (info.addAstInfo(key, false) == null) {
                    String str = "Unable to generate ast -- using %s.\nError:%s";
                    ErrorDescription errorDesc = null;
                    throw new RuntimeException(StringUtils.format(str, PyParser.getGrammarVersionStr(grammarVersion), (errorDesc != null && errorDesc.message != null) ? errorDesc.message : "unable to determine"));
                }
            } catch (Throwable e) {
                Log.log(IStatus.ERROR, "Problem parsing the file :" + key.file + ".", e);
            }
        }
    }
    // timer.printDiff("Time to restore additional info");
    return info;
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ModulesKey(org.python.pydev.core.ModulesKey)

Example 2 with ErrorDescription

use of org.python.pydev.shared_core.model.ErrorDescription in project Pydev by fabioz.

the class PyParser method createParserErrorMarkers.

/**
 * Adds the error markers for some error that was found in the parsing process.
 *
 * @param error the error find while parsing the document
 * @param resource the resource that should have the error added
 * @param doc the document with the resource contents
 * @return the error description (or null)
 *
 * @throws BadLocationException
 * @throws CoreException
 */
public static ErrorDescription createParserErrorMarkers(Throwable error, IAdaptable resource, IDocument doc) {
    ErrorDescription errDesc;
    errDesc = createErrorDesc(error, doc);
    // Create marker only if possible...
    if (resource != null) {
        IResource fileAdapter = resource.getAdapter(IResource.class);
        if (fileAdapter != null) {
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(IMarker.MESSAGE, errDesc.message);
                map.put(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                map.put(IMarker.LINE_NUMBER, errDesc.errorLine);
                map.put(IMarker.CHAR_START, errDesc.errorStart);
                map.put(IMarker.CHAR_END, errDesc.errorEnd);
                map.put(IMarker.TRANSIENT, true);
                createMarker(fileAdapter, map, IMarker.PROBLEM);
            } catch (Exception e) {
                Log.log(e);
            }
        }
    }
    return errDesc;
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) HashMap(java.util.HashMap) IResource(org.eclipse.core.resources.IResource) ResourceException(org.eclipse.core.internal.resources.ResourceException) CoreException(org.eclipse.core.runtime.CoreException) ParseException(org.python.pydev.parser.jython.ParseException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 3 with ErrorDescription

use of org.python.pydev.shared_core.model.ErrorDescription in project Pydev by fabioz.

the class OccurrencesVisitor method reportParserError.

private void reportParserError(Str node, int startInternalStrLineOffset, int startInternalStrColOffset, IDocument doc, Throwable parserError) {
    ErrorDescription errorDescription = PyParser.createErrorDesc(parserError, doc);
    // line
    int errorDescriptionBeginLine = errorDescription.getBeginLine(doc);
    // -1 to account that startInternalStrLineOffset is 1-based and getBeginLine is also 1-based
    int startLine = errorDescriptionBeginLine + startInternalStrLineOffset - 1;
    int endLine = errorDescription.getEndLine(doc) + startInternalStrLineOffset - 1;
    // col
    int startCol = errorDescription.getBeginColumn(doc);
    int endCol = errorDescription.getEndCol(doc);
    if (errorDescriptionBeginLine == 1) {
        startCol += startInternalStrColOffset;
        if (startLine == endLine) {
            endCol += startInternalStrColOffset;
        }
    }
    Message message = new Message(IAnalysisPreferences.TYPE_FSTRING_SYNTAX_ERROR, errorDescription.message, startLine, endLine, startCol, endCol, prefs);
    messagesManager.addMessage(AbstractVisitor.makeToken(node, this.moduleName, nature), message);
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) Message(org.python.pydev.ast.analysis.messages.Message) IMessage(org.python.pydev.ast.analysis.messages.IMessage) Print(org.python.pydev.parser.jython.ast.Print)

Example 4 with ErrorDescription

use of org.python.pydev.shared_core.model.ErrorDescription in project Pydev by fabioz.

the class PyParser method createErrorDesc.

/**
 * Creates the error description for a given error in the parse.
 *
 * Must return an error!
 */
public static ErrorDescription createErrorDesc(Throwable error, IDocument doc) {
    try {
        int errorStart = -1;
        int errorEnd = -1;
        int errorLine = -1;
        String message = null;
        int tokenBeginLine = -1;
        if (error instanceof ParseException) {
            ParseException parseErr = (ParseException) error;
            message = parseErr.getMessage();
            // marker for it
            if (parseErr.currentToken == null) {
                try {
                    IRegion endLine = doc.getLineInformationOfOffset(doc.getLength());
                    errorStart = endLine.getOffset();
                    errorEnd = endLine.getOffset() + endLine.getLength();
                } catch (BadLocationException e) {
                // ignore (can have changed in the meanwhile)
                }
            } else {
                Token errorToken = parseErr.currentToken.next != null ? parseErr.currentToken.next : parseErr.currentToken;
                if (errorToken != null) {
                    tokenBeginLine = errorToken.beginLine - 1;
                    try {
                        IRegion startLine = doc.getLineInformation(getDocPosFromAstPos(errorToken.beginLine));
                        IRegion endLine;
                        if (errorToken.endLine == 0) {
                            endLine = startLine;
                        } else {
                            endLine = doc.getLineInformation(getDocPosFromAstPos(errorToken.endLine));
                        }
                        errorStart = startLine.getOffset() + getDocPosFromAstPos(errorToken.beginColumn);
                        errorEnd = endLine.getOffset() + errorToken.endColumn;
                    } catch (BadLocationException e) {
                    // ignore (can have changed in the meanwhile)
                    }
                }
            }
        } else if (error instanceof TokenMgrError) {
            TokenMgrError tokenErr = (TokenMgrError) error;
            message = tokenErr.getMessage();
            tokenBeginLine = tokenErr.errorLine - 1;
            try {
                IRegion startLine = doc.getLineInformation(tokenErr.errorLine - 1);
                errorStart = startLine.getOffset();
                errorEnd = startLine.getOffset() + tokenErr.errorColumn;
            } catch (BadLocationException e) {
            // ignore (can have changed in the meanwhile)
            }
        } else {
            Log.log("Error, expecting ParseException or TokenMgrError. Received: " + error, error);
            return new ErrorDescription("Internal PyDev Error", 0, 0, 0);
        }
        try {
            errorLine = doc.getLineOfOffset(errorStart) + 1;
        } catch (BadLocationException e) {
            errorLine = tokenBeginLine;
        }
        // in task manager
        if (message != null) {
            // prettyprint
            message = StringUtils.replaceNewLines(message, " ");
        }
        return new ErrorDescription(message, errorLine, errorStart, errorEnd);
    } catch (Exception e) {
        Log.log(e);
        return new ErrorDescription("Internal PyDev Error", 0, 0, 0);
    }
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) Token(org.python.pydev.parser.jython.Token) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ParseException(org.python.pydev.parser.jython.ParseException) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ResourceException(org.eclipse.core.internal.resources.ResourceException) CoreException(org.eclipse.core.runtime.CoreException) ParseException(org.python.pydev.parser.jython.ParseException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 5 with ErrorDescription

use of org.python.pydev.shared_core.model.ErrorDescription in project Pydev by fabioz.

the class BaseOutlinePage method createControl.

/**
 * create the outline view widgets
 */
@Override
public void createControl(Composite parent) {
    // this creates a tree viewer
    super.createControl(parent);
    try {
        createParsedOutline();
        // selecting an item in the outline scrolls the document
        selectionListener = new ISelectionChangedListener() {

            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                if (linkWithEditor == null) {
                    return;
                }
                try {
                    unlinkAll();
                    StructuredSelection sel = (StructuredSelection) event.getSelection();
                    boolean alreadySelected = false;
                    if (sel.size() == 1) {
                        // only sync the editing view if it is a single-selection
                        IParsedItem firstElement = (IParsedItem) sel.getFirstElement();
                        ErrorDescription errorDesc = firstElement.getErrorDesc();
                        // select the error
                        if (errorDesc != null && errorDesc.message != null) {
                            int len = errorDesc.errorEnd - errorDesc.errorStart;
                            editorView.setSelection(errorDesc.errorStart, len);
                            alreadySelected = true;
                        }
                    }
                    if (!alreadySelected) {
                        ISimpleNode[] node = getOutlineModel().getSelectionPosition(sel);
                        editorView.revealModelNodes(node);
                    }
                } finally {
                    relinkAll();
                }
            }
        };
        addSelectionChangedListener(selectionListener);
        createActions();
        // OK, instead of using the default selection engine, we recreate it only to handle mouse
        // and key events directly, because it seems that sometimes, SWT creates spurious select events
        // when those shouldn't be created, and there's also a risk of creating loops with the selection,
        // as when one selection arrives when we're linked, we have to perform a selection and doing that
        // selection could in turn trigger a new selection, so, we remove that treatment and only start
        // selections from interactions the user did.
        // see: Cursor jumps to method definition when an error is detected
        // https://sourceforge.net/tracker2/?func=detail&aid=2057092&group_id=85796&atid=577329
        TreeViewer treeViewer = getTreeViewer();
        treeViewer.removeSelectionChangedListener(this);
        Tree tree = treeViewer.getTree();
        tree.addMouseListener(new MouseListener() {

            @Override
            public void mouseDoubleClick(MouseEvent e) {
                tryToMakeSelection();
            }

            @Override
            public void mouseDown(MouseEvent e) {
            }

            @Override
            public void mouseUp(MouseEvent e) {
                tryToMakeSelection();
            }
        });
        tree.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN) {
                    tryToMakeSelection();
                }
            }
        });
        onControlCreated.call(getTreeViewer());
        createdCallbacksForControls = callRecursively(onControlCreated, filter, new ArrayList());
    } catch (Throwable e) {
        Log.log(e);
    }
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) MouseEvent(org.eclipse.swt.events.MouseEvent) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) MouseListener(org.eclipse.swt.events.MouseListener) Tree(org.eclipse.swt.widgets.Tree) KeyListener(org.eclipse.swt.events.KeyListener)

Aggregations

ErrorDescription (org.python.pydev.shared_core.model.ErrorDescription)6 CoreException (org.eclipse.core.runtime.CoreException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ResourceException (org.eclipse.core.internal.resources.ResourceException)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 ParseException (org.python.pydev.parser.jython.ParseException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IResource (org.eclipse.core.resources.IResource)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 IRegion (org.eclipse.jface.text.IRegion)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1 KeyListener (org.eclipse.swt.events.KeyListener)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1