|
| 1 | +-- Apply Git status highlights in Oil file manager |
| 2 | + |
| 3 | +local augroup = vim.api.nvim_create_augroup("ruicsh/filetype/oil", { clear = true }) |
| 4 | +local ns = vim.api.nvim_create_namespace("ruicsh/filetype/oil") |
| 5 | + |
| 6 | +local function get_highlight_group(status_code) |
| 7 | + if not status_code then |
| 8 | + return nil, nil |
| 9 | + end |
| 10 | + |
| 11 | + local first_char = status_code:sub(1, 1) |
| 12 | + local second_char = status_code:sub(2, 2) |
| 13 | + |
| 14 | + -- Check staged changes first (prioritize staged over unstaged) |
| 15 | + if first_char == "A" then |
| 16 | + return "OilGitAdded", "+" |
| 17 | + elseif first_char == "M" then |
| 18 | + return "OilGitModified", "~" |
| 19 | + elseif first_char == "R" then |
| 20 | + return "OilGitRenamed", "→" |
| 21 | + end |
| 22 | + |
| 23 | + -- Check unstaged changes |
| 24 | + if second_char == "M" then |
| 25 | + return "OilGitModified", "~" |
| 26 | + end |
| 27 | + |
| 28 | + -- Untracked files |
| 29 | + if status_code == "??" then |
| 30 | + return "OilGitUntracked", "?" |
| 31 | + end |
| 32 | + |
| 33 | + -- Ignored files |
| 34 | + if status_code == "!!" then |
| 35 | + return "OilGitIgnored", "!" |
| 36 | + end |
| 37 | + |
| 38 | + return nil, nil |
| 39 | +end |
| 40 | + |
| 41 | +local function get_git_status() |
| 42 | + local oil = require("oil") |
| 43 | + local current_dir = oil.get_current_dir() |
| 44 | + local git_root = vim.git.get_root_dir() |
| 45 | + |
| 46 | + if not git_root or not current_dir then |
| 47 | + return {} |
| 48 | + end |
| 49 | + |
| 50 | + -- Get relative path from git root to current directory |
| 51 | + local rel_path = vim.fs.normalize(vim.fn.fnamemodify(current_dir, ":~:.")) |
| 52 | + if rel_path == "" then |
| 53 | + rel_path = "." |
| 54 | + end |
| 55 | + |
| 56 | + local result = vim.system({ |
| 57 | + "git", |
| 58 | + "-C", |
| 59 | + git_root, |
| 60 | + "status", |
| 61 | + "--porcelain", |
| 62 | + "--ignored", |
| 63 | + "--", |
| 64 | + rel_path, |
| 65 | + }, { text = true }):wait() |
| 66 | + |
| 67 | + if result.code ~= 0 then |
| 68 | + return {} |
| 69 | + end |
| 70 | + |
| 71 | + local output = result.stdout |
| 72 | + if not output or output == "" then |
| 73 | + return {} |
| 74 | + end |
| 75 | + |
| 76 | + local status = {} |
| 77 | + for line in output:gmatch("[^\r\n]+") do |
| 78 | + if #line >= 3 then |
| 79 | + local status_code = line:sub(1, 2) |
| 80 | + local filepath = line:sub(4) |
| 81 | + |
| 82 | + -- Handle renames (format: "old-name -> new-name") |
| 83 | + if status_code:sub(1, 1) == "R" then |
| 84 | + local arrow_pos = filepath:find(" %-> ") |
| 85 | + if arrow_pos then |
| 86 | + filepath = filepath:sub(arrow_pos + 4) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + -- Remove leading "./" if present |
| 91 | + if filepath:sub(1, 2) == "./" then |
| 92 | + filepath = filepath:sub(3) |
| 93 | + end |
| 94 | + |
| 95 | + -- Convert to absolute path |
| 96 | + local abs_path = vim.fs.joinpath(git_root, filepath) |
| 97 | + |
| 98 | + status[abs_path] = status_code |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + return status |
| 103 | +end |
| 104 | + |
| 105 | +local function clear_highlights() |
| 106 | + local bufnr = vim.api.nvim_get_current_buf() |
| 107 | + vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) |
| 108 | +end |
| 109 | + |
| 110 | +local function apply_git_highlights() |
| 111 | + local oil = require("oil") |
| 112 | + local current_dir = oil.get_current_dir() |
| 113 | + |
| 114 | + local git_status = get_git_status() |
| 115 | + if vim.tbl_isempty(git_status) then |
| 116 | + clear_highlights() |
| 117 | + return |
| 118 | + end |
| 119 | + |
| 120 | + local bufnr = vim.api.nvim_get_current_buf() |
| 121 | + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 122 | + |
| 123 | + clear_highlights() |
| 124 | + |
| 125 | + for i, line in ipairs(lines) do |
| 126 | + local entry = oil.get_entry_on_line(bufnr, i) |
| 127 | + if entry and entry.type == "file" then |
| 128 | + local filepath = vim.fs.joinpath(current_dir, entry.name) |
| 129 | + |
| 130 | + local status_code = git_status[filepath] |
| 131 | + local hl_group, symbol = get_highlight_group(status_code) |
| 132 | + |
| 133 | + if hl_group and symbol then |
| 134 | + -- Find the filename part in the line and highlight it |
| 135 | + local name_start = line:find(entry.name, 1, true) |
| 136 | + if name_start then |
| 137 | + -- Highlight the filename |
| 138 | + vim.fn.matchaddpos(hl_group, { { i, name_start, #entry.name } }) |
| 139 | + |
| 140 | + -- Add symbol as virtual text at the end of the line |
| 141 | + vim.api.nvim_buf_set_extmark(bufnr, ns, i - 1, 0, { |
| 142 | + virt_text = { { symbol, hl_group } }, |
| 143 | + virt_text_pos = "eol", |
| 144 | + hl_mode = "combine", |
| 145 | + }) |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +vim.api.nvim_create_autocmd("BufEnter", { |
| 153 | + group = augroup, |
| 154 | + pattern = "oil://*", |
| 155 | + callback = function() |
| 156 | + -- Small delay to ensure oil is fully loaded |
| 157 | + vim.defer_fn(apply_git_highlights, 50) |
| 158 | + end, |
| 159 | +}) |
0 commit comments