fix(parser): detect filetype from existing buffer
Files detected via shebang or modeline (e.g., `build` with `#!/bin/bash`) weren't getting syntax highlighting because `vim.filetype.match()` only does filename-based detection. Now `get_ft_from_filename()` first checks if a buffer already exists for the file and uses its detected filetype. This requires knowing the repo root to construct the full path, so: - `parse_buffer()` reads `b:diffs_repo_root` or `b:git_dir` (fugitive) - `commands.lua` sets `b:diffs_repo_root` on diff buffers it creates Co-authored-by: phanen <phanen@qq.com>
This commit is contained in:
parent
b4e40e4093
commit
c51d625dc6
3 changed files with 98 additions and 2 deletions
|
|
@ -14,8 +14,21 @@ local M = {}
|
|||
local dbg = require('diffs.log').dbg
|
||||
|
||||
---@param filename string
|
||||
---@param repo_root string?
|
||||
---@return string?
|
||||
local function get_ft_from_filename(filename)
|
||||
local function get_ft_from_filename(filename, repo_root)
|
||||
if repo_root then
|
||||
local full_path = vim.fs.joinpath(repo_root, filename)
|
||||
local buf = vim.fn.bufnr(full_path)
|
||||
if buf ~= -1 then
|
||||
local ft = vim.api.nvim_get_option_value('filetype', { buf = buf })
|
||||
if ft and ft ~= '' then
|
||||
dbg('filetype from existing buffer %d: %s', buf, ft)
|
||||
return ft
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ft = vim.filetype.match({ filename = filename })
|
||||
if not ft then
|
||||
dbg('no filetype for: %s', filename)
|
||||
|
|
@ -39,10 +52,27 @@ local function get_lang_from_ft(ft)
|
|||
return nil
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return string?
|
||||
local function get_repo_root(bufnr)
|
||||
local ok, repo_root = pcall(vim.api.nvim_buf_get_var, bufnr, 'diffs_repo_root')
|
||||
if ok and repo_root then
|
||||
return repo_root
|
||||
end
|
||||
|
||||
local ok2, git_dir = pcall(vim.api.nvim_buf_get_var, bufnr, 'git_dir')
|
||||
if ok2 and git_dir then
|
||||
return vim.fn.fnamemodify(git_dir, ':h')
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return diffs.Hunk[]
|
||||
function M.parse_buffer(bufnr)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local repo_root = get_repo_root(bufnr)
|
||||
---@type diffs.Hunk[]
|
||||
local hunks = {}
|
||||
|
||||
|
|
@ -95,7 +125,7 @@ function M.parse_buffer(bufnr)
|
|||
if filename then
|
||||
flush_hunk()
|
||||
current_filename = filename
|
||||
current_ft = get_ft_from_filename(filename)
|
||||
current_ft = get_ft_from_filename(filename, repo_root)
|
||||
current_lang = current_ft and get_lang_from_ft(current_ft) or nil
|
||||
if current_lang then
|
||||
dbg('file: %s -> lang: %s', filename, current_lang)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue