summaryrefslogtreecommitdiff
path: root/lua/functions.lua
blob: 12eca3a1965b795a5de8ce44b4b797223ab3a53c (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
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

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