Example #1
0
 def onTimerMiddleButton(self):
     middleButton = windll.user32.GetKeyState(VK_MBUTTON)
     if self.middleButton != middleButton and (middleButton -
                                               (middleButton & 1)) != 0:
         x, y = GetCursorPos()
         hFore = windll.user32.GetForegroundWindow()
         hPoint = windll.user32.WindowFromPoint(x, y)
         if hPoint == hFore:
             hPoint = windll.user32.ChildWindowFromPoint(hFore, x, y)
         hSelf = self.windowHandle
         x0, y0, x1, y1 = GetWindowRect(hPoint)
         if x0 <= x <= x1 and y0 <= y <= y1 and hSelf == hFore:
             editor.grabFocus()
             pos = editor.positionFromPoint(x - x0, y - y0)
             iLineClick = editor.lineFromPosition(pos)
             iStart = editor.getSelectionStart()
             iEnd = editor.getSelectionEnd()
             iLineStart = editor.lineFromPosition(iStart)
             iLineEnd = editor.lineFromPosition(iEnd)
             if iStart != iEnd and iLineStart <= iLineClick <= iLineEnd:
                 self.runCodeAtCursor(moveCursor=False,
                                      onlyInsideCodeLines=True)
             elif 0 <= pos < editor.getLength():
                 self.runCodeAtCursor(moveCursor=False,
                                      nonSelectedLine=iLineClick,
                                      onlyInsideCodeLines=True)
     self.middleButton = middleButton
     threading.Timer(self.tTimerMiddleButton,
                     self.onTimerMiddleButton).start()
