Skip to content

Commit 7bb9016

Browse files
committed
consistent stylua formatting
1 parent 197cb43 commit 7bb9016

File tree

5 files changed

+127
-112
lines changed

5 files changed

+127
-112
lines changed

.stylua.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
column_width = 120
2+
line_endings = "Unix"
3+
indent_type = "Spaces"
4+
indent_width = 2
5+
quote_style = "AutoPreferDouble"
6+
no_call_parentheses = false

lua/code_runner.lua

+19-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ local M = {}
55
local loadTable = require("code_runner.load_json")
66
M.setup = function(user_options)
77
o.set(user_options)
8-
vim.cmd [[lua require('code_runner').load_json_files()]]
9-
vim.api.nvim_exec([[
8+
vim.cmd([[lua require('code_runner').load_json_files()]])
9+
vim.api.nvim_exec(
10+
[[
1011
command! CRFiletype lua require('code_runner').open_filetype_suported()
1112
command! CRProjects lua require('code_runner').open_project_manager()
1213
command! RunCode lua require('code_runner').run_code()
1314
command! RunFile lua require('code_runner').run_filetype()
1415
command! RunProject lua require('code_runner').run_project()
15-
]], false)
16+
]],
17+
false
18+
)
1619
if o.get().filetype.map == o.get().project_context.map then
17-
vim.api.nvim_set_keymap('n', o.get().filetype.map, ":RunCode<CR>", {noremap = true})
20+
vim.api.nvim_set_keymap("n", o.get().filetype.map, ":RunCode<CR>", { noremap = true })
1821
else
19-
vim.api.nvim_set_keymap('n', o.get().filetype.map, ":RunFile<CR>", {noremap = true})
20-
vim.api.nvim_set_keymap('n', o.get().project_context.map, ":RunProject<CR>", {noremap = true})
22+
vim.api.nvim_set_keymap("n", o.get().filetype.map, ":RunFile<CR>", { noremap = true })
23+
vim.api.nvim_set_keymap("n", o.get().project_context.map, ":RunProject<CR>", { noremap = true })
2124
end
2225
end
2326

@@ -31,12 +34,18 @@ M.load_json_files = function()
3134
end
3235
end
3336

34-
M.run_code = function() commands.run() end
35-
M.run_filetype = function() commands.run_filetype() end
36-
M.run_project = function() commands.run_project() end
37+
M.run_code = function()
38+
commands.run()
39+
end
40+
M.run_filetype = function()
41+
commands.run_filetype()
42+
end
43+
M.run_project = function()
44+
commands.run_project()
45+
end
3746

3847
local function open_json(json_path)
39-
local command ="tabnew " .. json_path
48+
local command = "tabnew " .. json_path
4049
vim.cmd(command)
4150
end
4251

lua/code_runner/commands.lua

+73-76
Original file line numberDiff line numberDiff line change
@@ -12,120 +12,117 @@ end --]]
1212

1313
-- Create file modifiers
1414
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)
1919
end
2020

21-
2221
-- Replace json variables with vim variables in command.
2322
-- 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)
2524
local vars_json = {
2625
["%$fileNameWithoutExt"] = string.gsub(path, "." .. ext, ""),
2726
["$fileName"] = fileName,
2827
["$file"] = path,
2928
["$dir"] = dir
3029
} --]]
3130
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
4748
end
4849

49-
5050
-- Check if current buffer is in project
5151
-- if a project return table of project
5252
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
6565
end
6666

67-
6867
-- Return a command for filetype
6968
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]
8180
end
8281

83-
8482
-- Run command in project context
8583
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)
9896
end
9997

100-
10198
local M = {}
10299

103100
-- Execute filetype or project
104101
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
109106
end
110107

111108
-- Execute filetype
112109
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)
116113
end
117114

118115
-- Execute project
119116
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
129126
end
130127

131128
return M

lua/code_runner/load_json.lua

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
function LoadTable(json_path)
2-
local contents = ""
3-
local file = io.open( json_path, "r" )
2+
local contents = ""
3+
local file = io.open(json_path, "r")
44

5-
if file then
6-
-- read all contents of file into a string
7-
contents = file:read( "*a" )
8-
local status, result = pcall(vim.fn.json_decode, contents)
9-
io.close( file )
10-
if status then
11-
return result
12-
else
13-
return nil
14-
end
5+
if file then
6+
-- read all contents of file into a string
7+
contents = file:read("*a")
8+
local status, result = pcall(vim.fn.json_decode, contents)
9+
io.close(file)
10+
if status then
11+
return result
12+
else
13+
return nil
1514
end
16-
return nil
15+
end
16+
return nil
1717
end
1818

1919
return LoadTable

lua/code_runner/options.lua

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
local options = {
2-
term = {
3-
position = "belowright",
4-
size = 8,
5-
},
6-
filetype = {
7-
map = "<leader>r",
8-
json_path = vim.fn.stdpath("data") .. '/site/pack/packer/start/code_runner.nvim/lua/code_runner/code_runner.json',
9-
},
10-
project_context = {
11-
map = "<leader>r",
12-
json_path = vim.fn.stdpath("data") .. '/site/pack/packer/start/code_runner.nvim/lua/code_runner/project_manager.json'
13-
}
2+
term = {
3+
position = "belowright",
4+
size = 8,
5+
},
6+
filetype = {
7+
map = "<leader>r",
8+
json_path = vim.fn.stdpath("data") .. "/site/pack/packer/start/code_runner.nvim/lua/code_runner/code_runner.json",
9+
},
10+
project_context = {
11+
map = "<leader>r",
12+
json_path = vim.fn.stdpath("data")
13+
.. "/site/pack/packer/start/code_runner.nvim/lua/code_runner/project_manager.json",
14+
},
1415
}
1516

1617
local M = {}
@@ -23,6 +24,8 @@ M.set = function(user_options)
2324
end
2425

2526
-- get user options
26-
M.get = function() return options end
27+
M.get = function()
28+
return options
29+
end
2730

2831
return M

0 commit comments

Comments
 (0)