Diff options
| -rw-r--r-- | custom/extensions/syntax-highlight.lua | 55 | |
| -rw-r--r-- | tests/extensions/fake-lexers/lexer.lua | 14 | |
| -rw-r--r-- | tests/extensions/test-syntax-highlight.lua | 5 |
3 files changed, 13 insertions, 61 deletions
diff --git a/custom/extensions/syntax-highlight.lua b/custom/extensions/syntax-highlight.lua index 6605d71..639ad3f 100644 --- a/custom/extensions/syntax-highlight.lua +++ b/custom/extensions/syntax-highlight.lua @@ -74,25 +74,6 @@ local css = { pseudoelement = "hl-func", } --- Extension to lexer name fixes for the fallback path, reached only when --- this Scintillua has no detect(). Most extensions already equal their lexer --- name and these are the frequent exceptions. -local ext_lexer = { - py = "python", - js = "javascript", - ts = "typescript", - rb = "ruby", - pl = "perl", - pm = "perl", - sh = "bash", - md = "markdown", - htm = "html", - yml = "yaml", - rs = "rust", - c = "ansi_c", - h = "ansi_c", -} - local scintillua = nil local filename = "" @@ -170,37 +151,17 @@ local function load_scintillua() return nil end --- Loading a lexer runs its Lua, and one written for another Scintillua can --- raise, so a failure here just leaves this file uncoloured. -local function load_lexer_name(name) - if name == nil then - return nil - end - local ok, lexer = pcall(scintillua.load, name) - if ok and lexer then - return lexer - end - return nil -end - +-- Detecting and loading a lexer run Scintillua's own Lua, and code written +-- for another version of it can raise, so a failure in either just leaves +-- this file uncoloured. The first line goes along to detect(), so a script +-- with no extension is still known by its shebang. local function lexer_for(name, text) - if type(scintillua.detect) == "function" then - -- The first line goes along, so a script with no extension is still - -- known by its shebang. - local ok, lang = pcall(scintillua.detect, name, string.match(text, "^[^\n]*")) - if ok and lang then - local lexer = load_lexer_name(lang) - if lexer then - return lexer - end - end - end - local ext = string.match(name, "%.([^.]+)$") - if not ext then + local ok, lang = pcall(scintillua.detect, name, string.match(text, "^[^\n]*")) + if not ok or not lang then return nil end - ext = string.lower(ext) - return load_lexer_name(ext_lexer[ext]) or load_lexer_name(ext) + local loaded, lexer = pcall(scintillua.load, lang) + return loaded and lexer or nil end local function highlight(text) diff --git a/tests/extensions/fake-lexers/lexer.lua b/tests/extensions/fake-lexers/lexer.lua index ea7768e..dd7545f 100644 --- a/tests/extensions/fake-lexers/lexer.lua +++ b/tests/extensions/fake-lexers/lexer.lua @@ -1,10 +1,9 @@ -- A miniature stand-in for the Scintillua lexer module, just enough for the -- syntax-highlight checks to drive every path through the filter without the --- real collection installed. load() hands out one of the toy lexers below by --- name and detect() recognises a single filename suffix or a shebang naming --- fake, so a check can tell the detection path apart from the extension path --- by which lexer ends up running. Anything the filter treats as optional, a lexer that will not --- load or one that raises while lexing, has a name here too. +-- real collection installed. detect() names a lexer after the filename's +-- suffix, or fake for a shebang naming it, and load() hands out the toy lexer +-- of that name. Besides fake there is one that stops short and one that +-- raises, and any other name fails to load. local M = {} @@ -69,13 +68,10 @@ function M.load(name) end function M.detect(filename, line) - if filename:match("%.viadetect$") then - return "fake" - end if line ~= nil and line:match("^#!.*fake") then return "fake" end - return nil + return filename:match("%.([^.]+)$") end return M diff --git a/tests/extensions/test-syntax-highlight.lua b/tests/extensions/test-syntax-highlight.lua index 50c7bc8..f9f99a8 100644 --- a/tests/extensions/test-syntax-highlight.lua +++ b/tests/extensions/test-syntax-highlight.lua @@ -70,11 +70,6 @@ h.equals("a lexer stopping early leaves an escaped tail", out, "<span class='hl- out = render("boom.badlex", "some <code>") h.equals("a lexer that raises falls back to escaped text", out, "some <code>") --- The viadetect lexer name only resolves through detect(), so highlighted --- output proves the detection path ran ahead of the extension path. -out = render("script.viadetect", "if") -h.equals("filename detection picks the lexer", out, "<span class='hl-keyword'>if</span>") - out = render("post-receive", "#!/usr/bin/fake\nif") h.contains("a shebang picks the lexer of a file with no extension", out, "<span class='hl-keyword'>if</span>") |
