Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 #1

Merged
merged 7 commits into from
Feb 19, 2024
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
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
/lua_modules
/.luarocks

# OS
# Cargo
target

# Nodejs
node_modules
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
output.css
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<img src="https://img.shields.io/badge/Made%20With%20Lua-2C2D72?logo=lua&logoColor=fff&style=for-the-badge" alt="made with lua" >

<img src="https://img.shields.io/github/actions/workflow/status/mistricky/nvim-plugin-template/release.yml?style=for-the-badge&label=release" alt="release action status" />
<img src="https://img.shields.io/github/actions/workflow/status/mistricky/codesnap.nvim/release.yml?style=for-the-badge&label=release" alt="release action status" />

<img src="https://img.shields.io/github/actions/workflow/status/mistricky/nvim-plugin-template/lint.yml?style=for-the-badge&label=Lint" alt="release action status" />
<img src="https://img.shields.io/github/actions/workflow/status/mistricky/codesnap.nvim/lint.yml?style=for-the-badge&label=Lint" alt="release action status" />

</p>
59 changes: 59 additions & 0 deletions lua/codesnap/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local logger = require("codesnap.utils.logger")
local path_utils = require("codesnap.utils.path")

local client = {
job_id = 0,
}

function client:connect()
return vim.fn.jobstart({
path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])")))
.. "/snap-server/target/debug/snap-server",
}, {
stderr_buffered = true,
rpc = true,
on_stderr = function(_, err)
vim.fn.jobstop(self.job_id)
logger.error(err)
end,
on_exit = function()
vim.fn.chanclose(self.job_id)
self.job_id = 0
end,
})
end

function client:init()
return self.job_id == 0 and client:connect() or self.job_id
end

function client:start()
self.job_id = client:init()

if self.job_id == 0 then
logger.error("cannot start rpc process")
return
end

if self.job_id == -1 then
logger.error("rpc process is not executable")
vim.fn.jobstop(self.job_id)
return
end

return self
end

function client:send(event, message)
vim.fn.rpcnotify(self.job_id, event, message)
end

function client:stop()
if self.job_id == 0 or self.job_id == -1 then
return
end

vim.fn.jobstop(self.job_id)
end

return client
26 changes: 26 additions & 0 deletions lua/codesnap/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local table_utils = require("codesnap.utils.table")
local static = require("codesnap.static")
local client = require("codesnap.client")
local visual_utils = require("codesnap.utils.visual")

local main = {}

function main.setup(config)
static.config = table_utils.merge(static.config, config == nil and {} or config)

print(vim.inspect(static.config))
print(table_utils.serialize_json(static.config))
print()

if static.config.auto_load then
client:start()
end

client:send("config_setup", static.config)
end

function main.preview_code()
client:send("preview_code", { content = visual_utils.get_selected_text(), language = vim.bo.filetype })
end

return main
11 changes: 11 additions & 0 deletions lua/codesnap/static.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
return {
config = {
breadcrumbs = true,
column_number = true,
mac_window_bar = true,
opacity = true,
watermark = "CodeSnap.nvim",
auto_load = true,
},
preview_switch = true,
}
File renamed without changes.
10 changes: 10 additions & 0 deletions lua/plugin-name/utils/list.lua → lua/codesnap/utils/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ function list_utils.includes(list, value)
end) ~= nil
end

function list_utils.map(list, fn)
local result = {}

for i, value in ipairs(list) do
table.insert(result, fn(value, i))
end

return result
end

return list_utils
11 changes: 11 additions & 0 deletions lua/codesnap/utils/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local logger = {}

function logger.log(level, message)
vim.api.nvim_notify("[" .. level .. "] CodeSnap: " .. tostring(vim.inspect(message)), vim.log.levels[level], {})
end

function logger.error(message)
logger.log("ERROR", message)
end

return logger
9 changes: 9 additions & 0 deletions lua/codesnap/utils/path.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local path_utils = {}

function path_utils.back(path)
local parsed_path, _ = path:gsub("/[^\\/]+/?$", "")

return parsed_path
end

