Skip to content

Commit af0713d

Browse files
refactor: Move filetype detection to config
Also cache already detected filetypes
1 parent 41bfa3a commit af0713d

File tree

4 files changed

+58
-37
lines changed

4 files changed

+58
-37
lines changed

lua/orgmode/config/init.lua

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function Config:setup_ts_predicates()
421421
if not text or vim.trim(text) == '' then
422422
return
423423
end
424-
metadata['injection.language'] = utils.detect_filetype(text, true)
424+
metadata['injection.language'] = self:detect_filetype(text)
425425
end, { force = true, all = true })
426426

427427
vim.treesitter.query.add_directive('org-set-inline-block-language!', function(match, _, bufnr, pred, metadata)
@@ -438,7 +438,7 @@ function Config:setup_ts_predicates()
438438
text = text:sub(5)
439439
-- Remove opening brackend and parameters: lua[params]{ -> lua
440440
text = text:gsub('[%{%[].*', '')
441-
metadata['injection.language'] = utils.detect_filetype(text, true)
441+
metadata['injection.language'] = self:detect_filetype(text)
442442
end, { force = true, all = true })
443443

444444
vim.treesitter.query.add_predicate('org-is-headline-level?', function(match, _, _, predicate)
@@ -542,6 +542,60 @@ function Config:use_property_inheritance(property_name)
542542
end
543543
end
544544

545+
---@param filetype_name string
546+
---@param use_ftmatch? boolean Use vim.filetype.match to detect filetype
547+
function Config:detect_filetype(filetype_name, use_ftmatch)
548+
local name = filetype_name:lower()
549+
550+
if not self._ft_map then
551+
self._ft_map = {}
552+
end
553+
554+
if self._ft_map[name] then
555+
return self._ft_map[name]
556+
end
557+
558+
local filetype = self:_get_filetype_name(name)
559+
560+
if use_ftmatch then
561+
local filename = '__org__detect_filetype__.' .. filetype
562+
local ft = vim.filetype.match({ filename = filename })
563+
if ft then
564+
self._ft_map[name] = ft
565+
return ft
566+
end
567+
end
568+
569+
self._ft_map[name] = filetype
570+
return filetype
571+
end
572+
573+
---@private
574+
---@param filetype string
575+
function Config:_get_filetype_name(filetype)
576+
local map = {
577+
['emacs-lisp'] = 'lisp',
578+
elisp = 'lisp',
579+
js = 'javascript',
580+
ts = 'typescript',
581+
md = 'markdown',
582+
ex = 'elixir',
583+
pl = 'perl',
584+
sh = 'bash',
585+
shell = 'bash',
586+
uxn = 'uxntal',
587+
}
588+
if map[filetype] then
589+
return map[filetype]
590+
end
591+
592+
if self.opts.org_edit_src_filetype_map[filetype] then
593+
return self.opts.org_edit_src_filetype_map[filetype]
594+
end
595+
596+
return filetype
597+
end
598+
545599
---@type OrgConfig
546600
instance = Config:new()
547601
return instance

lua/orgmode/files/elements/block.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function Block:get_language()
7676
if not language or language == '' then
7777
return nil
7878
end
79-
return utils.detect_filetype(language)
79+
return config:detect_filetype(language, true)
8080
end
8181

8282
---@return table<string, string>

lua/orgmode/objects/edit_special/types/src.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function EditSpecialSrc:init()
7474
-- so base the range off of the name of the block
7575
local ft = self.src_block.children.parameters.text
7676
if ft then
77-
ft = utils.detect_filetype(ft)
77+
ft = config:detect_filetype(ft, true)
7878
end
7979

8080
local bufnr = es_utils.make_temp_buf()

lua/orgmode/utils/init.lua

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -560,39 +560,6 @@ function utils.find(entries, check_fn)
560560
return nil
561561
end
562562

563-
---@param name string
564-
---@param skip_ftmatch? boolean
565-
---@return string
566-
function utils.detect_filetype(name, skip_ftmatch)
567-
local config = require('orgmode.config')
568-
local map = {
569-
['emacs-lisp'] = 'lisp',
570-
elisp = 'lisp',
571-
js = 'javascript',
572-
ts = 'typescript',
573-
md = 'markdown',
574-
ex = 'elixir',
575-
pl = 'perl',
576-
sh = 'bash',
577-
shell = 'bash',
578-
uxn = 'uxntal',
579-
}
580-
if not skip_ftmatch then
581-
local filename = '__org__detect_filetype__.' .. (map[name] or name)
582-
local ft = vim.filetype.match({ filename = filename })
583-
if ft then
584-
return ft
585-
end
586-
end
587-
if map[name] then
588-
return map[name]
589-
end
590-
if config.org_edit_src_filetype_map[name] then
591-
return config.org_edit_src_filetype_map[name]
592-
end
593-
return name:lower()
594-
end
595-
596563
---@param filename string
597564
---@return boolean
598565
function utils.is_org_file(filename)

0 commit comments

Comments
 (0)