Skip to content

Commit

Permalink
fix: do not initialize LS when opening JSON files other than message …
Browse files Browse the repository at this point in the history
…files (#50)
  • Loading branch information
nabekou29 authored Dec 21, 2024
1 parent 73615ce commit ad901a6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lua/js-i18n/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lua/js-i18n/lsp/protocol/notify/text_document_did_change.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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({
Expand Down
9 changes: 8 additions & 1 deletion lua/js-i18n/lsp/protocol/notify/text_document_did_open.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[] キー
Expand Down

0 comments on commit ad901a6

Please sign in to comment.