-
Notifications
You must be signed in to change notification settings - Fork 148
/
refreshproject.lua
56 lines (46 loc) · 1.62 KB
/
refreshproject.lua
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
-- Copyright 2014 Paul Kulchenko, ZeroBrane LLC; All rights reserved
local winapi
local flags
local needrefresh = {}
local function refreshProjectTree()
for file, kind in pairs(needrefresh) do
-- if the file is removed, try to find a non-existing file in the same folder
-- as this will trigger a refresh of that folder
ide:GetProjectTree():FindItem(
file..(kind == winapi.FILE_ACTION_REMOVED and "/../\1" or ""))
end
needrefresh = {}
end
local function handler(plugin, kind, file)
needrefresh[file] = kind
plugin.onIdleOnce = refreshProjectTree
end
local watches = {}
return {
name = "Refresh project tree",
description = "Refreshes project tree when files change (Windows only).",
author = "Paul Kulchenko",
version = 0.21,
dependencies = {0.71, osname = "Windows"},
onRegister = function(self)
local ok
ok, winapi = pcall(require, 'winapi')
if not ok then return false end
flags = winapi.FILE_NOTIFY_CHANGE_DIR_NAME + winapi.FILE_NOTIFY_CHANGE_FILE_NAME
end,
onIdle = function(self) if next(watches) then winapi.sleep(1) end end,
onProjectLoad = function(plugin, project)
if watches[project] then return end
for _, watcher in pairs(watches) do watcher:kill() end
watches = {}
local enc = winapi.get_encoding(winapi.CP_UTF8)
winapi.set_encoding(winapi.CP_UTF8)
local watcher, err = winapi.watch_for_file_changes(project, flags, true,
function(...) return handler(plugin, ...) end)
winapi.set_encoding(enc)
if not watcher then
error(("Can't set watcher for project '%s': %s"):format(project, err))
end
watches[project] = watcher
end,
}