diff --git a/lua/js-i18n/client.lua b/lua/js-i18n/client.lua index f0f0c2f..e0d63d5 100644 --- a/lua/js-i18n/client.lua +++ b/lua/js-i18n/client.lua @@ -2,7 +2,7 @@ local async = require("plenary.async") local analyzer = require("js-i18n.analyzer") local c = require("js-i18n.config") -local translation_source = require("js-i18n.translation-source") +local translation_source = require("js-i18n.translation_source") local utils = require("js-i18n.utils") local virt_text = require("js-i18n.virt_text") @@ -230,7 +230,8 @@ function Client:edit_translation(lang, key) local namespace = nil if c.config.namespace_separator ~= nil then - local split_first_key = vim.split(split_key[1], c.config.namespace_separator, { plain = true }) + local split_first_key = + vim.split(split_key[1], c.config.namespace_separator, { plain = true }) namespace = split_first_key[1] split_key[1] = split_first_key[2] end diff --git a/lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua b/lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua index aaeb551..274fb1c 100644 --- a/lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua +++ b/lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua @@ -1,5 +1,6 @@ local lsp_config = require("js-i18n.lsp.config") local reference_table = require("js-i18n.reference_table") +local translation_source = require("js-i18n.translation_source") --- ハンドラ --- @param params lsp.DidChangeTextDocumentParams @@ -10,6 +11,13 @@ local function handler(params, client) local bufnr = vim.uri_to_bufnr(uri) local workspace_dir = require("js-i18n.utils").get_workspace_root(bufnr) + + -- 文言ファイル以外の JSON ファイルは無視する + local file_name = vim.uri_to_fname(uri) + if file_name:match("%.json$") and not translation_source.is_translation_file(file_name) then + return + end + local ref_table = lsp_config.ref_table_by_workspace[workspace_dir] if lsp_config.ref_table_by_workspace[workspace_dir] == nil then local ref_table = reference_table.ReferenceTable.new({ diff --git a/lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua b/lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua index 7e21dfa..11d23ce 100644 --- a/lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua +++ b/lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua @@ -1,6 +1,6 @@ local lsp_config = require("js-i18n.lsp.config") local reference_table = require("js-i18n.reference_table") -local utils = require("js-i18n.utils") +local translation_source = require("js-i18n.translation_source") --- ハンドラ --- @param params lsp.DidOpenTextDocumentParams @@ -10,6 +10,13 @@ local function handler(params, client) local bufnr = vim.uri_to_bufnr(uri) local workspace_dir = require("js-i18n.utils").get_workspace_root(bufnr) + + -- 文言ファイル以外の JSON ファイルは無視する + local file_name = vim.uri_to_fname(uri) + if file_name:match("%.json$") and not translation_source.is_translation_file(file_name) then + return + end + if lsp_config.ref_table_by_workspace[workspace_dir] == nil then local ref_table = reference_table.ReferenceTable.new({ workspace_dir = workspace_dir, diff --git a/lua/js-i18n/translation-source.lua b/lua/js-i18n/translation_source.lua similarity index 87% rename from lua/js-i18n/translation-source.lua rename to lua/js-i18n/translation_source.lua index 8c6c5bd..6a34005 100644 --- a/lua/js-i18n/translation-source.lua +++ b/lua/js-i18n/translation_source.lua @@ -7,27 +7,59 @@ local c = require("js-i18n.config") local M = {} +--- 文言ファイルかを判定するための正規表現を取得する +--- @return vim.regex[] +local function get_translation_source_regex() + local patterns = c.config.translation_source + return vim + .iter(patterns) + :map(function(pattern) + return vim.regex(vim.fn.glob2regpat(pattern)) + end) + :totable() +end + --- 文言ファイルの一覧を取得する --- @param dir string ディレクトリ --- @return string[] function M.get_translation_files(dir) local result = {} - -- OPTIMIZE: pattern は on_insert の中で回すほうがいい - for _, pattern in ipairs(c.config.translation_source) do - local regexp = vim.regex(vim.fn.glob2regpat(pattern)) - scan.scan_dir(dir, { - search_pattern = "%.json$", - on_insert = function(path) + + local regexps = get_translation_source_regex() + + scan.scan_dir(dir, { + search_pattern = "%.json$", + on_insert = function(path) + for _, regexp in ipairs(regexps) do local match_s = regexp:match_str(path) if match_s then table.insert(result, path) + break end - end, - }) - end + end + end, + }) return result end +--- ファイルが文言ファイルかを判定する +--- @param filename string ファイル名 +--- @return boolean +function M.is_translation_file(filename) + if not filename:match("%.json$") then + return false + end + + for _, regexp in ipairs(get_translation_source_regex()) do + local match_s = regexp:match_str(filename) + if match_s then + return true + end + end + + return false +end + --- 文言の更新 --- @param file string ファイルパス --- @param key string[] キー