Skip to content

Commit

Permalink
lazy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Nov 7, 2023
1 parent f9de0b2 commit 03567a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion RELEASE_PROCESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Steps

1. Update the `VERSION` field in `lua/obsidian/init.lua`.
1. Update the version in `lua/obsidian/version.lua`.

3. Run the release script:

Expand Down
86 changes: 57 additions & 29 deletions lua/obsidian/init.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
local Path = require "plenary.path"

local echo = require "obsidian.echo"
local config = require "obsidian.config"
local async = require "plenary.async"
local channel = require("plenary.async.control").channel
local Note = require "obsidian.note"
local module_lookups = {
async = "obsidian.async",
backlinks = "obsidian.backlinks",
command = "obsidian.command",
completion = "obsidian.completion",
config = "obsidian.config",
echo = "obsidian.echo",
mapping = "obsidian.mapping",
Note = "obsidian.note",
search = "obsidian.search",
util = "obsidian.util",
VERSION = "obsidian.version",
workspace = "obsidian.workspace",
yaml = "obsidian.yaml",
}

local obsidian = setmetatable({}, {
__index = function(t, k)
local require_path = module_lookups[k]
if not require_path then
return
end

local obsidian = {}
local mod = require(require_path)
t[k] = mod

obsidian.VERSION = "1.15.0"
obsidian.completion = require "obsidian.completion"
obsidian.Note = Note
obsidian.util = require "obsidian.util"
obsidian.search = require "obsidian.search"
obsidian.yaml = require "obsidian.yaml"
obsidian.mapping = require "obsidian.mapping"
obsidian.workspace = require "obsidian.workspace"
return mod
end,
})

---@class obsidian.Client
---@field current_workspace obsidian.Workspace
Expand All @@ -30,6 +41,8 @@ local client = {}
---@param opts obsidian.config.ClientOpts
---@return obsidian.Client
obsidian.new = function(opts)
local Path = require "plenary.path"

local self = setmetatable({}, { __index = client })

self.current_workspace = obsidian.workspace.get_from_opts(opts)
Expand All @@ -49,7 +62,7 @@ end
---@param dir string
---@return obsidian.Client
obsidian.new_from_dir = function(dir)
local opts = config.ClientOpts.default()
local opts = obsidian.config.ClientOpts.default()
opts.workspaces = vim.tbl_extend("force", { obsidian.workspace.new_from_dir(dir) }, opts.workspaces)
return obsidian.new(opts)
end
Expand All @@ -59,7 +72,9 @@ end
---@param opts obsidian.config.ClientOpts
---@return obsidian.Client
obsidian.setup = function(opts)
opts = config.ClientOpts.normalize(opts)
local Path = require "plenary.path"

opts = obsidian.config.ClientOpts.normalize(opts)
local self = obsidian.new(opts)

-- Ensure directories exist.
Expand All @@ -82,7 +97,10 @@ obsidian.setup = function(opts)
if self.opts.templates ~= nil and self.opts.templates.subdir ~= nil then
self.templates_dir = Path:new(self.dir) / self.opts.templates.subdir
if not self.templates_dir:is_dir() then
echo.err(string.format("%s is not a valid directory for templates", self.templates_dir), self.opts.log_level)
obsidian.echo.err(
string.format("%s is not a valid directory for templates", self.templates_dir),
self.opts.log_level
)
self.templates_dir = nil
end
end
Expand Down Expand Up @@ -146,7 +164,7 @@ obsidian.setup = function(opts)
end

local bufnr = vim.api.nvim_get_current_buf()
local note = Note.from_buffer(bufnr, self.dir)
local note = obsidian.Note.from_buffer(bufnr, self.dir)
if not note:should_save_frontmatter() or self.opts.disable_frontmatter == true then
return
end
Expand All @@ -157,7 +175,7 @@ obsidian.setup = function(opts)
end
local lines = note:frontmatter_lines(nil, frontmatter)
vim.api.nvim_buf_set_lines(bufnr, 0, note.frontmatter_end_line and note.frontmatter_end_line or 0, false, lines)
echo.info("Updated frontmatter", self.opts.log_level)
obsidian.echo.info("Updated frontmatter", self.opts.log_level)
end,
})

