Package org.python.pydev.core.docutils

Examples of org.python.pydev.core.docutils.PySelection$LineStartingScope


    public String getCommandLine() {

        IDocument document = this.viewer.getDocument();
        ITextSelection selection = (ITextSelection) this.viewer.getSelection();
        PySelection ps = new PySelection(document, selection);
        return ps.getCursorLineContents();
    }
View Full Code Here


    }

    public int getCommandLineOffset() {
        IDocument document = this.viewer.getDocument();
        ITextSelection selection = (ITextSelection) this.viewer.getSelection();
        PySelection ps = new PySelection(document, selection);
        return ps.getStartLineOffset();
    }
View Full Code Here

        buf.clear();

        if (!pythonCommentOrMultiline) {
            if (textViewer instanceof PySourceViewer) {
                PySourceViewer s = (PySourceViewer) textViewer;
                PySelection ps = new PySelection(s.getDocument(), hoverRegion.getOffset() + hoverRegion.getLength());

                List<IPyHoverParticipant> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_HOVER);
                for (IPyHoverParticipant pyHoverParticipant : participants) {
                    try {
                        String hoverText = pyHoverParticipant.getHoverText(hoverRegion, s, ps, textSelection);
View Full Code Here

            pythonNature = getPyEdit().getPythonNature();
        } catch (MisconfigurationException e1) {
            handle(e1);
            return;
        }
        PySelection ps = new PySelection(this.getPyEdit());
        String selectedText = ps.getSelectedText();
        if (selectedText == null || selectedText.length() == 0) {
            try {
                selectedText = ps.getCurrToken().o1;
            } catch (BadLocationException e) {
                //ignore
            }
        }
View Full Code Here

                    if (dup.o1.getStartLine() < userSelection.getStartLine()) {
                        userSelection = dup.o1;
                    }
                }
            }
            PySelection selection = new PySelection(info.getDocument(), userSelection);
            int startLineIndexIndocCoords = selection.getStartLineIndex();
            int startLineIndexInASTCoords = startLineIndexIndocCoords + 1; //from doc to ast
            Module module = info.getModuleAdapter().getASTNode();
            SimpleNode currentScope = module;

            try {
                FindScopeVisitor scopeVisitor = new FindScopeVisitor(startLineIndexInASTCoords,
                        selection.getCursorColumn() + 1);
                module.accept(scopeVisitor);
                ILocalScope scope = scopeVisitor.scope;
                FastStack scopeStack = scope.getScopeStack();
                currentScope = (SimpleNode) scopeStack.peek(); //at least the module should be there if we don't have anything.
            } catch (Exception e1) {
View Full Code Here

        return lineForLocal;
    }

    @Override
    public int getOffset() {
        PySelection selection = new PySelection(info.getDocument(), calculateLineForLocal(), 0);
        return selection.getStartLineOffset();
    }
View Full Code Here

        return 0;
    }

    @Override
    public String getIndent() {
        PySelection selection = new PySelection(info.getDocument(), calculateLineForLocal(), 0);
        return selection.getIndentationFromLine();
    }
View Full Code Here

                //No selection... let's see if we can get a word there...
                //(note: not using getDocument because only 3.5 has it)
                Object document = Reflection.getAttrObj(textSelection, "fDocument");
                //returns null if we couldn't get it.
                if (document instanceof IDocument) { // document != null
                    PySelection ps = new PySelection((IDocument) document, textSelection);
                    try {
                        text = ps.getCurrToken().o1;
                    } catch (BadLocationException e) {
                        //ignore
                    }
                }
            }
