Skip to content

Commit

Permalink
Refact: minor config optimization + doc and style improv
Browse files Browse the repository at this point in the history
* docs: started typing  _setup, Setup, and cfg

# Conflicts:
#	lua/telekasten.lua

* docs: typing config.lua

* ci: add lua langauge server config file

* refactor: remove unnecessary indirection from config.lua. add docs

* refactor: add class for config module

* docs: make VaultConfig docs more correct

---------

Co-authored-by: ujisati <>
  • Loading branch information
ujisati authored Jun 28, 2024
1 parent 311b1a1 commit 609aa7e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
"workspace.library": ["lua"],
"runtime.version": "Lua 5.4",
"hint.enable": false
}

9 changes: 5 additions & 4 deletions lua/telekasten.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1809,9 +1809,9 @@ local function FindAllTags(opts)
end

--- Setup(cfg)
-- Overrides config with elements from cfg. See lua/telekasten/config.lua for defaults.
-- @param cfg table Table of configuration values to override defaults
-- Maybe fold into _setup? Also used in chdir, though...
--- Overrides config with elements from cfg. See lua/telekasten/config.lua for defaults.
--- Maybe fold into _setup? Also used in chdir, though...
---@param cfg VaultConfig table of configuration values to override defaults
local function Setup(cfg)
cfg = cfg or {}

Expand Down Expand Up @@ -1914,7 +1914,7 @@ end

--- _setup(cfg)
-- Sets the available vaults and passes further configuration options to Setup
-- @param cfg table Table of configuration values
---@param cfg MultiVaultConfig | VaultConfig table of configuration values
local function _setup(cfg)
if cfg.vaults ~= nil and cfg.default_vault ~= nil then
M.vaults = cfg.vaults
Expand All @@ -1927,6 +1927,7 @@ local function _setup(cfg)
elseif cfg.home ~= nil then
M.vaults = cfg.vaults or {}
cfg.vaults = nil
---@cast cfg VaultConfig
M.vaults["default"] = cfg
Setup(cfg)
end
Expand Down
43 changes: 21 additions & 22 deletions lua/telekasten/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,48 @@
--
local vim = vim

---@class M: Config
local M = {}

---The local class instance of the merged user's configuration this includes all
---default values and highlights filled out
---@type Config
local config = {}

---The class definition for the user configuration
---@class Config
---@field options VaultConfig
---@field user {options: VaultConfig}
local Config = {}

---Merges the user passed opts with defaults on instantiation
---@param opts VaultConfig
---@return Config
function Config:new(opts)
local o = { options = opts }
assert(o, "User options must be passed in")
self.__index = self

-- save a copy of the user's preferences so we can reference exactly what they
-- wanted after the config and defaults have been merged. Do this using a copy
-- so that reference isn't unintentionally mutated
self.user = vim.deepcopy(o)
setmetatable(o, self)
return o
end

---Combine user preferences with defaults preferring the user's own settings
function Config:merge(defaults)
local defaults = Config:get_defaults(o.options.home)
assert(
defaults and type(defaults) == "table",
"A valid config table must be passed to merge"
)
self.options =
vim.tbl_deep_extend("force", defaults.options, self.options or {})
return self
o.options = vim.tbl_deep_extend("force", defaults.options, o.options or {})
return o
end

--
-- Default setup. Ideally anyone should be able to start using Telekasten
-- directly without fiddling too much with the options. The only one of real
-- interest should be the path for the few relevant directories.
local function get_defaults(home)
--- Default setup. Ideally anyone should be able to start using Telekasten
--- directly without fiddling too much with the options. The only one of real
--- interest should be the path for the few relevant directories.
---@param home string | nil
---@return {options: VaultConfig}
function Config:get_defaults(home)
local _home = home or vim.fn.expand("~/zettelkasten") -- Default home directory
local opts = {
home = _home,
Expand Down Expand Up @@ -96,21 +101,15 @@ local function get_defaults(home)
return { options = opts }
end

--- Merge user config with defaults
function M.apply()
local defaults = get_defaults(config.options.home)
config:merge(defaults)
return config
end

---Keep track of a users config for use throughout the plugin as well as
---ensuring defaults are set.
---@param c VaultConfig
function M.setup(c)
config = Config:new(c or {})
M.apply()
end

---Get the user's configuration or a key from it
---Get the user's configuration
---@return Config | nil
function M.get()
if config then
return config
Expand Down
86 changes: 86 additions & 0 deletions lua/telekasten/utils/definitions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--luacheck: ignore 211
---@meta

---@alias MediaExtensions
---| '".png"'
---| '".jpg"'
---| '".bmp"'
---| '".gif"'
---| '".pdf"'
---| '".mp4"'
---| '".webm"'
---| '".webp"'

---@class VaultConfig
---@field home string
---@field take_over_my_home boolean
---@field auto_set_filetype boolean
---@field auto_set_syntax boolean
---@field dailies string
---@field weeklies string
---@field templates string
---@field image_subdir string|nil Should be deprecated gracefully and replaced by "images"
---@field extension "md" | string
---@field new_note_filename "title" | "uuid" | "uuid-title"
---@field uuid_type "%Y%m%d%H%M" | string
---@field uuid_sep "-" | string
---@field filename_space_subst string|nil
---@field follow_creates_nonexisting boolean
---@field dailies_create_nonexisting boolean
---@field weeklies_create_nonexisting boolean
---@field journal_auto_open boolean
---@field image_link_style "wiki" | "markdown"
---@field sort "filename" | "modified"
---@field subdirs_in_links boolean
---@field plug_into_calendar boolean
---@field calendar_opts CalendarOpts
---@field close_after_yanking boolean
---@field insert_after_inserting boolean
---@field tag_notation "#tag" | "@tag" | ":tag:" | "yaml-bare"
---@field command_palette_theme "dropdown" | "ivy"
---@field show_tags_theme string
---@field template_handling "smart" | "prefer_new_note" | "always_ask"
---@field new_note_location "smart" |"prefer_home" | "same_as_current"
---@field rename_update_links boolean
---@field media_previewer "telescope-media-files" | "catimg-previewer" | "viu-previewer"
---@field media_extensions MediaExtensions[]
---@field follow_url_fallback string|nil
---@field enable_create_new boolean
---@field clipboard_program string
---@field filter_extensions string[]
---@field template_new_note string|nil
---@field template_new_daily string|nil
---@field template_new_weekly string|nil
---@field find_command string[]
---@field rg_pcre boolean
---
---For defaults,
---@see Config.get_defaults
local VaultConfig = {}

---@alias WeekNumberFormat
---| 1 # WK01
---| 2 # WK 1
---| 3 # KW01
---| 4 # KW 1
---| 5 # 1

---@alias CalendarStartDay
---| 0 # weeks start on Sundays
---| 1 # weeks start on Mondays

---@alias CalendarMarkPosition
---| 'left' # ugly
---| 'right' # right to the day
---| 'left-fit' # left of the day

---@class CalendarOpts
---@field weeknm WeekNumberFormat
---@field calendar_monday CalendarStartDay
---@field calendar_mark CalendarMarkPosition
local CalendarOpts = {}

---@class MultiVaultConfig
---@field vaults table<string, VaultConfig>
---@field default_vault? string
local MultiVaultConfig = {}

0 comments on commit 609aa7e

Please sign in to comment.