コード例 #1
0
    def apply(self, document):
        """Replace the current line with the populated template.
        
        IN:
        document: <IDocument>
            The edited document.
        
        OUT:
        None.

        """
        self.vars['newline'] = PyAction.getDelimiter(document)
        sNewCode = self.template % self.vars
        
        # Move to insert point:
        iStartLineOffset = self.selection.getLineOffset()
        iEndLineOffset = iStartLineOffset + len(self.current_line)
        self.editor.setSelection(iEndLineOffset, 0)
        self.selection = PySelection(self.editor)
        
        # Replace the old code with the new assignment expression:
        self.selection.replaceLineContentsToSelection(sNewCode)
        
        #mark the value so that the user can change it
        selection = PySelection(self.editor)
        absoluteCursorOffset = selection.getAbsoluteCursorOffset()
        val = self.vars['value']
        self.editor.selectAndReveal(absoluteCursorOffset-len(val),len(val))
コード例 #2
0
    def apply(self, document):
        """Replace the current line with the populated template.
        
        IN:
        document: <IDocument>
            The edited document.
        
        OUT:
        None.

        """
        self.vars['newline'] = PyAction.getDelimiter(document)
        sNewCode = self.template % self.vars

        # Move to insert point:
        iStartLineOffset = self.selection.getLineOffset()
        iEndLineOffset = iStartLineOffset + len(self.current_line)
        self.editor.setSelection(iEndLineOffset, 0)
        self.selection = PySelection(self.editor)

        # Replace the old code with the new assignment expression:
        self.selection.replaceLineContentsToSelection(sNewCode)

        #mark the value so that the user can change it
        selection = PySelection(self.editor)
        absoluteCursorOffset = selection.getAbsoluteCursorOffset()
        val = self.vars['value']
        self.editor.selectAndReveal(absoluteCursorOffset - len(val), len(val))
コード例 #3
0
    def isValid(self, selection, current_line, editor, offset):
        """Is this proposal applicable to this line of code?
        
        If current_line .match():es against self.regex then we will store
        a lot of information on the match and environment, and return True.
        Otherwise return False.
        
        IN:
        pyselection: <PySelection>
            The current selection. Highly useful.
        current_line: <str>
            The text on the current line.
        editor: <PyEdit>
            The current editor.
        offset: <int>
            The current position in the editor.

        OUT:
        Boolean. Is the proposal applicable in the current situation?
        
        """
        m = self.regex.match(current_line)
        if not m:
            return False
        self.vars = {'indent': PyAction.getStaticIndentationString(editor)}
        self.vars.update(self.base_vars)
        self.vars.update(m.groupdict())
        self.selection = selection
        self.current_line = current_line
        self.editor = editor
        self.offset = offset
        return True
コード例 #4
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
コード例 #5
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
コード例 #6
0
 def __init__(self, *args, **kws):
     PyAction.__init__(self, *args, **kws)
コード例 #7
0
 def getNewLineDelim(self):
     if not hasattr(self, '_sNewline'):
         self._sNewline = PyAction.getDelimiter(self.editor.getDocument())
     return self._sNewline
コード例 #8
0
ファイル: pyedit_pythontidy.py プロジェクト: pfluegdk/SGpp
 def __init__(self, *args, **kws):
     PyAction.__init__(self, *args, **kws)
コード例 #9
0
 def __init__(self, editor):
     PyAction.__init__(self)
     self._editor = editor
コード例 #10
0
 def getNewLineDelim(self):
     if not hasattr(self, "_sNewline"):
         self._sNewline = PyAction.getDelimiter(self.editor.getDocument())
     return self._sNewline
コード例 #11
0
 def __init__(self, editor):
     PyAction.__init__(self)
     self._editor = editor