summaryrefslogtreecommitdiff
path: root/start/indent-blankline-2.20.4/lua/indent_blankline/utils.lua
blob: e21a5c9012a8983756d485fd1487aeaeea74ffd2 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
local M = {}

M.memo = setmetatable({
    put = function(cache, params, result)
        local node = cache
        for i = 1, #params do
            local param = vim.inspect(params[i])
            node.children = node.children or {}
            node.children[param] = node.children[param] or {}
            node = node.children[param]
        end
        node.result = result
    end,
    get = function(cache, params)
        local node = cache
        for i = 1, #params do
            local param = vim.inspect(params[i])
            node = node.children and node.children[param]
            if not node then
                return nil
            end
        end
        return node.result
    end,
}, {
    __call = function(memo, func)
        local cache = {}

        return function(...)
            local params = { ... }
            local result = memo.get(cache, params)
            if not result then
                result = { func(...) }
                memo.put(cache, params, result)
            end
            return unpack(result)
        end
    end,
})

M.error_handler = function(err, level)
    if err:match "Invalid buffer id.*" then
        return
    end
    if not pcall(require, "notify") then
        err = string.format("indent-blankline: %s", err)
    end
    vim.notify_once(err, level or vim.log.levels.DEBUG, {
        title = "indent-blankline",
    })
end

M.is_indent_blankline_enabled = M.memo(
    function(
        b_enabled,
        g_enabled,
        disable_with_nolist,
        opt_list,
        filetype,
        filetype_include,
        filetype_exclude,
        buftype,
        buftype_exclude,
        bufname_exclude,
        bufname
    )
        if b_enabled ~= nil then
            return b_enabled
        end
        if g_enabled ~= true then
            return false
        end
        if disable_with_nolist and not opt_list then
            return false
        end

        local plain = M._if(vim.fn.has "nvim-0.6.0" == 1, { plain = true }, true)
        local undotted_filetypes = vim.split(filetype, ".", plain)
        table.insert(undotted_filetypes, filetype)

        for _, ft in ipairs(filetype_exclude) do
            for _, undotted_filetype in ipairs(undotted_filetypes) do
                if undotted_filetype == ft then
                    return false
                end
            end
        end

        for _, bt in ipairs(buftype_exclude) do
            if bt == buftype then
                return false
            end
        end

        for _, bn in ipairs(bufname_exclude) do
            if vim.fn["matchstr"](bufname, bn) == bufname then
                return false
            end
        end

        if #filetype_include > 0 then
            for _, ft in ipairs(filetype_include) do
                if ft == filetype then
                    return true
                end
            end
            return false
        end

        return true
    end
)

M.clear_line_indent = function(buf, lnum)
    xpcall(vim.api.nvim_buf_clear_namespace, M.error_handler, buf, vim.g.indent_blankline_namespace, lnum - 1, lnum)
end

M.clear_buf_indent = function(buf)
    xpcall(vim.api.nvim_buf_clear_namespace, M.error_handler, buf, vim.g.indent_blankline_namespace, 0, -1)
end

