Esempio n. 1
0
    def run(self):
        #gotten here (and not in the class resolution as before) because we want it to be resolved
        #when we execute it, and not when setting it
        oSelection = PySelection(self.editor)
        oDocument = self.editor.getDocument()
        if not self.isScriptApplicable(oSelection):
            return None

        oParamInfo = oSelection.getInsideParentesisToks(True)
        lsParams = list(oParamInfo.o1)

        # Determine insert point:
        iClosingParOffset = oParamInfo.o2
        iClosingParLine = oSelection.getLineOfOffset(iClosingParOffset)
        iInsertAfterLine = iClosingParLine
        sIndent = self._indent(
            oSelection) + PyAction.getStaticIndentationString(self.editor)

        parsingUtils = ParsingUtils.create(oDocument)

        # Is there a docstring? In that case we need to skip past it.
        sDocstrFirstLine = oSelection.getLine(iClosingParLine + 1)
        sDocstrStart = sDocstrFirstLine.strip()[:2]
        if sDocstrStart and (sDocstrStart[0] in ['"', "'"]
                             or sDocstrStart in ['r"', "r'"]):
            iDocstrLine = iClosingParLine + 1
            iDocstrLineOffset = oSelection.getLineOffset(iDocstrLine)
            li = [sDocstrFirstLine.find(s) for s in ['"', "'"]]
            iDocstrStartCol = min([i for i in li if i >= 0])
            iDocstrStart = iDocstrLineOffset + iDocstrStartCol
            iDocstrEnd = parsingUtils.eatLiterals(None, iDocstrStart)
            iInsertAfterLine = oSelection.getLineOfOffset(iDocstrEnd)
            sIndent = PySelection.getIndentationFromLine(sDocstrFirstLine)

        # Workaround for bug in PySelection.addLine() in
        # pydev < v1.0.6. Inserting at the last line in the file
        # would raise an exception if the line wasn't newline
        # terminated.
        iDocLength = oDocument.getLength()
        iLastLine = oSelection.getLineOfOffset(iDocLength)
        sLastChar = str(parsingUtils.charAt(iDocLength - 1))
        if (iInsertAfterLine == iLastLine
                and not self.getNewLineDelim().endswith(sLastChar)):
            oDocument.replace(iDocLength, 0, self.getNewLineDelim())

        line = oSelection.getLine(iInsertAfterLine + 1)
        if line.strip() == 'pass':
            oSelection.deleteLine(iInsertAfterLine + 1)

        # Assemble assignment lines and insert them into the document:
        sAssignments = self._assignmentLines(lsParams, sIndent)
        oSelection.addLine(sAssignments, iInsertAfterLine)

        # Leave cursor at the last char of the new lines.
        iNewOffset = oSelection.getLineOffset(iInsertAfterLine +
                                              1) + len(sAssignments)
        self.editor.setSelection(iNewOffset, 0)
        del oSelection
    def run(self):
        #gotten here (and not in the class resolution as before) because we want it to be resolved 
        #when we execute it, and not when setting it
        oSelection = PySelection(self.editor)            
        oDocument = self.editor.getDocument()
        if not self.isScriptApplicable(oSelection):
            return None

        oParamInfo = oSelection.getInsideParentesisToks(True)
        lsParams = list(oParamInfo.o1)

        # Determine insert point:
        iClosingParOffset = oParamInfo.o2
        iClosingParLine = oSelection.getLineOfOffset(iClosingParOffset)
        iInsertAfterLine = iClosingParLine
        sIndent = self._indent(oSelection) + PyAction.getStaticIndentationString(self.editor)
        
        parsingUtils = ParsingUtils.create(oDocument)
        
        # Is there a docstring? In that case we need to skip past it.
        sDocstrFirstLine = oSelection.getLine(iClosingParLine + 1)
        sDocstrStart = sDocstrFirstLine.strip()[:2]
        if sDocstrStart and (sDocstrStart[0] in ['"', "'"] 
                             or sDocstrStart in ['r"', "r'"]):
            iDocstrLine = iClosingParLine + 1
            iDocstrLineOffset = oSelection.getLineOffset(iDocstrLine)
            li = [sDocstrFirstLine.find(s) for s in ['"', "'"]]
            iDocstrStartCol = min([i for i in li if i >= 0])
            iDocstrStart = iDocstrLineOffset + iDocstrStartCol
            iDocstrEnd = parsingUtils.eatLiterals(None, iDocstrStart)
            iInsertAfterLine = oSelection.getLineOfOffset(iDocstrEnd)
            sIndent = PySelection.getIndentationFromLine(sDocstrFirstLine)

        # Workaround for bug in PySelection.addLine() in 
        # pydev < v1.0.6. Inserting at the last line in the file
        # would raise an exception if the line wasn't newline 
        # terminated.
        iDocLength = oDocument.getLength()
        iLastLine = oSelection.getLineOfOffset(iDocLength)
        sLastChar = str(parsingUtils.charAt(iDocLength - 1))
        if (iInsertAfterLine == iLastLine
            and not self.getNewLineDelim().endswith(sLastChar)):
            oDocument.replace(iDocLength, 0, self.getNewLineDelim())
            
        line = oSelection.getLine(iInsertAfterLine+1)
        if line.strip() == 'pass':
            oSelection.deleteLine(iInsertAfterLine+1)
            
        # Assemble assignment lines and insert them into the document:
        sAssignments = self._assignmentLines(lsParams, sIndent)
        oSelection.addLine(sAssignments, iInsertAfterLine)
        
        # Leave cursor at the last char of the new lines.
        iNewOffset = oSelection.getLineOffset(iInsertAfterLine + 1) + len(sAssignments)
        self.editor.setSelection(iNewOffset, 0)
        del oSelection