Skip to content

Commit

Permalink
feat: Setting filetype to org will treat file as an org file
Browse files Browse the repository at this point in the history
Fixes #808
  • Loading branch information
kristijanhusak committed Sep 18, 2024
1 parent c5940d3 commit a006c93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lua/orgmode/files/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ end
---Load the file
---@return OrgPromise<OrgFile | false>
function OrgFile.load(filename)
if not utils.is_org_file(filename) then
return Promise.resolve(false)
end
local bufnr = vim.fn.bufnr(filename) or -1

if bufnr > -1 and vim.api.nvim_buf_is_loaded(bufnr) then
if
bufnr > -1
and vim.api.nvim_buf_is_loaded(bufnr)
and vim.api.nvim_get_option_value('filetype', { buf = bufnr }) == 'org'
then
return Promise.resolve(OrgFile:new({
filename = filename,
lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false),
bufnr = bufnr,
}))
end

if not vim.loop.fs_stat(filename) then
if not vim.loop.fs_stat(filename) or not utils.is_org_file(filename) then
return Promise.resolve(false)
end

Expand Down
22 changes: 21 additions & 1 deletion tests/plenary/init_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local helpers = require('tests.plenary.helpers')
local orgmode = require('orgmode')
local Date = require('orgmode.objects.date')

describe('Init', function()
local org = orgmode.setup({
Expand All @@ -10,6 +10,7 @@ describe('Init', function()
local todo_archive_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org_archive'
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
local txt_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/text_notes.txt'

it('should load and parse files from folder', function()
assert.is.Nil(rawget(org, 'files'))
assert.is.Nil(rawget(org, 'agenda'))
Expand Down Expand Up @@ -53,4 +54,23 @@ describe('Init', function()
-- Existing file in path not appended to paths
assert.are.same({ vim.fn.getcwd() .. '/tests/plenary/fixtures/*', fname }, org.files.paths)
end)

it('should load a file as org file if it has correct filetype', function()
local fname = vim.fn.resolve(vim.fn.tempname() .. '.txt')

-- Behaves as text file
vim.fn.writefile({ '* TODO Test' }, fname)
vim.cmd('edit ' .. fname)
assert.are.same('text', vim.api.nvim_get_option_value('filetype', { buf = vim.api.nvim_get_current_buf() }))
vim.cmd('norm >>')
assert.are.same({ ' * TODO Test' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
vim.cmd('bw!')

-- Behaves as org file
vim.fn.writefile({ '* TODO Test' }, fname)
vim.cmd('edit ' .. fname)
vim.cmd('set filetype=org')
vim.cmd('norm >>')
assert.are.same({ '** TODO Test' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
end)

0 comments on commit a006c93

Please sign in to comment.