@@ -45,6 +45,119 @@ local function get_priority_git_status_code(status, other_status)
4545 end
4646end
4747
48+ local diag_severity_to_string = function (severity )
49+ if severity == vim .diagnostic .severity .ERROR then
50+ return ' Error'
51+ elseif severity == vim .diagnostic .severity .WARN then
52+ return ' Warning'
53+ elseif severity == vim .diagnostic .severity .INFO then
54+ return ' Information'
55+ elseif severity == vim .diagnostic .severity .HINT then
56+ return ' Hint'
57+ else
58+ return nil
59+ end
60+ end
61+
62+ local tracked_functions = {}
63+
64+
65+ --- Call fn, but not more than once every x milliseconds.
66+ --- @param id string Identifier for the debounce group , such as the function name.
67+ --- @param fn function Function to be executed.
68+ --- @param frequency_in_ms number Miniumum amount of time between invocations of fn.
69+ --- @param callback function Called with the result of executing fn as : callback (success , result )
70+ M .debounce = function (id , fn , frequency_in_ms , callback )
71+ local fn_data = tracked_functions [id ]
72+ if fn_data == nil then
73+ -- first call for this id
74+ fn_data = {
75+ id = id ,
76+ fn = nil ,
77+ frequency_in_ms = frequency_in_ms ,
78+ postponed_callback = nil ,
79+ in_debounce_period = true ,
80+ }
81+ tracked_functions [id ] = fn_data
82+ else
83+ if fn_data .in_debounce_period then
84+ -- This id was called recently and can't be executed again yet.
85+ -- Just keep track of the details for this request so it
86+ -- can be executed at the end of the debounce period.
87+ -- Last one in wins.
88+ fn_data .fn = fn
89+ fn_data .frequency_in_ms = frequency_in_ms
90+ fn_data .postponed_callback = callback
91+ return
92+ end
93+ end
94+
95+ -- Run the requested function normally.
96+ -- Use a pcall to ensure the debounce period is still respected even if
97+ -- this call throws an error.
98+ fn_data .in_debounce_period = true
99+ local success , result = pcall (fn )
100+
101+ if not success then
102+ print (" Error in neo-tree.utils.debounce: " , result )
103+ end
104+
105+ -- Now schedule the next earliest execution.
106+ -- If there are no calls to run the same function between now
107+ -- and when this deferred executes, nothing will happen.
108+ -- If there are several calls, only the last one in will run.
109+ vim .defer_fn (function ()
110+ local current_data = tracked_functions [id ]
111+ local _callback = current_data .postponed_callback
112+ local _fn = current_data .fn
113+ current_data .postponed_callback = nil
114+ current_data .fn = nil
115+ current_data .in_debounce_period = false
116+ if _fn ~= nil then
117+ M .debounce (id , _fn , current_data .frequency_in_ms , _callback )
118+ end
119+ end , frequency_in_ms )
120+
121+ -- The callback function is outside the scope of the debounce period
122+ if type (callback ) == " function" then
123+ callback (success , result )
124+ end
125+ end
126+
127+
128+ --- Gets diagnostic severity counts for all files
129+ --- @return table table { file_path = { Error = int , Warning = int , Information = int , Hint = int , Unknown = int } }
130+ M .get_diagnostic_counts = function ()
131+ local d = vim .diagnostic .get ()
132+ local lookup = {}
133+ for _ , diag in ipairs (d ) do
134+ local file_name = vim .api .nvim_buf_get_name (diag .bufnr )
135+ local sev = diag_severity_to_string (diag .severity )
136+ if sev then
137+ local entry = lookup [file_name ] or { severity_number = 4 }
138+ entry [sev ] = (entry [sev ] or 0 ) + 1
139+ entry .severity_number = math.min (entry .severity_number , diag .severity )
140+ entry .severity_string = diag_severity_to_string (entry .severity_number )
141+ lookup [file_name ] = entry
142+ end
143+ end
144+
145+ for file_name , entry in pairs (lookup ) do
146+ -- Now bubble this status up to the parent directories
147+ local parts = M .split (file_name , M .path_separator )
148+ table.remove (parts ) -- pop the last part so we don't override the file's status
149+ M .reduce (parts , " " , function (acc , part )
150+ local path = acc .. M .path_separator .. part
151+ local path_entry = lookup [path ] or { severity_number = 4 }
152+ path_entry .severity_number = math.min (path_entry .severity_number , entry .severity_number )
153+ path_entry .severity_string = diag_severity_to_string (path_entry .severity_number )
154+ lookup [path ] = path_entry
155+ return path
156+ end )
157+ end
158+ return lookup
159+ end
160+
48161--- Parse "git status" output for the current working directory.
49162--- @return table table Table with the path as key and the status as value.
50163M .get_git_status = function ()
0 commit comments