Example #1
0
def start_regex_tester():
    if REGEX_TESTER_FILE_NAME == '':
        notepad.messageBox('You need to indicate, in the REGEX_TESTER_FILE_NAME variable,\n' +
                           'the fully qualified file name of the TEXT file containing ' +
                           'the REGEX(ES) to test', 'REGEX_TESTER_FILE_NAME is not set')
        return False

    current_document = 0 if notepad.getCurrentView() == 1 else notepad.getCurrentBufferID()

    global REGEX_TESTER_INPUT_TAB
    REGEX_TESTER_INPUT_TAB = regex_tester_doc_already_exists()
    if REGEX_TESTER_INPUT_TAB == 0 :
        notepad.open(REGEX_TESTER_FILE_NAME)
        if notepad.getCurrentFilename().upper() == REGEX_TESTER_FILE_NAME.upper():
            REGEX_TESTER_INPUT_TAB = notepad.getCurrentBufferID()
        else:
            notepad.messageBox('Could not open specified file\n' +
                               '{0}'.format(REGEX_TESTER_FILE_NAME),
                               'Regex Tester Startup Failed', 0)
            return False
    else:
        notepad.activateBufferID(REGEX_TESTER_INPUT_TAB)

    if notepad.getCurrentView() != 1:
        notepad.menuCommand(MENUCOMMAND.VIEW_GOTO_ANOTHER_VIEW)

    STATUS_LINE = 'RegexTester isActive [] flags:sitpr'

    current_status_line = editor2.getLine(0)
    if 'RegexTester' in current_status_line:
        editor2.replace('RegexTester inActive', 'RegexTester isActive')
    else:
        editor2.insertText(0, STATUS_LINE)


    global REGEX_TESTER_IS_RUNNING
    REGEX_TESTER_IS_RUNNING = True

    color_regex_tester_status()

    set_current_buffer_id()

    global PREVIOUS_REGEX
    PREVIOUS_REGEX[CURRENT_BUFFER_ID] = ''

    editor.callbackSync(regex_tester_updateui_callback, [SCINTILLANOTIFICATION.UPDATEUI])

    if current_document != 0:
        notepad.activateBufferID(current_document)

    editor2.setFocus(True)
    editor2.gotoLine(0)
    notepad.save()

    notepad.callback(regex_tester_file_before_close_callback, [NOTIFICATION.FILEBEFORECLOSE])
    notepad.callback(regex_tester_buffer_activated_callback, [NOTIFICATION.BUFFERACTIVATED])

    return True
Example #2
0
def start_regex_tester():
    if REGEX_TESTER_FILE_NAME == '':
        notepad.messageBox('You need to indicate, in the REGEX_TESTER_FILE_NAME variable,\n' +
                           'the fully qualified file name of the TEXT file containing ' +
                           'the REGEX(ES) to test', 'REGEX_TESTER_FILE_NAME is not set')
        return False

    current_document = 0 if notepad.getCurrentView() == 1 else notepad.getCurrentBufferID()

    global REGEX_TESTER_INPUT_TAB
    REGEX_TESTER_INPUT_TAB = regex_tester_doc_already_exists()
    if REGEX_TESTER_INPUT_TAB == 0 :
        notepad.open(REGEX_TESTER_FILE_NAME)
        if notepad.getCurrentFilename().upper() == REGEX_TESTER_FILE_NAME.upper():
            REGEX_TESTER_INPUT_TAB = notepad.getCurrentBufferID()
        else:
            notepad.messageBox('Could not open specified file\n' +
                               '{0}'.format(REGEX_TESTER_FILE_NAME),
                               'Regex Tester Startup Failed', 0)
            return False
    else:
        notepad.activateBufferID(REGEX_TESTER_INPUT_TAB)

    if notepad.getCurrentView() != 1:
        notepad.menuCommand(MENUCOMMAND.VIEW_GOTO_ANOTHER_VIEW)

    STATUS_LINE = 'RegexTester isActive [] flags:sitpr'

    current_status_line = editor2.getLine(0)
    if 'RegexTester' in current_status_line:
        editor2.replace('RegexTester inActive', 'RegexTester isActive')
    else:
        editor2.insertText(0, STATUS_LINE)


    global REGEX_TESTER_IS_RUNNING
    REGEX_TESTER_IS_RUNNING = True

    color_regex_tester_status()

    set_current_buffer_id()

    global PREVIOUS_REGEX
    PREVIOUS_REGEX[CURRENT_BUFFER_ID] = ''

    editor.callbackSync(regex_tester_updateui_callback, [SCINTILLANOTIFICATION.UPDATEUI])

    if current_document != 0:
        notepad.activateBufferID(current_document)

    editor2.setFocus(True)
    editor2.gotoLine(0)
    notepad.save()

    notepad.callback(regex_tester_file_before_close_callback, [NOTIFICATION.FILEBEFORECLOSE])
    notepad.callback(regex_tester_buffer_activated_callback, [NOTIFICATION.BUFFERACTIVATED])

    return True
    def check_lexers(self):
        '''
            Checks if the current document is of interest.

            Args:
                None
            Returns:
                None
        '''

        has_no_lexer_assigned = editor.getLexerLanguage() == 'null'
        _, _, file_extension = notepad.getCurrentFilename().rpartition('.')
        if has_no_lexer_assigned and file_extension in self.known_extensions:
            self.init_lexer()
