Example #1
0
    def __init__(self,
                 externalPython=None,
                 matplotlib_eventHandler=True,
                 cellHighlight=True,
                 popupForUnselectedVariable=True,
                 popupForSelectedExpression=False,
                 mouseDwellTime=200):
        '''Initializes PyPadPlusPlus to prepare Notepad++
        for interactive Python development'''
        console.show()
        editor.grabFocus()
        self.windowHandle = windll.user32.GetForegroundWindow()

        sys.stdout = PseudoFileOut(Npp.console.write)
        sys.stderr = PseudoFileOut(Npp.console.writeError)
        sys.stdout.outp = PseudoFileOut(Npp.console.write)

        self.matplotlib_eventHandler = matplotlib_eventHandler
        self.matplotlib_enabled = False
        self.popupForUnselectedVariable = popupForUnselectedVariable
        self.popupForSelectedExpression = popupForSelectedExpression
        self.mouseDwellTime = mouseDwellTime

        self.externalPython = bool(externalPython)
        if self.externalPython:
            from . import pyPadHost
            self.interp = pyPadHost.interpreter(externalPython,
                                                outBuffer=self.outBuffer)
        else:
            from . import pyPadClient
            self.interp = pyPadClient.interpreter()

        if cellHighlight:
            self.lexer = EnhancedPythonLexer()
            self.lexer.main()
        else:
            self.lexer = None

        self.thread = None
        self.threadMarker = None
        self.bufferActive = 1
        self.delayedMarker = False
        self.activeCalltip = None
        editor.setTargetStart(0)
        self.specialMarkers = None
        self.bufferMarkerAction = {}
        self.lastActiveBufferID = -1

        # Marker
        self.markerWidth = 3
        editor.setMarginWidthN(3, self.markerWidth)
        editor.setMarginMaskN(3, (256 + 128 + 64) * (1 + 2**3 + 2**6))
        self.markers = {}
        self.m_active, self.m_error, self.m_finish = [
            6 + 3 * i for i in [0, 1, 2]
        ]
        self.preCalculateMarkers()
        for iMarker in self.m_active, self.m_error, self.m_finish:
            self.drawMarker(iMarker)

        self.setCallbacks()

        editor.callTipSetBack((255, 255, 225))
        editor.autoCSetSeparator(ord('\t'))
        editor.autoCSetIgnoreCase(False)
        editor.autoCSetCaseInsensitiveBehaviour(False)
        editor.autoCSetCancelAtStart(False)
        editor.autoCSetDropRestOfWord(False)

        console.clear()
        console.editor.setReadOnly(0)

        self.tTimerFlush = 0.15
        self.tTimerMiddleButton = 0.1
        self.middleButton = 0

        self.bufferActive = 0
        self.onTimerFlush(
        )  # start periodic timer to check output of subprocess
        self.onTimerMiddleButton(
        )  # start periodic timer to check state of middleButton
Example #2
0
    def _notification_handler(self, decoded_message):
        _method = decoded_message.get('method', None)
        if _method == 'textDocument/publishDiagnostics':
            # if editor.getModify():
            # return
            # TODO: for now every diagnostic message clears the console
            console.clear()
            _file = url2pathname(decoded_message['params']['uri'].replace(
                'file:', ''))
            if decoded_message['params']['diagnostics']:
                diag_dict = dict()
                console_output = []
                for item in decoded_message['params']['diagnostics']:
                    # _code = item.get('code', '')
                    _message = item.get('message', 'MESSAGE:???')
                    _severity = item.get('severity', '1')
                    _source = item.get('source', 'SOURCE:???')
                    __range = item.get('range', None)
                    if __range:
                        _start = (__range['start']['line'],
                                  __range['start']['character'])
                        _end = (__range['end']['line'],
                                __range['end']['character'])
                        _range = (_start, _end)
                        console_output.append(
                            f'  File "{_file}", line {_start[0]+1}  -  {_message}'
                        )
                    else:
                        _range = 'RANGE:???'

                    if _severity not in diag_dict:
                        diag_dict[
                            _severity] = f'    {_range} {_source} {_message}'
                    else:
                        diag_dict[
                            _severity] += f'    {_range} {_source} {_message}'
                    diag_dict[_severity] += '\n'

                error_msgs = ''
                warning_msgs = ''
                info_msgs = ''
                hint_msgs = ''
                for k, v in diag_dict.items():
                    num = max([v.count('\n'), v.count('\r')])
                    if k == 1:
                        error_msgs += '  Errors ({})\n'.format(num) + v
                    elif k == 2:
                        warning_msgs += '  Warning ({})\n'.format(num) + v
                    elif k == 3:
                        info_msgs += '  Info ({})\n'.format(num) + v
                    else:  # k == 4
                        hint_msgs += '  Hint ({})\n'.format(num) + v

                diag_msgs = error_msgs + warning_msgs + info_msgs + hint_msgs
                log(f'{_file}, {diag_msgs}')
                print('\n'.join(console_output))

        elif _method == 'window/progress':
            if decoded_message['params']['done']:
                pass  # TODO: reset statusbar
            else:
                _message = decoded_message['params']['message']
                _title = decoded_message['params']['title']
                notepad.setStatusBar(STATUSBARSECTION.DOCTYPE,
                                     f'{_title} - {_message}')
        else:
            log(f'unknown notification received: {decoded_message}')