Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ on:
branches: [main, master]

jobs:
format:
name: Format Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup StyLua
uses: JohnnyMorganz/stylua-action@v4
with:
version: latest
token: ${{ github.token }}
args: false

- name: Check formatting
run: make fmt-check

test:
name: Test
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
column_width = 100
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferSingle"
call_parentheses = "Always"
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test test-file clean
.PHONY: test test-file clean fmt fmt-check

# Test runner
PLENARY_DIR ?= /tmp/plenary.nvim
Expand Down Expand Up @@ -31,3 +31,11 @@ docs:
# Health check
health:
nvim --headless -c "checkhealth comment-translate" -c "qa"

# Format Lua files
fmt:
stylua lua plugin tests

# Check Lua formatting
fmt-check:
stylua --check lua plugin tests
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ hover = { auto = false }

⚠️ Any text you translate (hover, replace, or immersive translation) is sent to the configured external translation service.

## Development

Use [StyLua](https://github.com/JohnnyMorganz/StyLua) for Lua formatting.

* Format all Lua files:

```sh
make fmt
```

* Check formatting (used in CI):

```sh
make fmt-check
```

## License

MIT
2 changes: 1 addition & 1 deletion lua/comment-translate/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function M.hover_translate()
vim.notify('No comment or string found', vim.log.levels.INFO)
return
end

translate.translate(text, nil, nil, function(result)
if result then
ui.hover.show(result)
Expand Down
10 changes: 8 additions & 2 deletions lua/comment-translate/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ M.config = vim.deepcopy(default_config)
local function warn_unknown(prefix, tbl, allowed)
for key, _ in pairs(tbl) do
if not allowed[key] then
vim.notify(string.format('comment-translate: unknown config key %s.%s', prefix, key), vim.log.levels.WARN)
vim.notify(
string.format('comment-translate: unknown config key %s.%s', prefix, key),
vim.log.levels.WARN
)
end
end
end
Expand Down Expand Up @@ -126,7 +129,10 @@ local function validate(user_config)
})
-- Ensure max_entries is at least 1 to prevent infinite loops
if user_config.cache.max_entries and user_config.cache.max_entries < 1 then
vim.notify('comment-translate: cache.max_entries must be >= 1, defaulting to 1', vim.log.levels.WARN)
vim.notify(
'comment-translate: cache.max_entries must be >= 1, defaulting to 1',
vim.log.levels.WARN
)
user_config.cache.max_entries = 1
end
end
Expand Down
5 changes: 4 additions & 1 deletion lua/comment-translate/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ local function load_modules()
end)

if not ok then
vim.notify('comment-translate: Failed to load modules - ' .. tostring(err), vim.log.levels.ERROR)
vim.notify(
'comment-translate: Failed to load modules - ' .. tostring(err),
vim.log.levels.ERROR
)
return false
end

Expand Down
2 changes: 1 addition & 1 deletion lua/comment-translate/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function M.get_text_at_cursor(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1

local text, node_type = treesitter.get_text_at_position(bufnr, row, col)
if text then
local cleaned = normalize_text(text, node_type)
Expand Down
28 changes: 14 additions & 14 deletions lua/comment-translate/parser/regex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ local string_patterns = {

-- Block comment delimiters: { start_pattern, end_pattern, start_literal, end_literal }
local block_comment_delimiters = {
{ '/%*', '%*/', '/*', '*/' }, -- C/C++/Java/JS/etc.
{ '%-%-+%[%[', '%]%]', '--[[', ']]' }, -- Lua (]]-- or ]] both work)
{ '<!%-%-', '%-%->', '<!--', '-->' }, -- HTML/XML
{ '/%*', '%*/', '/*', '*/' }, -- C/C++/Java/JS/etc.
{ '%-%-+%[%[', '%]%]', '--[[', ']]' }, -- Lua (]]-- or ]] both work)
{ '<!%-%-', '%-%->', '<!--', '-->' }, -- HTML/XML
}

---@param line_text string
Expand All @@ -45,7 +45,7 @@ local function check_block_start(line_text)
for i, delim in ipairs(block_comment_delimiters) do
local start_pattern = delim[1]
local end_pattern = delim[2]

local start_pos = line_text:find(start_pattern)
if start_pos then
local end_pos = line_text:find(end_pattern, start_pos + 1)
Expand All @@ -67,7 +67,7 @@ local function check_block_end(line_text, delimiter_index)
if not delim then
return false, nil
end

local end_pattern = delim[2]
local end_pos = line_text:find(end_pattern)
if end_pos then
Expand All @@ -88,18 +88,18 @@ function M.get_all_comments(bufnr)
local comments = {}
local line_count = vim.api.nvim_buf_line_count(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, line_count, false)