M.get_from_list = function(list, i, default)
    if not list or #list == 0 then
        return default
    end
    return list[((i - 1) % #list) + 1]
end

M._if = function(bool, a, b)
    if bool then
        return a
    else
        return b
    end
end

M.find_indent = function(whitespace, only_whitespace, shiftwidth, strict_tabs, list_chars)
    local indent = 0
    local spaces = 0
    local tab_width
    local virtual_string = {}

    if whitespace then
        for ch in whitespace:gmatch "." do
            if ch == "\t" then
                if strict_tabs and indent == 0 and spaces ~= 0 then
                    return 0, false, {}
                end
                indent = indent + math.floor(spaces / shiftwidth) + 1
                spaces = 0
                -- replace dynamic-width tab with fixed-width string (ta..ab)
                tab_width = shiftwidth - table.maxn(virtual_string) % shiftwidth
                -- check if tab_char_end is set, see :help listchars
                if list_chars["tab_char_end"] then
                    if tab_width == 1 then
                        table.insert(virtual_string, list_chars["tab_char_end"])
                    else
                        table.insert(virtual_string, list_chars["tab_char_start"])
                        for _ = 1, (tab_width - 2) do
                            table.insert(virtual_string, list_chars["tab_char_fill"])
                        end
                        table.insert(virtual_string, list_chars["tab_char_end"])
                    end
                else
                    table.insert(virtual_string, list_chars["tab_char_start"])
                    for _ = 1, (tab_width - 1) do
                        table.insert(virtual_string, list_chars["tab_char_fill"])
                    end
                end
            else
                if strict_tabs and indent ~= 0 then
                    -- return early when no more tabs are found
                    return indent, true, virtual_string
                end
                if only_whitespace then
                    -- if the entire line is only whitespace use trail_char instead of lead_char
                    table.insert(virtual_string, list_chars["trail_char"])
                else
                    table.insert(virtual_string, list_chars["lead_char"])
                end
                spaces = spaces + 1
            end
        end
    end

    return indent + math.floor(spaces / shiftwidth), table.maxn(virtual_string) % shiftwidth ~= 0, virtual_string
end

M.get_current_context = function(type_patterns, use_treesitter_scope)
    local ts_utils_status, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
    if not ts_utils_status then
        vim.schedule_wrap(function()
            M.error_handler("nvim-treesitter not found. Context will not work", vim.log.levels.WARN)
        end)()
        return false
    end
    local locals = require "nvim-treesitter.locals"
    local cursor_node = ts_utils.get_node_at_cursor()

    if use_treesitter_scope then
        local current_scope = locals.containing_scope(cursor_node, 0)
        if not current_scope then
            return false
        end
        local node_start, _, node_end, _ = current_scope:range()
        if node_start == node_end then
            return false
        end
        return true, node_start + 1, node_end + 1, current_scope:type()
    end

    while cursor_node do
        local node_type = cursor_node:type()
        for _, rgx in ipairs(type_patterns) do
            if node_type:find(rgx) then
                local node_start, _, node_end, _ = cursor_node:range()
                if node_start ~= node_end then
                    return true, node_start + 1, node_end + 1, rgx
                end
            end
        end
        cursor_node = cursor_node:parent()
    end

    return false
end

M.reset_highlights = function()
    local whitespace_highlight = vim.fn.synIDtrans(vim.fn.hlID "Whitespace")
    local label_highlight = vim.fn.synIDtrans(vim.fn.hlID "Label")

    local whitespace_fg = {
        vim.fn.synIDattr(whitespace_highlight, "fg", "gui"),
        vim.fn.synIDattr(whitespace_highlight, "fg", "cterm"),
    }
    local label_fg = {
        vim.fn.synIDattr(label_highlight, "fg", "gui"),
        vim.fn.synIDattr(label_highlight, "fg", "cterm"),
    }

    for highlight_name, highlight in pairs {
        IndentBlanklineChar = whitespace_fg,
        IndentBlanklineSpaceChar = whitespace_fg,
        IndentBlanklineSpaceCharBlankline = whitespace_fg,
        IndentBlanklineContextChar = label_fg,
        IndentBlanklineContextStart = label_fg,
    } do
        local current_highlight = vim.fn.synIDtrans(vim.fn.hlID(highlight_name))
        if
            vim.fn.synIDattr(current_highlight, "fg") == ""
            and vim.fn.synIDattr(current_highlight, "bg") == ""
            and vim.fn.synIDattr(current_highlight, "sp") == ""
        then
            if highlight_name == "IndentBlanklineContextStart" then
                vim.cmd(
                    string.format(
                        "highlight %s guisp=%s gui=underline cterm=underline",
                        highlight_name,
                        M._if(highlight[1] == "", "NONE", highlight[1])
                    )
                )
            else
                vim.cmd(
                    string.format(
                        "highlight %s guifg=%s ctermfg=%s gui=nocombine cterm=nocombine",
                        highlight_name,
                        M._if(highlight[1] == "", "NONE", highlight[1]),
                        M._if(highlight[2] == "", "NONE", highlight[2])
                    )
                )
            end
        end
    end
end

M.first_not_nil = function(...)
    for _, value in pairs { ... } do -- luacheck: ignore
        return value
    end
end

M.get_variable = function(key)
    if vim.b[key] ~= nil then
        return vim.b[key]
    end
    if vim.t[key] ~= nil then
        return vim.t[key]
    end
    return vim.g[key]
end

M.merge_ranges = function(ranges)
    local merged_ranges = { { unpack(ranges[1]) } }

    for i = 2, #ranges do
        local current_end = merged_ranges[#merged_ranges][2]
        local next_start, next_end = unpack(ranges[i])
        if current_end >= next_start - 1 then
            if current_end < next_end then
                merged_ranges[#merged_ranges][2] = next_end
            end
        else
            table.insert(merged_ranges, { next_start, next_end })
        end
    end

    return merged_ranges
end

M.binary_search_ranges = function(ranges, target_range)
    local exact_match = false
    local idx_start = 1
    local idx_end = #ranges
    local idx_mid

    local range_start
    local target_start = target_range[1]

    while idx_start < idx_end do
        idx_mid = math.ceil((idx_start + idx_end) / 2)
        range_start = ranges[idx_mid][1]

        if range_start == target_start then
            exact_match = true
            break
        elseif range_start < target_start then
            idx_start = idx_mid -- it's important to make the low-end inclusive
        else
            idx_end = idx_mid - 1
        end
    end

    -- if we don't have an exact match, choose the smallest index
    if not exact_match then
        idx_mid = idx_start
    end

    return idx_mid
end

return M