Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nest filetype in Markdown properly #53

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/fittencode/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end
---@param name string
---@param hi table
function M.set_hi(name, hi)
if vim.fn.has('nvim-0.10') == 1 then
if fn.has('nvim-0.10') == 1 then
-- https://github.com/neovim/neovim/pull/25229
-- https://github.com/luozhiya/fittencode.nvim/issues/20
-- hi.force = true
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/bindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function M.setup_keymaps()
Base.map('i', '<C-Right>', API.accept_word)
end

function M.setup_onkey()
function M.setup_keyfilters()
-- '<80>kd', '<80>kD' in Lua
local keycodes = {
'<Backspace>',
Expand Down
50 changes: 46 additions & 4 deletions lua/fittencode/engines/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,39 @@ local function on_error(err)
end
end

local function _find_nospace(line)
for i = 1, #line do
if line:sub(i, i) ~= ' ' then
return i
end
end
end

local function _get_tslangs(buffer, start_row, end_row)
local row = start_row
local col = 0

for i = start_row, end_row do
local line = api.nvim_buf_get_lines(buffer, i, i + 1, false)[1]
local pos = _find_nospace(line)
if pos then
row = i
col = pos
break
end
end

local info = vim.inspect_pos(buffer, row, col)
local ts = info.treesitter
local langs = {}
for _, node in ipairs(ts) do
if not vim.tbl_contains(langs, node.lang) then
langs[#langs + 1] = node.lang
end
end
return langs
end

---@param action number
---@param opts? ActionOptions
---@return nil
Expand Down Expand Up @@ -199,19 +232,28 @@ function ActionsEngine.start_action(action, opts)
local vmode = { 'v', 'V', '<C-V>' }
Log.debug('mode: {}', api.nvim_get_mode().mode)
if vim.tbl_contains(vmode, api.nvim_get_mode().mode) then
sln = vim.fn.getpos("'<")[2]
eln = vim.fn.getpos("'>")[2]
sln = fn.getpos("'<")[2]
eln = fn.getpos("'>")[2]
Log.debug('v mode sln: {}, eln: {}', sln, eln)
end

chat:show()
vim.fn.win_gotoid(window)
fn.win_gotoid(window)

local filetype = api.nvim_get_option_value('filetype', { buf = buffer })
Log.debug('Action filetype: {}', filetype)
local langs = _get_tslangs(buffer, sln, eln)
Log.debug('Action langs: {}', langs)
if filetype == 'markdown' and #langs >= 2 then
filetype = vim.tbl_filter(function(lang) return lang ~= 'markdown' end, langs)[1]
end
Log.debug('Action filetype: {}', filetype)

local prompt_opts = {
window = window,
buffer = buffer,
range = { sln - 1, eln - 1 },
filetype = vim.bo.filetype,
filetype = filetype,
prompt_ty = get_action_type(action),
solved_content = opts and opts.content,
solved_prefix = nil,
Expand Down
3 changes: 2 additions & 1 deletion lua/fittencode/fs/path.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local api = vim.api
local fn = vim.fn

local Base = require('fittencode.base')

Expand Down Expand Up @@ -41,7 +42,7 @@ end

function M.name(buffer)
local path = api.nvim_buf_get_name(buffer)
return vim.fn.fnamemodify(path, ':t')
return fn.fnamemodify(path, ':t')
end

return M
2 changes: 1 addition & 1 deletion lua/fittencode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function M.setup(opts)
Bindings.setup_keymaps()
end
if Config.options.inline_completion.disable_completion_when_delete then
Bindings.setup_onkey()
Bindings.setup_keyfilters()
end
elseif Config.options.completion_mode == 'source' then
require('fittencode.sources').setup()
Expand Down
4 changes: 3 additions & 1 deletion lua/fittencode/rest/manager.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local api = vim.api

local Config = require('fittencode.config')
local Log = require('fittencode.log')

Expand Down Expand Up @@ -26,7 +28,7 @@ end
function M.setup()
if not vim.tbl_contains(vim.tbl_keys(builtin_backends), Config.options.rest.backend) then
local msg = 'Invalid rest backend: ' .. Config.options.rest.backend
vim.api.nvim_err_writeln(msg)
api.nvim_err_writeln(msg)
end
end

Expand Down
44 changes: 23 additions & 21 deletions lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local api = vim.api

local Base = require('fittencode.base')
local Log = require('fittencode.log')

Expand All @@ -21,30 +23,30 @@ end

function M:show()
if self.win == nil then
self.buffer = vim.api.nvim_create_buf(false, true)
self.buffer = api.nvim_create_buf(false, true)
vim.cmd('topleft vsplit')
vim.cmd('vertical resize ' .. 40)
self.win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.win, self.buffer)
vim.api.nvim_buf_set_name(self.buffer, 'FittenCodeChat')
self.win = api.nvim_get_current_win()
api.nvim_win_set_buf(self.win, self.buffer)
api.nvim_buf_set_name(self.buffer, 'FittenCodeChat')

vim.api.nvim_set_option_value('filetype', 'markdown', { buf = self.buffer })
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
vim.api.nvim_set_option_value('wrap', true, { win = self.win })
vim.api.nvim_set_option_value('linebreak', true, { win = self.win })
vim.api.nvim_set_option_value('cursorline', true, { win = self.win })
vim.api.nvim_set_option_value('spell', false, { win = self.win })
vim.api.nvim_set_option_value('number', false, { win = self.win })
vim.api.nvim_set_option_value('relativenumber', false, { win = self.win })
vim.api.nvim_set_option_value('conceallevel', 3, { win = self.win })
api.nvim_set_option_value('filetype', 'markdown', { buf = self.buffer })
api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
api.nvim_set_option_value('wrap', true, { win = self.win })
api.nvim_set_option_value('linebreak', true, { win = self.win })
api.nvim_set_option_value('cursorline', true, { win = self.win })
api.nvim_set_option_value('spell', false, { win = self.win })
api.nvim_set_option_value('number', false, { win = self.win })
api.nvim_set_option_value('relativenumber', false, { win = self.win })
api.nvim_set_option_value('conceallevel', 3, { win = self.win })

Base.map('n', 'q', function()
self:close()
end, { buffer = self.buffer })

if #self.text > 0 then
vim.api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text)
vim.api.nvim_win_set_cursor(self.win, { #self.text, 0 })
api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text)
api.nvim_win_set_cursor(self.win, { #self.text, 0 })
end
end
end
Expand All @@ -53,7 +55,7 @@ function M:close()
if self.win == nil then
return
end
vim.api.nvim_win_close(self.win, true)
api.nvim_win_close(self.win, true)
self.win = nil
self.buffer = nil
end
Expand All @@ -75,16 +77,16 @@ function M:commit(text, linebreak)
end
end
if self.buffer then
vim.api.nvim_set_option_value('modifiable', true, { buf = self.buffer })
api.nvim_set_option_value('modifiable', true, { buf = self.buffer })
if #self.text == 0 then
vim.api.nvim_buf_set_lines(self.buffer, 0, -1, false, lines)
api.nvim_buf_set_lines(self.buffer, 0, -1, false, lines)
else
vim.api.nvim_buf_set_lines(self.buffer, -1, -1, false, lines)
api.nvim_buf_set_lines(self.buffer, -1, -1, false, lines)
end
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
end
table.move(lines, 1, #lines, #self.text + 1, self.text)
vim.api.nvim_win_set_cursor(self.win, { #self.text, 0 })
api.nvim_win_set_cursor(self.win, { #self.text, 0 })
end

local function _sub_match(s, pattern)
Expand Down