diff --git a/README.md b/README.md index a63fe8b..562a271 100644 --- a/README.md +++ b/README.md @@ -134,12 +134,14 @@ require("global-note").open_note("food") local get_project_name = function() local project_directory, err = vim.loop.cwd() if project_directory == nil then - error(err) + vim.notify(err, vim.log.levels.WARN) + return nil end local project_name = vim.fs.basename(project_directory) if project_name == nil then - error("Unable to get the project name") + vim.notify("Unable to get the project name", vim.log.levels.WARN) + return nil end return project_name @@ -161,14 +163,16 @@ local get_project_name = function() }):wait() if result.stderr ~= "" then - error(result.stderr) + vim.notify(result.stderr, vim.log.levels.WARN) + return nil end local project_directory = result.stdout:gsub("\n", "") local project_name = vim.fs.basename(project_directory) if project_name == nil then - error("Unable to get the project name") + vim.notify("Unable to get the project name", vim.log.levels.WARN) + return nil end return project_name diff --git a/lua/global-note/init.lua b/lua/global-note/init.lua index caf5aa2..f4936d6 100644 --- a/lua/global-note/init.lua +++ b/lua/global-note/init.lua @@ -29,9 +29,9 @@ local M = { } ---@class GlobalNote_UserPreset ----@field filename? string|fun(): string Filename of the note. ----@field directory? string|fun(): string Directory to keep notes. ----@field title? string|fun(): string Floating window title. +---@field filename? string|fun(): string? Filename of the note. +---@field directory? string|fun(): string? Directory to keep notes. +---@field title? string|fun(): string? Floating window title. ---@field command_name? string Ex command name. ---@field window_config? table|fun(): table A nvim_open_win config. ---@field post_open? fun() It's called after the window creation. diff --git a/lua/global-note/preset.lua b/lua/global-note/preset.lua index 28f3ce1..d6f321c 100644 --- a/lua/global-note/preset.lua +++ b/lua/global-note/preset.lua @@ -1,9 +1,9 @@ local utils = require("global-note.utils") ---@class GlobalNote_PresetOptions ----@field filename string|fun(): string Filename of the note. ----@field directory string|fun(): string Directory to keep notes. ----@field title string|fun(): string Floating window title. +---@field filename string|fun(): string? Filename of the note. +---@field directory string|fun(): string? Directory to keep notes. +---@field title string|fun(): string? Floating window title. ---@field command_name? string Ex command name. ---@field window_config table|fun(): table A nvim_open_win config. ---@field post_open fun() It's called after the window creation. @@ -72,11 +72,16 @@ local new = function(name, options) end ---Expands preset fields to a finite values. - ---@return GlobalNote_ExpandedPreset + ---If user can't produce a critical value then nil is returned. + ---@return GlobalNote_ExpandedPreset? function p:_expand_options() local filename = self.filename if type(filename) == "function" then + ---@diagnostic disable-next-line: cast-local-type filename = filename() + if filename == nil then + return + end if type(filename) ~= "string" or filename == "" then error("Filename function should return a non empty string") end @@ -84,7 +89,11 @@ local new = function(name, options) local directory = self.directory if type(directory) == "function" then + ---@diagnostic disable-next-line: cast-local-type directory = directory() + if directory == nil then + return + end if type(directory) ~= "string" or directory == "" then error("Directory function should return a non empty string") end @@ -101,6 +110,7 @@ local new = function(name, options) local title = self.title if type(title) == "function" then + ---@diagnostic disable-next-line: cast-local-type title = title() if type(title) ~= "string" and title ~= nil then error("Title function should return a string or a nil") @@ -155,6 +165,9 @@ local new = function(name, options) ---Opens the preset in a floating window function p:open() local expanded_preset = self:_expand_options() + if expanded_preset == nil then + return + end self:_open_in_float_window(expanded_preset) end