Example #2
0
    def runThread(self,
                  moveCursor=True,
                  nonSelectedLine=None,
                  onlyInsideCodeLines=False):
        '''Executes the smallest possible code element for
        the current selection. Or execute one marked cell.'''

        bufferID = notepad.getCurrentBufferID()
        self.bufferActive = bufferID
        lang = notepad.getLangType()
        filename = notepad.getCurrentFilename()
        if lang == Npp.LANGTYPE.TXT and '.' not in os.path.basename(filename):
            notepad.setLangType(Npp.LANGTYPE.PYTHON)
        elif lang != Npp.LANGTYPE.PYTHON:
            self.bufferActive = 0
            return

        if nonSelectedLine is None:
            iSelStart = editor.getSelectionStart()
            iSelEnd = editor.getSelectionEnd()
            iPos = editor.getCurrentPos()
            iLineCursor = iLineStart = editor.lineFromPosition(iSelStart)
            iLineEnd = max(iLineStart, editor.lineFromPosition(iSelEnd - 1))
        else:
            iLineCursor = iLineStart = iLineEnd = nonSelectedLine
            iSelStart = iSelEnd = 0
        selection = iSelStart != iSelEnd
        startLine = editor.getLine(iLineStart).rstrip()
        cellMode = not selection and (startLine.startswith('#%%')
                                      or startLine.startswith('# %%'))
        err = None
        if not cellMode:
            getLineEnd = self.completeBlockEnd(iLineStart,
                                               iLineMin=iLineEnd,
                                               iLineMax=editor.getLineCount() -
                                               1)
            iFirstCodeLine, iLineEnd, isEmpty, inspectLineBefore = next(
                getLineEnd)
            if not inspectLineBefore and iFirstCodeLine:
                iLineStart = iFirstCodeLine
            if isEmpty:
                self.hideMarkers(bufferID)
                self.bufferActive = 0
                return
            iLineStart = self.completeBlockStart(iLineStart, inspectLineBefore)

            requireMore = True

        iStart = editor.positionFromLine(iLineStart)
        iDocEnd = editor.getLength()

        if cellMode:
            iMatch = []
            editor.research('^# ?%%(.*)$',
                            lambda m: iMatch.append(m.span(0)[0] - 1), 0,
                            iStart + 4, iDocEnd - 1, 1)
            iEnd = iMatch[0] if len(iMatch) else iDocEnd
            iLineEnd = editor.lineFromPosition(iEnd)
            block = editor.getTextRange(iStart, iEnd).rstrip()
            r = self.interp.tryCode(iLineStart, filename, block)
            if r is None:
                self.hideMarkers(bufferID)
                self.bufferActive = 0
                return
            err, requireMore, isValue = r
            if requireMore:
                err = True

        else:
            # add more lines until the parser is happy or finds
            # a syntax error

            while requireMore:
                iEnd = editor.getLineEndPosition(iLineEnd)
                block = editor.getTextRange(iStart, iEnd).rstrip()
                if block:
                    res = self.interp.tryCode(iLineStart, filename, block)
                    if res is None:
                        self.bufferActive = 0
                        return
                    else:
                        err, requireMore, isValue = res
                else:
                    err, requireMore, isValue = None, True, False
                if requireMore:
                    nextLine = next(getLineEnd, None)
                    if nextLine is None:
                        self.bufferActive = 0
                        iEnd = editor.getLength()
                        block = editor.getTextRange(iStart, iEnd).rstrip()
                        err, buff = self.interp.execute(
                            block, iLineStart, filename)
                        self.outBuffer(buff)
                        self.setMarkers(iLineStart,
                                        iLineEnd,
                                        block,
                                        iMarker=self.m_error,
                                        bufferID=bufferID)
                        return
                    iCodeLineStart, iLineEnd, isEmpty, inspectLineBefore = nextLine

        if onlyInsideCodeLines and not selection and not iLineStart <= iLineCursor <= iLineEnd:
            self.hideMarkers()
            self.bufferActive = 0
            return

        if self.activeCalltip:
            editor.callTipCancel()
            self.activeCalltip = None

        self.setMarkers(iLineStart,
                        iLineEnd,
                        block,
                        iMarker=(self.m_active if not err else self.m_error),
                        bufferID=bufferID)

        if err is not None:
            if moveCursor:
                editor.setSelectionStart(iStart)
                editor.scrollRange(iEnd, iStart)
            if err is not True: self.outBuffer(err)

        else:

            # Check if correct path is set
            if self.lastActiveBufferID != bufferID and '.' in os.path.basename(
                    filename):
                filePath = os.path.normpath(os.path.split(filename)[0])
                self.interp.execute('os.chdir(' + repr(filePath) + ')')
                self.lastActiveBufferID = bufferID

            # Start a thread to execute the code
            if moveCursor:
                iNewPos = max(iPos, editor.positionFromLine(iLineEnd + 1))
                editor.setSelectionStart(iNewPos)
                editor.setCurrentPos(iNewPos)
                if iNewPos >= iDocEnd and iLineEnd == editor.getLineCount(
                ) - 1:
                    editor.newLine()
                editor.scrollCaret()

            if self.matplotlib_eventHandler and not self.matplotlib_enabled:
                if 'matplotlib' in block:
                    self.interp.execute(init_matplotlib_eventHandler)
                    self.matplotlib_enabled = True

            if isValue:
                res = self.interp.evaluate()
                if res is not None:
                    err, result = res
                    if not err:
                        if self.bufferActive:
                            self.changeMarkers(iMarker=self.m_finish,
                                               bufferID=bufferID)
                        if result: self.stdout(result + '\n')
                    else:
                        self.changeMarkers(iMarker=self.m_error,
                                           bufferID=bufferID)
                        self.outBuffer(result)

            else:
                res = self.interp.execute()
                if res is not None:
                    err, result = res
                    if not err and self.bufferActive:
                        self.changeMarkers(iMarker=self.m_finish,
                                           bufferID=bufferID)
                    else:
                        self.changeMarkers(iMarker=self.m_error,
                                           bufferID=bufferID)
                    self.outBuffer(result)

        if err:
            self.changeMarkers(iMarker=self.m_error, bufferID=bufferID)

        self.bufferActive = 0
Example #3
0
 def callback_scintillawrapper_int_void_void(self, args):
     length = editor.getLength()
     self.assertEqual(length, 5)
     self.callbackCalled = True
Example #4
0
 def test_scintillawrapper_int_void_void(self):
     editor.setText('12345')
     length = editor.getLength()
     self.assertEqual(length, 5)