1
1
local M = {}
2
+ local H = {}
2
3
local uv = vim .uv or vim .loop
3
4
local gitpad_win_id = nil
4
5
5
6
M .config = {
6
7
border = ' single' ,
7
- dir = vim .fn .stdpath (' data' ) .. ' /gitpad' ,
8
+ dir = vim .fs . normalize ( vim . fn .stdpath (' data' ) .. ' /gitpad' ) ,
8
9
style = ' ' ,
9
10
default_text = nil ,
10
11
on_attach = nil ,
11
12
}
12
13
13
- local function is_git_dir ()
14
+ function H . is_git_dir ()
14
15
if vim .fn .system (' git rev-parse --is-inside-work-tree 2>/dev/null' ) ~= ' ' then
15
16
return true
16
17
end
@@ -24,17 +25,34 @@ local function is_git_dir()
24
25
return false
25
26
end
26
27
27
- function M .init_gitpad_file (params )
28
- local is_branch = params .is_branch or false
28
+ function H .get_branch_filename ()
29
+ -- local branch_name = vim.fn.systemlist('basename `git rev-parse --abbrev-ref HEAD`')[1]
30
+ local branch_name = vim .fn .systemlist (' git branch --show-current' )[1 ]
31
+ return H .clean_filename (branch_name )
32
+ end
33
+
34
+ function H .clean_filename (filename )
35
+ if filename == nil then
36
+ return nil
37
+ end
38
+
39
+ -- remove any spaces in the branch name and replace with a hyphen
40
+ -- replace any forward slashes with a colon so that the file is not created in a subdirectory
41
+ filename = filename :gsub (' %s+' , ' -' ):gsub (' /' , ' :' )
42
+ return filename
43
+ end
44
+
45
+ function M .init_gitpad_file (opts )
46
+ local clean_name = H .clean_filename (opts .filename ) or nil
29
47
30
48
-- get the git repository name of the current directory
31
- if not is_git_dir () then
49
+ if not H . is_git_dir () then
32
50
return
33
51
end
34
52
35
53
-- create the repository directory if it doesn't exist
36
54
local repository_name = vim .fn .systemlist (' basename `git rev-parse --show-toplevel`' )[1 ]
37
- local notes_dir = vim .fs .normalize (M .config .dir ) .. ' /' .. repository_name
55
+ local notes_dir = vim .fs .normalize (M .config .dir .. ' /' .. repository_name )
38
56
39
57
-- create the notes directory if it doesn't exist
40
58
if not uv .fs_stat (notes_dir ) then
@@ -44,18 +62,15 @@ function M.init_gitpad_file(params)
44
62
local gitpad_file_path
45
63
local gitpad_default_text
46
64
47
- if is_branch then
48
- -- local branch_name = vim.fn.systemlist('basename `git rev-parse --abbrev-ref HEAD`')[1]
49
- local branch_name = vim .fn .systemlist (' git branch --show-current' )[1 ]
50
-
51
- -- remove any spaces in the branch name and replace with a hyphen
52
- local filename = branch_name :gsub (' %s+' , ' -' )
65
+ if clean_name ~= nil then
66
+ gitpad_default_text = ' # ' .. clean_name .. ' \n\n This is your new gitpad file.\n '
53
67
54
- -- replace any forward slashes with a colon so that the file is not created in a subdirectory
55
- filename = branch_name :gsub (' /' , ' :' )
68
+ if clean_name == H .get_branch_filename () then
69
+ clean_name = clean_name .. ' -branchpad.md'
70
+ gitpad_default_text = ' # ' .. clean_name .. ' Branchpad\n\n This is your gitpad branch file.\n '
71
+ end
56
72
57
- gitpad_file_path = notes_dir .. ' /' .. vim .fn .fnameescape (filename ) .. ' -branchpad.md'
58
- gitpad_default_text = ' # ' .. branch_name .. ' Branchpad\n\n This is your Gitpad Branch file.\n '
73
+ gitpad_file_path = notes_dir .. ' /' .. vim .fn .fnameescape (clean_name )
59
74
else
60
75
gitpad_file_path = notes_dir .. ' /gitpad.md'
61
76
gitpad_default_text = ' # Gitpad\n\n This is your Gitpad file.\n '
@@ -84,7 +99,7 @@ function M.init_gitpad_file(params)
84
99
return gitpad_file_path
85
100
end
86
101
87
- function M .close_window (params )
102
+ function M .close_window (opts )
88
103
local wininfo = vim .fn .getwininfo (gitpad_win_id )
89
104
90
105
-- We might have closed the window not via this method so we need to
@@ -99,7 +114,7 @@ function M.close_window(params)
99
114
100
115
-- Just ensure that we are closing the correct window
101
116
-- This is just to prevent closing a gitpad project window or gitpad branch window
102
- if bufname == params .path then
117
+ if bufname == opts .path then
103
118
vim .api .nvim_win_close (gitpad_win_id , true )
104
119
gitpad_win_id = nil
105
120
return true
@@ -108,13 +123,9 @@ function M.close_window(params)
108
123
return false
109
124
end
110
125
111
- function M .open_window (params )
112
- local path = params .path
113
- local is_branch = params .is_branch or false
126
+ function M .open_window (opts )
127
+ local path = opts .path
114
128
local title = ' gitpad '
115
- if is_branch then
116
- title = ' gitpad:branch '
117
- end
118
129
119
130
local ui = vim .api .nvim_list_uis ()[1 ]
120
131
local width = math.floor ((ui .width * 0.5 ) + 0.5 )
@@ -171,21 +182,20 @@ function M.open_window(params)
171
182
end
172
183
end
173
184
174
- function M .toggle_window (params )
175
- if not M .close_window (params ) then
176
- M .open_window (params )
185
+ function M .toggle_window (opts )
186
+ if not M .close_window (opts ) then
187
+ M .open_window (opts )
177
188
end
178
189
end
179
190
180
191
function M .toggle_gitpad (opts )
181
192
opts = opts or {}
182
- opts .is_branch = opts .is_branch or false
183
- local path = M .init_gitpad_file { is_branch = opts .is_branch }
184
- M .toggle_window { is_branch = opts .is_branch , path = path }
193
+ local path = M .init_gitpad_file (opts )
194
+ M .toggle_window { path = path }
185
195
end
186
196
187
197
function M .toggle_gitpad_branch ()
188
- M .toggle_gitpad { is_branch = true }
198
+ M .toggle_gitpad { filename = H . get_branch_filename () }
189
199
end
190
200
191
201
M .setup = function (opts )
0 commit comments