Expand All @@ -168,6 +186,8 @@ end
---
---@return string|?
client.vault = function(self)
local Path = require "plenary.path"

local vault_indicator_folder = ".obsidian"
local dirs = self.dir:parents()
table.insert(dirs, 0, self.dir:absolute())
Expand All @@ -186,6 +206,7 @@ end
---@param search_opts string[]|?
---@return function
client._search_iter_async = function(self, search, search_opts)
local channel = require("plenary.async.control").channel
local tx, rx = channel.mpsc()
local found = {}

Expand Down Expand Up @@ -258,12 +279,13 @@ end
---@param search_opts string[]|?
---@param callback function
client.search_async = function(self, search, search_opts, callback)
local async = require "plenary.async"
local next_path = self:_search_iter_async(search, search_opts)
local executor = require("obsidian.async").AsyncExecutor.new()
local dir = tostring(self.dir)

local function task_fn(path)
return Note.from_file_async(path, dir)
return obsidian.Note.from_file_async(path, dir)
end

async.run(function()
Expand Down Expand Up @@ -302,6 +324,8 @@ end
---
---@return string|?,string,Path
client.parse_title_id_path = function(self, title, id, dir)
local Path = require "plenary.path"

---@type Path
local base_dir = dir == nil and Path:new(self.dir) or Path:new(dir)
local title_is_path = false
Expand Down Expand Up @@ -370,13 +394,13 @@ client.new_note = function(self, title, id, dir, aliases)
end

-- Create Note object and save.
local note = Note.new(new_id, aliases, {}, path)
local note = obsidian.Note.new(new_id, aliases, {}, path)
local frontmatter = nil
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)
obsidian.echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)

return note
end
Expand All @@ -386,6 +410,8 @@ end
---@param id string
---@return Path
client.daily_note_path = function(self, id)
local Path = require "plenary.path"

---@type Path
local path = Path:new(self.dir)

Expand Down Expand Up @@ -427,7 +453,7 @@ client._daily = function(self, datetime)
end

-- Create Note object and save if it doesn't already exist.
local note = Note.new(id, { alias }, { "daily-notes" }, path)
local note = obsidian.Note.new(id, { alias }, { "daily-notes" }, path)
if not note:exists() then
if self.opts.daily_notes.template then
obsidian.util.clone_template(self.opts.daily_notes.template, tostring(path), self, note:display_name())
Expand All @@ -437,7 +463,7 @@ client._daily = function(self, datetime)
frontmatter = self.opts.note_frontmatter_func(note)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)
obsidian.echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)
end

return note
Expand All @@ -462,13 +488,15 @@ end
---@param query string
---@return obsidian.Note|?
client.resolve_note = function(self, query)
local Path = require "plenary.path"

-- Autocompletion for command args will have this format.
local note_path, count = string.gsub(query, "^.*  ", "")
if count > 0 then
---@type Path
---@diagnostic disable-next-line: assign-type-mismatch
local full_path = self.dir / note_path
return Note.from_file(full_path, self.dir)
return obsidian.Note.from_file(full_path, self.dir)
end

-- Query might be a path.
Expand All @@ -481,7 +509,7 @@ client.resolve_note = function(self, query)
end
for _, path in pairs(paths_to_check) do
if path:is_file() then
local ok, note = pcall(Note.from_file, path)
local ok, note = pcall(obsidian.Note.from_file, path)
if ok then
return note
end
Expand Down Expand Up @@ -526,7 +554,7 @@ client._run_with_finder_backend = function(self, command_name, implementations)
local success, err = pcall(obsidian.util.run_first_supported, command_name, finders_order, implementations)
if not success then
if type(err) == "string" then
echo.err(err, client.opts.log_level)
obsidian.echo.err(err, client.opts.log_level)
else
error(err)
end
Expand Down
1 change: 1 addition & 0 deletions lua/obsidian/version.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return "1.15.0"

0 comments on commit 03567a0

Please sign in to comment.