Skip to content

Commit d8d55f5

Browse files
committed
[Fix] code style using stylua
1 parent bd5668f commit d8d55f5

9 files changed

Lines changed: 234 additions & 232 deletions

File tree

lua/codesnap/highlight.lua

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ local table_utils = require("codesnap.utils.table")
33
local highlight_module = {}
44

55
function highlight_module.call_cb_with_parsed_config(cb_name, highlight_start_line_number, highlight_end_line_number)
6-
vim.api.nvim_buf_delete(0, {})
7-
vim.schedule(function()
8-
local main = require("codesnap")
9-
local config = table_utils.merge(main.highlight_mode_config, {
10-
highlight_start_line_number = highlight_start_line_number,
11-
highlight_end_line_number = highlight_end_line_number,
12-
})
13-
14-
main[cb_name](config)
15-
end)
6+
vim.api.nvim_buf_delete(0, {})
7+
vim.schedule(function()
8+
local main = require("codesnap")
9+
local config = table_utils.merge(main.highlight_mode_config, {
10+
highlight_start_line_number = highlight_start_line_number,
11+
highlight_end_line_number = highlight_end_line_number,
12+
})
13+
14+
main[cb_name](config)
15+
end)
1616
end
1717

1818
function highlight_module.create_highlight_selector_window(cb_name, code)
19-
local width = 100
20-
local height = #code + 2
21-
local row = vim.fn.winheight(0) / 2 - height / 2
22-
local col = vim.fn.winwidth(0) / 2 - width / 2
23-
local bufnr = vim.api.nvim_create_buf(false, true)
24-
25-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, code)
26-
27-
local window_id = vim.api.nvim_open_win(bufnr, false, {
28-
relative = "editor",
29-
width = width,
30-
height = height,
31-
col = col,
32-
row = row,
33-
style = "minimal",
34-
border = "rounded",
35-
title = "Select highlight lines",
36-
title_pos = "center",
37-
})
19+
local width = 100
20+
local height = #code + 2
21+
local row = vim.fn.winheight(0) / 2 - height / 2
22+
local col = vim.fn.winwidth(0) / 2 - width / 2
23+
local bufnr = vim.api.nvim_create_buf(false, true)
24+
25+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, code)
26+
27+
local window_id = vim.api.nvim_open_win(bufnr, false, {
28+
relative = "editor",
29+
width = width,
30+
height = height,
31+
col = col,
32+
row = row,
33+
style = "minimal",
34+
border = "rounded",
35+
title = "Select highlight lines",
36+
title_pos = "center",
37+
})
3838

3939
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
4040
vim.api.nvim_buf_set_option(bufnr, "filetype", vim.bo.filetype)

lua/codesnap/modal.lua

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@ local M = {}
77
function M.pop_modal(selected_text, filetype, callback)
88
if not selected_text or selected_text == "" then
99
vim.notify("No text provided to modal", vim.log.levels.ERROR)
10-
if callback then callback(nil) end
10+
if callback then
11+
callback(nil)
12+
end
1113
return
1214
end
1315

14-
local selected_lines = vim.split(selected_text, '\n', { plain = true })
16+
local selected_lines = vim.split(selected_text, "\n", { plain = true })
1517

1618
-- Create a new buffer for the floating window
1719
local buf = vim.api.nvim_create_buf(false, true)
1820
vim.api.nvim_buf_set_lines(buf, 0, -1, false, selected_lines)
1921

2022
-- Set filetype for syntax highlighting if provided
2123
if filetype and filetype ~= "" then
22-
vim.api.nvim_buf_set_option(buf, 'filetype', filetype)
24+
vim.api.nvim_buf_set_option(buf, "filetype", filetype)
2325
end
2426

2527
-- Make the buffer read-only
26-
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
27-
vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile')
28-
vim.api.nvim_buf_set_option(buf, 'readonly', true)
28+
vim.api.nvim_buf_set_option(buf, "modifiable", false)
29+
vim.api.nvim_buf_set_option(buf, "buftype", "nofile")
30+
vim.api.nvim_buf_set_option(buf, "readonly", true)
2931

3032
-- Calculate window size and position
3133
local width = 0
@@ -42,22 +44,22 @@ function M.pop_modal(selected_text, filetype, callback)
4244

4345
-- Create floating window
4446
local win = vim.api.nvim_open_win(buf, true, {
45-
relative = 'editor',
47+
relative = "editor",
4648
row = row,
4749
col = col,
4850
width = width,
4951
height = height,
50-
style = 'minimal',
51-
border = 'rounded',
52-
title = ' Select text to highlight (Press Enter to confirm, Esc to cancel) ',
53-
title_pos = 'center',
52+
style = "minimal",
53+
border = "rounded",
54+
title = " Select text to highlight (Press Enter to confirm, Esc to cancel) ",
55+
title_pos = "center",
5456
focusable = true,
5557
})
5658

