def style(self): ''' Calculates the text area to be searched for in the current document. Calls up the regexes to find the position and calculates the length of the text to be colored. Deletes the old indicators before setting new ones. Args: None Returns: None ''' start_line = editor.docLineFromVisible(editor.getFirstVisibleLine()) end_line = editor.docLineFromVisible(start_line + editor.linesOnScreen()) if editor.getWrapMode(): end_line = sum([editor.wrapCount(x) for x in range(end_line)]) onscreen_start_position = editor.positionFromLine(start_line) onscreen_end_pos = editor.getLineEndPosition(end_line) editor.setIndicatorCurrent(0) editor.indicatorClearRange(0, editor.getTextLength()) for color, regex in self.regexes.items(): editor.research( regex[0], lambda m: self.paint_it( color[1], m.span(regex[1])[0], m.span(regex[1])[1] - m.span(regex[1])[0], onscreen_start_position, onscreen_end_pos), 0, onscreen_start_position, onscreen_end_pos)
def select_words(current_position, action_type): ''' Creates a list of position tuples by using a regular expression with boundary flag /b, current cursor position gets removed from list In addition, creates multiple selection cursors. Has undo functionality ''' matches = [] editor.research('\\b{}\\b'.format(current_word), lambda m: matches.append(m.span(0))) matches.remove((word_start_position, word_end_position)) if action_type == ACTION_PREPEND: editor.setSelection(current_position, current_position) [editor.addSelection(x[0], x[0]) for x in matches] elif action_type == ACTION_APPEND: editor.setSelection(current_position, current_position) [editor.addSelection(x[1], x[1]) for x in matches] elif action_type == ACTION_OVERWRITE: editor.setSelection(word_start_position, word_end_position) [editor.addSelection(x[0], x[1]) for x in matches] else: return None editor.setMainSelection(0)
def do_regex(self, regex, indicator, start_position, end_position): editor.setIndicatorCurrent(indicator) editor.indicatorClearRange(start_position, end_position-start_position) editor.research(regex[0], lambda m: self.paint_it(indicator, m.span(regex[1])[0], m.span(regex[1])[1] - m.span(regex[1])[0]), 0, start_position, end_position)
def style(self): line_number = editor.getFirstVisibleLine() start_position = editor.positionFromLine(line_number) end_position = editor.getLineEndPosition(line_number + editor.linesOnScreen()) editor.setIndicatorCurrent(self.indicator) editor.indicatorClearRange(start_position, end_position - start_position) flag = 0 editor.research( '^# ?%%(.*)$', lambda m: self.paint_it(self.indicator, m.span(flag)[0], m.span(flag)[1] - m.span(flag)[0]), 0, start_position, end_position)
def check_words(self): words = [] def __get_words(m): if m.group(2): words.append(m.group(2).decode('utf8')) editor.research('(^//.*)|([[:alpha:]]+(?=\h|[[:punct:]]|\R|\Z))', __get_words) if self.DEBUG_MODE: print(u'words contains:\n {}'.format(' '.join(words))) error_words = [ word.lower() for word in words if word.lower() not in self.current_dict and # insensitive word check not word.isupper() # ignore all uppercase only words ] if self.DEBUG_MODE: print(u'error_words contains:\n {}'.format( ' '.join(error_words))) print(u'error_words unique contains:\n {}'.format(' '.join( set(error_words)))) total = len(words) unique = len(set(words)) misspelled = len(error_words) misspelled_unique = len(set(error_words)) notepad.setStatusBar( STATUSBARSECTION.DOCTYPE, self.report.format( total, unique, total - misspelled, # non-misspelled (float(total - misspelled) / total) if misspelled else 1, # non-misspelled % misspelled, (float(misspelled) / total) if misspelled else 0, misspelled_unique, (float(misspelled_unique) / total) if misspelled_unique else 0))
def select_words(current_position, action_type): ''' Creates a list of position tuples by using a regular expression with boundary flag /b, current cursor position gets removed from list In addition, creates multiple selection cursors. Has undo functionality ''' matches = [] editor.research('\\b{}\\b'.format(current_word),lambda m: matches.append(m.span(0))) matches.remove((word_start_position,word_end_position)) if action_type == ACTION_PREPEND: editor.setSelection(current_position,current_position) [editor.addSelection(x[0],x[0]) for x in matches] elif action_type == ACTION_APPEND: editor.setSelection(current_position,current_position) [editor.addSelection(x[1],x[1]) for x in matches] elif action_type == ACTION_OVERWRITE: editor.setSelection(word_start_position,word_end_position) [editor.addSelection(x[0],x[1]) for x in matches] else: return None editor.setMainSelection(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