Skip to content

Commit

Permalink
Allow user to return nil from functions
Browse files Browse the repository at this point in the history
  • Loading branch information
backdround committed Feb 13, 2024
1 parent 7a50113 commit 4861b1c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lua/global-note/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 17 additions & 4 deletions lua/global-note/preset.lua
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -72,19 +72,28 @@ 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
end

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
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4861b1c

Please sign in to comment.