-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.lua
87 lines (70 loc) · 2.03 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
local M = {}
function M.termcode(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
---@param filename string
---@return table | nil
function M.read_json_file(filename)
local Path = require 'plenary.path'
local path = Path:new(filename)
if not path:exists() then
return nil
end
local json_contents = path:read()
local json = vim.fn.json_decode(json_contents)
return json
end
function M.read_package_json()
return M.read_json_file 'package.json'
end
---Check if the given NPM package is installed in the current project.
---@param package string
---@return boolean
function M.is_npm_package_installed(package)
local package_json = M.read_package_json()
if not package_json then
return false
end
if package_json.dependencies and package_json.dependencies[package] then
return true
end
if package_json.devDependencies and package_json.devDependencies[package] then
return true
end
return false
end
---@param path string
---@return string
function M.shorten_path_relative(path)
return path
-- Remove CWD
:gsub(vim.pesc(vim.loop.cwd()) .. '/', '')
-- Remove home dir
:gsub(vim.pesc(vim.fn.expand '$HOME'), '~')
-- Remove trailing slash
:gsub('/$', '')
end
---@param path string
---@return string
function M.shorten_path_absolute(path)
return path:gsub(vim.pesc(vim.fn.expand '$HOME'), '~')
end
---@param ignored_filetypes? string[]
function M.close_all_floating_windows(ignored_filetypes)
ignored_filetypes = ignored_filetypes or {}
for _, window in ipairs(vim.api.nvim_list_wins()) do
local config = vim.api.nvim_win_get_config(window)
local bufnr = vim.fn.winbufnr(window)
local buf_filetype = vim.fn.getbufvar(bufnr, '&filetype')
if config.relative ~= '' and not vim.tbl_contains(ignored_filetypes, buf_filetype) then
vim.api.nvim_win_close(window, false)
end
end
end
-- Useful function for debugging
-- Print the given items
function _G.P(...)
local objects = vim.tbl_map(vim.inspect, { ... })
print(unpack(objects))
end
return M