Skip to content

Commit

Permalink
Identify programming language for current buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 8, 2024
1 parent ceedc0d commit 1eddbed
Show file tree
Hide file tree
Showing 23 changed files with 515 additions and 227 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ use {
-- Show "Fitten Code - Start Chat" in the editor context menu, when you right-click on the code.
show_in_editor_context_menu = true,
},
identify_programming_language = {
-- Identify programming language of the current buffer
-- * Unnamed buffer
-- * Buffer without file extension
-- * Buffer no filetype detected
identify_buffer = true,
}
},
disable_specific_inline_completion = {
-- Disable auto-completion for some specific file suffixes by entering them below
Expand Down Expand Up @@ -134,6 +141,10 @@ use {
---@type integer
delaytime = 0,
},
prompt = {
-- Maximum number of characters to prompt for completion/chat.
max_characters = 1000000,
},
-- Enable/Disable the default keymaps in inline completion.
use_default_keymaps = true,
-- Default keymaps
Expand All @@ -147,7 +158,13 @@ use {
['<A-\\>'] = 'triggering_completion',
},
chat = {
['q'] = 'close'
['q'] = 'close',
['[c'] = 'goto_previous_conversation',
[']c'] = 'goto_next_conversation',
['c'] = 'copy_conversation',
['C'] = 'copy_all_conversations',
['d'] = 'delete_conversation',
['D'] = 'delete_all_conversations',
}
},
-- Setting for source completion.
Expand All @@ -157,8 +174,8 @@ use {
},
-- Set the mode of the completion.
-- Available options:
-- - 'inline' (VSCode style inline completion)
-- - 'source' (integrates into other completion plugins)
-- * 'inline' (VSCode style inline completion)
-- * 'source' (integrates into other completion plugins)
completion_mode = 'inline',
---@class LogOptions
log = {
Expand Down Expand Up @@ -296,7 +313,6 @@ vim.log = {
| `translate_text_into_chinese(TranslateTextOptions)` | Translate text into Chinese |
| `translate_text_into_english(TranslateTextOptions)` | Translate text into English |
| `start_chat(ActionOptions)` | Start chat |
| `stop_eval()` | Stop the evaluation |

## 🎉 Special Thanks

Expand Down
101 changes: 101 additions & 0 deletions lua/fittencode/actions/identify_programming_language.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
local api = vim.api

local API = require('fittencode.api').api
local Base = require('fittencode.base')
local Config = require('fittencode.config')
local Log = require('fittencode.log')

local M = {}

local DEFER = 1000

-- milliseconds
local IPL_DEBOUNCE_TIME = 500

---@type uv_timer_t
local ipl_timer = nil

local function _identify_current_buffer()
local buffer = api.nvim_get_current_buf()
local name = api.nvim_buf_get_name(buffer)
local ext = vim.fn.fnamemodify(name, ':e')
if #name > 0 and #ext > 0 then
return
end
local ipl = ''
local success, result = pcall(api.nvim_buf_get_var, buffer, 'fittencode_identify_programming_language')
if success and result and #result > 0 then
ipl = result
end
local filetype = api.nvim_get_option_value('filetype', {
buf = buffer,
})
if #filetype > 0 and #ipl == 0 then
return
end

local count, lines = Base.buffer_characters(buffer)
if not count or not lines then
return
end
if count > Config.options.prompt.max_characters then
return
end

local content = table.concat(lines, '\n')
API.identify_programming_language({
headless = true,
content = content,
on_success = function(suggestions)
if not suggestions or #suggestions == 0 then
return
end
local lang = suggestions[1]
if #lang == 0 then
return
end
lang = lang:lower()
lang = lang:gsub('^%s*(.-)%s*$', '%1')
if #lang == 0 then
return
end
lang = lang:gsub('c%+%+', 'cpp')
lang = lang:match('^(%w+)')
api.nvim_set_option_value('filetype', lang, {
buf = buffer,
})
api.nvim_buf_set_var(buffer, 'fittencode_identify_programming_language', lang)
end,
})
end

local function _ipl_wrap()
Base.debounce(ipl_timer, function()
_identify_current_buffer()
end, IPL_DEBOUNCE_TIME)
end

local function register_identify_current_buffer()
api.nvim_create_autocmd({ 'TextChangedI', 'BufReadPost' }, {
group = Base.augroup('Actions', 'IdentifyProgrammingLanguage'),
pattern = '*',
callback = function(params)
if not API.ready_for_generate() then
vim.defer_fn(function()
_ipl_wrap()
end, DEFER)
return
end
_ipl_wrap()
end,
desc = 'Identify programming language for current buffer',
})
end

function M.setup()
if Config.options.action.identify_programming_language.identify_buffer then
register_identify_current_buffer()
end
end

return M
7 changes: 7 additions & 0 deletions lua/fittencode/actions/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local M = {}

function M.setup()
require('fittencode.actions.identify_programming_language').setup()
end

return M
6 changes: 3 additions & 3 deletions lua/fittencode/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ M.api = {
get_current_status = function()
return Engines.get_status()
end,
ready_for_generate = function()
return Sessions.ready_for_generate()
end,
triggering_completion = function()
InlineEngine.triggering_completion()
end,
Expand Down Expand Up @@ -128,9 +131,6 @@ M.api = {
start_chat = function(opts)
return ActionsEngine.start_chat(opts)
end,
stop_eval = function()
return ActionsEngine.stop_eval()
end,
show_chat = function()
return ActionsEngine.show_chat()
end,
Expand Down
19 changes: 17 additions & 2 deletions lua/fittencode/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ function M.map(mode, lhs, rhs, opts)
vim.keymap.set(mode, lhs, rhs, opts)
end

---@param tag string
---@param name string
function M.augroup(name)
return api.nvim_create_augroup('FittenCode/' .. name, { clear = true })
function M.augroup(tag, name)
return api.nvim_create_augroup('FittenCode/' .. tag .. '/' .. name, { clear = true })
end

---@param name string
Expand Down Expand Up @@ -186,6 +187,20 @@ function M.rfind(s, sub)
end)()
end

---@param buffer? number
function M.buffer_characters(buffer)
buffer = buffer or api.nvim_get_current_buf()
if not api.nvim_buf_is_valid(buffer) then
return
end
local count = 0
local lines = api.nvim_buf_get_lines(buffer, 0, -1, false)
vim.tbl_map(function(line)
count = count + #line
end, lines)
return count, lines
end

---@class NeovimVersion
---@field nvim string
---@field buildtype string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local Base = require('fittencode.base')
local NetworkError = require('fittencode.client.network_error')
local Process = require('fittencode.concurrency.process')
local Promise = require('fittencode.concurrency.promise')
local RestManager = require('fittencode.rest.manager')
Expand Down Expand Up @@ -84,7 +83,7 @@ function M:generate_one_stage(api_key, params, on_success, on_error)
}, data, function(response)
resolve(response)
end, function()
schedule(on_error, NetworkError:new())
schedule(on_error)
end)
end):forward(function(response)
local generated_text = Resopnse._on_stage_response(response)
Expand Down
9 changes: 0 additions & 9 deletions lua/fittencode/client/network_error.lua

This file was deleted.

5 changes: 5 additions & 0 deletions lua/fittencode/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ M.FittenSuggestionSpacesLine = 'FittenSuggestionSpacesLine'
M.FittenNoMoreSuggestion = 'FittenNoMoreSuggestion'
M.FittenSuggestionStage = 'FittenSuggestionStage'
M.FittenSuggestionStageSpacesLine = 'FittenSuggestionStage'
M.FittenChatConversation = 'FittenChatConversation'

-- Define FittenCode colors
local colors = {}
Expand All @@ -33,6 +34,10 @@ function M.setup_highlight()
bg = colors.gray2,
ctermfg = 'LightYellow',
})
Base.set_hi(M.FittenChatConversation, {
bg = colors.gray2,
ctermbg = 'LightGrey',
})
end

