Skip to content

Commit 67c2486

Browse files
Optimize file loading to trigger only when necessary
1 parent fdc77eb commit 67c2486

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

lua/orgmode/init.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function Org:setup_autocmds()
5353
group = org_augroup,
5454
callback = function()
5555
require('orgmode').reload(vim.fn.expand('<afile>:p'))
56+
require('orgmode.org.diagnostics').report()
5657
end,
5758
})
5859
vim.api.nvim_create_autocmd('FileType', {
@@ -62,13 +63,6 @@ function Org:setup_autocmds()
6263
require('orgmode').reload(vim.fn.expand('<afile>:p'))
6364
end,
6465
})
65-
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
66-
pattern = { '*.org', '*.org_archive' },
67-
group = org_augroup,
68-
callback = function()
69-
require('orgmode.org.diagnostics').report()
70-
end,
71-
})
7266
end
7367

7468
--- @param revision string?

lua/orgmode/parser/file.lua

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ local utils = require('orgmode.utils')
2222
local File = {}
2323

2424
function File:new(tree, file_content, file_content_str, category, filename, is_archive_file)
25+
local changedtick = 0
26+
if filename then
27+
local bufnr = vim.fn.bufnr(filename)
28+
if bufnr > 0 then
29+
changedtick = vim.api.nvim_buf_get_var(bufnr, 'changedtick')
30+
end
31+
end
2532
local data = {
2633
tree = tree,
2734
file_content = file_content,
2835
file_content_str = file_content_str,
2936
category = category,
3037
filename = filename,
31-
changedtick = 0,
38+
changedtick = changedtick,
3239
sections = {},
3340
sections_by_line = {},
3441
source_code_filetypes = {},
@@ -128,6 +135,15 @@ function File:get_headlines()
128135
return self.sections
129136
end
130137

138+
---@return boolean
139+
function File:should_reload()
140+
local bufnr = vim.fn.bufnr(self.filename)
141+
if bufnr < 0 then
142+
return false
143+
end
144+
return self.changedtick ~= vim.api.nvim_buf_get_var(bufnr, 'changedtick')
145+
end
146+
131147
---@param path string
132148
---@returns File
133149
function File.load(path, callback)
@@ -163,14 +179,10 @@ function File.from_content(content, category, filename, is_archive_file)
163179
end
164180

165181
function File:refresh()
166-
local bufnr = vim.fn.bufnr(self.filename)
167-
if bufnr < 0 then
168-
return self
169-
end
170-
local changed = self.changedtick ~= vim.api.nvim_buf_get_var(bufnr, 'changedtick')
171-
if not changed then
182+
if not self:should_reload() then
172183
return self
173184
end
185+
local bufnr = vim.fn.bufnr(self.filename)
174186
local content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
175187
local refreshed_file = File.from_content(content, self.category, self.filename, self.is_archive_file)
176188
refreshed_file.changedtick = vim.api.nvim_buf_get_var(bufnr, 'changedtick')

lua/orgmode/parser/files.lua

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,33 @@ function Files._set_loaded_file(filename, orgfile)
6262
end
6363

6464
function Files.reload(file, callback)
65-
if file then
66-
local prev_file = Files.get(file)
67-
return File.load(file, function(orgfile)
68-
if orgfile then
69-
Files._set_loaded_file(file, orgfile)
70-
Files._check_source_blocks(prev_file, Files.get(file))
71-
end
72-
Files.loaded = true
73-
if callback then
74-
callback()
75-
end
76-
Files._build_tags()
77-
return Files.get(file)
78-
end)
65+
if not file then
66+
return Files.load(callback)
67+
end
68+
69+
local old_file = Files.orgfiles[file]
70+
local new_file = Files.get(file)
71+
72+
if old_file then
73+
Files._check_source_blocks(old_file, new_file)
74+
if callback then
75+
callback()
76+
end
77+
return new_file
7978
end
8079

81-
return Files.load(callback)
80+
return File.load(file, function(orgfile)
81+
if orgfile then
82+
Files._set_loaded_file(file, orgfile)
83+
Files._check_source_blocks(old_file, orgfile)
84+
end
85+
Files.loaded = true
86+
if callback then
87+
callback()
88+
end
89+
Files._build_tags()
90+
return orgfile
91+
end)
8292
end
8393

8494
---@return File[]
@@ -111,7 +121,7 @@ function Files.get(file)
111121
end
112122

113123
if vim.bo.filetype == 'org' and vim.fn.filereadable(file) == 0 then
114-
return File.from_content(vim.api.nvim_buf_get_lines(0, 0, -1, false))
124+
return File.from_content(vim.api.nvim_buf_get_lines(0, 0, -1, false), nil, nil, false)
115125
end
116126

117127
return nil

0 commit comments

Comments
 (0)