def run(self): line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType()] if editor.getSelectionEmpty(): editor.setText(line_ending.join(self.__sort(editor.getCharacterPointer()))) elif editor.getSelectionMode() == 1: print '-->> {}'.format(editor.getSelectionNStart(0)) start = editor.getSelectionNStart(0) end = editor.getSelectionNEnd(0) start_column = editor.getColumn(start) end_column = editor.getColumn(end) line_start, line_end = editor.getUserLineSelection() start_position_selected_lines = editor.positionFromLine(line_start) end_position_selected_lines = editor.getLineEndPosition(line_end) def sort_as_int_if_possible(text): return int(text.strip()) if text.strip().isdigit() else text if (line_start==0) and (line_end==editor.getLineCount()-1): editor.setText(line_ending.join(self.__sort(editor.getCharacterPointer(),lambda x: sort_as_int_if_possible(x[start_column:end_column])))) else: lines = self.__sort(editor.getTextRange(start_position_selected_lines, end_position_selected_lines), lambda x: sort_as_int_if_possible(x[start_column:end_column])) print line_ending.join(lines) editor.setTarget(start_position_selected_lines, end_position_selected_lines) editor.replaceTarget(line_ending.join(lines)) else: text_of_sel = editor.getTextRange(*editor.getUserCharSelection()) line_end_of_sel = line_ending if text_of_sel.endswith(line_ending) else '' lines = self.__sort(text_of_sel) editor.replaceSel(line_ending.join(lines) + line_end_of_sel) self.window.destroy()
def async_callback_MARGINCLICK(args): # as unfold hasn't finished yet # we need asynchronous call to be # able to rehide if needed, if args['margin'] == 2: # folding margin clicked_line = editor.lineFromPosition(args['position']) if editor.getFoldExpanded(clicked_line): rehide_lines(clicked_line, editor.getLineCount())
def run(self): line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType()] if editor.getSelectionEmpty(): editor.setText( line_ending.join(self.__sort(editor.getCharacterPointer()))) elif editor.getSelectionMode() == 1: print '-->> {}'.format(editor.getSelectionNStart(0)) start = editor.getSelectionNStart(0) end = editor.getSelectionNEnd(0) start_column = editor.getColumn(start) end_column = editor.getColumn(end) line_start, line_end = editor.getUserLineSelection() start_position_selected_lines = editor.positionFromLine(line_start) end_position_selected_lines = editor.getLineEndPosition(line_end) def sort_as_int_if_possible(text): return int(text.strip()) if text.strip().isdigit() else text if (line_start == 0) and (line_end == editor.getLineCount() - 1): editor.setText( line_ending.join( self.__sort( editor.getCharacterPointer(), lambda x: sort_as_int_if_possible(x[start_column: end_column])))) else: lines = self.__sort( editor.getTextRange(start_position_selected_lines, end_position_selected_lines), lambda x: sort_as_int_if_possible(x[start_column:end_column])) print line_ending.join(lines) editor.setTarget(start_position_selected_lines, end_position_selected_lines) editor.replaceTarget(line_ending.join(lines)) else: text_of_sel = editor.getTextRange(*editor.getUserCharSelection()) line_end_of_sel = line_ending if text_of_sel.endswith( line_ending) else '' lines = self.__sort(text_of_sel) editor.replaceSel(line_ending.join(lines) + line_end_of_sel) self.window.destroy()
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
def main(): if editor.getSelectionEmpty(): # user hasn't selected anything, hide cursor line start_line = end_line = editor.lineFromPosition(editor.getCurrentPos()) else: start_line, end_line = editor.getUserLineSelection() total_number_less_one_line = editor.getLineCount() - 1 # zero-based # recalculate which lines to hide as first and last line cannot be hide start_line = start_line if start_line > 0 else 1 end_line = end_line if total_number_less_one_line > end_line else end_line - 1 # calculate at which lines marker need to be placed marker_start_line = start_line - 1 if start_line > 0 else start_line marker_end_line = end_line + 1 if end_line < total_number_less_one_line else end_line # recalculate in case that marker(start/end)lines are not visible # either because they are part of a folding tree or already hidden while not editor.getLineVisible(marker_start_line): marker_start_line -= 1 if not editor.getLineVisible(marker_end_line): visible_line = editor.visibleFromDocLine(marker_end_line) marker_end_line = editor.docLineFromVisible(visible_line) # check if there is already a marker set at those lines marker_at_marker_start_line = editor.markerGet(marker_start_line) marker_at_marker_end_line = editor.markerGet(marker_end_line) marker_already_set = False if (marker_at_marker_start_line & MARK_HIDELINESBEGIN_MASK) == MARK_HIDELINESBEGIN_MASK: marker_type = 'start' marker_already_set = True elif (marker_at_marker_end_line & MARK_HIDELINESEND_MASK) == MARK_HIDELINESEND_MASK: marker_type = 'end' marker_already_set = True # already markers set - inform user if marker_already_set: if EXTEND_AUTOMATICALLY is False: answer = notepad.messageBox(( 'There can only be one {} marker per line\r\n' 'Should it be extended instead?\r\n' "If it shouldn't, it doesn't do anything").format(marker_type), 'Info!', 4) if EXTEND_AUTOMATICALLY or answer == MESSAGEBOXFLAGS.RESULTYES: if marker_type == 'start': _matching_marker_line = find_correspondig_marker( marker_start_line, MARK_HIDELINESEND_MASK) _start_marker_line_to_delete = marker_start_line _end_marker_line_to_delete = _matching_marker_line else: _matching_marker_line = find_correspondig_marker( marker_end_line, MARK_HIDELINESBEGIN_MASK) _start_marker_line_to_delete = _matching_marker_line _end_marker_line_to_delete = marker_end_line editor.markerDelete(_start_marker_line_to_delete, MARK_HIDELINESBEGIN) editor.markerDelete(_start_marker_line_to_delete, MARK_HIDELINESUNDERLINE) editor.markerDelete(_end_marker_line_to_delete, MARK_HIDELINESEND) else: return editor.hideLines(start_line, end_line) editor.markerAdd(marker_start_line, MARK_HIDELINESBEGIN) editor.markerAdd(marker_start_line, MARK_HIDELINESUNDERLINE) editor.markerAdd(marker_end_line, MARK_HIDELINESEND) editor.gotoLine(marker_start_line)
def list_all_markers(): # used only for debugging purpose for i in range(editor.getLineCount()): _marker = editor.markerGet(i) if _marker > 0: console.writeError('{:>3} = {}\n'.format(i, _marker))