Skip to content

Commit 9051141

Browse files
fei6409FabijanZulj
authored andcommitted
feat: add focus_blame config (#39)
Introduce a new config "focus_blame" to automatically focus on the blame window when it's opened as well as blame stack push/pop.
1 parent 50f385d commit 9051141

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Default config:
5050
virtual = virtual_view,
5151
default = window_view,
5252
},
53+
focus_blame = true,
5354
merge_consecutive = false,
5455
max_summary_width = 30,
5556
colors = nil,
@@ -70,6 +71,7 @@ These are the fields you can configure by passing them to the `require('blame').
7071
- `date_format` - string - Pattern for the date
7172
- `virtual_style` - "right_align" or "float" - Float moves the virtual text close to the content of the file.
7273
- `views` - views that can be used when toggling blame
74+
- `focus_blame` - boolean - Focus on the blame window when it's opened as well as blame stack push/pop
7375
- `merge_consecutive` - boolean - Merge consecutive blames that are from the same commit
7476
- `max_summary_width` - If date_message is used, cut the summary if it excedes this number of characters
7577
- `colors` - list of RGB strings to use instead of randomly generated RGBs for highlights

lua/blame.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local formats = require("blame.formats.default_formats")
2222
---@class Config
2323
---@field date_format? string Format of the output date
2424
---@field views table<string, BlameView>
25+
---@field focus_blame boolean Focus on the blame window when it's opened as well as blame stack push/pop
2526
---@field merge_consecutive boolean Merge consecutive commits and don't repeat
2627
---@field virtual_style 'float' | 'right_align' Style of the virtual view
2728
---@field colors string[] | nil List of colors to use for highlights. If nill will use random RGB
@@ -38,6 +39,7 @@ local config = {
3839
virtual = virtual_view,
3940
default = window_view,
4041
},
42+
focus_blame = true,
4143
merge_consecutive = false,
4244
max_summary_width = 30,
4345
colors = nil,

lua/blame/blame_stack.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local mappings = require("blame.mappings")
99
---@field git_client Git
1010
---@field stack_buffer integer
1111
---@field stack_info_float_win integer
12+
---@field blame_window integer
1213
---@field original_window integer
1314
---@field original_buffer integer
1415
---@field git_root string
@@ -18,13 +19,14 @@ local mappings = require("blame.mappings")
1819
local BlameStack = {}
1920

2021
---@return BlameStack
21-
function BlameStack:new(config, blame_view, original_window, file_path, cwd)
22+
function BlameStack:new(config, blame_view, file_path, cwd)
2223
local o = {}
2324
setmetatable(o, { __index = self })
2425

2526
o.config = config
2627
o.blame_view = blame_view
27-
o.original_window = original_window
28+
o.blame_window = blame_view.blame_window
29+
o.original_window = blame_view.original_window
2830
o.original_buffer = vim.api.nvim_win_get_buf(o.original_window)
2931
o.file_path = file_path
3032
o.cwd = cwd
@@ -147,6 +149,9 @@ function BlameStack:create_blame_buf()
147149
vim.schedule(function()
148150
self:reset_to_original_buf()
149151
self:close()
152+
if self.config.focus_blame then
153+
vim.api.nvim_set_current_win(self.blame_window)
154+
end
150155
end)
151156
end,
152157
buffer = self.stack_buffer,

lua/blame/views/window_view.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ function WindowView:open(lines)
106106

107107
-- blame window already opened, updating the content
108108
if self.blame_window ~= nil then
109+
if self.config.focus_blame then
110+
vim.api.nvim_set_current_win(self.blame_window)
111+
else
112+
vim.api.nvim_set_current_win(self.original_window)
113+
end
109114
return self:update_opened_blame_view(blame_lines, lines_with_hl)
110115
end
111116

@@ -162,15 +167,20 @@ function WindowView:open(lines)
162167
{ win = self.blame_window }
163168
)
164169
end
165-
vim.api.nvim_set_current_win(self.original_window)
170+
171+
if self.config.focus_blame then
172+
vim.api.nvim_set_current_win(self.blame_window)
173+
else
174+
vim.api.nvim_set_current_win(self.original_window)
175+
end
166176

167177
local file_path = vim.api.nvim_buf_get_name(
168178
vim.api.nvim_win_get_buf(self.original_window)
169179
)
170180
local cwd = vim.fn.expand("%:p:h")
171181
self.cwd = cwd
172182
self.blame_stack_client =
173-
BlameStack:new(self.config, self, self.original_window, file_path, cwd)
183+
BlameStack:new(self.config, self, file_path, cwd)
174184
end
175185

176186
function WindowView:update_opened_blame_view(blame_lines, lines_with_hl)

0 commit comments

Comments
 (0)