Examples of PySelection


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

                        if (selection instanceof ITextSelection) {

                            //Don't bother in getting the indent prefs from the editor: the default indent prefs are
                            //always global for the settings we want.
                            pyPeerLinker.setIndentPrefs(new DefaultIndentPrefs());
                            PySelection ps = new PySelection(viewer.getDocument(), (ITextSelection) selection);

                            if (pyPeerLinker.perform(ps, event.character, viewer)) {
                                event.doit = false;
                            }
                        }
View Full Code Here

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

        cache.push(new Region(newSelection.getOffset(), newSelection.getLength()));
    }

    public ITextSelection getNewSelection(IDocument doc, ITextSelection selection) {
        try {
            PySelection ps = new PySelection(doc, selection);
            String selectedText = ps.getSelectedText();
            if (selectedText.length() == 0) {
                //Select the current word
                Tuple<String, Integer> currToken = ps.getCurrToken();
                if (currToken.o1.length() > 0) {
                    return new TextSelection(currToken.o2, currToken.o1.length());
                } else {
                    char c = '\0';
                    try {
                        c = ps.getCharAtCurrentOffset();
                    } catch (BadLocationException e) {
                        //Ignore (end of document is selected).
                    }
                    if (StringUtils.isClosingPeer(c)) {
                        PythonPairMatcher pairMatcher = new PythonPairMatcher();
                        int openingOffset = pairMatcher.searchForOpeningPeer(ps.getAbsoluteCursorOffset(),
                                StringUtils.getPeer(c), c, doc);
                        if (openingOffset >= 0) {
                            return new TextSelection(openingOffset, ps.getAbsoluteCursorOffset() - openingOffset + 1);
                        }
                    }
                }
            } else {
                //There's already some text selected
                boolean tryMatchWithQualifier = true;
                boolean hasDotSelected = false; //value only valid if tryMatchWithQualifier == true!
                for (int i = 0; i < selectedText.length(); i++) {
                    char c = selectedText.charAt(i);
                    if (c == '.') {
                        hasDotSelected = true;
                        continue;
                    }
                    if (!Character.isJavaIdentifierPart(c)) {
                        tryMatchWithQualifier = false;
                        break;
                    }
                }
                if (tryMatchWithQualifier) {
                    Tuple<String, Integer> currToken = ps.getCurrToken();
                    if (!hasDotSelected && !currToken.o1.equals(selectedText)) {
                        return new TextSelection(currToken.o2, currToken.o1.length());
                    } else {
                        //The selected text is not equal to the current token, let's see if we have to select a full dotted word
                        Tuple<String, Integer> currDottedToken = ps.getCurrDottedStatement();
                        if (!currDottedToken.o1.equals(selectedText)) {
                            return new TextSelection(currDottedToken.o2, currDottedToken.o1.length());
                        }
                    }
                }
View Full Code Here

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

    public ColorAndStyleCache getColorCache() {
        return colorCache;
    }

    public PySelection createPySelection() {
        return new PySelection(this);
    }
View Full Code Here

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

                        Log.log("Was expecting PyDocumentProvider. Found: " + documentProvider);
                    }
                }

                ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
                PySelection ps = new PySelection(document, selection);

                if (!hasSyntaxError(ps.getDoc())) {
                    PyFormatStd std = new PyFormatStd();
                    boolean throwSyntaxError = true;
                    try {
                        std.applyFormatAction(this, ps, regionsForSave, throwSyntaxError);
                        statusLineManager.setErrorMessage(null);
View Full Code Here

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

            return;
        }

        try {
            int startOffset = -1, endOffset = -1;
            PySelection selection = new PySelection(this);

            for (SimpleNode node : nodes) {
                int nodeStartoffset = selection.getLineOffset(node.beginLine - 1) + node.beginColumn - 1;
                int[] colLineEnd = NodeUtils.getColLineEnd(node);

                int nodeEndOffset = selection.getLineOffset(colLineEnd[0] - 1) + colLineEnd[1] - 1;

                if (startOffset == -1 || nodeStartoffset < startOffset) {
                    startOffset = nodeStartoffset;
                }
                if (endOffset == -1 || nodeEndOffset > endOffset) {
View Full Code Here

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

        }

        int offset, length, endOffset;

        try {
            PySelection selection = new PySelection(this);
            offset = selection.getLineOffset(node.beginLine - 1) + node.beginColumn - 1;
            int[] colLineEnd = NodeUtils.getColLineEnd(node);

            endOffset = selection.getLineOffset(colLineEnd[0] - 1) + colLineEnd[1] - 1;
            length = endOffset - offset;
            setSelection(offset, length);
        } catch (Exception e) {
            Log.log(e);
        }
View Full Code Here

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

    /**
     * @param ret OUT: this is where the completions are stored
     */
    private void fillWithParams(ITextViewer viewer, CompletionRequest request, ArrayList<ICompletionProposal> ret) {
        PySelection ps = new PySelection(request.doc, request.documentOffset);
        try {
            String lineContentsToCursor = ps.getLineContentsToCursor();
            String trimmed = lineContentsToCursor.trim();

            //only add params on param and type tags
            if (!trimmed.startsWith("@param") && !trimmed.startsWith("@type")) {
                return;
            }

            //for params, we never have an activation token (just a qualifier)
            if (request.activationToken.trim().length() != 0) {
                return;
            }

            String initial = request.qualifier;

            DocIterator iterator = new DocIterator(false, ps);
            while (iterator.hasNext()) {
                String line = iterator.next().trim();
                if (line.startsWith("def ")) {
                    int currentLine = iterator.getCurrentLine() + 1;
                    PySelection selection = new PySelection(request.doc, currentLine, 0);
                    if (selection.isInFunctionLine(true)) {
                        Tuple<List<String>, Integer> insideParentesisToks = selection.getInsideParentesisToks(false);
                        for (String str : insideParentesisToks.o1) {
                            if (str.startsWith(initial)) {
                                ret.add(new PyLinkedModeCompletionProposal(str, request.documentOffset - request.qlen,
                                        request.qlen, str.length(), PyCodeCompletionImages
                                                .getImageForType(IToken.TYPE_PARAM), null, null, "", 0,
View Full Code Here

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

     */
    private Tuple<String, Boolean> autoIndentNewline(IDocument document, int length, String text, int offset)
            throws BadLocationException {

        if (offset > 0) {
            PySelection selection = new PySelection(document, offset);

            String lineWithoutComments = selection.getLineContentsToCursor(true, true);

            Tuple<Integer, Boolean> tup = determineSmartIndent(offset, document, prefs);
            int smartIndent = tup.o1;
            boolean isInsidePar = tup.o2;

            if (lineWithoutComments.length() > 0) {
                //ok, now let's see the auto-indent
                int curr = lineWithoutComments.length() - 1;
                char lastChar = lineWithoutComments.charAt(curr);

                //we dont want whitespaces
                while (curr > 0 && Character.isWhitespace(lastChar)) {
                    curr--;
                    lastChar = lineWithoutComments.charAt(curr);
                }

                //we have to check if smartIndent is -1 because otherwise we are inside some bracket
                if (smartIndent == -1 && !isInsidePar && StringUtils.isClosingPeer(lastChar)) {
                    //ok, not inside brackets
                    PythonPairMatcher matcher = new PythonPairMatcher(StringUtils.BRACKETS);
                    int bracketOffset = selection.getLineOffset() + curr;
                    IRegion region = matcher.match(document, bracketOffset + 1);
                    if (region != null) {
                        if (!PySelection.endsInSameLine(document, region)) {
                            //we might not have a match if there is an error in the program...
                            //e.g. a single ')' without its counterpart.
                            int openingBracketLine = document.getLineOfOffset(region.getOffset());
                            String openingBracketLineStr = PySelection.getLine(document, openingBracketLine);
                            int first = PySelection.getFirstCharPosition(openingBracketLineStr);
                            String initial = getCharsBeforeNewLine(text);
                            text = initial + openingBracketLineStr.substring(0, first);
                            return new Tuple<String, Boolean>(text, isInsidePar);
                        }
                    }
                } else if (smartIndent == -1 && lastChar == ':') {
                    //we have to check if smartIndent is -1 because otherwise we are in a dict
                    //ok, not inside brackets
                    text = indentBasedOnStartingScope(text, selection, false);
                    return new Tuple<String, Boolean>(text, isInsidePar);
                }
            }

            String trimmedLine = lineWithoutComments.trim();

            if (smartIndent >= 0
                    && (StringUtils.hasOpeningBracket(trimmedLine) || StringUtils.hasClosingBracket(trimmedLine))) {
                return new Tuple<String, Boolean>(makeSmartIndent(text, smartIndent), isInsidePar);
            }
            //let's check for dedents...
            if (PySelection.startsWithDedentToken(trimmedLine)) {
                if (lineWithoutComments.endsWith("\\")) {
                    //Okay, we're in something as return \, where the next line will be part of this statement, so, don't really
                    //go back an indent, but go up an indent.
                    return new Tuple<String, Boolean>(text + prefs.getIndentationString(), isInsidePar);
                }
                return new Tuple<String, Boolean>(dedent(text), isInsidePar);
            }

            boolean indentBasedOnStartingScope = false;
            try {
                if (PySelection.containsOnlyWhitespaces(selection.getLineContentsFromCursor())) {
                    indentBasedOnStartingScope = true;
                }
            } catch (BadLocationException e) {
                //(end of the file)
                indentBasedOnStartingScope = true;
            }

            if (indentBasedOnStartingScope) {
                String lineContentsToCursor = selection.getLineContentsToCursor();
                String trimmed = lineContentsToCursor.trim();
                if (trimmed.length() == 0) {
                    return new Tuple<String, Boolean>(indentBasedOnStartingScope(text, selection, false), isInsidePar);
                } else {
                    boolean endsWithTrippleSingle = trimmed.endsWith("'''");
                    if (endsWithTrippleSingle || trimmed.endsWith("\"\"\"")) {
                        //ok, as we're out of a string scope at this point, this means we just closed a string, so,
                        //we should go back to indent based on starting scope.

                        if (endsWithTrippleSingle) {
                            int cursorLine = -1;
                            try {
                                ParsingUtils parsingUtils = ParsingUtils.create(selection.getDoc(), true);
                                int cursorOffset = selection.getAbsoluteCursorOffset();
                                char c;
                                do {
                                    cursorOffset--;
                                    c = parsingUtils.charAt(cursorOffset);

                                } while (Character.isWhitespace(c));

                                int startOffset = parsingUtils.eatLiteralsBackwards(null, cursorOffset);
                                cursorLine = selection.getLineOfOffset(startOffset);
                            } catch (Exception e) {
                                //may throw error if not balanced or if the char we're at is not a ' or "
                            }

                            if (cursorLine == -1) {
                                cursorLine = selection.getCursorLine();
                            }

                            return new Tuple<String, Boolean>(indentBasedOnStartingScope(text, new PySelection(
                                    selection.getDoc(), cursorLine, 0), false), isInsidePar);

                        }
                    }
                }
            }
View Full Code Here

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

            switch (c) {
                case '[':
                case '{':
                    if (prefs.getAutoParentesis()) {
                        PySelection ps = new PySelection(document, command.offset);
                        char peer = StringUtils.getPeer(c);
                        if (shouldClose(ps, c, peer)) {
                            command.shiftsCaret = false;
                            command.text = c + "" + peer;
                            command.caretOffset = command.offset + 1;
                        }
                    }
                    return;

                case '(':
                    handleParens(document, command, prefs);
                    return;

                case ':':
                    /*
                     * The following code will auto-replace colons in function
                     * declaractions
                     * e.g.,
                     * def something(self):
                     *                    ^ cursor before the end colon
                     *
                     * Typing another colon (i.e, ':') at that position will not insert
                     * another colon
                     */
                    if (prefs.getAutoColon()) {
                        performColonReplacement(document, command);
                    }

                    /*
                     * Now, let's also check if we are in an 'else:' or 'except:' or 'finally:' that must be dedented in the doc
                     */
                    autoDedentAfterColon(document, command, prefs);
                    return;

                case ' ':
                    /*
                     * this is a space... so, if we are in 'from xxx ', we may auto-write
                     * the import
                     */
                    if (prefs.getAutoWriteImport()) {
                        PySelection ps = new PySelection(document, command.offset);
                        String completeLine = ps.getLineWithoutCommentsOrLiterals();
                        String lineToCursor = ps.getLineContentsToCursor().trim();
                        String lineContentsFromCursor = ps.getLineContentsFromCursor();

                        if (completeLine.indexOf(" import ") == -1
                                && StringUtils.leftTrim(completeLine).startsWith("from ")
                                && !completeLine.startsWith("import ") && !completeLine.endsWith(" import")
                                && !lineToCursor.endsWith(" import") && !lineContentsFromCursor.startsWith("import")) {
View Full Code Here

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

     */
    private void handleLiteral(IDocument document, DocumentCommand command, boolean isDefaultContext, char literalChar) {
        if (!prefs.getAutoLiterals()) {
            return;
        }
        PySelection ps = new PySelection(document, new TextSelection(document, command.offset, command.length));
        if (command.length > 0) {
            try {
                //We have more contents selected. Delete it so that we can properly use the heuristics.
                ps.deleteSelection();
                command.length = 0;
                ps.setSelection(command.offset, command.offset);
            } catch (BadLocationException e) {
            }
        }

        try {
            char nextChar = ps.getCharAfterCurrentOffset();
            if (Character.isJavaIdentifierPart(nextChar)) {
                //we're just before a word (don't try to do anything in this case)
                //e.g. |var (| is cursor position)
                return;
            }
        } catch (BadLocationException e) {
        }

        String cursorLineContents = ps.getCursorLineContents();
        if (cursorLineContents.indexOf(literalChar) == -1) {
            if (!isDefaultContext) {
                //only add additional chars if on default context.
                return;
            }
            command.text = StringUtils.getWithClosedPeer(literalChar);
            command.shiftsCaret = false;
            command.caretOffset = command.offset + 1;
            return;
        }

        boolean balanced = isLiteralBalanced(cursorLineContents);

        Tuple<String, String> beforeAndAfterMatchingChars = ps.getBeforeAndAfterMatchingChars(literalChar);

        int matchesBefore = beforeAndAfterMatchingChars.o1.length();
        int matchesAfter = beforeAndAfterMatchingChars.o2.length();

        boolean hasMatchesBefore = matchesBefore != 0;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.