@@ -12,120 +12,117 @@ end --]]
12
12
13
13
-- Create file modifiers
14
14
local function filename_modifiers (path , modifiers )
15
- if path == " %%" then
16
- return path .. modifiers
17
- end
18
- return vim .fn .fnamemodify (path , modifiers )
15
+ if path == " %%" then
16
+ return path .. modifiers
17
+ end
18
+ return vim .fn .fnamemodify (path , modifiers )
19
19
end
20
20
21
-
22
21
-- Replace json variables with vim variables in command.
23
22
-- If a command has no arguments, one is added with the current file path
24
- --[[ local dir, fileName, ext = split_filename(path)
23
+ --[[ local dir, fileName, ext = split_filename(path)
25
24
local vars_json = {
26
25
["%$fileNameWithoutExt"] = string.gsub(path, "." .. ext, ""),
27
26
["$fileName"] = fileName,
28
27
["$file"] = path,
29
28
["$dir"] = dir
30
29
} --]]
31
30
local function re_jsonvar_with_vimvar (command , path )
32
- local no_sub_command = command
33
- local vars_json = {
34
- [" %$fileNameWithoutExt" ] = filename_modifiers (path , " :t:r" ),
35
- [" $fileName" ] = filename_modifiers (path , " :t" ),
36
- [" $file" ] = path ,
37
- [" $dir" ] = filename_modifiers (path , " :p:h" )
38
- }
39
- for var , var_vim in pairs (vars_json ) do
40
- command = command :gsub (var , var_vim )
41
- end
42
- if command == no_sub_command then
43
- if path == " %%" then path = " %" end
44
- command = command .. " " .. path
45
- end
46
- return command
31
+ local no_sub_command = command
32
+ local vars_json = {
33
+ [" %$fileNameWithoutExt" ] = filename_modifiers (path , " :t:r" ),
34
+ [" $fileName" ] = filename_modifiers (path , " :t" ),
35
+ [" $file" ] = path ,
36
+ [" $dir" ] = filename_modifiers (path , " :p:h" ),
37
+ }
38
+ for var , var_vim in pairs (vars_json ) do
39
+ command = command :gsub (var , var_vim )
40
+ end
41
+ if command == no_sub_command then
42
+ if path == " %%" then
43
+ path = " %"
44
+ end
45
+ command = command .. " " .. path
46
+ end
47
+ return command
47
48
end
48
49
49
-
50
50
-- Check if current buffer is in project
51
51
-- if a project return table of project
52
52
local function get_project_rootpath ()
53
- local path = " %:p:~:h"
54
- local expand = " "
55
- while expand ~= " ~" do
56
- expand = vim .fn .expand (path )
57
- local project = vim .g .projectManager [expand ]
58
- if project then
59
- project [" path" ] = expand
60
- return project
61
- end
62
- path = path .. " :h"
63
- end
64
- return nil
53
+ local path = " %:p:~:h"
54
+ local expand = " "
55
+ while expand ~= " ~" do
56
+ expand = vim .fn .expand (path )
57
+ local project = vim .g .projectManager [expand ]
58
+ if project then
59
+ project [" path" ] = expand
60
+ return project
61
+ end
62
+ path = path .. " :h"
63
+ end
64
+ return nil
65
65
end
66
66
67
-
68
67
-- Return a command for filetype
69
68
local function get_command (filetype , path )
70
- local nvim_files = {
71
- lua = " luafile %" ,
72
- vim = " source %"
73
- }
74
- path = path or " %%"
75
- local command = vim .g .fileCommands [filetype ]
76
- if command then
77
- local command_vim = re_jsonvar_with_vimvar (command , path )
78
- return prefix .. command_vim
79
- end
80
- return nvim_files [filetype ]
69
+ local nvim_files = {
70
+ lua = " luafile %" ,
71
+ vim = " source %" ,
72
+ }
73
+ path = path or " %%"
74
+ local command = vim .g .fileCommands [filetype ]
75
+ if command then
76
+ local command_vim = re_jsonvar_with_vimvar (command , path )
77
+ return prefix .. command_vim
78
+ end
79
+ return nvim_files [filetype ]
81
80
end
82
81
83
-
84
82
-- Run command in project context
85
83
local function run_project (context )
86
- local command = " "
87
- if context .file_name then
88
- local file = context .path .. " /" .. context .file_name
89
- if context .command then
90
- command = prefix .. re_jsonvar_with_vimvar (context .command , file )
91
- else
92
- command = get_command (context .filetype , file )
93
- end
94
- else
95
- command = prefix .. " cd " .. context .path .. context .command
96
- end
97
- vim .cmd (command )
84
+ local command = " "
85
+ if context .file_name then
86
+ local file = context .path .. " /" .. context .file_name
87
+ if context .command then
88
+ command = prefix .. re_jsonvar_with_vimvar (context .command , file )
89
+ else
90
+ command = get_command (context .filetype , file )
91
+ end
92
+ else
93
+ command = prefix .. " cd " .. context .path .. context .command
94
+ end
95
+ vim .cmd (command )
98
96
end
99
97
100
-
101
98
local M = {}
102
99
103
100
-- Execute filetype or project
104
101
function M .run ()
105
- local is_a_project = M .run_project ()
106
- if not is_a_project then
107
- M .run_filetype ()
108
- end
102
+ local is_a_project = M .run_project ()
103
+ if not is_a_project then
104
+ M .run_filetype ()
105
+ end
109
106
end
110
107
111
108
-- Execute filetype
112
109
function M .run_filetype ()
113
- local filetype = vim .bo .filetype
114
- local command = get_command (filetype ) or " "
115
- vim .cmd (command )
110
+ local filetype = vim .bo .filetype
111
+ local command = get_command (filetype ) or " "
112
+ vim .cmd (command )
116
113
end
117
114
118
115
-- Execute project
119
116
function M .run_project ()
120
- local context = nil
121
- if vim .g .projectManager then
122
- context = get_project_rootpath ()
123
- end
124
- if context then
125
- run_project (context )
126
- return true
127
- end
128
- return false
117
+ local context = nil
118
+ if vim .g .projectManager then
119
+ context = get_project_rootpath ()
120
+ end
121
+ if context then
122
+ run_project (context )
123
+ return true
124
+ end
125
+ return false
129
126
end
130
127
131
128
return M
0 commit comments