View Full Code Here

            newForcedOffset = offset + 1; //+1 because that's the len of the trigger
            return;
        }

        try {
            PySelection selection = new PySelection(document);
            int lineToAddImport = -1;
            ImportHandleInfo groupInto = null;
            ImportHandleInfo realImportHandleInfo = null;

            boolean groupImports = ImportsPreferencesPage.getGroupImports();

            LineStartingScope previousLineThatStartsScope = null;
            PySelection ps = null;
            if (this.addLocalImport) {
                ps = new PySelection(document, offset);
                int startLineIndex = ps.getStartLineIndex();
                if (startLineIndex == 0) {
                    this.addLocalImport = false;
                } else {
                    previousLineThatStartsScope = ps.getPreviousLineThatStartsScope(PySelection.INDENT_TOKENS,
                            startLineIndex - 1, PySelection.getFirstCharPosition(ps.getCursorLineContents()));
                    if (previousLineThatStartsScope == null) {
                        //note that if we have no previous scope, it means we're actually on the global scope, so,
                        //proceed as usual...
                        this.addLocalImport = false;
                    }
                }
            }

            if (realImportRep.length() > 0 && !this.addLocalImport) {

                //Workaround for: https://sourceforge.net/tracker/?func=detail&aid=2697165&group_id=85796&atid=577329
                //when importing from __future__ import with_statement, we actually want to add a 'with' token, not
                //with_statement token.
                boolean isWithStatement = realImportRep.equals("from __future__ import with_statement");
                if (isWithStatement) {
                    this.fReplacementString = "with";
                }

                if (groupImports) {
                    try {
                        realImportHandleInfo = new ImportHandleInfo(realImportRep);
                        PyImportsHandling importsHandling = new PyImportsHandling(document);
                        for (ImportHandle handle : importsHandling) {
                            if (handle.contains(realImportHandleInfo)) {
                                lineToAddImport = -2; //signal that there's no need to find a line available to add the import
                                break;

                            } else if (groupInto == null && realImportHandleInfo.getFromImportStr() != null) {
                                List<ImportHandleInfo> handleImportInfo = handle.getImportInfo();

                                for (ImportHandleInfo importHandleInfo : handleImportInfo) {

                                    if (realImportHandleInfo.getFromImportStr().equals(
                                            importHandleInfo.getFromImportStr())) {
                                        List<String> commentsForImports = importHandleInfo.getCommentsForImports();
                                        if (commentsForImports.size() > 0
                                                && commentsForImports.get(commentsForImports.size() - 1).length() == 0) {
                                            groupInto = importHandleInfo;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    } catch (ImportNotRecognizedException e1) {
                        Log.log(e1);//that should not happen at this point
                    }
                }

                if (lineToAddImport == -1) {
                    boolean isFutureImport = PySelection.isFutureImportLine(this.realImportRep);
                    lineToAddImport = selection.getLineAvailableForImport(isFutureImport);
                }
            } else {
                lineToAddImport = -1;
            }
            String delimiter = PyAction.getDelimiter(document);

            appliedWithTrigger = trigger == '.' || trigger == '(';
            String appendForTrigger = "";
            if (appliedWithTrigger) {
                if (trigger == '(') {
                    appendForTrigger = "()";

                } else if (trigger == '.') {
                    appendForTrigger = ".";
                }
            }

            //if the trigger is ')', just let it apply regularly -- so, ')' will only be added if it's already in the completion.

            //first do the completion
            if (fReplacementString.length() > 0) {
                int dif = offset - fReplacementOffset;
                document.replace(offset - dif, dif + this.fLen, fReplacementString + appendForTrigger);
            }
            if (this.addLocalImport) {
                //All the code below is because we don't want to work with a generated AST (as it may not be there),
                //so, we go to the previous scope, find out the valid indent inside it and then got backwards
                //from the position we're in now to find the closer location to where we're now where we're
                //actually able to add the import.
                try {
                    int iLineStartingScope;
                    if (previousLineThatStartsScope != null) {
                        iLineStartingScope = previousLineThatStartsScope.iLineStartingScope;

                        //Go to a non-empty line from the line we have and the line we're currently in.
                        int iLine = iLineStartingScope + 1;
                        String line = ps.getLine(iLine);
                        int startLineIndex = ps.getStartLineIndex();
                        while (iLine < startLineIndex && (line.startsWith("#") || line.trim().length() == 0)) {
                            iLine++;
                            line = ps.getLine(iLine);
                        }
                        if (iLine >= startLineIndex) {
                            //Sanity check!
                            iLine = startLineIndex;
                            line = ps.getLine(iLine);
                        }
                        int firstCharPos = PySelection.getFirstCharPosition(line);
                        //Ok, all good so far, now, this would add the line to the beginning of
                        //the element (after the if statement, def, etc.), let's try to put
                        //it closer to where we're now (but still in a valid position).
                        int j = startLineIndex;
                        while (j >= 0) {
                            String line2 = ps.getLine(j);
                            if (PySelection.getFirstCharPosition(line2) == firstCharPos) {
                                iLine = j;
                                break;
                            }
                            if (j == iLineStartingScope) {
                                break;
                            }
                            j--;
                        }

                        String indent = line.substring(0, firstCharPos);
                        String strToAdd = indent + realImportRep + delimiter;
                        ps.addLine(strToAdd, iLine - 1); //Will add it just after the line passed as a parameter.
                        importLen = strToAdd.length();
                        return;
                    }
                } catch (Exception e) {
                    Log.log(e); //Something went wrong, add it as global (i.e.: BUG)
View Full Code Here

    public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
        IDocument document = viewer.getDocument();
        int finalOffset = applyOnDocument(viewer, document, trigger, stateMask, offset);
        if (finalOffset >= 0) {
            try {
                PySelection ps = new PySelection(document, finalOffset);
                int firstCharPosition = PySelection.getFirstCharPosition(ps.getLine());
                int lineOffset = ps.getLineOffset();
                int location = lineOffset + firstCharPosition;
                int len = finalOffset - location;
                fCursorPosition = location;
                fReplacementLength = len;
View Full Code Here

TOP

Related Classes of org.python.pydev.core.docutils.PySelection$LineStartingScope

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.