Skip to content

Commit

Permalink
Use commit indexes in commit lookups
Browse files Browse the repository at this point in the history
When transferring variables from Lua into Neovim, Neovim copies tables.

Previously, objects were used in commit lookups. This meant that Neovim
would use a copy of the object instead of a reference to the object.
  • Loading branch information
rbong committed Jul 27, 2024
1 parent 96f2e5b commit f4df6e1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
13 changes: 9 additions & 4 deletions autoload/flog/floggraph/commit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ function! flog#floggraph#commit#GetAtLine(...) abort

let l:lnum = type(l:line) == v:t_number ? l:line : line(l:line)

return get(l:state.line_commits, l:lnum - 1, {})
let l:commit_index = get(l:state.line_commits, l:lnum - 1, -1)
if l:commit_index < 0
return {}
endif

return l:state.commits[l:commit_index]
endfunction

function! flog#floggraph#commit#GetByHash(hash) abort
call flog#floggraph#buf#AssertFlogBuf()
let l:state = flog#state#GetBufState()

let l:commit = get(l:state.commits_by_hash, a:hash, {})
if empty(l:commit)
let l:commit_index = get(l:state.commits_by_hash, a:hash, -1)
if l:commit_index < 0
return {}
endif

return l:commit
return l:state.commits[l:commit_index]
endfunction

function! flog#floggraph#commit#GetByRef(ref) abort
Expand Down
6 changes: 4 additions & 2 deletions autoload/flog/floggraph/nav.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ function! flog#floggraph#nav#JumpToCommit(hash, set_jump_mark = v:true, push_to_
return [-1, -1]
endif

let l:commit = get(l:state.commits_by_hash, a:hash, {})
if empty(l:commit)
let l:commit_index = get(l:state.commits_by_hash, a:hash, -1)
if l:commit_index < 0
return [-1, -1]
endif

let l:commit = l:state.commits[l:commit_index]

let l:prev_line = line('.')
let l:prev_commit = flog#floggraph#commit#GetAtLine(l:prev_line)

Expand Down
11 changes: 4 additions & 7 deletions autoload/flog/graph/nvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,27 @@ function! flog#graph#nvim#Update(graph) abort
let len = l:commit.len
let suffix_len = l:commit.suffix_len

" Record commit
let l:commits_by_hash[l:hash] = l:commit

" Update line position
let l:commit.line = l:total_lines + 1

" Add subject
call add(l:output, l:commit.subject)
call add(l:line_commits, l:commit)
call add(l:line_commits, l:commit_index)
let l:total_lines += 1

