def do(self): view = self.view doc = view.document # if there's a selection when the user types, it should be deleted # first so that we "override" it if view.selection: self.delete_selection() offset = doc.cursor_pos_to_offset(view.cursor_pos) d = InsertDelta(doc, offset, self.text) d.do() self.deltas.append(d) offset += tab_len(self.text, doc.tab_size) view.cursor_pos = doc.offset_to_cursor_pos(offset)
def do(self): view = self.view doc = view.document settings = self.editor.settings # this only works on selections if not view.selection: return selection = view.selection.get_normalised() d = InsertDelta(doc, selection.start, u"/*") d.do() self.deltas.append(d) d = InsertDelta(doc, selection.end+2, u"*/") d.do() self.deltas.append(d) view.cursor_pos = doc.offset_to_cursor_pos(selection.end+4) view.selection = None
def do(self): view = self.view doc = view.document settings = self.editor.settings if view.selection: # indent the selection selection = view.selection.get_normalised() # selections that grow upwards need slightly different # attention to selections that grow downwards if selection.start == view.selection.start: selection_direction = "down" else: selection_direction = "up" # convert the offsets and unpack start_pos = doc.offset_to_cursor_pos(selection.start) start_y, start_x = start_pos end_pos = doc.offset_to_cursor_pos(selection.end) end_y, end_x = end_pos # add the actual insert deltas for i in xrange(start_y, end_y+1): offset = doc.cursor_pos_to_offset((i, 0)) d = InsertDelta(doc, offset, u" "*settings.indent_width) d.do() self.deltas.append(d) # Adjust the start and end positions of the selection. # The checks on start_x and end_x are there to keep the selection # anchored to the start of a line if it started or ended at # the start (for no reason other than this is what jedit does) num_lines = end_y-start_y+1 if selection_direction == "down": if start_x != 0: view.selection.start += settings.indent_width else: if start_x != 0: view.selection.end += settings.indent_width if selection_direction == "down": n = num_lines if end_x == 0: n -= 1 view.selection.end += (settings.indent_width*n) view.cursor_pos = doc.offset_to_cursor_pos(view.selection.end) else: n = num_lines if end_x == 0: n -= 1 view.selection.start += (settings.indent_width*n) view.cursor_pos = doc.offset_to_cursor_pos(view.selection.start) else: # indent only the current line y, x = view.cursor_pos offset = doc.cursor_pos_to_offset((y, 0)) d = InsertDelta(doc, offset, u" "*settings.indent_width) d.do() self.deltas.append(d) # only move the cursor if we're not at the start of the line # (this is just to clone jedit's behavior) if x != 0: view.cursor_pos = (y, x+settings.indent_width)