def document_range_formatting_handler(self, decoded_message): log(decoded_message) editor.beginUndoAction() current_caret_pos = editor.getCurrentPos() for item in reversed(decoded_message['result']): self.__format_document(item) editor.gotoPos(current_caret_pos) editor.endUndoAction()
def reformat(separator, area): separator = window.clipboard_get() if separator_dict.get( separator) == 'clipboard' else separator_dict.get(separator) used_eol = format_types_dict.get(notepad.getFormatType()) editor.beginUndoAction() if area == 0: rows = editor.getCharacterPointer().splitlines() rows = prepare(separator, rows, used_eol) editor.setText(used_eol.join(rows)) elif area == 1: if editor.selectionIsRectangle(): selections = [(editor.getSelectionNStart(i), editor.getSelectionNEnd(i)) for i in range(editor.getSelections())] rows = [ editor.getRangePointer(x, y - x) for x, y in selections ] rows = prepare(separator, rows, used_eol) editor.clearSelections() for i, position in zip(reversed(range(len(selections))), reversed(selections)): editor.setTarget(*position) editor.replaceTarget(rows[i]) else: first_selected_line, last_selected_line = editor.getUserLineSelection( ) startpos = editor.positionFromLine(first_selected_line) endpos = editor.positionFromLine(last_selected_line) rows = editor.getRangePointer(startpos, endpos - startpos) rows = prepare(separator, rows.split(used_eol), used_eol) editor.setTarget(startpos, endpos) editor.replaceTarget(used_eol.join(rows)) else: first_visible_line = editor.getFirstVisibleLine() startpos = editor.positionFromLine(first_visible_line) endpos = editor.positionFromLine(editor.linesOnScreen() + first_visible_line) rows = editor.getRangePointer(startpos, endpos - startpos) rows = prepare(separator, rows.split(used_eol), used_eol) editor.setTarget(startpos, endpos) editor.replaceTarget(used_eol.join(rows)) editor.endUndoAction()
def multi_edit(): ''' main entry point ''' # multi_edit flags ACTION_PREPEND = 0 ACTION_APPEND = 1 ACTION_OVERWRITE = 2 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) editor.beginUndoAction() current_word = editor.getCurrentWord() if current_word != '': current_position = editor.getCurrentPos() word_start_position = editor.wordStartPosition(current_position, True) word_end_position = editor.wordEndPosition(current_position, True) if word_start_position == current_position: select_words(current_position, ACTION_PREPEND) elif word_end_position == current_position: select_words(current_position, ACTION_APPEND) else: select_words(current_position, ACTION_OVERWRITE) editor.endUndoAction()
def multi_edit(): ''' main entry point ''' # multi_edit flags ACTION_PREPEND = 0 ACTION_APPEND = 1 ACTION_OVERWRITE = 2 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) editor.beginUndoAction() current_word = editor.getCurrentWord() if current_word != '': current_position = editor.getCurrentPos() word_start_position = editor.wordStartPosition(current_position, True) word_end_position = editor.wordEndPosition(current_position, True) if word_start_position == current_position: select_words(current_position, ACTION_PREPEND) elif word_end_position == current_position: select_words(current_position, ACTION_APPEND) else: select_words(current_position, ACTION_OVERWRITE) editor.endUndoAction()
def reformat(separator,area): separator = window.clipboard_get() if separator_dict.get(separator) == 'clipboard' else separator_dict.get(separator) used_eol = format_types_dict.get(notepad.getFormatType()) editor.beginUndoAction() if area == 0: rows = editor.getCharacterPointer().splitlines() rows = prepare(separator, rows, used_eol) editor.setText(used_eol.join(rows)) elif area == 1: if editor.selectionIsRectangle(): selections = [(editor.getSelectionNStart(i),editor.getSelectionNEnd(i)) for i in range(editor.getSelections())] rows = [editor.getRangePointer(x, y-x) for x,y in selections] rows = prepare(separator, rows, used_eol) editor.clearSelections() for i, position in zip(reversed(range(len(selections))), reversed(selections)): editor.setTarget(*position) editor.replaceTarget(rows[i]) else: first_selected_line, last_selected_line = editor.getUserLineSelection() startpos = editor.positionFromLine(first_selected_line) endpos = editor.positionFromLine(last_selected_line) rows = editor.getRangePointer(startpos, endpos-startpos) rows = prepare(separator, rows.split(used_eol), used_eol) editor.setTarget(startpos, endpos) editor.replaceTarget(used_eol.join(rows)) else: first_visible_line = editor.getFirstVisibleLine() startpos = editor.positionFromLine(first_visible_line) endpos = editor.positionFromLine(editor.linesOnScreen() + first_visible_line) rows = editor.getRangePointer(startpos, endpos-startpos) rows = prepare(separator, rows.split(used_eol), used_eol) editor.setTarget(startpos, endpos) editor.replaceTarget(used_eol.join(rows)) editor.endUndoAction()
def rename_response_handler(self, decoded_message): log(decoded_message) # 'result': {'documentChanges': [{'edits': [{'newText': u"...", # 'range': {'end': {'character': 0, 'line': 417}, # 'start': {'character': 0, 'line': 0}}}], # 'textDocument': {'uri': 'file:///d:/...', 'version': None}}]}} if decoded_message['result']: editor.beginUndoAction() for changes in decoded_message['result']['documentChanges']: for change in changes['edits']: _file = url2pathname( changes['textDocument']['uri'].replace('file:', '')) notepad.open(_file) start_line = change['range']['start']['line'] end_line = change['range']['end']['line'] start_position = editor.positionFromLine( start_line) + change['range']['start']['character'] end_position = editor.positionFromLine( end_line) + change['range']['end']['character'] editor.setTargetRange(start_position, end_position) editor.replaceTarget(change['newText']) editor.endUndoAction()