]> git.madduck.net Git - etc/vim.git/blob - lua/ale/init.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / lua / ale / init.lua
1 local ale = {}
2
3 local global_settings = setmetatable({}, {
4     __index = function(_, key)
5         return vim.g['ale_' .. key]
6     end,
7     __newindex = function(_, key, value)
8         vim.g['ale_' .. key] = value
9     end
10 })
11
12 local buffer_settings = setmetatable({}, {
13     __index = function(_, key)
14         return vim.b['ale_' .. key]
15     end,
16     __newindex = function(_, key, value)
17         vim.b['ale_' .. key] = value
18     end
19 })
20
21 ale.set_global = function(c)
22     for key, value in pairs(c) do
23         global_settings[key] = value
24     end
25 end
26
27 ale.set_buffer = function(c)
28     for key, value in pairs(c) do
29         buffer_settings[key] = value
30     end
31 end
32
33 ---(when called) Set global ALE settings, just like ale.setup.global.
34 ---@class ALESetup
35 ---@field global fun(c: table): nil  -- Set global ALE settings.
36 ---@field buffer fun(c: table): nil  -- Set buffer-local ALE settings.
37 ---@overload fun(c: table): nil
38 ---@type ALESetup
39 ale.setup = setmetatable({
40     ---Set global ALE settings.
41     ---@param c table The table of ALE settings to set.
42     ---@return nil
43     global = function(c)
44         ale.set_global(c)
45     end,
46     ---Set buffer-local ALE settings.
47     ---@param c table The table of ALE settings to set.
48     ---@return nil
49     buffer = function(c)
50         ale.set_buffer(c)
51     end,
52 }, {
53     __call = function(self, c)
54         self.global(c)
55     end,
56 })
57
58 ---Run ALE linters on a buffer after a delay.
59 ---
60 ---If a delay in milliseconds multiple times, the internal timer used by ALE
61 ---will be reset, so ALE doesn't lint too often.
62 ---
63 ---If the `linting_flag` is not 'lint_file' then linters that require files to
64 ---be saved will no be run.
65 ---@param delay number Milliseconds to wait for. A delay of 0 lints immediately.
66 ---@param linting_flag string|nil If set to 'lint_file', run all linters.
67 ---@param buffer number|nil The buffer to check. Defaults to the current buffer.
68 ---@return nil
69 ale.queue = function(delay, linting_flag, buffer)
70     vim.fn["ale#Queue"](delay, linting_flag, buffer)
71 end
72
73 ---Check if ALE supports a given feature.
74 ---
75 ---The ALE version can be checked with ale.has("ale-1.0.0"), etc.
76 ---@param feature string The feature to test for.
77 ---@return boolean supported If the feature is supported.
78 ale.has = function(feature)
79     return vim.fn["ale#Has"](feature) == 1
80 end
81
82 ---Prefix a string with a single space if it is not empty.
83 ---nil will be treated the same as an empty string.
84 ---
85 ---This function is a convenience for chaining options for commands together
86 ---without adding redundant whitespace.
87 ---@param str string|nil A value to pad with whitespace.
88 ---@return string padded A value padded with whitespace.
89 ale.pad = function(str)
90     if str == nil or str == "" then
91         return ""
92     end
93
94     return " " .. str
95 end
96
97 ---Get an ALE variable for a buffer (first) or globally (second)
98 ---@param buffer number The buffer number to retreive the variable for.
99 ---@param variable_name string The variable to retrieve.
100 ---@return any value The value for the ALE variable
101 ale.var = function(buffer, variable_name)
102     variable_name = "ale_" .. variable_name
103     local exists, value = pcall(vim.api.nvim_buf_get_var, buffer, variable_name)
104
105     if exists then
106         return value
107     end
108
109     return vim.g[variable_name]
110 end
111
112 ---Escape a string for use in a shell command
113 ---@param str string The string to escape.
114 ---@return string escaped The escaped string.
115 ale.escape = function(str)
116     local shell = vim.fn.fnamemodify(vim.o.shell, ":t")
117
118     if shell:lower() == "cmd.exe" then
119         local step1
120
121         if str:find(" ") then
122             step1 = '"' .. str:gsub('"', '""') .. '"'
123         else
124             step1 = str:gsub("([&|<>^])", "^%1")
125         end
126
127         local percent_subbed = step1:gsub("%%", "%%%%")
128
129         return percent_subbed
130     end
131
132     return vim.fn.shellescape(str)
133 end
134
135 ---Create a prefix for a shell command for adding environment variables.
136 ---@param variable_name string The environment variable name.
137 ---@param value string The value to set for the environment variable.
138 ---@return string prefix The shell code for prefixing a command.
139 ale.env = function(variable_name, value)
140     if vim.fn.has("win32") then
141         return "set " .. ale.escape(variable_name .. "=" .. value) .. " && "
142     end
143
144     return variable_name .. "=" .. ale.escape(value) .. " "
145 end
146
147 ---Get an array of arrays for mapping paths to and from filesystems for an ALE
148 ---linter, as configured in the `filename_mappings` setting.
149 ---
150 ---The result can be used to instruct ALE how to map between filesystems.
151 ---@param buffer number The buffer number.
152 ---@param name string The linter name.
153 ---@return table mappings An array of arrays for mapping filenames.
154 ale.get_filename_mappings = function(buffer, name)
155     local linter_mappings = ale.var(buffer, "filename_mappings")
156
157     if linter_mappings[1] ~= nil then
158         return linter_mappings
159     end
160
161     if linter_mappings[name] == nil then
162         name = "*"
163     end
164
165     return linter_mappings[name] or {}
166 end
167
168 return ale