From 918b1df33f8dbca94e3e1064d993f7b8b2d437af Mon Sep 17 00:00:00 2001 From: Michael Peter Date: Sun, 3 Nov 2024 23:24:42 +0100 Subject: [PATCH] feat: Make path handling more robust #29 (#30) --- README.md | 2 ++ lua/cmp_emoji/init.lua | 7 +++---- lua/emoji/init.lua | 10 ++++++---- lua/emoji/utils.lua | 22 ++++++++++++++++++++++ lua/telescope/_extensions/emoji.lua | 5 ++--- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c69d011..828955f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ With [Lazy.nvim](https://github.com/folke/lazy.nvim): version = "1.0.0", -- optionally pin to a tag ft = "markdown", -- adjust to your needs dependencies = { + -- util for handling paths + "nvim-lua/plenary.nvim", -- optional for nvim-cmp integration "hrsh7th/nvim-cmp", -- optional for telescope integration diff --git a/lua/cmp_emoji/init.lua b/lua/cmp_emoji/init.lua index ae435a6..1c365dd 100644 --- a/lua/cmp_emoji/init.lua +++ b/lua/cmp_emoji/init.lua @@ -1,9 +1,8 @@ local utils = require("emoji.utils") -local plugin_path = require("emoji.config").options.plugin_path -local emoji_path = require("emoji.config").paths.emoji --- TODO: use plenary.path -local emojis = utils.load_from_json(plugin_path .. emoji_path) +local emoji_path = utils.get_emoji_data_path().filename +local emojis = utils.load_from_json(emoji_path) + local source = {} source.new = function() diff --git a/lua/emoji/init.lua b/lua/emoji/init.lua index 423595c..7d1e9f1 100644 --- a/lua/emoji/init.lua +++ b/lua/emoji/init.lua @@ -20,24 +20,26 @@ function Main.setup(opts) end Main.insert = function() - local data = utils.load_from_json(config.options.plugin_path .. config.paths.emoji) + local data = utils.load_from_json(utils.get_emoji_data_path().filename) local options = utils.create_emoji_options(data) require("emoji.ui").select_and_insert(options) end Main.insert_by_group = function() - local data = utils.load_from_json(config.options.plugin_path .. config.paths.emoji) + local data = utils.load_from_json(utils.get_emoji_data_path().filename) local groups = utils.get_groups(data) require("emoji.ui").select_and_insert_emoji_by_group(data, groups) end Main.insert_kaomoji = function() - local data = require("emoji.kaomoji").normalized_data(config.options.plugin_path .. config.paths.kaomoji) + local file = utils.get_kaomoji_data_path().filename + local data = require("emoji.kaomoji").normalized_data(file) local options = utils.create_kaomoji_options(data) require("emoji.ui").select_and_insert(options) end Main.insert_kaomoji_by_group = function() - local data = require("emoji.kaomoji").normalized_data(config.options.plugin_path .. config.paths.kaomoji) + local file = utils.get_kaomoji_data_path().filename + local data = require("emoji.kaomoji").normalized_data(file) local groups = utils.get_groups(data) require("emoji.ui").select_and_insert_kaomoji_by_group(data, groups) end diff --git a/lua/emoji/utils.lua b/lua/emoji/utils.lua index 339f308..6c9c9ba 100644 --- a/lua/emoji/utils.lua +++ b/lua/emoji/utils.lua @@ -124,4 +124,26 @@ M.create_kaomoji_options = function(data) return options end +M.get_emoji_data_path = function() + local plugin_path = require("emoji.config").options.plugin_path + local emoji_path = require("emoji.config").paths.emoji + local ok, path = pcall(require, "plenary.path") + if not ok then + error("failed to load plenary.nvim") + return + end + return path:new(plugin_path, emoji_path) +end + +M.get_kaomoji_data_path = function() + local plugin_path = require("emoji.config").options.plugin_path + local kaomoji_path = require("emoji.config").paths.kaomoji + local ok, path = pcall(require, "plenary.path") + if not ok then + error("failed to load plenary.nvim") + return + end + return path:new(plugin_path, kaomoji_path) +end + return M diff --git a/lua/telescope/_extensions/emoji.lua b/lua/telescope/_extensions/emoji.lua index 3d9aabc..7c6bcdc 100644 --- a/lua/telescope/_extensions/emoji.lua +++ b/lua/telescope/_extensions/emoji.lua @@ -27,10 +27,9 @@ local make_display = function(entry) end function M.finder() - local plugin_path = require("emoji.config").options.plugin_path - local emoji_path = require("emoji.config").paths.emoji - local emojis = utils.load_from_json(plugin_path .. emoji_path) local results = {} + local emoji_path = utils.get_emoji_data_path().filename + local emojis = utils.load_from_json(emoji_path) for _, e in ipairs(emojis) do table.insert(results, { name = e.unicodeName, character = e.character, group = e.group }) end