Skip to content

Commit c064b1f

Browse files
fix(#668): Auto instantiate when requesting files
1 parent ee67f20 commit c064b1f

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

lua/orgmode/api/agenda.lua

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local Date = require('orgmode.objects.date')
22
local orgmode = require('orgmode')
33

4-
---@class OrgAgenda
4+
---@class OrgApiAgenda
55
local OrgAgenda = {}
66

7-
---@alias OrgAgendaFilter string see Filters to apply to the current view. See `:help orgmode-org_agenda_filter` for more information
7+
---@alias OrgApiAgendaFilter string see Filters to apply to the current view. See `:help orgmode-org_agenda_filter` for more information
88

99
local function get_date(date, name)
1010
if not date then
@@ -20,60 +20,47 @@ local function get_date(date, name)
2020
error(('Invalid format for "%s" date in Org Agenda'):format(name))
2121
end
2222

23-
---@return table
24-
local function org_instance()
25-
local org = orgmode.instance()
26-
if not org then
27-
error('Orgmode was not set up. Run require("orgmode").setup() first.')
28-
end
29-
org:init()
30-
return org
31-
end
32-
33-
---@class OrgAgendaOptions
34-
---@field filters? OrgAgendaFilter
23+
---@class OrgApiAgendaOptions
24+
---@field filters? OrgApiAgendaFilter
3525
---@field from? string | OrgDate
3626
---@field span? number | 'day' | 'week' | 'month' | 'year'
3727

38-
---@param options? OrgAgendaOptions
28+
---@param options? OrgApiAgendaOptions
3929
function OrgAgenda.agenda(options)
4030
options = options or {}
41-
local org = org_instance()
4231
if options.filters and options.filters ~= '' then
43-
org.agenda.filters:parse(options.filters, true)
32+
orgmode.agenda.filters:parse(options.filters, true)
4433
end
4534
local from = get_date(options.from, 'from')
46-
org.agenda:agenda({
35+
orgmode.agenda:agenda({
4736
from = from,
4837
span = options.span,
4938
})
5039
end
5140

5241
---@class OrgAgendaTodosOptions
53-
---@field filters? OrgAgendaFilter
42+
---@field filters? OrgApiAgendaFilter
5443

5544
---@param options? OrgAgendaTodosOptions
5645
function OrgAgenda.todos(options)
5746
options = options or {}
58-
local org = org_instance()
5947
if options.filters and options.filters ~= '' then
60-
org.agenda.filters:parse(options.filters, true)
48+
orgmode.agenda.filters:parse(options.filters, true)
6149
end
62-
org.agenda:todos()
50+
orgmode.agenda:todos()
6351
end
6452

6553
---@class OrgAgendaTagsOptions
66-
---@field filters? OrgAgendaFilter
54+
---@field filters? OrgApiAgendaFilter
6755
---@field todo_only? boolean
6856

6957
---@param options? OrgAgendaTagsOptions
7058
function OrgAgenda.tags(options)
7159
options = options or {}
72-
local org = org_instance()
7360
if options.filters and options.filters ~= '' then
74-
org.agenda.filters:parse(options.filters, true)
61+
orgmode.agenda.filters:parse(options.filters, true)
7562
end
76-
org.agenda:tags({
63+
orgmode.agenda:tags({
7764
todo_only = options.todo_only,
7865
})
7966
end

lua/orgmode/init.lua

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_G.orgmode = _G.orgmode or {}
22
local ts_revision = 'f8c6b1e72f82f17e41004e04e15f62a83ecc27b0'
33
local setup_ts_grammar_used = false
4-
---@type Org
4+
---@type Org | nil
55
local instance = nil
66

77
local auto_instance_keys = {
@@ -23,16 +23,16 @@ local auto_instance_keys = {
2323
---@field org_mappings OrgMappings
2424
---@field notifications OrgNotifications
2525
local Org = {}
26-
27-
function Org:new()
28-
local data = {}
29-
setmetatable(data, self)
30-
self.__index = function(tbl, key)
31-
if auto_instance_keys[key] and not instance then
26+
setmetatable(Org, {
27+
__index = function(tbl, key)
28+
if auto_instance_keys[key] then
3229
Org.instance()
3330
end
3431
return rawget(tbl, key)
35-
end
32+
end,
33+
})
34+
35+
function Org:new()
3636
self.initialized = false
3737
self:setup_autocmds()
3838
return self
@@ -142,7 +142,7 @@ function Org.setup(opts)
142142
config:setup_ts_predicates()
143143
vim.defer_fn(function()
144144
if config.notifications.enabled and #vim.api.nvim_list_uis() > 0 then
145-
Org.instance().files:load():next(vim.schedule_wrap(function()
145+
Org.files:load():next(vim.schedule_wrap(function()
146146
instance.notifications = require('orgmode.notifications')
147147
:new({
148148
files = Org.files,
@@ -209,11 +209,11 @@ function Org.cron(opts)
209209
if not config.notifications.cron_enabled then
210210
return vim.cmd([[qa!]])
211211
end
212-
Org.instance().files:load():next(vim.schedule_wrap(function()
212+
Org.files:load():next(vim.schedule_wrap(function()
213213
---@diagnostic disable-next-line: inject-field
214214
instance.notifications = require('orgmode.notifications')
215215
:new({
216-
files = instance.files,
216+
files = Org.files,
217217
})
218218
:cron()
219219
end))
@@ -227,6 +227,13 @@ function Org.instance()
227227
return instance
228228
end
229229

230+
function Org.destroy()
231+
if instance then
232+
instance = nil
233+
collectgarbage()
234+
end
235+
end
236+
230237
function _G.orgmode.statusline()
231238
if not instance or not instance.initialized then
232239
return ''

tests/plenary/autoload_spec.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local orgmode = require('orgmode')
2+
3+
describe('Autoload', function()
4+
it('should autoload dependencies when requested', function()
5+
local org = orgmode.setup({
6+
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
7+
org_default_notes_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org',
8+
})
9+
assert.is.False(org.initialized)
10+
assert.is.Nil(rawget(org, 'files'))
11+
assert.is.Nil(rawget(org, 'agenda'))
12+
assert.is.Nil(rawget(org, 'capture'))
13+
assert.is.Nil(rawget(org, 'org_mappings'))
14+
org.files:all()
15+
assert.is.True(org.initialized)
16+
assert.is.Not.Nil(rawget(org, 'files'))
17+
assert.is.Not.Nil(rawget(org, 'agenda'))
18+
assert.is.Not.Nil(rawget(org, 'capture'))
19+
assert.is.Not.Nil(rawget(org, 'org_mappings'))
20+
end)
21+
end)

tests/plenary/init_spec.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ describe('Init', function()
1010
local todo_archive_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org_archive'
1111
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
1212
local txt_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/text_notes.txt'
13-
assert.is.Nil(org.files)
14-
assert.is.Nil(org.agenda)
15-
assert.is.Nil(org.capture)
16-
assert.is.Nil(org.org_mappings)
13+
assert.is.Nil(rawget(org, 'files'))
14+
assert.is.Nil(rawget(org, 'agenda'))
15+
assert.is.Nil(rawget(org, 'capture'))
16+
assert.is.Nil(rawget(org, 'org_mappings'))
1717
org:init()
18-
assert.is.Not.Nil(org.files)
19-
assert.is.Not.Nil(org.agenda)
20-
assert.is.Not.Nil(org.capture)
21-
assert.is.Not.Nil(org.org_mappings)
18+
assert.is.Not.Nil(rawget(org, 'files'))
19+
assert.is.Not.Nil(rawget(org, 'agenda'))
20+
assert.is.Not.Nil(rawget(org, 'capture'))
21+
assert.is.Not.Nil(rawget(org, 'org_mappings'))
2222
assert.is.error(function()
2323
return org.files:get(txt_file)
2424
end)

0 commit comments

Comments
 (0)