Package org.python.pydev.core.docutils

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


    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testMatch() throws Exception {
        PythonPairMatcher matcher = getMatcher();
        String s = "test (";
        assertEquals(5, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
        s = "test ";
        assertEquals(-1, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
        s = "test () ";
        assertEquals(-1, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
    }
View Full Code Here


        s = "test () ";
        assertEquals(-1, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
    }

    public void testSearchesOnlyInCurrentStatement() throws Exception {
        PythonPairMatcher matcher = getMatcher();
        String s = "" +
                "a = (\n" +
                "def m1():\n" +
                "    b = ()";
        assertEquals(-1, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
    }
View Full Code Here

                "    b = ()";
        assertEquals(-1, matcher.searchForOpeningPeer(s.length(), '(', ')', new Document(s)));
    }

    public void testMatch1() throws Exception {
        PythonPairMatcher matcher = getMatcher();
        String s = "\ntest ('[#') ";
        assertEquals(-1, matcher.searchForAnyOpeningPeer(s.length(), new Document(s)));

    }
View Full Code Here

        assertEquals(-1, matcher.searchForAnyOpeningPeer(s.length(), new Document(s)));

    }

    public void testMatch2() throws Exception {
        PythonPairMatcher matcher = getMatcher();
        String s = "\ntest ('''\n[#''') ";
        assertEquals(-1, matcher.searchForAnyOpeningPeer(s.length(), new Document(s)));

        s = "\ntest (    ";
        assertEquals(6, matcher.searchForAnyOpeningPeer(s.length(), new Document(s)));

    }
View Full Code Here

        assertEquals(6, matcher.searchForAnyOpeningPeer(s.length(), new Document(s)));

    }

    private PythonPairMatcher getMatcher() {
        return new PythonPairMatcher(new char[] { '(', ')', '[', ']' });
    }
View Full Code Here

                        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);
                        }
                    }
View Full Code Here

                }

                //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());
View Full Code Here

    /**
     * @return true if we should close the opening pair (parameter c) and false if we shouldn't
     */
    public static boolean shouldClose(PySelection ps, char c, char peer) throws BadLocationException {
        PythonPairMatcher matcher = new PythonPairMatcher(StringUtils.BRACKETS);
        String lineContentsFromCursor = ps.getLineContentsFromCursor();

        for (int i = 0; i < lineContentsFromCursor.length(); i++) {
            char charAt = lineContentsFromCursor.charAt(i);
            if (!Character.isWhitespace(charAt)) {

                if (charAt == ',') {
                    break;
                }
                if (StringUtils.isClosingPeer(charAt)) {
                    break;
                }

                return false;
            }
        }

        //Ok, we have to analyze the current context and see if each closing peer
        //in this context has a match. If one doesn't, we won't close it.
        LineStartingScope nextLineThatStartsScope = ps.getNextLineThatStartsScope();
        int lineStartingNextScope;
        if (nextLineThatStartsScope == null) {
            lineStartingNextScope = Integer.MAX_VALUE;
        } else {
            lineStartingNextScope = nextLineThatStartsScope.iLineStartingScope;
        }

        int closingPeerLine;
        int closingPeerFoundAtOffset = ps.getAbsoluteCursorOffset() - 1; //start to search at the current position

        do {
            //closingPeerFoundAtOffset doesn't need +1 here as it's already added in the matcher.
            closingPeerFoundAtOffset = matcher.searchForClosingPeer(closingPeerFoundAtOffset, c, peer, ps.getDoc());
            if (closingPeerFoundAtOffset == -1) {
                //no more closing peers there, ok to go
                return true;
            }

            //the +1 is needed because we match closing ones that are right before the current cursor
            IRegion match = matcher.match(ps.getDoc(), closingPeerFoundAtOffset + 1);
            if (match == null) {
                //we don't have a match for a close, so, this open is that match.
                return false;
            }

View Full Code Here

     * and a boolean indicating if we're inside a parenthesis
     */
    public static Tuple<Integer, Boolean> determineSmartIndent(int offset, IDocument document, IIndentPrefs prefs)
            throws BadLocationException {

        PythonPairMatcher matcher = new PythonPairMatcher(StringUtils.BRACKETS);
        int openingPeerOffset = matcher.searchForAnyOpeningPeer(offset, document);
        if (openingPeerOffset == -1) {
            return new Tuple<Integer, Boolean>(-1, false);
        }

        final IRegion lineInformationOfOffset = document.getLineInformationOfOffset(openingPeerOffset);
        //ok, now, if the opening peer is not on the line we're currently, we do not want to make
        //an 'auto-indent', but keep the current indentation level
        boolean openingPeerIsInCurrentLine = PySelection.isInside(offset, lineInformationOfOffset);

        int len = -1;
        String contents = "";
        if (prefs.getIndentToParLevel()) {
            //now, a catch, if we didn't change the indent level, we've to indent in the same level
            //as the previous line, as this means that the user 'customized' the indent level at this place.
            PySelection ps = new PySelection(document, offset);
            String lineContentsToCursor = ps.getLineContentsToCursor();
            if (!openingPeerIsInCurrentLine && !StringUtils.hasUnbalancedClosingPeers(lineContentsToCursor)) {
                try {
                    char openingChar = document.getChar(openingPeerOffset);
                    int closingPeerOffset = matcher.searchForClosingPeer(openingPeerOffset, openingChar,
                            StringUtils.getPeer(openingChar), document);
                    if (closingPeerOffset == -1 || offset <= closingPeerOffset) {
                        return new Tuple<Integer, Boolean>(-1, true); // True because we're inside a parens
                    }

View Full Code Here

                        //To do that, we go backwards in the document searching for an opening match and then
                        //search its match. If it's found, it means we can delete both, otherwise, this
                        //delete will make things correct.

                        //Create a matcher only matching this char
                        PythonPairMatcher pythonPairMatcher = new PythonPairMatcher(new char[] { c, peer });
                        int openingPeerOffset = pythonPairMatcher.searchForAnyOpeningPeer(replaceOffset, doc);
                        if (openingPeerOffset == -1) {
                            replaceLength += 1;
                        } else {
                            int closingPeerOffset = pythonPairMatcher.searchForClosingPeer(openingPeerOffset, c, peer,
                                    doc);
                            if (closingPeerOffset != -1) {
                                //we have a match, so, things are balanced and we can delete the next
                                replaceLength += 1;
                            }
View Full Code Here

TOP

Related Classes of org.python.pydev.core.docutils.PythonPairMatcher

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.