forked from NvChad/NvChad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open_package_repository 추가, node_modules 폴더에서 repo를 여는 역할
원래는 blob 을 통해서 코드위치를 열려고했는데 .git 정보가 로컬에 없어서 할수 없었음 별 유용하지는 않지만 개선 여지가 있을 수 있어서 구현한 것 커밋
- Loading branch information
Showing
6 changed files
with
116 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
local get_git_root = require('lab/gx/lib/get_git_root') | ||
local get_directory_of_current_file = require("lab/gx/lib/get_directory_of_current_file") | ||
local parse_repository = require("lab.gx.lib.parse_repository") | ||
local get_package_repository = require("lab.gx.lib.get_package_repository") | ||
local M = {} | ||
|
||
M.handler = function(args) | ||
local remote_info = M.match(args.lines) | ||
if not remote_info then | ||
return vim.notify(string.format('remote_info is nil'), vim.log.levels.ERROR) | ||
end | ||
|
||
local domain = remote_info.domain | ||
local username = remote_info.username | ||
local repository = remote_info.repository | ||
|
||
local Job = require("plenary.job") | ||
local command = "open" | ||
local url = string.format("https://%s/%s/%s", domain, username, repository) | ||
|
||
Job:new({ | ||
command, | ||
args = { | ||
url, | ||
}, | ||
}):sync() | ||
|
||
vim.notify(string.format('Open url: %s', url), vim.log.levels.INFO) | ||
end | ||
M.match = function(lines) | ||
local cwd = get_directory_of_current_file() | ||
local cwd_expanded = vim.fn.expand(cwd) | ||
local git_root = get_git_root(cwd) | ||
local pattern = vim.fn.expand(git_root):gsub('-', '%%-') .. '/node_modules/[%w%-_]+' | ||
local package_in_node_modules = cwd_expanded:match(pattern) | ||
|
||
if not package_in_node_modules then | ||
return | ||
end | ||
|
||
local package_repo = get_package_repository(package_in_node_modules) | ||
if not package_repo then | ||
return | ||
end | ||
|
||
return parse_repository(package_repo) | ||
end | ||
M.name = 'open package repository' | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
local file_exists = function (file) | ||
local f = io.open(file, "rb") | ||
|
||
if f then | ||
f:close() | ||
end | ||
|
||
return f ~= nil | ||
end | ||
|
||
return file_exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
local try_read_file = require("lab/gx/lib/try_read_file") | ||
|
||
local get_package_repository = function (cwd) | ||
local f = vim.fn.expand(cwd) .. '/package.json' | ||
local json = try_read_file(f) | ||
|
||
if json then | ||
local encoded = vim.fn.json_decode(json) | ||
|
||
if encoded then | ||
return encoded.repository | ||
end | ||
end | ||
end | ||
|
||
return get_package_repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
local remove_suffix = require("lab/gx/lib/remove_suffix") | ||
|
||
local parse_repository = function (origin) | ||
local parts = {} | ||
|
||
for part in origin:gmatch("[^@:/]+") do | ||
table.insert(parts, part) | ||
end | ||
|
||
local domain = parts[2] | ||
local username = parts[3] | ||
local repository = remove_suffix('.git', parts[4]) | ||
|
||
return { | ||
domain = domain, | ||
username = username, | ||
repository = repository, | ||
} | ||
end | ||
|
||
return parse_repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
local file_exists = require("lab/gx/lib/file_exists") | ||
|
||
local function try_read_file(file) | ||
if not file_exists(file) then | ||
return nil | ||
end | ||
|
||
local lines = {} | ||
for line in io.lines(file) do | ||
lines[#lines + 1] = line | ||
end | ||
|
||
return table.concat(lines) | ||
end | ||
|
||
return try_read_file |