Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nabekou29 committed Nov 17, 2024
1 parent 12e2d29 commit 48c20f9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
17 changes: 12 additions & 5 deletions lua/js-i18n/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ local function calculate_node_depth(node)
end

--- Treesitterパーサーをセットアップしてキーにマッチするノードを取得する関数
--- @param bufnr number 文言ファイルのバッファ番号
--- @param source integer|string バッファ番号 or ソース
--- @param keys string[] キー
--- @return TSNode | nil, string | nil
function M.get_node_for_key(bufnr, keys)
function M.get_node_for_key(source, keys)
local ts = vim.treesitter

local parser = ts.get_parser(bufnr, "json")
local parser = (function()
if type(source) == "string" then
return ts.get_string_parser(source, "json")
else
return ts.get_parser(source)
end
end)()
local tree = parser:parse()[1]
local root = tree:root()

Expand All @@ -94,7 +100,7 @@ function M.get_node_for_key(bufnr, keys)
local key_node_depth = 9999
local value_node = nil
local value_node_depth = 9999
for id, node, _ in query:iter_captures(json_node, bufnr) do
for id, node, _ in query:iter_captures(json_node, source) do
local name = query.captures[id]
local depth = calculate_node_depth(node)
if name == "key" and depth < key_node_depth then
Expand Down Expand Up @@ -264,8 +270,9 @@ end
function M.find_call_t_expressions(source, lib, lang)
local ok, parser = pcall(function()
if type(source) == "string" then
vim.validate({ lang = { lang, "string", true } })
if lang == nil then
return {}
error("lang is required when source is string")
end
return vim.treesitter.get_string_parser(source, lang)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function handler(params, client)
lsp_config.ref_table_by_workspace[workspace_dir] = ref_table
ref_table:load_all()
else
ref_table:load_path(vim.uri_to_fname(uri))
ref_table:load_path(vim.uri_to_fname(uri), params.contentChanges[1].text)
end

vim.schedule(function()
Expand Down
11 changes: 6 additions & 5 deletions lua/js-i18n/lsp/protocol/request/text_document_definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ local function handler(params, client)

local workspace_dir = utils.get_workspace_root(bufnr)
local t_source = client.t_source_by_workspace[workspace_dir]
if not t_source then
return "Translation source not found", nil
end

local lang = utils.get_language(
client.current_language,
c.config.primary_language,
Expand All @@ -29,15 +33,13 @@ local function handler(params, client)
local library = utils.detect_library(workspace_dir)

for file, _ in pairs(t_source:get_translation_source_by_lang(lang)) do
local bufnr = vim.api.nvim_create_buf(false, true)
local content = Path:new(file):read()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(content, "\n"))

--- @type lsp.Location[]
local result = {}

local keys = vim.split(key, c.config.key_separator, { plain = true })
local node = analyzer.get_node_for_key(bufnr, keys)
local node = analyzer.get_node_for_key(content, keys)
if node ~= nil then
local row_start, col_start, row_end, col_end = node:range()
table.insert(result, {
Expand All @@ -54,7 +56,7 @@ local function handler(params, client)
local key_with_suffix = { unpack(keys) }
key_with_suffix[#key_with_suffix] = key_with_suffix[#key_with_suffix] .. suffix

local node = analyzer.get_node_for_key(bufnr, key_with_suffix)
local node = analyzer.get_node_for_key(content, key_with_suffix)
if node ~= nil then
local row_start, col_start, row_end, col_end = node:range()
table.insert(result, {
Expand All @@ -68,7 +70,6 @@ local function handler(params, client)
end
end

vim.api.nvim_buf_delete(bufnr, { force = true })
if #result > 0 then
return nil, result
end
Expand Down
10 changes: 2 additions & 8 deletions lua/js-i18n/lsp/protocol/request/text_document_references.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ local function handler(params, client)

local result = {}
for _, ref in ipairs(refs) do
local path = ref.path
local t_call = ref.t_call
local row_start, col_start, row_end, col_end = t_call.node:range()
table.insert(result, {
uri = vim.uri_from_fname(path),
range = {
start = { line = row_start, character = col_start },
["end"] = { line = row_end, character = col_end },
},
uri = ref.uri,
range = ref.range,
})
end

Expand Down
59 changes: 39 additions & 20 deletions lua/js-i18n/reference_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ local utils = require("js-i18n.utils")

local M = {}

--- @class I18n.Ref
--- @field key string
--- @field uri string
--- @field range lsp.Range

--- 翻訳の参照テーブル
--- @class I18n.ReferenceTable
--- @field workspace_dir string ワークスペースディレクトリ
--- @field _ref_table table<string, table<string, FindTExpressionResultItem[]>>
--- @field _ref_table table<string, table<string, I18n.Ref[]>>
--- @field _ref_processing table<string, boolean>
--- @field config I18n.ReferenceTableConfig
local ReferenceTable = {}
Expand Down Expand Up @@ -43,13 +48,14 @@ function ReferenceTable:load_all()
})
end

function ReferenceTable:load_path(path)
function ReferenceTable:load_path(path, content)
self._ref_processing[path] = true
if path == nil or path == "" then
self._ref_processing[path] = false
return
end
local lib = utils.detect_library(self.config.workspace_dir)

self._ref_processing[path] = true
vim.schedule(function()
local ft = vim.filetype.match({ filename = path }) or "typescriptreact"
local lang = (function()
Expand All @@ -62,19 +68,37 @@ function ReferenceTable:load_path(path)
elseif ft == "javascriptreact" then
return "javascript"
end
vim.notify("Unknown filetype: " .. ft, vim.log.levels.ERROR)
return nil
end)()
local content = Path:new(path):read()
content = content or Path:new(path):read()

local result = analyzer.find_call_t_expressions(content, lib, lang)
self._ref_table[path] = result
self._ref_table[path] = vim
.iter(result)
:map(function(t_call)
local row_start, col_start, row_end, col_end = t_call.node:range()
return {
key = t_call.key,
uri = vim.uri_from_fname(path),
range = {
start = { line = row_start, character = col_start },
["end"] = { line = row_end, character = col_end },
},
}
end)
:totable()

self._ref_processing[path] = false
end)
end

function ReferenceTable:wait_processed()
while true do
local timeout = 10000
local interval = 50
local timeout_cnt = timeout / interval

for _ = 1, timeout_cnt do
local processing = false
for _, v in pairs(self._ref_processing) do
if v then
Expand All @@ -83,33 +107,28 @@ function ReferenceTable:wait_processed()
end
end
if not processing then
break
return
end
vim.wait(50)
vim.wait(interval)
end

vim.notify("Timeout: ReferenceTable:wait_processed", vim.log.levels.ERROR)
end

function ReferenceTable:is_processing(path)
return self._ref_processing[path] ~= nil and not self._ref_processing[path]
end

--- @class ReferenceTable__find_by_key_result
--- @field path string
--- @field t_call FindTExpressionResultItem

--- キーから参照を検索する
--- @param key string キー
--- @return ReferenceTable__find_by_key_result[]
--- @return I18n.Ref[]
function ReferenceTable:find_by_key(key)
self:wait_processed()
local result = {}
for path, t_calls in pairs(self._ref_table) do
for _, t_call in ipairs(t_calls) do
if t_call.key == key then
table.insert(result, {
path = path,
t_call = t_call,
})
for _, refs in pairs(self._ref_table) do
for _, ref in ipairs(refs) do
if ref.key == key then
table.insert(result, ref)
end
end
end
Expand Down

0 comments on commit 48c20f9

Please sign in to comment.