/
config.lua
127 lines (115 loc) · 3.07 KB
/
config.lua
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
local path = require("flutter-tools.utils.path")
local M = {}
local fn = vim.fn
local fmt = string.format
--- @param prefs table user preferences
local function validate_prefs(prefs)
if prefs.flutter_path and prefs.flutter_lookup_cmd then
vim.schedule(function()
vim.notify(
'Only one of "flutter_path" and "flutter_lookup_cmd" are required. Please remove one of the keys',
vim.log.levels.ERROR
)
end)
end
vim.validate({
outline = { prefs.outline, "table", true },
dev_log = { prefs.dev_log, "table", true },
closing_tags = { prefs.closing_tags, "table", true },
})
end
---Create a proportional split using a percentage specified as a float
---@param percentage number
---@param fallback number
---@return string
local function get_split_cmd(percentage, fallback)
return string.format("botright %dvnew", math.max(vim.o.columns * percentage, fallback))
end
local function get_default_lookup()
local exepath = fn.exepath("flutter")
local is_snap_installation = exepath and exepath:match("snap") or false
return (path.is_linux and is_snap_installation) and "flutter sdk-path" or nil
end
M.debug_levels = {
DEBUG = 1,
WARN = 2,
}
local defaults = {
flutter_path = nil,
flutter_lookup_cmd = get_default_lookup(),
widget_guides = {
enabled = false,
debug = false,
},
ui = {
border = "rounded",
},
debugger = {
enabled = false,
},
closing_tags = {
highlight = "Comment",
prefix = "// ",
enabled = true,
},
lsp = {
debug = M.debug_levels.WARN,
},
outline = setmetatable({}, {
__index = function(_, k)
return k == "open_cmd" and get_split_cmd(0.3, 40) or nil
end,
}),
dev_log = setmetatable({}, {
__index = function(_, k)
return k == "open_cmd" and get_split_cmd(0.4, 50) or nil
end,
}),
dev_tools = {
autostart = false,
},
}
local deprecations = {
flutter_outline = {
fallback = "widget_guides",
message = "please use 'widget_guides' instead",
},
}
local function handle_deprecation(key, value, config)
local echomsg = require("flutter-tools.utils").echomsg
local deprecation = deprecations[key]
if not deprecation then
return
end
vim.defer_fn(function()
echomsg(fmt("%s is deprecated: %s", key, deprecation.message), "WarningMsg")
end, 1000)
if deprecation.fallback then
config[deprecation.fallback] = value
end
end
local config = setmetatable({}, { __index = defaults })
---Get the configuration or just a key of the config
---@param key string
---@return table|number|boolean
function M.get(key)
if key then
return config[key]
end
return config
end
function M.set(user_config)
if not user_config or type(user_config) ~= "table" then
return config
end
validate_prefs(user_config)
for key, value in pairs(user_config) do
handle_deprecation(key, value, user_config)
if user_config[key] and type(user_config[key]) == "table" then
setmetatable(user_config[key], { __index = defaults[key] })
end
end
config = setmetatable(user_config, { __index = defaults })
return config
end
return M