summaryrefslogtreecommitdiff
path: root/script/src/files/files.lua
blob: 88ff7444533e2c833716009eca6cf352b598a995 (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
local file = require 'files.file'

---@class files
local mt = {}
mt.__index = mt
mt.type = 'files'
mt._fileCount = 0
---@type table<uri, file>
mt._files = nil

---@param uri uri
---@param text string
function mt:save(uri, text, version)
    local f = self._files[uri]
    if not f then
        f = file(uri)
        self._files[uri] = f
        self._fileCount = self._fileCount + 1
    end
    f:setText(text)
    f:setVersion(version)
end

---@param uri uri
function mt:remove(uri)
    local f = self._files[uri]
    if not f then
        return
    end

    f:remove()
    self._files[uri] = nil
    self._fileCount = self._fileCount - 1
end

---@param uri uri
function mt:open(uri, text)
    self._open[uri] = text
end

---@param uri uri
function mt:close(uri)
    self._open[uri] = nil
end

---@param uri uri
---@return boolean
function mt:isOpen(uri)
    return self._open[uri] ~= nil
end

---@param uri uri
function mt:setLibrary(uri)
    self._library[uri] = true
end

---@param uri uri
---@return uri
function mt:isLibrary(uri)
    return self._library[uri] == true
end

---@param uri uri
function mt:isDead(uri)
    local f = self._files[uri]
    if not f then
        return true
    end
    if f:isRemoved() then
        return true
    end
    return f:getVersion() == -1
end

---@param uri uri
---@return file
function mt:get(uri)
    return self._files[uri]
end

function mt:clear()
    for _, f in pairs(self._files) do
        f:remove()
    end
    self._files = {}
    self._library = {}
    self._fileCount = nil
end

function mt:clearVM()
    for _, f in pairs(self._files) do
        f:removeVM()
    end
end

function mt:eachFile()
    return pairs(self._files)
end

function mt:eachOpened()
    return pairs(self._open)
end

function mt:count()
    return self._fileCount
end

return function ()
    local self = setmetatable({
        _files = {},
        _open = {},
        _library = {},
    }, mt)
    return self
end