Skip to content

Commit ad901a6

Browse files
authored
fix: do not initialize LS when opening JSON files other than message files (#50)
1 parent 73615ce commit ad901a6

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

lua/js-i18n/client.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local async = require("plenary.async")
22

33
local analyzer = require("js-i18n.analyzer")
44
local c = require("js-i18n.config")
5-
local translation_source = require("js-i18n.translation-source")
5+
local translation_source = require("js-i18n.translation_source")
66
local utils = require("js-i18n.utils")
77
local virt_text = require("js-i18n.virt_text")
88

@@ -230,7 +230,8 @@ function Client:edit_translation(lang, key)
230230
local namespace = nil
231231

232232
if c.config.namespace_separator ~= nil then
233-
local split_first_key = vim.split(split_key[1], c.config.namespace_separator, { plain = true })
233+
local split_first_key =
234+
vim.split(split_key[1], c.config.namespace_separator, { plain = true })
234235
namespace = split_first_key[1]
235236
split_key[1] = split_first_key[2]
236237
end

lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local lsp_config = require("js-i18n.lsp.config")
22
local reference_table = require("js-i18n.reference_table")
3+
local translation_source = require("js-i18n.translation_source")
34

45
--- ハンドラ
56
--- @param params lsp.DidChangeTextDocumentParams
@@ -10,6 +11,13 @@ local function handler(params, client)
1011

1112
local bufnr = vim.uri_to_bufnr(uri)
1213
local workspace_dir = require("js-i18n.utils").get_workspace_root(bufnr)
14+
15+
-- 文言ファイル以外の JSON ファイルは無視する
16+
local file_name = vim.uri_to_fname(uri)
17+
if file_name:match("%.json$") and not translation_source.is_translation_file(file_name) then
18+
return
19+
end
20+
1321
local ref_table = lsp_config.ref_table_by_workspace[workspace_dir]
1422
if lsp_config.ref_table_by_workspace[workspace_dir] == nil then
1523
local ref_table = reference_table.ReferenceTable.new({

lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local lsp_config = require("js-i18n.lsp.config")
22
local reference_table = require("js-i18n.reference_table")
3-
local utils = require("js-i18n.utils")
3+
local translation_source = require("js-i18n.translation_source")
44

55
--- ハンドラ
66
--- @param params lsp.DidOpenTextDocumentParams
@@ -10,6 +10,13 @@ local function handler(params, client)
1010

1111
local bufnr = vim.uri_to_bufnr(uri)
1212
local workspace_dir = require("js-i18n.utils").get_workspace_root(bufnr)
13+
14+
-- 文言ファイル以外の JSON ファイルは無視する
15+
local file_name = vim.uri_to_fname(uri)
16+
if file_name:match("%.json$") and not translation_source.is_translation_file(file_name) then
17+
return
18+
end
19+
1320
if lsp_config.ref_table_by_workspace[workspace_dir] == nil then
1421
local ref_table = reference_table.ReferenceTable.new({
1522
workspace_dir = workspace_dir,

lua/js-i18n/translation-source.lua renamed to lua/js-i18n/translation_source.lua

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,59 @@ local c = require("js-i18n.config")
77

88
local M = {}
99

10+
--- 文言ファイルかを判定するための正規表現を取得する
11+
--- @return vim.regex[]
12+
local function get_translation_source_regex()
13+
local patterns = c.config.translation_source
14+
return vim
15+
.iter(patterns)
16+
:map(function(pattern)
17+
return vim.regex(vim.fn.glob2regpat(pattern))
18+
end)
19+
:totable()
20+
end
21+
1022
--- 文言ファイルの一覧を取得する
1123
--- @param dir string ディレクトリ
1224
--- @return string[]
1325
function M.get_translation_files(dir)
1426
local result = {}
15-
-- OPTIMIZE: pattern は on_insert の中で回すほうがいい
16-
for _, pattern in ipairs(c.config.translation_source) do
17-
local regexp = vim.regex(vim.fn.glob2regpat(pattern))
18-
scan.scan_dir(dir, {
19-
search_pattern = "%.json$",
20-
on_insert = function(path)
27+
28+
local regexps = get_translation_source_regex()
29+
30+
scan.scan_dir(dir, {
31+
search_pattern = "%.json$",
32+
on_insert = function(path)
33+
for _, regexp in ipairs(regexps) do
2134
local match_s = regexp:match_str(path)
2235
if match_s then
2336
table.insert(result, path)
37+
break
2438
end
25-
end,
26-
})
27-
end
39+
end
40+
end,
41+
})
2842
return result
2943
end
3044

45+
--- ファイルが文言ファイルかを判定する
46+
--- @param filename string ファイル名
47+
--- @return boolean
48+
function M.is_translation_file(filename)
49+
if not filename:match("%.json$") then
50+
return false
51+
end
52+
53+
for _, regexp in ipairs(get_translation_source_regex()) do
54+
local match_s = regexp:match_str(filename)
55+
if match_s then
56+
return true
57+
end
58+
end
59+
60+
return false
61+
end
62+
3163
--- 文言の更新
3264
--- @param file string ファイルパス
3365
--- @param key string[] キー

0 commit comments

Comments
 (0)