コード例 #1
0
ファイル: base.py プロジェクト: lerouxb/ni
    def delete_selection(self):
        """
        Common code for deleting a selection used by many edit actions.
        """

        view = self.view
        doc = view.document

        # delete the selection
        selection = view.selection.get_normalised()
        d = DeleteDelta(doc, selection.start, selection.end - selection.start + 1)
        d.do()
        self.deltas.append(d)
        view.selection = None

        # move the cursor (insert point) to the start of where the selection
        # was before we deleted it
        view.cursor_pos = doc.offset_to_cursor_pos(selection.start)
コード例 #2
0
ファイル: defaultactions.py プロジェクト: lerouxb/ni
 def do(self):
     view = self.view
     doc = view.document
     
     if view.selection:
         self.delete_selection()
     
     else:
         offset = doc.cursor_pos_to_offset(view.cursor_pos)
         
         # if we're at the end of the file, do nothing
         if offset >= len(doc.content)-1:
             return
         
         # delete the character under the cursor            
         d = DeleteDelta(doc, offset, 1)
         d.do()
         self.deltas.append(d)
コード例 #3
0
ファイル: base.py プロジェクト: lerouxb/ni
    def do(self):
        view = self.view
        doc = view.document
        settings = self.editor.settings

        if view.selection:
            selection = view.selection.get_normalised()
            from_line = doc.offset_to_cursor_pos(selection.start)[0]
            to_line = doc.offset_to_cursor_pos(selection.end)[0]
        else:
            from_line = view.cursor_pos[0]
            to_line = from_line

        for y in xrange(from_line, to_line + 1):
            line = doc.get_line(y)
            offset = doc.cursor_pos_to_offset((y, 0))
            if line[: len(self.comment_string)] == self.comment_string:
                d = DeleteDelta(doc, offset, len(self.comment_string))
            else:
                d = InsertDelta(doc, offset, self.comment_string)
            d.do()
            self.deltas.append(d)

        # move the cursor if necessary
        y, x = view.cursor_pos
        line = doc.get_line(y)
        if line[: len(self.comment_string)] == self.comment_string:
            # we added comment_string, so increase cursor pos
            if x != 0:
                x += len(self.comment_string)
                if x > len(line):
                    x = len(line)
                view.cursor_pos = (y, x)
        else:
            # we removed comment_string, so decrease cursor pos
            x -= len(self.comment_string)
            if x < 0:
                x = 0
            view.cursor_pos = (y, x)

        # not sure how best to grow/shrink the selection right now,
        # so just destroying it for now
        view.selection = None
コード例 #4
0
ファイル: defaultactions.py プロジェクト: lerouxb/ni
 def do(self):
     view = self.view
     doc = view.document
     
     if view.selection:
         self.delete_selection()
     
     else:
         offset = doc.cursor_pos_to_offset(view.cursor_pos)
         
         # if we're at the start of the file, do nothing
         if offset <= 0:
             return
         
         # delete the character before the cursor            
         d = DeleteDelta(doc, offset-1, 1)
         d.do()
         self.deltas.append(d)
         
         view.cursor_pos = doc.offset_to_cursor_pos(offset-1)
コード例 #5
0
ファイル: defaultactions.py プロジェクト: lerouxb/ni
 def do(self):
     view = self.view
     doc = view.document
     settings = self.editor.settings
     
     if view.selection:
         # unindent 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 delete deltas
         spaces_per_line = []
         for y in xrange(start_y, end_y+1):
             line = doc.get_line(y).replace('\t', u' '*settings.tab_size)
             oline = line
             for i in xrange(settings.indent_width):
                 if line and line[0] == u' ':
                     line = line[1:]
                 else:
                     break
             num_spaces = len(oline)-len(line)
             spaces_per_line.append(num_spaces)
             if num_spaces:
                 offset = doc.cursor_pos_to_offset((y, 0))
                 d = DeleteDelta(doc, offset, num_spaces)
                 d.do()
                 self.deltas.append(d)
          
         if selection_direction == "down":
             start_x -= spaces_per_line[0]
             if start_x < 0:
                 start_x = 0
             view.selection.start = doc.cursor_pos_to_offset(
                 (start_y, start_x))                
             end_x -= spaces_per_line[-1]
             if end_x < 0:
                 end_x = 0
             view.selection.end = doc.cursor_pos_to_offset((end_y, end_x))
         else:
             start_x -= spaces_per_line[-1]
             if start_x < 0:
                 start_x = 0
             view.selection.start = doc.cursor_pos_to_offset(
                 (start_y, start_x))                
             end_x -= spaces_per_line[0]
             if end_x < 0:
                 end_x = 0
             view.selection.end = doc.cursor_pos_to_offset((end_y, end_x))
         
     else:
         # unindent only the current line
         y, x = view.cursor_pos 
         line = doc.get_line(y).replace('\t', u' '*settings.tab_size)
         oline = line
         for i in xrange(settings.indent_width):
             if line and line[0] == u' ':
                 line = line[1:]
             else:
                 break
         num_spaces = len(oline)-len(line)
         if num_spaces:
             offset = doc.cursor_pos_to_offset((y, 0))
             d = DeleteDelta(doc, offset, num_spaces)
             d.do()
             self.deltas.append(d)
             x -= num_spaces
             if x < 0:
                 x = 0
             view.cursor_pos = (y, x)