Search in sources :

Example 1 with IPyEdit

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

the class AdditionalInfoIntegrityChecker method onCreateActions.

@Override
public void onCreateActions(ListResourceBundle resources, final BaseEditor baseEditor, IProgressMonitor monitor) {
    IPyEdit edit = (IPyEdit) baseEditor;
    edit.addOfflineActionListener("--internal-test-modules", new Action() {

        @Override
        public void run() {
            List<IPythonNature> allPythonNatures = PythonNature.getAllPythonNatures();
            StringBuffer buf = new StringBuffer();
            try {
                for (IPythonNature nature : allPythonNatures) {
                    buf.append(checkIntegrity(nature, new NullProgressMonitor(), true));
                }
            } catch (MisconfigurationException e) {
                buf.append(e.getMessage());
            }
            PyDialogHelpers.showString(buf.toString());
        }
    }, "Used just for testing (do not use).", true);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Action(org.eclipse.jface.action.Action) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPyEdit(org.python.pydev.core.IPyEdit) IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with IPyEdit

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

the class AbstractAnalysisMarkersParticipants method getProps.

@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException {
    fillParticipants();
    PySourceViewer s = ((PyEdit) edit).getPySourceViewer();
    int line = ps.getLineOfOffset(offset);
    OrderedSet<MarkerAnnotationAndPosition> markersAtLine = new OrderedSet<MarkerAnnotationAndPosition>();
    // Add it to a set to make sure that the entries are unique.
    // -- i.e.: the code analysis seems to be creating 2 markers in the following case (when sys is undefined):
    // sys.call1().call2()
    // So, we add it to a set to make sure we'll only analyze unique markers.
    // Note that it'll check equality by the marker type and text (not by position), so, if a given error
    // appears twice in the same line being correct, we'll only show the options once here (which is what
    // we want).
    List<MarkerAnnotationAndPosition> markersAtLine2 = s.getMarkersAtLine(line, getMarkerType());
    markersAtLine.addAll(markersAtLine2);
    ArrayList<ICompletionProposalHandle> props = new ArrayList<ICompletionProposalHandle>();
    if (markersAtLine != null) {
        IAnalysisPreferences analysisPreferences = new AnalysisPreferences(edit);
        String currLine = ps.getLine();
        for (MarkerAnnotationAndPosition marker : markersAtLine) {
            for (IAnalysisMarkersParticipant participant : participants) {
                try {
                    participant.addProps(marker, analysisPreferences, currLine, ps, offset, nature, (PyEdit) edit, props);
                } catch (Exception e) {
                    Log.log("Error when getting proposals.", e);
                }
            }
        }
    }
    return props;
}
Also used : OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) AnalysisPreferences(com.python.pydev.analysis.AnalysisPreferences) IAnalysisPreferences(org.python.pydev.ast.analysis.IAnalysisPreferences) ArrayList(java.util.ArrayList) BadLocationException(org.eclipse.jface.text.BadLocationException) IAnalysisPreferences(org.python.pydev.ast.analysis.IAnalysisPreferences) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) IPyEdit(org.python.pydev.core.IPyEdit) PyEdit(org.python.pydev.editor.PyEdit) MarkerAnnotationAndPosition(org.python.pydev.editor.codefolding.MarkerAnnotationAndPosition)

Example 3 with IPyEdit

use of org.python.pydev.core.IPyEdit 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 4 with IPyEdit

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

the class AssistImportToLocal method getProps.

@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException, MisconfigurationException {
    boolean addOnlyGlobalImports = true;
    boolean allowBadInput = false;
    Tuple<String, Integer> currToken = ps.getCurrToken();
    List<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>();
    if (currToken.o1 != null && currToken.o1.length() > 0) {
        int startOfImportLine = ps.getStartOfImportLine();
        if (startOfImportLine == -1) {
            return ret;
        }
        int startOffset = ps.getLineOffset(startOfImportLine);
        PyImportsIterator pyImportsIterator = new PyImportsIterator(ps.getDoc(), addOnlyGlobalImports, allowBadInput, startOffset);
        OUT: while (pyImportsIterator.hasNext()) {
            ImportHandle handle = pyImportsIterator.next();
            List<ImportHandleInfo> importInfo = handle.getImportInfo();
            for (ImportHandleInfo importHandleInfo : importInfo) {
                List<String> importedStr = importHandleInfo.getImportedStr();
                int startLine = importHandleInfo.getStartLine();
                int endLine = importHandleInfo.getEndLine();
                if (ps.getLineOfOffset() < startLine) {
                    continue;
                }
                if (ps.getLineOfOffset() > endLine) {
                    // Stop iterating.
                    break OUT;
                }
                for (String s : importedStr) {
                    if (s.equals(currToken.o1)) {
                        // Found!
                        IProgressMonitor monitor = new NullProgressMonitor();
                        final RefactoringRequest req = PyRefactorAction.createRefactoringRequest(monitor, (PyEdit) edit, ps);
                        req.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
                        req.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, true);
                        req.fillActivationTokenAndQualifier();
                        ret.add(CompletionProposalFactory.get().createMoveImportsToLocalCompletionProposal(req, s, importHandleInfo, imageCache.get(UIConstants.ASSIST_MOVE_IMPORT), "Move import to local scope(s)"));
                    }
                }
            }
        }
    }
    return ret;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PyImportsIterator(org.python.pydev.core.docutils.PyImportsIterator) ImportHandleInfo(org.python.pydev.core.docutils.ImportHandle.ImportHandleInfo) ArrayList(java.util.ArrayList) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) ArrayList(java.util.ArrayList) List(java.util.List) ImportHandle(org.python.pydev.core.docutils.ImportHandle) IPyEdit(org.python.pydev.core.IPyEdit) PyEdit(org.python.pydev.editor.PyEdit)

Example 5 with IPyEdit

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

the class AnalyzeOnRequestSetter method onCreateActions.

@Override
public void onCreateActions(ListResourceBundle resources, BaseEditor baseEditor, IProgressMonitor monitor) {
    IPyEdit edit = (IPyEdit) baseEditor;
    AnalyzeOnRequestAction action = new AnalyzeOnRequestAction(edit);
    edit.addOfflineActionListener("c", action, "Code-analysis on request", true);
}
Also used : IPyEdit(org.python.pydev.core.IPyEdit)

Aggregations

IPyEdit (org.python.pydev.core.IPyEdit)7 ArrayList (java.util.ArrayList)3 IPySourceViewer (org.python.pydev.core.IPySourceViewer)3 MisconfigurationException (org.python.pydev.core.MisconfigurationException)3 List (java.util.List)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IPythonNature (org.python.pydev.core.IPythonNature)2 PyEdit (org.python.pydev.editor.PyEdit)2 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)2 AnalysisPreferences (com.python.pydev.analysis.AnalysisPreferences)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Action (org.eclipse.jface.action.Action)1 IDocument (org.eclipse.jface.text.IDocument)1 Point (org.eclipse.swt.graphics.Point)1 IAnalysisPreferences (org.python.pydev.ast.analysis.IAnalysisPreferences)1 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)1 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)1 IIndentPrefs (org.python.pydev.core.IIndentPrefs)1