@@ -2,132 +2,72 @@ local Cache = require("lazy.core.cache")
2
2
3
3
local M = {}
4
4
5
- --- @type table<string , { file : string , hash ?: string } >
6
- M .modules = {}
5
+ --- @type table<string , string>
6
+ M .hashes = {}
7
7
8
- function M .add (modname , file )
9
- if not M .modules [modname ] then
10
- M .modules [modname ] = { file = file }
11
- end
8
+ function M .is_dirty (modname , modpath )
9
+ return not (Cache .get (modname ) and M .hashes [modname ] and M .hashes [modname ] == Cache .hash (modpath ))
12
10
end
13
11
14
12
--- @param modname string
15
- function M .load (modname )
16
- if type (package.loaded [modname ]) == " table" then
17
- return package.loaded [modname ]
18
- end
19
-
20
- local info = M .modules [modname ]
21
- if info then
22
- local err
23
- --- @type string | fun ()| nil
24
- local chunk = Cache .get (modname )
25
-
26
- if not chunk then
27
- vim .schedule (function ()
28
- vim .notify (" loading " .. modname )
29
- end )
30
- chunk , err = loadfile (info .file )
31
- if chunk then
32
- Cache .set (modname , string.dump (chunk ))
33
- info .hash = info .hash or Cache .hash (info .file )
34
- end
13
+ --- @param modpath string
14
+ --- @return table
15
+ function M .load (modname , modpath )
16
+ local err
17
+ --- @type (string | fun ())?
18
+ local chunk = Cache .get (modname )
19
+
20
+ if chunk then
21
+ local hash = Cache .hash (modpath )
22
+ if hash ~= M .hashes [modname ] then
23
+ M .hashes [modname ] = hash
24
+ chunk = nil
35
25
end
36
-
37
- if type (chunk ) == " string" then
38
- chunk , err = loadstring (chunk --[[ @as string]] , " @" .. info .file )
39
- end
40
-
41
- if not chunk then
42
- error (err )
43
- end
44
-
45
- --- @type table
46
- local mod = chunk ()
47
- package.loaded [modname ] = mod
48
- return mod
49
26
end
50
- end
51
27
52
- local function _add_module (dir , modname )
53
- local d = vim .loop .fs_opendir (dir , nil , 100 )
54
- if d then
55
- --- @type { name : string , type : " file" | " directory" | " link" } []
56
- local entries = vim .loop .fs_readdir (d )
57
- while entries do
58
- for _ , entry in ipairs (entries ) do
59
- local path = dir .. " /" .. entry .name
60
- if entry .type == " directory" then
61
- _add_module (path , modname and (modname .. " ." .. entry .name ) or entry .name )
62
- else
63
- local childname = entry .name :match (" ^(.*)%.lua$" )
64
- if childname then
65
- local child = entry .name == " init.lua" and modname or modname and (modname .. " ." .. childname ) or childname
66
- if child then
67
- M .add (child , path )
68
- end
69
- end
70
- end
71
- end
72
- entries = vim .loop .fs_readdir (d )
28
+ if chunk then
29
+ chunk , err = loadstring (chunk --[[ @as string]] , " @" .. modpath )
30
+ else
31
+ vim .schedule (function ()
32
+ vim .notify (" loadfile(" .. modname .. " )" )
33
+ end )
34
+ chunk , err = loadfile (modpath )
35
+ if chunk then
36
+ Cache .set (modname , string.dump (chunk ))
37
+ M .hashes [modname ] = M .hashes [modname ] or Cache .hash (modpath )
73
38
end
74
- vim .loop .fs_closedir (d )
75
39
end
76
- end
77
40
78
- function M .add_module (path )
79
- if path :find (" /lua/?$" ) then
80
- return _add_module (path )
81
- end
82
- --- @type string
83
- local modname = path :match (" /lua/(.*)/?" )
84
- assert (modname )
85
- modname = modname :gsub (" /" , " ." )
86
- if vim .loop .fs_stat (path .. " .lua" ) then
87
- M .add (modname , path .. " .lua" )
41
+ if chunk then
42
+ --- @diagnostic disable-next-line : no-unknown
43
+ package.loaded [modname ] = chunk ()
44
+ return package.loaded [modname ]
45
+ else
46
+ error (err )
88
47
end
89
- _add_module (path , modname )
90
48
end
91
49
92
50
function M .setup ()
93
51
-- load cache
94
52
local value = Cache .get (" cache.modules" )
95
53
if value then
96
- M .modules = vim .json .decode (value )
97
- for k , v in pairs (M .modules ) do
98
- if Cache .hash (v .file ) ~= v .hash then
99
- Cache .del (k )
100
- M .changed = true
101
- M .modules [k ] = nil
102
- end
103
- end
54
+ M .hashes = vim .json .decode (value )
104
55
end
105
56
106
57
-- preload core modules
107
58
local root = vim .fn .fnamemodify (debug.getinfo (1 , " S" ).source :sub (2 ), " :p:h:h" )
108
59
for _ , name in ipairs ({ " util" , " config" , " loader" , " state" }) do
109
60
local modname = " lazy.core." .. name
110
- M .add (modname , root .. " /core/" .. name :gsub (" %." , " /" ) .. " .lua" )
111
- end
112
-
113
- table.insert (package.loaders , 2 , function (modname )
114
- if M .modules [modname ] then
115
- return function ()
116
- return M .load (modname )
117
- end
61
+ --- @diagnostic disable-next-line : no-unknown
62
+ package.preload [modname ] = function ()
63
+ return M .load (modname , root .. " /core/" .. name :gsub (" %." , " /" ) .. " .lua" )
118
64
end
119
- end )
65
+ end
120
66
return M
121
67
end
122
68
123
69
function M .save ()
124
- local value = {}
125
- for k , v in pairs (M .modules ) do
126
- if v .hash then
127
- value [k ] = v
128
- end
129
- end
130
- Cache .set (" cache.modules" , vim .json .encode (value ))
70
+ Cache .set (" cache.modules" , vim .json .encode (M .hashes ))
131
71
end
132
72
133
73
return M
0 commit comments