summaryrefslogtreecommitdiff
path: root/lua/functions.lua
blob: 8278ff06dd03606fa54eb3d58ca66d955354499e (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require("utility.misc")

function table.filter(list, predicate)
	local filtered_list = {}

	for _, item in ipairs(list) do
		if predicate(item) then
			table.insert(filtered_list, item)
		end
	end

	return filtered_list
end

local function split(inputstr, sep)
	if sep == nil then
			sep = "%s"
	end
	local t={}
	for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
			table.insert(t, str)
	end
	return t
end

local function file_exists(name)
   local f=io.open(name,"r")

   if f~=nil then io.close(f) return true else return false end
end

function _G.check_back_space()
	local col = vim.api.nvim_win_get_cursor(0)[2]

	if col == 0 then
		return true
	end

	return vim.api.nvim_get_current_line():sub(col, col):find("%s") and true
end

function _G.show_documentation()
	if vim.fn.index({"vim", "help"}, vim.bo.filetype) >= 0 then
		vim.cmd("help " .. vim.fn.expand("<cword>"))
	elseif vim.fn["coc#rpc#ready()"] then
		vim.fn.CocActionAsync("doHover")
	else
		vim.cmd(vim.go.keywordprg .. " " .. vim.fn.expand("<cword>"))
	end
end

function _G.toggle_terminal(height)
    if vim.fn.win_gotoid(vim.g.term_win) == 1 then
        vim.g.is_term_open = 0
		vim.cmd("quit!")
    else
        vim.cmd("botright new")

        vim.cmd("resize " .. height)

        if not pcall(function() vim.cmd("buffer " .. vim.g.term_buf) end) then
            vim.fn.termopen(vim.env.SHELL, {detach=0})

            vim.g.term_buf = vim.fn.bufnr("")

            vim.o.number = false
            vim.o.relativenumber = false
            vim.o.signcolumn = "no"
		end

        vim.cmd("startinsert!")

        vim.g.term_win = vim.fn.win_getid()
        vim.g.is_term_open = 1

        if vim.fn.exists("g:NERDTree") and vim.api.nvim_eval("g:NERDTree.IsOpen()") then
            vim.cmd("NERDTreeFocus")

            vim.cmd("wincmd H")

            local width_diff = vim.fn.winwidth(0) - vim.g.NERDTreeWinSize

            vim.cmd(width_diff .. "wincmd <")

            vim.cmd("wincmd p")

            vim.fn.feedkeys("A")
        end
    end
end

function _G.get_current_file()
    return vim.fn.expand("%")
end

function _G.remove_hidden_windowless_buffers()
	local all_buffers = vim.fn.getbufinfo()

	local buffers = table.filter(all_buffers, function (item)
		local windows = item.windows

		return #windows == 0 and item.hidden
	end)

	for _, buffer in ipairs(buffers) do
		vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
	end
end

function _G.close_current_buffer()
	local current_buf_nr = vim.fn.bufnr()

	local all_buffers = vim.fn.getbufinfo()

	local buffers = table.filter(
		all_buffers,
		function(buf_info)
			return buf_info.loaded == 1
				and buf_info.listed == 1
				and buf_info.bufnr ~= current_buf_nr
				and not string.find(buf_info.name, "NERD_tree_")
				and not string.find(buf_info.name, "term://")
		end
	)

	if #buffers ~= 0 then
		vim.cmd("buffer " .. buffers[1].bufnr)
	else
		vim.cmd("new")
	end

	vim.api.nvim_buf_delete(current_buf_nr, { force = true })

	-- Resize terminal
	if vim.g.is_term_open == 1 then
		vim.cmd("wincmd j")
		vim.cmd("resize " .. vim.g.terminal_height)
		vim.cmd("wincmd k")
	end
end

num = vim.fn.system("od -A n -t d -N 3 /dev/random")

num = num:gsub("%s+", "")
num = num:gsub("%s+", "")

function _G.prepare_pr_review_curr_buf()
	local buf_name = vim.fn.bufname()

	local buf_name_parts = split(buf_name, "/")

	local buf_file = buf_name_parts[#buf_name_parts]
	local side = buf_name_parts[#buf_name_parts - 1]

	local tmp_file = "/tmp/nvim-pr-review-" ..  num .. "-"
		.. side .. "-" .. buf_file

	if file_exists(tmp_file) then
		return
	end

	local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)

	local file = io.open(tmp_file, "a")

	if file == nil then
		error("Temporary file not created")
		return
	end

	for _, line in next, lines, nil do
		file:write(line)
	end

	file:close()

	vim.api.nvim_buf_set_name(0, tmp_file)
end

function _G.unprepare_pr_review_curr_buf()
	local buf_name = vim.fn.bufname()

	local buf_name_parts = split(buf_name, "/")

	local buf_file = buf_name_parts[#buf_name_parts]
	local side = buf_name_parts[#buf_name_parts - 1]

	local tmp_file = "/tmp/nvim-pr-review-" ..  num .. "-"
		.. side .. "-" .. buf_file

	if file_exists(tmp_file) == false then
		return
	end

	os.remove(tmp_file)
end