return M
2 changes: 0 additions & 2 deletions lua/fittencode/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ function M.setup()
-- Arguments: language
start_chat = _start_chat,
-- Arguments: Nop
stop_eval = API.stop_eval,
-- Arguments: Nop
show_chat = API.show_chat,
-- Arguments: Nop
toggle_chat = API.toggle_chat,
Expand Down
37 changes: 27 additions & 10 deletions lua/fittencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ local defaults = {
-- Show "Fitten Code - Start Chat" in the editor context menu, when you right-click on the code.
show_in_editor_context_menu = true,
},
identify_programming_language = {
-- Identify programming language of the current buffer
-- * Unnamed buffer
-- * Buffer without file extension
-- * Buffer no filetype detected
identify_buffer = true,
}
},
disable_specific_inline_completion = {
-- Disable auto-completion for some specific file suffixes by entering them below
Expand Down Expand Up @@ -71,6 +78,14 @@ local defaults = {
---@type integer
delaytime = 0,
},
prompt = {
-- Maximum number of characters to prompt for completion/chat.
max_characters = 1000000,
},
chat = {
-- Highlight the conversation in the chat window at the current cursor position.
highlight_conversation_at_cursor = false,
},
-- Enable/Disable the default keymaps in inline completion.
use_default_keymaps = true,
-- Default keymaps
Expand All @@ -84,7 +99,13 @@ local defaults = {
['<A-\\>'] = 'triggering_completion',
},
chat = {
['q'] = 'close'
['q'] = 'close',
['[c'] = 'goto_previous_conversation',
[']c'] = 'goto_next_conversation',
['c'] = 'copy_conversation',
['C'] = 'copy_all_conversations',
['d'] = 'delete_conversation',
['D'] = 'delete_all_conversations',
}
},
-- Setting for source completion.
Expand All @@ -106,8 +127,8 @@ local defaults = {
},
-- Set the mode of the completion.
-- Available options:
-- - 'inline' (VSCode style inline completion)
-- - 'source' (integrates into other completion plugins)
-- * 'inline' (VSCode style inline completion)
-- * 'source' (integrates into other completion plugins)
completion_mode = 'inline',
rest = {
-- Rest backend to use. Available options:
Expand All @@ -133,13 +154,6 @@ local defaults = {
},
}

-- Private options
M.internal = {
virtual_text = {
inline = vim.fn.has('nvim-0.10') == 1,
},
}

---@param opts? FittenCodeOptions
function M.setup(opts)
---@class FittenCodeOptions
Expand All @@ -148,6 +162,9 @@ function M.setup(opts)
M.options.keymaps.inline = {}
M.options.keymaps.chat = {}
end
if vim.fn.has('nvim-0.10') ~= 1 then
M.options.inline_completion.disable_completion_within_the_line = true
end
end

return M
Loading

0 comments on commit 1eddbed

Please sign in to comment.