summaryrefslogtreecommitdiff
path: root/script/provider/formatting.lua
blob: 73f9a534f092f7e61706e3bf0944882db41f3b99 (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
local suc, codeFormat = pcall(require, 'code_format')
if not suc then
    return
end

local ws          = require 'workspace'
local furi        = require 'file-uri'
local fs          = require 'bee.filesystem'
local fw          = require 'filewatch'
local util        = require 'utility'
local diagnostics = require 'provider.diagnostic'
local config      = require 'config'

local loadedUris = {}

local updateType = {
    Created = 1,
    Changed = 2,
    Deleted = 3,
}

fw.event(function(ev, path)
    if util.stringEndWith(path, '.editorconfig') then
        for uri, fsPath in pairs(loadedUris) do
            loadedUris[uri] = nil
            if fsPath ~= true then
                local status, err = codeFormat.update_config(updateType.Deleted, uri, fsPath:string())
                if not status and err then
                    log.error(err)
                end
            end
        end
    end
end)


local m = {}

m.loadedDefaultConfig = false

---@param uri uri
function m.updateConfig(uri)
    if not m.loadedDefaultConfig then
        m.loadedDefaultConfig = true
        codeFormat.set_default_config(config.get(uri, 'Lua.format.defaultConfig'))
        m.updateNonStandardSymbols(config.get(nil, 'Lua.runtime.nonstandardSymbol'))
    end

    local currentUri = uri
    while true do
        currentUri = currentUri:match('^(.+)/[^/]*$')
        if not currentUri or loadedUris[currentUri] then
            return
        end
        loadedUris[currentUri] = true

        local currentPath        = furi.decode(currentUri)
        local editorConfigFSPath = fs.path(currentPath) / '.editorconfig'
        if fs.exists(editorConfigFSPath) then
            loadedUris[currentUri] = editorConfigFSPath
            local status, err = codeFormat.update_config(updateType.Created, currentUri, editorConfigFSPath:string())
            if not status and err then
                log.error(err)
            end
        end

        if not ws.rootUri then
            return
        end

        for _, scp in ipairs(ws.folders) do
            if scp.uri == currentUri then
                return
            end
        end
    end
end

---@param symbols? string[]
function m.updateNonStandardSymbols(symbols)
    if symbols == nil then
        return
    end

    local eqTokens = {}
    for _, token in ipairs(symbols) do
        if token:find("=") and token ~= "!=" then
            table.insert(eqTokens, token)
        end
    end

    if #eqTokens ~= 0 then
        codeFormat.set_nonstandard_symbol()
    end
end

config.watch(function(uri, key, value)
    if  key == "Lua.format.defaultConfig" then
        codeFormat.set_default_config(value)
    elseif key == "Lua.runtime.nonstandardSymbol" then
        m.updateNonStandardSymbols(value)
    end
end)

return m