local in_block = false
local block_delimiter_index = nil
local block_start_line = nil
local block_content_lines = {}

for line_idx = 0, line_count - 1 do
local line_text = lines[line_idx + 1]
if not line_text then
goto continue
end

if in_block then
local is_end, content_before_end = check_block_end(line_text, block_delimiter_index)
if is_end then
Expand Down Expand Up @@ -139,7 +139,7 @@ function M.get_all_comments(bufnr)
break
end
end

if not comment then
for _, pattern in ipairs(inline_comment_patterns) do
comment = line_text:match(pattern)
Expand All @@ -148,16 +148,16 @@ function M.get_all_comments(bufnr)
end
end
end

if comment then
comments[line_idx] = comment
end
end
end

::continue::
end

return comments
end

Expand Down Expand Up @@ -199,7 +199,7 @@ function M.get_string_at_position(bufnr, line, col)
if not config.config.targets.string then
return nil
end

local line_text = vim.api.nvim_buf_get_lines(bufnr, line, line + 1, false)[1]
if not line_text then
return nil
Expand All @@ -222,7 +222,7 @@ function M.get_string_at_position(bufnr, line, col)
search_start = e + 1
end
end

return nil
end

Expand Down
29 changes: 16 additions & 13 deletions lua/comment-translate/parser/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ local string_node_types = {
---@param col number
---@return string?, string?
function M.get_text_at_position(bufnr, row, col)

local ok, parser = pcall(vim.treesitter.get_parser, bufnr)
if not ok or not parser then
return nil, nil
Expand Down Expand Up @@ -72,16 +71,20 @@ function M.get_text_at_position(bufnr, row, col)
break
end
end
if is_comment then break end

if is_comment then
break
end

for _, type_name in ipairs(string_node_types) do
if parent_type == type_name then
is_string = true
node = parent
break
end
end
if is_string then break end
if is_string then
break
end

parent = parent:parent()
end
Expand Down Expand Up @@ -110,24 +113,24 @@ function M.get_all_comments(bufnr)
if not config.config.targets.comment then
return {}
end

local comments = {}

local ok, parser = pcall(vim.treesitter.get_parser, bufnr)
if not ok or not parser then
return {}
end

local tree = parser:parse()[1]
if not tree then
return {}
end

local root = tree:root()

local function traverse(node)
local node_type = node:type()

for _, type_name in ipairs(comment_node_types) do
if node_type == type_name then
local text = vim.treesitter.get_node_text(node, bufnr)
Expand All @@ -138,14 +141,14 @@ function M.get_all_comments(bufnr)
return
end
end

for child in node:iter_children() do
traverse(child)
end
end

traverse(root)

return comments
end

Expand Down
24 changes: 14 additions & 10 deletions lua/comment-translate/translate/google.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,27 @@ function M.translate(text, target_lang, source_lang, callback)
local ok, Job = get_plenary_job()
if not ok then
vim.schedule(function()
vim.notify('comment-translate: plenary.nvim is required for translation', vim.log.levels.ERROR)
vim.notify(
'comment-translate: plenary.nvim is required for translation',
vim.log.levels.ERROR
)
callback(nil)
end)
return
end

source_lang = source_lang or 'auto'
target_lang = utils.normalize_lang_code(target_lang)
source_lang = utils.normalize_lang_code(source_lang)

local encoded_text = utils.url_encode(text)
local url = string.format(
'https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s',
source_lang,
target_lang,
encoded_text
)

local stderr_output = {}

Job:new({
Expand All @@ -90,7 +93,8 @@ function M.translate(text, target_lang, source_lang, callback)
'--silent',
'--show-error',
'--fail',
'--max-time', '10',
'--max-time',
'10',
url,
},
on_stderr = function(_, data)
Expand All @@ -109,20 +113,20 @@ function M.translate(text, target_lang, source_lang, callback)
callback(nil)
return
end

local result = table.concat(j:result(), '')
if not result or result == '' then
callback(nil)
return
end

local parse_ok, json = pcall(vim.fn.json_decode, result)
if not parse_ok or not json then
vim.notify('comment-translate: Failed to parse translation response', vim.log.levels.WARN)
callback(nil)
return
end

local translated_text = ''
if json[1] and type(json[1]) == 'table' then
for _, item in ipairs(json[1]) do
Expand All @@ -131,12 +135,12 @@ function M.translate(text, target_lang, source_lang, callback)
end
end
end

if translated_text == '' then
callback(nil)
return
end

cache.set(text, translated_text, target_lang, source_lang)
callback(translated_text)
end)
Expand Down