Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a006c93

Browse files
feat: Setting filetype to org will treat file as an org file
Fixes #808
1 parent c5940d3 commit a006c93

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lua/orgmode/files/file.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ end
5555
---Load the file
5656
---@return OrgPromise<OrgFile | false>
5757
function OrgFile.load(filename)
58-
if not utils.is_org_file(filename) then
59-
return Promise.resolve(false)
60-
end
6158
local bufnr = vim.fn.bufnr(filename) or -1
6259

63-
if bufnr > -1 and vim.api.nvim_buf_is_loaded(bufnr) then
60+
if
61+
bufnr > -1
62+
and vim.api.nvim_buf_is_loaded(bufnr)
63+
and vim.api.nvim_get_option_value('filetype', { buf = bufnr }) == 'org'
64+
then
6465
return Promise.resolve(OrgFile:new({
6566
filename = filename,
6667
lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false),
6768
bufnr = bufnr,
6869
}))
6970
end
7071

71-
if not vim.loop.fs_stat(filename) then
72+
if not vim.loop.fs_stat(filename) or not utils.is_org_file(filename) then
7273
return Promise.resolve(false)
7374
end
7475

tests/plenary/init_spec.lua

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
local helpers = require('tests.plenary.helpers')
21
local orgmode = require('orgmode')
2+
local Date = require('orgmode.objects.date')
33

44
describe('Init', function()
55
local org = orgmode.setup({
@@ -10,6 +10,7 @@ describe('Init', function()
1010
local todo_archive_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org_archive'
1111
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
1212
local txt_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/text_notes.txt'
13+
1314
it('should load and parse files from folder', function()
1415
assert.is.Nil(rawget(org, 'files'))
1516
assert.is.Nil(rawget(org, 'agenda'))
@@ -53,4 +54,23 @@ describe('Init', function()
5354
-- Existing file in path not appended to paths
5455
assert.are.same({ vim.fn.getcwd() .. '/tests/plenary/fixtures/*', fname }, org.files.paths)
5556
end)
57+
58+
it('should load a file as org file if it has correct filetype', function()
59+
local fname = vim.fn.resolve(vim.fn.tempname() .. '.txt')
60+
61+
-- Behaves as text file
62+
vim.fn.writefile({ '* TODO Test' }, fname)
63+
vim.cmd('edit ' .. fname)
64+
assert.are.same('text', vim.api.nvim_get_option_value('filetype', { buf = vim.api.nvim_get_current_buf() }))
65+
vim.cmd('norm >>')
66+
assert.are.same({ ' * TODO Test' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
67+
vim.cmd('bw!')
68+
69+
-- Behaves as org file
70+
vim.fn.writefile({ '* TODO Test' }, fname)
71+
vim.cmd('edit ' .. fname)
72+
vim.cmd('set filetype=org')
73+
vim.cmd('norm >>')
74+
assert.are.same({ '** TODO Test' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
75+
end)
5676
end)

0 commit comments

Comments
 (0)