if len > 1
if get(l:collapsed_commits, l:hash, l:default_collapsed)
" Add collapsed body
call add(l:output, l:commit.collapsed_body)
call add(l:line_commits, l:commit)
call add(l:line_commits, l:commit_index)
let l:total_lines += 1
else
" Add body
let l:body_index = 0
let l:body = l:commit.body
while l:body_index < len - 1
call add(l:output, l:body[l:body_index])
call add(l:line_commits, l:commit)
call add(l:line_commits, l:commit_index)
let l:body_index += 1
endwhile
let l:total_lines += len - 1
Expand All @@ -96,7 +93,7 @@ function! flog#graph#nvim#Update(graph) abort
let l:suffix = l:commit.suffix
while l:suffix_index <= suffix_len - 1
call add(l:output, l:suffix[l:suffix_index])
call add(l:line_commits, l:commit)
call add(l:line_commits, l:commit_index)
let l:suffix_index += 1
endwhile
let l:total_lines += suffix_len
Expand Down
31 changes: 14 additions & 17 deletions lua/flog/graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ local function flog_get_graph(

-- Init Vim commit
local vim_commit
local vim_commit_index = commit_index - 1
if enable_vim then
vim_commit = vim.dict()
else
Expand Down Expand Up @@ -609,11 +610,11 @@ local function flog_get_graph(

-- Add commit data
vim_commits[commit_index] = vim_commit
vim_commits_by_hash[commit_hash] = vim_commit
vim_commits_by_hash[commit_hash] = vim_commit_index

-- Add commit subject line

vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index

vim_commit.subject = table.concat(commit_prefix, '') .. commit_out[1]
vim_out[vim_out_index] = vim_commit.subject
Expand All @@ -633,7 +634,7 @@ local function flog_get_graph(
vim_commit.collapsed_body = prefix .. string.format('== %d hidden lines ==', ncommit_lines - 1)

if collapsed then
vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index
vim_out[vim_out_index] = vim_commit.collapsed_body
vim_out_index = vim_out_index + 1
end
Expand All @@ -642,7 +643,7 @@ local function flog_get_graph(
vim_commit_body[commit_out_index - 1] = prefix .. commit_out[commit_out_index]

if not collapsed then
vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index
vim_out[vim_out_index] = vim_commit_body[commit_out_index - 1]
vim_out_index = vim_out_index + 1
end
Expand All @@ -654,7 +655,7 @@ local function flog_get_graph(
-- Add merge lines

if should_out_merge then
vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index

vim_commit_suffix[vim_commit_suffix_index] = table.concat(merge_line, '')
vim_out[vim_out_index] = vim_commit_suffix[vim_commit_suffix_index]
Expand All @@ -663,7 +664,7 @@ local function flog_get_graph(
vim_commit_suffix_index = vim_commit_suffix_index + 1

if should_out_complex then
vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index

vim_commit_suffix[vim_commit_suffix_index] = table.concat(complex_merge_line, '')
vim_out[vim_out_index] = vim_commit_suffix[vim_commit_suffix_index]
Expand All @@ -676,15 +677,15 @@ local function flog_get_graph(
-- Add missing parents lines

if should_out_missing_parents then
vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index

vim_commit_suffix[vim_commit_suffix_index] = table.concat(missing_parents_line_1, '')
vim_out[vim_out_index] = vim_commit_suffix[vim_commit_suffix_index]

vim_out_index = vim_out_index + 1
vim_commit_suffix_index = vim_commit_suffix_index + 1

vim_line_commits[vim_out_index] = vim_commit
vim_line_commits[vim_out_index] = vim_commit_index

vim_commit_suffix[vim_commit_suffix_index] = table.concat(missing_parents_line_2, '')
vim_out[vim_out_index] = vim_commit_suffix[vim_commit_suffix_index]
Expand Down Expand Up @@ -826,6 +827,7 @@ local function flog_update_graph(

-- Rebuild output/line commits
while commit_index <= ncommits do
local vim_commit_index = commit_index - 1
local commit = commits[commit_index]
local hash = commit.hash
local len = commit.len
Expand All @@ -834,14 +836,9 @@ local function flog_update_graph(
-- Update line position
commit.line = total_lines

if enable_nvim then
-- Re-record commit
commits_by_hash[hash] = commit
end

-- Add subject
output[total_lines] = commit.subject
line_commits[total_lines] = commit
line_commits[total_lines] = vim_commit_index
total_lines = total_lines + 1

if len > 1 then
Expand All @@ -854,15 +851,15 @@ local function flog_update_graph(
if collapsed then
-- Add collapsed body
output[total_lines] = commit.collapsed_body
line_commits[total_lines] = commit
line_commits[total_lines] = vim_commit_index
total_lines = total_lines + 1
else
-- Add body
local body_index = 1
local body = commit.body
while body_index < len do
output[total_lines] = body[body_index]
line_commits[total_lines] = commit
line_commits[total_lines] = vim_commit_index
body_index = body_index + 1
total_lines = total_lines + 1
end
Expand All @@ -875,7 +872,7 @@ local function flog_update_graph(
local suffix = commit.suffix
while suffix_index <= suffix_len do
output[total_lines] = suffix[suffix_index]
line_commits[total_lines] = commit
line_commits[total_lines] = vim_commit_index
suffix_index = suffix_index + 1
total_lines = total_lines + 1
end
Expand Down
18 changes: 9 additions & 9 deletions plugin/flog_graph_vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def g:FlogGetVimBinGraph(git_cmd: string): dict<any>
# Parse subject
commit.subject = out[out_index]
add(final_out, out[out_index])
add(line_commits, commit)
add(line_commits, commit_index)
out_index += 1
total_lines += 1

Expand All @@ -113,7 +113,7 @@ def g:FlogGetVimBinGraph(git_cmd: string): dict<any>

if collapsed
add(final_out, commit.collapsed_body)
add(line_commits, commit)
add(line_commits, commit_index)
total_lines += 1
else
total_lines += len
Expand All @@ -130,7 +130,7 @@ def g:FlogGetVimBinGraph(git_cmd: string): dict<any>

if !collapsed
add(final_out, out[out_index])
add(line_commits, commit)
add(line_commits, commit_index)
endif

out_index += 1
Expand All @@ -147,7 +147,7 @@ def g:FlogGetVimBinGraph(git_cmd: string): dict<any>
while suffix_index <= suffix_len
suffix[suffix_index] = out[out_index]
add(final_out, out[out_index])
add(line_commits, commit)
add(line_commits, commit_index)

out_index += 1
suffix_index += 1
Expand All @@ -158,7 +158,7 @@ def g:FlogGetVimBinGraph(git_cmd: string): dict<any>

# Increment
add(commits, commit)
commits_by_hash[hash] = commit
commits_by_hash[hash] = commit_index
commit_index += 1
endwhile

Expand Down Expand Up @@ -198,22 +198,22 @@ def g:FlogUpdateVimBinGraph(graph: dict<any>): dict<any>

# Add subject
add(output, commit.subject)
add(line_commits, commit)
add(line_commits, commit_index)
total_lines += 1

if len > 1
if has_key(collapsed_commits, commit.hash)
# Add collapsed body
add(output, commit.collapsed_body)
add(line_commits, commit)
add(line_commits, commit_index)
total_lines += 1
else
# Add body
var body_index = 1
var body = commit.body
while body_index < len
add(output, body[body_index])
add(line_commits, commit)
add(line_commits, commit_index)
body_index += 1
endwhile
total_lines += len - 1
Expand All @@ -226,7 +226,7 @@ def g:FlogUpdateVimBinGraph(graph: dict<any>): dict<any>
var suffix = commit.suffix
while suffix_index <= suffix_len
add(output, suffix[suffix_index])
add(line_commits, commit)
add(line_commits, commit_index)
suffix_index += 1
endwhile
total_lines += suffix_len
Expand Down

0 comments on commit f4df6e1

Please sign in to comment.