5759
-- Set window options
58-
vim.api.nvim_win_set_option(win, 'cursorline', true)
59-
vim.api.nvim_win_set_option(win, 'number', true)
60-
vim.api.nvim_win_set_option(win, 'relativenumber', false)
60+
vim.api.nvim_win_set_option(win, "cursorline", true)
61+
vim.api.nvim_win_set_option(win, "number", true)
62+
vim.api.nvim_win_set_option(win, "relativenumber", false)
6163

6264
-- Ensure the window has focus
6365
vim.api.nvim_set_current_win(win)
@@ -75,40 +77,40 @@ function M.pop_modal(selected_text, filetype, callback)
7577
end
7678

7779
-- Set up keymaps for the floating window
78-
vim.keymap.set({'n', 'v'}, '<CR>', function()
80+
vim.keymap.set({ "n", "v" }, "<CR>", function()
7981
-- Get the current mode
8082
local mode = vim.api.nvim_get_mode().mode
8183

82-
if mode == 'v' or mode == 'V' or mode == '\22' then -- \22 is Ctrl-V (visual block mode)
84+
if mode == "v" or mode == "V" or mode == "\22" then -- \22 is Ctrl-V (visual block mode)
8385
-- Visual mode - get the selection range before exiting visual mode
84-
local start_pos = vim.fn.getpos('v')
85-
local end_pos = vim.fn.getpos('.')
86+
local start_pos = vim.fn.getpos("v")
87+
local end_pos = vim.fn.getpos(".")
8688

8789
-- Ensure start_pos is before end_pos
8890
if start_pos[2] > end_pos[2] then
8991
start_pos, end_pos = end_pos, start_pos
9092
end
9193

9294
-- Exit visual mode
93-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
95+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
9496

95-
close_and_callback({start_pos[2], end_pos[2]}) -- Return line numbers
97+
close_and_callback({ start_pos[2], end_pos[2] }) -- Return line numbers
9698
else
9799
-- No selection, return the entire buffer range
98100
local line_count = vim.api.nvim_buf_line_count(buf)
99-
close_and_callback({1, line_count})
101+
close_and_callback({ 1, line_count })
100102
end
101103
end, { buffer = buf })
102104

103105
-- Set up keymap to close window with Esc
104-
vim.keymap.set({'n', 'v'}, '<Esc>', function()
105-
close_and_callback(nil) -- User cancelled
106+
vim.keymap.set({ "n", "v" }, "<Esc>", function()
107+
close_and_callback(nil) -- User cancelled
106108
end, { buffer = buf })
107109

108110
-- Set up keymap to close window with q
109-
vim.keymap.set('n', 'q', function()
110-
close_and_callback(nil) -- User cancelled
111+
vim.keymap.set("n", "q", function()
112+
close_and_callback(nil) -- User cancelled
111113
end, { buffer = buf })
112114
end
113115

114-
return M
116+
return M

lua/codesnap/utils/command.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
local command_util = {}
22

33
function command_util.exec_command(command, mode)
4-
local handle = assert(io.popen(command, mode))
5-
local origin = assert(handle:read("*a"))
4+
local handle = assert(io.popen(command, mode))
5+
local origin = assert(handle:read("*a"))
66

7-
handle:close()
7+
handle:close()
88

9-
return origin
9+
return origin
1010
end
1111

1212
return command_util

lua/codesnap/utils/list.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
local list_utils = {}
22

33
function list_utils.find(list, predicate)
4-
for _, value in ipairs(list) do
5-
if predicate(value) then
6-
return value
7-
end
8-
end
4+
for _, value in ipairs(list) do
5+
if predicate(value) then
6+
return value
7+
end
8+
end
99

10-
return nil
10+
return nil
1111
end
1212

1313
function list_utils.some(list, predicate)
14-
return list_utils.find(list, predicate) ~= nil
14+
return list_utils.find(list, predicate) ~= nil
1515
end
1616

1717
function list_utils.includes(list, value)
18-
return list_utils.find(list, function(item)
19-
return item == value
20-
end) ~= nil
18+
return list_utils.find(list, function(item)
19+
return item == value
20+
end) ~= nil
2121
end
2222

2323
function list_utils.map(list, fn)
24-
local result = {}
24+
local result = {}
2525

26-
for i, value in ipairs(list) do
27-
table.insert(result, fn(value, i))
28-
end
26+
for i, value in ipairs(list) do
27+
table.insert(result, fn(value, i))
28+
end
2929

30-
return result
30+
return result
3131
end
3232

3333
return list_utils

lua/codesnap/utils/path.lua

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,85 @@ local path_utils = {}
44

55
-- Get the appropriate path separator for the current OS
66
function path_utils.get_separator()
7-
return package.config:sub(1, 1) -- Returns '\' on Windows, '/' on Unix-like systems
7+
return package.config:sub(1, 1) -- Returns '\' on Windows, '/' on Unix-like systems
88
end
99

1010
function path_utils.join(separator, ...)
11-
local args = { ... }
11+
local args = { ... }
1212

13-
return table.concat(args, separator)
13+
return table.concat(args, separator)
1414
end
1515

1616
function path_utils.dir_name()
17-
local sep = path_utils.get_separator()
18-
local pattern = "@?(.*" .. vim.pesc(sep) .. ")"
19-
return debug.getinfo(1).source:match(pattern)
17+
local sep = path_utils.get_separator()
18+
local pattern = "@?(.*" .. vim.pesc(sep) .. ")"
19+
return debug.getinfo(1).source:match(pattern)
2020
end
2121

2222
function path_utils.with_dir_name(path)
23-
return path_utils.dir_name() .. path
23+
return path_utils.dir_name() .. path
2424
end
2525

2626
function path_utils.get_escaped_cwd()
27-
local cwd = vim.fn.getcwd()
27+
local cwd = vim.fn.getcwd()
2828

29-
return string_utils.escape(cwd)
29+
return string_utils.escape(cwd)
3030
end
3131

3232
function path_utils.back(path)
33-
local sep = path_utils.get_separator()
34-
local pattern = vim.pesc(sep) .. "[^" .. vim.pesc(sep) .. "]+" .. vim.pesc(sep) .. "?$"
35-
local parsed_path, _ = path:gsub(pattern, "")
33+
local sep = path_utils.get_separator()
34+
local pattern = vim.pesc(sep) .. "[^" .. vim.pesc(sep) .. "]+" .. vim.pesc(sep) .. "?$"
35+
local parsed_path, _ = path:gsub(pattern, "")
3636

37-
return parsed_path
37+
return parsed_path
3838
end
3939

4040
function path_utils.get_workspace()
41-
local cwd = vim.fn.getcwd()
42-
local sep = path_utils.get_separator()
43-
local pattern = vim.pesc(sep) .. "([^" .. vim.pesc(sep) .. "]+)$"
44-
local _, _, workspace = string.find(cwd, pattern)
41+
local cwd = vim.fn.getcwd()
42+
local sep = path_utils.get_separator()
43+
local pattern = vim.pesc(sep) .. "([^" .. vim.pesc(sep) .. "]+)$"
44+
local _, _, workspace = string.find(cwd, pattern)
4545

46-
return workspace == nil and "" or workspace
46+
return workspace == nil and "" or workspace
4747
end
4848

4949
function path_utils.get_relative_path()
50-
local full_file_path = vim.fn.expand("%:p")
51-
local cwd = path_utils.get_escaped_cwd()
52-
local sep = path_utils.get_separator()
50+
local full_file_path = vim.fn.expand("%:p")
51+
local cwd = path_utils.get_escaped_cwd()
52+
local sep = path_utils.get_separator()
5353

54-
-- Remove the CWD from the full path
55-
local relative = full_file_path:gsub("^" .. cwd, "")
54+
-- Remove the CWD from the full path
55+
local relative = full_file_path:gsub("^" .. cwd, "")
5656

57-
-- Remove leading separator if present
58-
if relative:sub(1, 1) == sep then
59-
relative = relative:sub(2)
60-
end
57+
-- Remove leading separator if present
58+
if relative:sub(1, 1) == sep then
59+
relative = relative:sub(2)
60+
end
6161

62-
return relative
62+
return relative
6363
end
6464

6565
-- Get default save path by OS
6666
-- If Linux, use XDG_PICTURE_DIR
6767
-- if mac use ~/Pictures
6868
-- if windows use FOLDERID_Pictures
6969
function path_utils.get_default_save_path()
70-
local sep = path_utils.get_separator()
71-
local home = os.getenv("HOME") or os.getenv("USERPROFILE") -- USERPROFILE for Windows
72-
local home_picture_folder = home .. sep .. "Pictures"
73-
74-
return platform_utils.match_os({
75-
Darwin = function()
76-
return home_picture_folder
77-
end,
78-
Linux = function()
79-
return os.getenv("XDG_PICTURES_DIR") or home_picture_folder
80-
end,
81-
Windows_NT = function()
82-
-- On Windows, use %USERPROFILE%\Pictures
83-
return home_picture_folder
84-
end,
85-
})
70+
local sep = path_utils.get_separator()
71+
local home = os.getenv("HOME") or os.getenv("USERPROFILE") -- USERPROFILE for Windows
72+
local home_picture_folder = home .. sep .. "Pictures"
73+
74+
return platform_utils.match_os({
75+
Darwin = function()
76+
return home_picture_folder
77+
end,
78+
Linux = function()
79+
return os.getenv("XDG_PICTURES_DIR") or home_picture_folder
80+
end,
81+
Windows_NT = function()
82+
-- On Windows, use %USERPROFILE%\Pictures
83+
return home_picture_folder
84+
end,
85+
})
8686
end
8787

8888
return path_utils

0 commit comments

Comments
 (0)