Skip to content

Commit

Permalink
feat: Make path handling more robust #29 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman authored Nov 3, 2024
1 parent 0085695 commit 918b1df
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions lua/cmp_emoji/init.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 6 additions & 4 deletions lua/emoji/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lua/emoji/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions lua/telescope/_extensions/emoji.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 918b1df

Please sign in to comment.