Example #4
0
    def on_buffer_activated(self, args):
        log(f'{args}')
        self.current_language = notepad.getLanguageName(
            notepad.getLangType()).upper()
        if args['bufferID'] not in self.open_files_dict:
            self.current_file = notepad.getCurrentFilename()
            self.open_files_dict[args['bufferID']] = self.current_file
        else:
            self.current_file = self.open_files_dict[args['bufferID']]

        # temporary files are not supported
        if self.current_file.rpartition('\\')[0] == '':
            log('temporary files are not supported (yet?)')
            self.lsp_doc_flag = False
            return

        if self.current_language in self.available_lsp_servers:
            self.lsp_doc_flag = True
            if not self.com_manager.already_initialized(self.current_language):
                self.current_triggers[self.current_language] = {
                    'signatureHelpProvider': [],
                    'completionProvider': []
                }
                self.com_manager.send(
                    self.lsp_msg.initialize(
                        self.current_file.rpartition('\\')[0], os.getpid()))
                self.com_manager.waiting_for_initialize_result = True

            _version = self._get_file_version()

            if _version == 0:
                log(f'file {self.current_file} first seen')
                self.com_manager.send(
                    self.lsp_msg.didOpen(self.current_file,
                                         self.current_language.lower(),
                                         _version, editor.getText()))
                self.sent_didopen_files.append(args['bufferID'])
        else:
            log(f'{self.current_language} not in {self.available_lsp_servers}')
            self.lsp_doc_flag = False
Example #5
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
    MESSAGEBOXFLAGS.RESULTYES:
    u'D:\\ProgramData\\Python\\Python37_64\\python.exe',
    MESSAGEBOXFLAGS.RESULTNO:
    u'D:\\ProgramData\\Python\\Python27_64\\python.exe'
}


def run_command(command, __cwd):

    _startupinfo = subprocess.STARTUPINFO()
    _startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    process = subprocess.Popen(shlex.split(command),
                               startupinfo=_startupinfo,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               cwd=__cwd)
    _stdout, _stderr = process.communicate()

    console.show()
    if _stdout: console.write(_stdout)
    if _stderr: console.write(_stderr)


file = notepad.getCurrentFilename()
_cwd, _ = os.path.split(file)
answer = notepad.messageBox(('Yes = Python 37_64\n\n'
                             'No = Python 27_64\n\n'
                             'Cancel = No execution'),
                            'Select python interpreter', 3)
if answer != MESSAGEBOXFLAGS.RESULTCANCEL:
    run_command('"{}" -u "{}"'.format(python_interpreter[answer], file), _cwd)
import subprocess
import shlex
import os

python_interpreter = {
    MESSAGEBOXFLAGS.RESULTYES : u'D:\\ProgramData\\Python\\Python37_64\\python.exe',
    MESSAGEBOXFLAGS.RESULTNO : u'D:\\ProgramData\\Python\\Python27_64\\python.exe'}

def run_command(command, __cwd):
    
    _startupinfo = subprocess.STARTUPINFO() 
    _startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    process = subprocess.Popen(shlex.split(command),
                               startupinfo=_startupinfo,
                               stdout=subprocess.PIPE, 
                               stderr=subprocess.PIPE, 
                               cwd=__cwd)
    _stdout, _stderr = process.communicate()
    
    console.show()
    if _stdout: console.write(_stdout)
    if _stderr: console.write(_stderr)


file = notepad.getCurrentFilename()
_cwd, _ = os.path.split(file)
answer = notepad.messageBox(('Yes = Python 37_64\n\n'
                             'No = Python 27_64\n\n'
                             'Cancel = No execution'),'Select python interpreter',3)
if answer != MESSAGEBOXFLAGS.RESULTCANCEL:
    run_command('"{}" -u "{}"'.format(python_interpreter[answer], file), _cwd)