Compare commits
1 commit
doc/merge-
...
fix/filety
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dc8a0ec0f |
3 changed files with 94 additions and 0 deletions
|
|
@ -13,12 +13,33 @@ local M = {}
|
|||
|
||||
local dbg = require('diffs.log').dbg
|
||||
|
||||
---@param filepath string
|
||||
---@param n integer
|
||||
---@return string[]?
|
||||
local function read_first_lines(filepath, n)
|
||||
local f = io.open(filepath, 'r')
|
||||
if not f then
|
||||
return nil
|
||||
end
|
||||
local lines = {}
|
||||
for _ = 1, n do
|
||||
local line = f:read('*l')
|
||||
if not line then
|
||||
break
|
||||
end
|
||||
table.insert(lines, line)
|
||||
end
|
||||
f:close()
|
||||
return #lines > 0 and lines or nil
|
||||
end
|
||||
|
||||
---@param filename string
|
||||
---@param repo_root string?
|
||||
---@return string?
|
||||
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 })
|
||||
|
|
@ -27,6 +48,15 @@ local function get_ft_from_filename(filename, repo_root)
|
|||
return ft
|
||||
end
|
||||
end
|
||||
|
||||
local contents = read_first_lines(full_path, 10)
|
||||
if contents then
|
||||
local ft = vim.filetype.match({ filename = filename, contents = contents })
|
||||
if ft then
|
||||
dbg('filetype from file content: %s', ft)
|
||||
return ft
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ft = vim.filetype.match({ filename = filename })
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ local plugin_dir = vim.fn.getcwd()
|
|||
vim.opt.runtimepath:prepend(plugin_dir)
|
||||
vim.opt.packpath = {}
|
||||
|
||||
vim.cmd('filetype on')
|
||||
|
||||
local function ensure_parser(lang)
|
||||
local ok = pcall(vim.treesitter.language.inspect, lang)
|
||||
if not ok then
|
||||
|
|
|
|||
|
|
@ -359,5 +359,67 @@ describe('parser', function()
|
|||
delete_buffer(file_buf)
|
||||
delete_buffer(diff_buf)
|
||||
end)
|
||||
|
||||
it('detects filetype from file content shebang without open buffer', function()
|
||||
local repo_root = '/tmp/diffs-test-shebang'
|
||||
vim.fn.mkdir(repo_root, 'p')
|
||||
|
||||
local file_path = repo_root .. '/build'
|
||||
local f = io.open(file_path, 'w')
|
||||
f:write('#!/bin/bash\n')
|
||||
f:write('set -e\n')
|
||||
f:write('echo "hello"\n')
|
||||
f:close()
|
||||
|
||||
local diff_buf = create_buffer({
|
||||
'M build',
|
||||
'@@ -1,2 +1,3 @@',
|
||||
' #!/bin/bash',
|
||||
'+set -e',
|
||||
' echo "hello"',
|
||||
})
|
||||
vim.api.nvim_buf_set_var(diff_buf, 'diffs_repo_root', repo_root)
|
||||
|
||||
local hunks = parser.parse_buffer(diff_buf)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('build', hunks[1].filename)
|
||||
assert.are.equal('sh', hunks[1].ft)
|
||||
|
||||
delete_buffer(diff_buf)
|
||||
os.remove(file_path)
|
||||
vim.fn.delete(repo_root, 'rf')
|
||||
end)
|
||||
|
||||
it('detects python from shebang without open buffer', function()
|
||||
local repo_root = '/tmp/diffs-test-shebang-py'
|
||||
vim.fn.mkdir(repo_root, 'p')
|
||||
|
||||
local file_path = repo_root .. '/deploy'
|
||||
local f = io.open(file_path, 'w')
|
||||
f:write('#!/usr/bin/env python3\n')
|
||||
f:write('import sys\n')
|
||||
f:write('print("hi")\n')
|
||||
f:close()
|
||||
|
||||
local diff_buf = create_buffer({
|
||||
'M deploy',
|
||||
'@@ -1,2 +1,3 @@',
|
||||
' #!/usr/bin/env python3',
|
||||
'+import sys',
|
||||
' print("hi")',
|
||||
})
|
||||
vim.api.nvim_buf_set_var(diff_buf, 'diffs_repo_root', repo_root)
|
||||
|
||||
local hunks = parser.parse_buffer(diff_buf)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('deploy', hunks[1].filename)
|
||||
assert.are.equal('python', hunks[1].ft)
|
||||
|
||||
delete_buffer(diff_buf)
|
||||
os.remove(file_path)
|
||||
vim.fn.delete(repo_root, 'rf')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue