Skip to content

Commit

Permalink
Move to named dir
Browse files Browse the repository at this point in the history
  • Loading branch information
valchx committed Sep 30, 2023
1 parent fbd7afc commit 37392e3
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 139 deletions.
139 changes: 0 additions & 139 deletions lua/init.lua

This file was deleted.

141 changes: 141 additions & 0 deletions lua/nvim-i18n-tools/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
local function i(value)
print(vim.inspect(value))
end

---@return string: calls
---
local function node_text(value, bufnr)
return vim.treesitter.get_node_text(value, bufnr)
end

---@param bufnr integer Source buffer to search
---@param start ( integer | nil ) Starting line for the search
---@param stop ( integer | nil ) Stopping line for the search (end-exclusive)
---
local function get_t_calls(bufnr, start, stop)
local language_tree = vim.treesitter.get_parser(bufnr)
local syntax_tree = language_tree:parse()
local root = syntax_tree[1]:root()
local language = language_tree:lang()

if not vim.tbl_contains({ "javascript", "typescript" }, language) then
print("Language not supported")
return {}
end

local query = vim.treesitter.query.parse(
language_tree:lang(),
[[
(call_expression
function: (identifier) @t_func (#eq? @t_func "t")
arguments: (arguments
(string
(string_fragment) @str_frag
)
)
)
]]
)

local t_nodes = {}

for _, match in query:iter_matches(root, bufnr, start, stop) do
-- TODO : Getting the function name might be useless
local func = match[1]
local args = match[2]
table.insert(t_nodes, { func, args })
end

return t_nodes
end

local function get_translation_for_key(key)
local project_folder = vim.fn.getcwd()

local nvim_conf_dir = vim.fn.resolve(project_folder .. "/.nvim/")

local conf = dofile(vim.fn.resolve(nvim_conf_dir .. "/i18n-tools.lua"))

local translations_path = vim.fn.resolve(nvim_conf_dir .. "/" .. conf.translations)

-- TODO : Cache json file as to not load it on each refresh
local translations = vim.fn.json_decode(vim.fn.readfile(translations_path))

local result = translations
for part in string.gmatch(key, "[^.]+") do
if result[part] then
result = result[part]
else
result = "[error] Not found"
end
end

if type(result) ~= "string" then
result = "[error] Not a string"
end

return result
end

local bufnr_to_ns_id = {}

local function apply_translations()
local bufnr = vim.api.nvim_get_current_buf()
local t_nodes = get_t_calls(bufnr)

local ns_id = bufnr_to_ns_id[bufnr]
if ns_id then
-- TODO : This is not clearing the buffer
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)
else
ns_id = vim.api.nvim_create_namespace("i18n_hints" .. bufnr)
bufnr_to_ns_id[bufnr] = ns_id
end

-- iterate over t_nodes if it has a size
for _, t_node in ipairs(t_nodes) do
-- TODO : Check that arg is a string /["`'].*["`']/g
local t_key = t_node[2]
local start_row, start_col = t_key:range()
local replacement_text = "i18n : " .. get_translation_for_key(node_text(t_key, bufnr))

-- Set virtual text for the specified range on the line
vim.api.nvim_buf_set_extmark(bufnr, ns_id, start_row, start_col, {
virt_text = { { replacement_text, "WarningMsg" } },
-- TODO : For now the translation will be put at the end of the line
-- later we can replace the key with the translation
-- virt_text_pos = "overlay", -- Overlay on top of existing text
virt_text_pos = "eol",
})
end
end

local M = {
setup = function()
local group = vim.api.nvim_create_augroup("i18n_tools", {})
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.{ts,js,tsx,jsx}",
group = group,
callback = function()
apply_translations()
end,
})
vim.api.nvim_create_autocmd("BufWrite", {
pattern = "*.{ts,js,tsx,jsx}",
group = group,
callback = function()
apply_translations()
end,
})
end,
}

-- -- This is for development perposes
-- vim.keymap.set("n", "<leader>h", function()
-- package.loaded.i18n_tools = nil
-- require("plugins.nvim-i18n-tools").init()
-- end)

M.setup()

return M

0 comments on commit 37392e3

Please sign in to comment.