Examples of PySelection


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

        }
        return balanced;
    }

    private void handleTab(IDocument document, DocumentCommand command) throws BadLocationException {
        PySelection ps = new PySelection(document, command.offset);
        //it is a tab
        String lineContentsToCursor = ps.getLineContentsToCursor();
        int currSize = lineContentsToCursor.length();
        int cursorLine = ps.getCursorLine();

        //current line is empty
        if (lineContentsToCursor.trim().length() == 0) {
            String nextLine = ps.getLine(cursorLine + 1);

            String prevLine = ps.getLine(cursorLine - 1);
            boolean forceTryOnNext = false;
            if (prevLine.trim().length() == 0) {
                //previous line is empty, so, if the next line has contents, use it to make the match.
                if (nextLine.trim().length() > 0) {
                    forceTryOnNext = true;
                }
            }

            if (forceTryOnNext || nextLine.trim().startsWith("@") || ps.matchesFunctionLine(nextLine)) {
                int firstCharPosition = PySelection.getFirstCharPosition(nextLine);
                if (currSize < firstCharPosition) {
                    String txt = nextLine.substring(currSize, firstCharPosition);
                    //as it's the same indentation from the next line, we don't have to applyDefaultForTab.
                    command.text = txt;
                    return;
                }
            }
        }

        if (cursorLine > 0) {
            //this is to know which would be expected if it was a new line in the previous line
            //(so that we know the 'expected' output
            IRegion prevLineInfo = document.getLineInformation(cursorLine - 1);
            int prevLineEndOffset = prevLineInfo.getOffset() + prevLineInfo.getLength();
            String prevExpectedIndent = autoIndentSameAsPrevious(document, prevLineEndOffset, "\n", false);
            String txt = prevExpectedIndent;
            Tuple<String, Boolean> prevLineTup = autoIndentNewline(document, 0, txt, prevLineEndOffset);
            txt = prevLineTup.o1;
            txt = txt.substring(1);//remove the newline
            prevExpectedIndent = prevExpectedIndent.substring(1);

            if (txt.length() > 0) {
                //now, we should not apply that indent if we are already at the 'max' indent in this line
                //(or better: we should go to that max if it would pass it)
                int sizeExpected = txt.length();
                int sizeApplied = currSize + sizeExpected;

                if (currSize >= sizeExpected) {
                    //ok, we already passed what we expected from the indentation, so, let's indent
                    //to the next 'expected' position...

                    boolean applied = false;
                    //handle within parenthesis
                    if (prevLineTup.o2) {
                        int len = sizeApplied - sizeExpected;
                        if (prevExpectedIndent.length() > len) {
                            command.text = prevExpectedIndent.substring(len);
                            applied = true;
                        }
                    }

                    if (!applied) {
                        applyDefaultForTab(command, currSize);
                    }

                } else if (sizeExpected == sizeApplied) {
                    if (command.length == 0) {
                        ps.deleteSpacesAfter(command.offset);
                    }
                    command.text = txt;
                } else if (sizeApplied > sizeExpected) {
                    ps.deleteSpacesAfter(command.offset);
                    command.text = txt.substring(0, sizeExpected - currSize);
                }
            } else {
                applyDefaultForTab(command, currSize);
            }
View Full Code Here

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

    }

    public static void customizeParenthesis(IDocument document, DocumentCommand command,
            boolean considerOnlyCurrentLine, IIndentPrefs prefs) throws BadLocationException {
        if (prefs.getAutoParentesis()) {
            PySelection ps = new PySelection(document, command.offset);
            String line = ps.getLine();

            if (shouldClose(ps, '(', ')')) {

                boolean hasClass = line.indexOf("class ") != -1;
                boolean hasClassMethodDef = line.indexOf(" def ") != -1 || line.indexOf("\tdef ") != -1;
                boolean hasMethodDef = line.indexOf("def ") != -1;
                boolean hasDoublePoint = line.indexOf(":") != -1;

                command.shiftsCaret = false;
                if (!hasDoublePoint && (hasClass || hasClassMethodDef || hasMethodDef)) {
                    if (hasClass) {
                        //command.text = "(object):"; //TODO: put some option in the interface for that
                        //command.caretOffset = command.offset + 7;
                        command.text = "():";
                        command.caretOffset = command.offset + 1;

                    } else if (hasClassMethodDef && prefs.getAutoAddSelf()) {
                        String prevLine = ps.getLine(ps.getCursorLine() - 1);
                        if (prevLine.indexOf("@classmethod") != -1) {
                            command.text = "(cls):";
                            command.caretOffset = command.offset + 4;

                        } else if (prevLine.indexOf("@staticmethod") != -1) {
                            command.text = "():";
                            command.caretOffset = command.offset + 1;

                        } else {

                            boolean addRegular = true;
                            if (!considerOnlyCurrentLine) {
                                //ok, also analyze the scope we're in (otherwise, if we only have the current line
                                //that's the best guess we can give).
                                int firstCharPosition = PySelection.getFirstCharPosition(line);

                                LineStartingScope scopeStart = ps.getPreviousLineThatStartsScope(
                                        PySelection.CLASS_AND_FUNC_TOKENS, false, firstCharPosition);

                                if (scopeStart != null) {
                                    if (scopeStart.lineStartingScope != null
                                            && scopeStart.lineStartingScope.indexOf("def ") != -1) {
                                        int iCurrDef = PySelection.getFirstCharPosition(line);
                                        int iPrevDef = PySelection.getFirstCharPosition(scopeStart.lineStartingScope);
                                        if (iCurrDef > iPrevDef) {
                                            addRegular = false;

                                        } else if (iCurrDef == iPrevDef) {
                                            if (scopeStart.lineStartingScope.indexOf("self") == -1) {
                                                //only add self if the one in the same level also has it.
                                                //with a 'gotcha': if it's a classmethod or staticmethod, we
                                                //should still add it.
                                                if (scopeStart.iLineStartingScope <= 0) {
                                                    addRegular = false;
                                                } else {
                                                    addRegular = false;
                                                    int i = scopeStart.iLineStartingScope - 1;
                                                    String line2;
                                                    do {
                                                        line2 = ps.getLine(i).trim();
                                                        i--;
                                                        if (line2.startsWith("@classmethod")
                                                                || line2.startsWith("@staticmethod")) {
                                                            addRegular = true;
                                                            break;
View Full Code Here

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

    public void customizeNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
        prefs = getIndentPrefs();
        autoIndentSameAsPrevious(document, command);
        if (prefs.getSmartIndentPar()) {
            PySelection selection = new PySelection(document, command.offset);
            if (selection.getCursorLineContents().trim().length() > 0) {
                command.text = autoIndentNewline(document, command.length, command.text, command.offset).o1;
                if (PySelection.containsOnlyWhitespaces(selection.getLineContentsToCursor())) {
                    command.caretOffset = command.offset + selection.countSpacesAfter(command.offset);
                }
            }
        } else {
            PySelection selection = new PySelection(document, command.offset);
            if (selection.getLineContentsToCursor().trim().endsWith(":")) {
                command.text += prefs.getIndentationString();
            }
        }
    }
View Full Code Here

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

     * on subsequent things).
     */
    public static Tuple<String, Integer> autoDedentAfterColon(IDocument document, DocumentCommand command, String tok,
            String[] tokens, IIndentPrefs prefs) throws BadLocationException {
        if (prefs.getAutoDedentElse()) {
            PySelection ps = new PySelection(document, command.offset);
            String lineContents = ps.getCursorLineContents();
            if (lineContents.trim().equals(tok)) {

                String previousIfLine = ps.getPreviousLineThatStartsWithToken(tokens);
                if (previousIfLine != null) {
                    String ifIndent = PySelection.getIndentationFromLine(previousIfLine);
                    String lineIndent = PySelection.getIndentationFromLine(lineContents);

                    String indent = prefs.getIndentationString();
                    if (lineIndent.length() == ifIndent.length() + indent.length()) {
                        Tuple<String, Integer> dedented = removeFirstIndent(lineContents, prefs);
                        ps.replaceLineContentsToSelection(dedented.o1);
                        command.offset = command.offset - dedented.o2;
                        return dedented;
                    }
                }
            }
View Full Code Here

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

     * @param document
     * @param command
     * @throws BadLocationException
     */
    private void performColonReplacement(IDocument document, DocumentCommand command) {
        PySelection ps = new PySelection(document, command.offset);
        int absoluteOffset = ps.getAbsoluteCursorOffset();
        int documentLength = ps.getDoc().getLength();

        // need to check whether whether we're at the very end of the document
        if (absoluteOffset < documentLength) {
            try {
                char currentCharacter = document.getChar(absoluteOffset);
View Full Code Here

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

    /**
     * @return true if we should skip a ), ] or }
     */
    public boolean canSkipCloseParenthesis(IDocument document, DocumentCommand command) throws BadLocationException {
        PySelection ps = new PySelection(document, command.offset);

        char c = ps.getCharAtCurrentOffset();

        try {
            char peer = StringUtils.getPeer(c);

            FastStringBuffer doc = new FastStringBuffer(document.get(), 2);
View Full Code Here

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

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

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

            createFoldingEntries((ASTEntryWithChildren) entry, ret);
        }

        //and at last, get the comments
        if (prefs.getBoolean(PyDevCodeFoldingPrefPage.FOLD_COMMENTS)) {
            DocIterator it = new PySelection.DocIterator(true, new PySelection(doc, 0));
            while (it.hasNext()) {
                String string = it.next();
                if (string.trim().startsWith("#")) {
                    int l = it.getCurrentLine() - 1;
                    addFoldingEntry(ret, new FoldingEntry(FoldingEntry.TYPE_COMMENT, l, l + 1, new ASTEntry(null,
View Full Code Here

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

    public void run(IAction action) {
        FastStringBuffer buf = new FastStringBuffer();
        try {
            PyEdit pyEdit = getPyEdit();

            PySelection pySelection = new PySelection(pyEdit);

            IPythonNature nature = pyEdit.getPythonNature();
            File editorFile = pyEdit.getEditorFile();
            buf.append(nature.resolveModule(editorFile));

            List<stmtType> path = FastParser.parseToKnowGloballyAccessiblePath(pySelection.getDoc(),
                    pySelection.getStartLineIndex());
            for (stmtType stmtType : path) {
                if (buf.length() > 0) {
                    buf.append('.');
                }
                buf.append(NodeUtils.getRepresentationString(stmtType));
View Full Code Here

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

            if (!canModifyEditor()) {
                return;
            }

            PyEdit pyEdit = getPyEdit();
            PySelection ps = new PySelection(pyEdit);

            try {
                int[] regionsToFormat = null;
                if (ps.getSelLength() > 0) {
                    int startLineIndex = ps.getStartLineIndex();
                    int endLineIndex = ps.getEndLineIndex();
                    regionsToFormat = new int[endLineIndex - startLineIndex + 1];
                    for (int i = startLineIndex, j = 0; i <= endLineIndex; i++, j++) {
                        regionsToFormat[j] = i;
                    }
                } else {
                    //For full-formatting, we cannot have a syntax error.
                    if (pyEdit.hasSyntaxError(ps.getDoc())) {
                        return;
                    }
                }

                applyFormatAction(pyEdit, ps, regionsToFormat, true);
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.