return path_utils
File renamed without changes.
70 changes: 70 additions & 0 deletions lua/codesnap/utils/table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local list_utils = require("codesnap.utils.list")
local table_utils = {}

function table_utils.assign(t, props)
for k, v in pairs(props) do
t[k] = v
end
end

function table_utils.merge(t1, t2)
for k, v in pairs(t2) do
t1[k] = v
end

return t1
end

function table_utils.is_array(t)
return type(t[1]) == "number"
end

function table_utils.typeof(value)
if type(value) == "table" then
if table_utils.is_array(value) then
return "array"
else
return "table"
end
end

return type(value)
end

function table_utils.serialize_array(t)
local result = list_utils.map(t, function(ele)
table_utils.serialize_json(ele)
end)

return "[" .. result.concat(t, ",") .. "]"
end

function table_utils.serialize_table(t)
local result = {}

for key, value in pairs(t) do
table.insert(result, string.format('"%s":%s', key, table_utils.serialize_json(value)))
end

return "{" .. table.concat(result, ",") .. "}"
end

function table_utils.serialize_string(value)
return '"' .. value .. '"'
end

function table_utils.serialize_json(t)
local complex_type_parser = {
array = table_utils.serialize_array,
table = table_utils.serialize_table,
string = table_utils.serialize_string,
}

local parse = complex_type_parser[table_utils.typeof(t)] or function(v)
return v
end

return parse(t)
end

return table_utils
30 changes: 30 additions & 0 deletions lua/codesnap/utils/visual.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local visual_utils = {}

function visual_utils.get_selected_text()
local start_pos = vim.fn.getpos("v")
local end_pos = vim.fn.getpos(".")

if start_pos[2] == end_pos[2] then
return vim.api.nvim_buf_get_lines(0, start_pos[2] - 1, start_pos[2], false)[1]:sub(start_pos[3], end_pos[3] - 1)
else
-- 如果选中的是多行文本,则需要分别获取每一行的文本
local selected_text = {}
for i = start_pos[2], end_pos[2] do
-- 使用 vim.api.nvim_buf_get_lines() 函数获取选中的文本
local line_text = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]
-- 如果是选中的第一行,需要从 mark 'v' 的列开始获取
if i == start_pos[2] then
line_text = line_text:sub(start_pos[3])
end
-- 如果是选中的最后一行,需要获取到当前光标的列
if i == end_pos[2] then
line_text = line_text:sub(1, end_pos[3] - 1)
end
table.insert(selected_text, line_text)
end
-- 输出当前选中的文本
return table.concat(selected_text, "\n")
end
end

return visual_utils
9 changes: 0 additions & 9 deletions lua/plugin-name/init.lua

This file was deleted.

3 changes: 0 additions & 3 deletions lua/plugin-name/static.lua

This file was deleted.

17 changes: 0 additions & 17 deletions lua/plugin-name/utils/table.lua

This file was deleted.

29 changes: 29 additions & 0 deletions plugin/codesnap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local codesnap = require("codesnap")
local static = require("codesnap.static")
local client = require("codesnap.client")

-- snap code
vim.api.nvim_create_user_command("CodeSnap", function() end, {})

vim.api.nvim_create_user_command("CodeSnapPreviewOn", function() end, {})

vim.api.nvim_create_user_command("CodeSnapPreviewOff", function() end, {})

vim.api.nvim_create_autocmd({ "CursorMoved" }, {
callback = function()
local mode = vim.api.nvim_get_mode().mode

if mode ~= "v" or not static.preview_switch then
return
end

codesnap.preview_code()
end,
})

vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
pattern = "*",
callback = function()
client:stop()
end,
})
3 changes: 0 additions & 3 deletions plugin/plugin-name.lua

This file was deleted.

6 changes: 3 additions & 3 deletions project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "plugin-name"
version = "0.0.2"
description = "description"
name = "codesnap.nvim"
version = "0.0.1"
description = "Make pretty code snapshot just one command"
author = "Mist"
email = "[email protected]"
23 changes: 23 additions & 0 deletions snap-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading
Loading