Skip to content

Commit

Permalink
perf: move crucial components to the Rust codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Apr 23, 2024
1 parent bc1c47c commit 3cb21e5
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 152 deletions.
56 changes: 33 additions & 23 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ local timer = vim.loop.new_timer()
local enabled = false
local is_focused = true
local force_idle = false
local is_blacklisted = false
local problem_count = -1
local last_updated = os.clock()
local last_presence
local is_blacklisted

local function connect(config)
discord.init(
Expand All @@ -72,6 +72,14 @@ local function connect(config)
config.text.file_browser,
config.text.plugin_manager,
config.text.workspace,
vim.fn.getcwd(),
ffi.new(
'Buttons',
(config.buttons[1] and config.buttons[1].label) or '',
(config.buttons[1] and config.buttons[1].url) or '',
(config.buttons[2] and config.buttons[2].label) or '',
(config.buttons[2] and config.buttons[2].url) or ''
),
config.display.swap_fields
)
end
Expand Down Expand Up @@ -114,7 +122,7 @@ local function update_idle_presence(config)
return false
end

local function update_presence(config)
local function update_presence(config, initial)
if is_blacklisted then
return
end
Expand All @@ -140,6 +148,13 @@ local function update_presence(config)
local success = discord.update_presence(current_presence.name, current_presence.type, current_presence.readonly, cursor_pos, problem_count)
if success then
last_presence = current_presence
if is_blacklisted == nil then
is_blacklisted = utils.array_contains(config.display.workspace_blacklist, ffi.string(discord.update_workspace(vim.fn.getcwd())))
end
if initial then
timer:stop()
timer:start(0, config.timer.interval, vim.schedule_wrap(function() update_presence(config, false) end))
end
end
elseif not update_idle_presence(config) then
return
Expand All @@ -148,41 +163,36 @@ end

local function start_timer(config)
timer:stop()
if vim.g.cord_started == nil then
vim.g.cord_started = true
if not utils.validate_severity(config) then return end
is_blacklisted = utils.array_contains(config.display.workspace_blacklist, utils.update_cwd(config, discord))
cord.setup_autocmds(config)
if config.display.show_time then
discord.update_time()
end

if not utils.validate_severity(config) then return end
if config.display.show_time then
discord.update_time()
end
update_presence(config)
timer:start(0, config.timer.interval, vim.schedule_wrap(function() update_presence(config) end))

timer:start(0, 1000, vim.schedule_wrap(function() update_presence(config, true) end))
end

function cord.setup(userConfig)
if vim.g.cord_initialized == nil then
local config = vim.tbl_deep_extend('force', cord.config, userConfig or {})
config.timer.interval = math.max(config.timer.interval, 500)

local work = vim.loop.new_async(vim.schedule_wrap(function()
discord = utils.init_discord(ffi)
connect(config)
if config.timer.enable then
start_timer(config)
end
discord = utils.init_discord(ffi)
connect(config)
if config.timer.enable then
cord.setup_autocmds(config)
start_timer(config)
end

vim.api.nvim_create_autocmd('ExitPre', { callback = function() discord.disconnect() end })
if config.usercmds then cord.setup_usercmds(config) end

vim.api.nvim_create_autocmd('ExitPre', { callback = function() discord.disconnect() end })
if config.usercmds then cord.setup_usercmds(config) end
end))
work:send()
vim.g.cord_initialized = true
end
end

function cord.setup_autocmds(config)
vim.api.nvim_create_autocmd('DirChanged', { callback = function() is_blacklisted = utils.array_contains(config.display.workspace_blacklist, utils.update_cwd(config, discord)) end })
vim.api.nvim_create_autocmd('DirChanged', { callback = function() is_blacklisted = utils.array_contains(config.display.workspace_blacklist, ffi.string(discord.update_workspace(vim.fn.getcwd()))) end })
vim.api.nvim_create_autocmd('FocusGained', { callback = function() is_focused = true; last_presence = nil end })
vim.api.nvim_create_autocmd('FocusLost', { callback = function() is_focused = false end })
end
Expand Down
94 changes: 12 additions & 82 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ local function init_discord(ffi)
end

ffi.cdef[[
typedef struct {
const char* first_label;
const char* first_url;
const char* second_label;
const char* second_url;
} Buttons;
void init(
const char* client,
const char* image,
Expand All @@ -43,9 +49,11 @@ local function init_discord(ffi)
const char* fileBrowserText,
const char* pluginManagerText,
const char* workspaceText,
bool swap
const char* initialPath,
const Buttons* buttons,
const bool swap
);
bool update_presence(
const bool update_presence(
const char* filename,
const char* filetype,
bool isReadOnly,
Expand All @@ -54,49 +62,14 @@ local function init_discord(ffi)
);
void clear_presence();
void disconnect();
void set_cwd(const char* directory);
void set_buttons(
const char* first_label,
const char* first_url,
const char* second_label,
const char* second_url
);
const char* update_workspace(const char* workspace);
void update_time();
const char* get_workspace();
]]

return ffi.load(new_path)
end

local function fetch_repository()
local handle = io.popen('git config --get remote.origin.url')
if not handle then
vim.notify('[cord.nvim] Could not fetch Git repository URL', vim.log.levels.WARN)
return
end
local git_url = handle:read('*a')
handle:close()

return git_url:match('^%s*(.-)%s*$')
end

local function find_workspace()
local curr_dir = vim.fn.expand('%:p:h')
local vcs_markers = {'.git', '.svn', '.hg'}

while curr_dir ~= '' do
for _, dir in ipairs(vcs_markers) do
if vim.fn.isdirectory(curr_dir .. '/' .. dir) == 1 then
return vim.fn.fnamemodify(curr_dir, ':t')
end
end

curr_dir = vim.fn.fnamemodify(curr_dir, ':h')
if curr_dir == vim.fn.fnamemodify(curr_dir, ':h') then break end -- reached root
end

return vim.fn.fnamemodify(vim.fn.getcwd(), ':t') -- fallback to cwd
end

local function validate_severity(config)
config.lsp.severity = tonumber(config.lsp.severity)
if config.lsp.severity == nil or config.lsp.severity < 1 or config.lsp.severity > 4 then
Expand All @@ -106,46 +79,6 @@ local function validate_severity(config)
return true
end

local function validate_buttons(config)
if config.display.show_repository then
local buttons = {}
local repo
for i, button in ipairs(config.buttons) do
if i > 2 then
vim.notify('[cord.nvim] Detected more than two buttons in the config. Only the first two will be displayed', vim.log.levels.WARN)
return buttons
end
if button.url == 'git' then
if not repo then
repo = fetch_repository()
end
if repo and repo ~= '' then
table.insert(buttons, { label = button.label, url = repo })
end
else
table.insert(buttons, button)
end
end
return buttons
end
end

local function update_cwd(config, discord)
local workspace = find_workspace()
discord.set_cwd(workspace)

local buttons = validate_buttons(config)
if not buttons then return workspace end

if #buttons == 1 then
discord.set_buttons(buttons[1].label, buttons[1].url, nil, nil)
elseif #buttons >= 2 then
discord.set_buttons(buttons[1].label, buttons[1].url, buttons[2].label, buttons[2].url)
end

return workspace
end

local function get_problem_count(config)
if config.lsp.show_problem_count then
local bufnr = config.lsp.scope == 'buffer' and vim.api.nvim_get_current_buf() or nil
Expand All @@ -172,10 +105,7 @@ end

return {
init_discord = init_discord,
fetch_repository = fetch_repository,
find_workspace = find_workspace,
validate_severity = validate_severity,
update_cwd = update_cwd,
get_problem_count = get_problem_count,
array_contains = array_contains
}
Loading

0 comments on commit 3cb21e5

Please sign in to comment.