Search in sources :

Example 36 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class UnitTestLaunchShortcut method launch.

@Override
public void launch(IEditorPart editor, String mode) {
    this.arguments = "";
    if (editor instanceof PyEdit) {
        PyEdit pyEdit = (PyEdit) editor;
        PySelection ps = pyEdit.createPySelection();
        String selectedText;
        try {
            selectedText = ps.getSelectedText();
        } catch (BadLocationException e) {
            selectedText = "";
        }
        if (selectedText.length() > 0) {
            String last = null;
            FastStringBuffer buf = new FastStringBuffer();
            List<stmtType> path = FastParser.parseToKnowGloballyAccessiblePath(ps.getDoc(), ps.getStartLineIndex());
            for (stmtType stmtType : path) {
                if (buf.length() > 0) {
                    buf.append('.');
                }
                last = NodeUtils.getRepresentationString(stmtType);
                buf.append(last);
            }
            if (last != null) {
                if (last.equals(selectedText)) {
                    this.arguments = buf.toString();
                }
            }
        }
    }
    super.launch(editor, mode);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) PySelection(org.python.pydev.core.docutils.PySelection) PyEdit(org.python.pydev.editor.PyEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 37 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class DebugEditorBasedOnNatureTypeAction method run.

@Override
public void run(IAction action) {
    PyEdit pyEdit = getPyEdit();
    final Tuple<String, IInterpreterManager> launchConfigurationTypeAndInterpreterManager = this.getLaunchConfigurationTypeAndInterpreterManager(pyEdit, false);
    AbstractLaunchShortcut shortcut = new AbstractLaunchShortcut() {

        @Override
        protected String getLaunchConfigurationType() {
            return launchConfigurationTypeAndInterpreterManager.o1;
        }

        @Override
        protected IInterpreterManager getInterpreterManager(IProject project) {
            return launchConfigurationTypeAndInterpreterManager.o2;
        }
    };
    shortcut.launch(pyEdit, "debug");
}
Also used : AbstractLaunchShortcut(org.python.pydev.debug.ui.launching.AbstractLaunchShortcut) IInterpreterManager(org.python.pydev.core.IInterpreterManager) PyEdit(org.python.pydev.editor.PyEdit) IProject(org.eclipse.core.resources.IProject)

Example 38 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class ClearSyntaxMarkersPyeditListener method onDispose.

@Override
public void onDispose(BaseEditor baseEditor, IProgressMonitor monitor) {
    // remove the markers if we want problems only in the active editor.
    PyEdit edit = (PyEdit) baseEditor;
    IEditorInput input = edit.getEditorInput();
    removeMarkersFromInput(input);
}
Also used : PyEdit(org.python.pydev.editor.PyEdit) IEditorInput(org.eclipse.ui.IEditorInput)

Example 39 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class PythonCorrectionProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    int offset = invocationContext.getOffset();
    PySelection base = edit.createPySelection();
    if (!(this.edit instanceof PyEdit) || base == null) {
        return new ICompletionProposal[0];
    }
    PyEdit editor = (PyEdit) this.edit;
    List<ICompletionProposalHandle> results = new ArrayList<>();
    String sel = PyAction.getLineWithoutComments(base);
    List<IAssistProps> assists = new ArrayList<IAssistProps>();
    synchronized (PythonCorrectionProcessor.additionalAssists) {
        for (IAssistProps prop : additionalAssists.values()) {
            assists.add(prop);
        }
    }
    assists.add(new AssistSurroundWith());
    assists.add(new AssistImport());
    assists.add(new AssistDocString());
    assists.add(new AssistAssign());
    assists.add(new AssistPercentToFormat());
    assists.add(new AssistImportToLocal());
    assists.add(new AssistFString());
    assists.addAll(ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_CTRL_1));
    IImageCache imageCache = SharedUiPlugin.getImageCache();
    File editorFile = edit.getEditorFile();
    IPythonNature pythonNature = null;
    try {
        pythonNature = edit.getPythonNature();
    } catch (MisconfigurationException e1) {
        Log.log(e1);
    }
    for (IAssistProps assist : assists) {
        // Always create a new for each assist, as any given assist may change it.
        PySelection ps = new PySelection(base);
        try {
            if (assist.isValid(ps, sel, editor, offset)) {
                try {
                    results.addAll(assist.getProps(ps, imageCache, editorFile, pythonNature, editor, offset));
                } catch (Exception e) {
                    Log.log(e);
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    Collections.sort(results, new ProposalsComparator("", new ProposalsComparator.CompareContext(pythonNature)));
    try {
        // handling spelling... (we only want to show spelling fixes if a spell problem annotation is found at the current location).
        // we'll only show some spelling proposal if there's some spelling problem (so, we don't have to check the preferences at this place,
        // as no annotations on spelling will be here if the spelling is not enabled).
        ICompletionProposal[] spellProps = null;
        IAnnotationModel annotationModel = editor.getPySourceViewer().getAnnotationModel();
        Iterator<Annotation> it = annotationModel.getAnnotationIterator();
        while (it.hasNext()) {
            Annotation annotation = it.next();
            if (annotation instanceof SpellingAnnotation) {
                SpellingAnnotation spellingAnnotation = (SpellingAnnotation) annotation;
                SpellingProblem spellingProblem = spellingAnnotation.getSpellingProblem();
                int problemOffset = spellingProblem.getOffset();
                int problemLen = spellingProblem.getLength();
                if (problemOffset <= offset && problemOffset + problemLen >= offset) {
                    SpellingCorrectionProcessor spellingCorrectionProcessor = new SpellingCorrectionProcessor();
                    spellProps = spellingCorrectionProcessor.computeQuickAssistProposals(invocationContext);
                    break;
                }
            }
        }
        if (spellProps == null || (spellProps.length == 1 && spellProps[0] instanceof NoCompletionsProposal)) {
            // no proposals from the spelling
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        // ok, add the spell problems and return...
        ICompletionProposal[] ret = results.toArray(new ICompletionProposal[results.size() + spellProps.length]);
        System.arraycopy(spellProps, 0, ret, results.size(), spellProps.length);
        return ret;
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException || e instanceof LinkageError || e instanceof NoSuchMethodException || e instanceof NoSuchMethodError || e instanceof NoClassDefFoundError) {
            // Eclipse 3.2 support
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        throw new RuntimeException(e);
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) SpellingCorrectionProcessor(org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AssistAssign(org.python.pydev.editor.correctionassist.heuristics.AssistAssign) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) AssistPercentToFormat(org.python.pydev.editor.correctionassist.heuristics.AssistPercentToFormat) PyEdit(org.python.pydev.editor.PyEdit) IImageCache(org.python.pydev.shared_core.image.IImageCache) AssistSurroundWith(org.python.pydev.editor.correctionassist.heuristics.AssistSurroundWith) AssistImport(org.python.pydev.editor.correctionassist.heuristics.AssistImport) ProposalsComparator(org.python.pydev.ast.codecompletion.ProposalsComparator) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Annotation(org.eclipse.jface.text.source.Annotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) PySelection(org.python.pydev.core.docutils.PySelection) NoCompletionsProposal(org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal) AssistImportToLocal(org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal) File(java.io.File)

Example 40 with PyEdit

use of org.python.pydev.editor.PyEdit 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)

Aggregations

PyEdit (org.python.pydev.editor.PyEdit)64 PySelection (org.python.pydev.core.docutils.PySelection)22 IFile (org.eclipse.core.resources.IFile)15 ArrayList (java.util.ArrayList)14 BadLocationException (org.eclipse.jface.text.BadLocationException)13 Path (org.eclipse.core.runtime.Path)12 IDocument (org.eclipse.jface.text.IDocument)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)9 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)9 IPythonNature (org.python.pydev.core.IPythonNature)8 File (java.io.File)7 ICallbackListener (org.python.pydev.shared_core.callbacks.ICallbackListener)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IRegion (org.eclipse.jface.text.IRegion)5 ITextSelection (org.eclipse.jface.text.ITextSelection)5 IEditorInput (org.eclipse.ui.IEditorInput)5 SimpleNode (org.python.pydev.parser.jython.SimpleNode)5 CoreException (org.eclipse.core.runtime.CoreException)4 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)4 IInterpreterManager (org.python.pydev.core.IInterpreterManager)4