feat: remove existing cursor if clicked again in add cursor mode
closes #146
This commit is contained in:
		
							parent
							
								
									0791a82ca0
								
							
						
					
					
						commit
						e09df735a5
					
				
					 1 changed files with 34 additions and 6 deletions
				
			
		| 
						 | 
					@ -1527,6 +1527,29 @@ pub const Editor = struct {
 | 
				
			||||||
        self.cursels.clearRetainingCapacity();
 | 
					        self.cursels.clearRetainingCapacity();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn cursor_count(self: *const Self) usize {
 | 
				
			||||||
 | 
					        var count: usize = 0;
 | 
				
			||||||
 | 
					        for (self.cursels.items[0..]) |*cursel| if (cursel.*) |_| {
 | 
				
			||||||
 | 
					            count += 1;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        return count;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn cursor_at(self: *const Self, cursor: Cursor) ?usize {
 | 
				
			||||||
 | 
					        for (self.cursels.items[0..], 0..) |*cursel, i| if (cursel.*) |*result|
 | 
				
			||||||
 | 
					            if (cursor.eql(result.cursor))
 | 
				
			||||||
 | 
					                return i;
 | 
				
			||||||
 | 
					        return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn remove_cursor_at(self: *const Self, cursor: Cursor) bool {
 | 
				
			||||||
 | 
					        if (self.cursor_at(cursor)) |i| {
 | 
				
			||||||
 | 
					            if (self.cursor_count() > 1) // refuse to remove the last cursor
 | 
				
			||||||
 | 
					                self.cursels.items[i] = null;
 | 
				
			||||||
 | 
					            return true; // but return true anyway to indicate a cursor was found
 | 
				
			||||||
 | 
					        } else return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn collapse_cursors(self: *Self) void {
 | 
					    fn collapse_cursors(self: *Self) void {
 | 
				
			||||||
        const frame = tracy.initZone(@src(), .{ .name = "collapse cursors" });
 | 
					        const frame = tracy.initZone(@src(), .{ .name = "collapse cursors" });
 | 
				
			||||||
        defer frame.deinit();
 | 
					        defer frame.deinit();
 | 
				
			||||||
| 
						 | 
					@ -1964,15 +1987,20 @@ pub const Editor = struct {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn primary_click(self: *Self, y: c_int, x: c_int) !void {
 | 
					    pub fn primary_click(self: *Self, y: c_int, x: c_int) !void {
 | 
				
			||||||
        if (self.fast_scroll)
 | 
					        const root = self.buf_root() catch return;
 | 
				
			||||||
            try self.push_cursor()
 | 
					        if (self.fast_scroll) {
 | 
				
			||||||
        else
 | 
					            var at: Cursor = .{};
 | 
				
			||||||
 | 
					            at.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return;
 | 
				
			||||||
 | 
					            if (self.remove_cursor_at(at))
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            try self.push_cursor();
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
            self.cancel_all_selections();
 | 
					            self.cancel_all_selections();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        const primary = self.get_primary();
 | 
					        const primary = self.get_primary();
 | 
				
			||||||
        primary.selection = null;
 | 
					        primary.selection = null;
 | 
				
			||||||
        self.selection_mode = .char;
 | 
					        self.selection_mode = .char;
 | 
				
			||||||
        try self.send_editor_jump_source();
 | 
					        try self.send_editor_jump_source();
 | 
				
			||||||
        const root = self.buf_root() catch return;
 | 
					 | 
				
			||||||
        primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return;
 | 
					        primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return;
 | 
				
			||||||
        self.clamp_mouse();
 | 
					        self.clamp_mouse();
 | 
				
			||||||
        try self.send_editor_jump_destination();
 | 
					        try self.send_editor_jump_destination();
 | 
				
			||||||
| 
						 | 
					@ -2839,9 +2867,9 @@ pub const Editor = struct {
 | 
				
			||||||
    pub fn unindent(self: *Self, _: Context) Result {
 | 
					    pub fn unindent(self: *Self, _: Context) Result {
 | 
				
			||||||
        const b = try self.buf_for_update();
 | 
					        const b = try self.buf_for_update();
 | 
				
			||||||
        errdefer self.restore_cursels();
 | 
					        errdefer self.restore_cursels();
 | 
				
			||||||
        const cursor_count = self.cursels.items.len;
 | 
					        const previous_len = self.cursels.items.len;
 | 
				
			||||||
        const root = try self.with_cursels_mut(b.root, unindent_cursel, b.allocator);
 | 
					        const root = try self.with_cursels_mut(b.root, unindent_cursel, b.allocator);
 | 
				
			||||||
        if (self.cursels.items.len != cursor_count)
 | 
					        if (self.cursels.items.len != previous_len)
 | 
				
			||||||
            self.restore_cursels();
 | 
					            self.restore_cursels();
 | 
				
			||||||
        try self.update_buf(root);
 | 
					        try self.update_buf(root);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue