Skip to content

Commit

Permalink
feat: automatically load the plugin on startup without needing a `set…
Browse files Browse the repository at this point in the history
…up()` call
  • Loading branch information
vyfor committed Jan 23, 2025
1 parent 9ea3a22 commit 4732567
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/wiki/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require('cord').setup {
```lua
{
enabled = true,
editor = {
client = 'neovim',
tooltip = 'The Superior Text Editor',
Expand Down
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Cord requires the server executables to be present. To get it, you can either:
'vyfor/cord.nvim',
branch = 'client-server',
build = ':Cord update',
opts = {}, -- calls require('cord').setup()
-- opts = {}
}
```

Expand All @@ -77,9 +77,9 @@ use {
'vyfor/cord.nvim',
branch = 'client-server',
run = ':Cord update',
config = function()
require('cord').setup()
end
-- config = function()
-- require('cord').setup {}
-- end
}
```

Expand All @@ -94,7 +94,6 @@ Cord is available on [LuaRocks](https://luarocks.org/modules/vyfor/cord.nvim).
:Rocks install cord.nvim
```

Do not forget to call `require('cord').setup()` to initialize the plugin, or set `config = true`.
Invoke `:Cord update` whenever the plugin is updated.

</details>
Expand All @@ -112,23 +111,13 @@ git clone -b client-server --single-branch https://github.com/vyfor/cord.nvim ~/
git clone -b client-server --single-branch https://github.com/vyfor/cord.nvim $LOCALAPPDATA/nvim-data/site/pack/plugins/start/cord.nvim
```

Then call the following function somewhere in your configuration:
```lua
require('cord').setup()
```

Invoke `:Cord update` whenever the plugin is updated.

</details>

<details>
<summary>Other</summary>

Make sure you call the following function somewhere in your configuration:
```lua
require('cord').setup()
```

Invoke `:Cord update` whenever the plugin is updated.

</details>
Expand Down
14 changes: 7 additions & 7 deletions lua/cord.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local M = {}
local M

---@param opts? CordConfig Configuration options for Cord
---@return nil
function M.setup(opts)
local config = require('cord.plugin.config.util'):validate(opts or {})
if config then require('cord.server'):initialize(config) end
end
M = {
---@param opts? CordConfig Configuration options for Cord
setup = function(opts)
if opts then M.user_config = opts end
end,
}

return M
4 changes: 3 additions & 1 deletion lua/cord/api/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ local function get_nested_value(config, path)
end

M.validate = function(user_config)
if not user_config then return { is_valid = true } end

local errors = {}
local warnings = {}

Expand Down Expand Up @@ -210,7 +212,7 @@ M.check = function()
local err = health.error or health.report_error

start 'cord.nvim'
local results = M.validate(require('cord.plugin.config.util').user_config)
local results = M.validate(require('cord').user_config)
if results.is_valid then
ok 'Health check passed'
else
Expand Down
3 changes: 1 addition & 2 deletions lua/cord/api/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ function M.init()
end
end

local config_manager = require 'cord.plugin.config'
local user_config = config_manager.user_config or {}
local user_config = require('cord').user_config or {}

local plugin_configs = {}
for _, plugin_config in pairs(configs) do
Expand Down
2 changes: 2 additions & 0 deletions lua/cord/plugin/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
---@alias CordVariablesConfig { [string]: string|fun(opts: CordOpts):string }

---@class CordConfig
---@field enabled? boolean Whether Cord plugin is enabled
---@field editor? CordEditorConfig Editor configuration
---@field display? CordDisplayConfig Display configuration
---@field timestamp? CordTimestampConfig Timestamp configuration
Expand All @@ -115,6 +116,7 @@
local M = {}

local defaults = {
enabled = true,
editor = {
client = 'neovim',
tooltip = 'The Superior Text Editor',
Expand Down
6 changes: 2 additions & 4 deletions lua/cord/plugin/config/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function M:validate(user_config)
local icons = require 'cord.api.icon'

local config_manager = require 'cord.plugin.config'
local final_config = vim.tbl_deep_extend('force', config_manager.get(), user_config)
local final_config = vim.tbl_deep_extend('force', config_manager.get(), user_config or require('cord').user_config or {})
logger.set_level(final_config.advanced.plugin.log_level)
icons.set_theme(final_config.display.theme)

Expand Down Expand Up @@ -45,8 +45,6 @@ function M:validate(user_config)
end

config_manager.set(final_config)
self.user_config = user_config

return final_config
end

Expand Down Expand Up @@ -111,4 +109,4 @@ function M.get_buttons(opts)
return buttons
end

return M
return M
12 changes: 6 additions & 6 deletions lua/cord/server/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local async = require 'cord.core.async'
local config = require 'cord.plugin.config'
local logger = require 'cord.plugin.log'

local M = {}
Expand Down Expand Up @@ -39,7 +40,7 @@ function M:connect(path, retried)
::spawn::
logger.debug 'Pipe not found. Spawning server executable...'

local process = require('cord.server.spawn').spawn(self.config, path)
local process = require('cord.server.spawn').spawn(config.get(), path)
local should_continue, retry = process:await()
if not should_continue then return end

Expand All @@ -60,7 +61,7 @@ function M:run()
self.status = 'ready'
async.run(function()
logger.info 'Connected to Discord'
M.tx:initialize(self.config)
M.tx:initialize(config.get())

local ActivityManager = require 'cord.plugin.activity.manager'
local manager, err = ActivityManager.new({ tx = M.tx }):get()
Expand Down Expand Up @@ -88,7 +89,7 @@ function M:run()
M.manager:cleanup()
require('cord.plugin.activity.hooks').run 'shutdown'

if self.config.advanced.discord.reconnect.enabled then
if config.advanced.discord.reconnect.enabled then
logger.info 'Reconnecting...'
end

Expand All @@ -112,13 +113,12 @@ function M:run()
end)()
end

function M:initialize(config)
function M:initialize()
self.status = 'connecting'
self.config = config or self.config
async.run(function()
logger.debug 'Initializing server...'

local path = self.config.advanced.server.pipe_path
local path = config.advanced.server.pipe_path
or require('cord.plugin.constants').get_pipe_path()

local _, err = M:connect(path):get()
Expand Down
23 changes: 18 additions & 5 deletions plugin/cord.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
augroup Cord
autocmd!
autocmd VimLeavePre * lua require 'cord.server':cleanup()
augroup END

function! CordCompleteList(ArgLead, CmdLine, CmdPos)
let l:starting_new_arg = a:ArgLead == '' && a:CmdLine[a:CmdPos-1] =~ '\s'
let l:args = split(a:CmdLine[:a:CmdPos-1], '\s\+')
Expand All @@ -28,3 +23,21 @@ function! CordCompleteList(ArgLead, CmdLine, CmdPos)
endfunction

command! -nargs=+ -complete=customlist,CordCompleteList Cord lua require'cord.api.command'.handle('<q-args>')

lua << EOF
vim.schedule(function()
local config = require('cord.plugin.config.util'):validate()
if not config then return end

if config.enabled then
vim.cmd [[
augroup Cord
autocmd!
autocmd VimLeavePre * lua require 'cord.server':cleanup()
augroup END
]]

require('cord.server'):initialize()
end
end)
EOF

0 comments on commit 4732567

Please sign in to comment.