Diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Find a capitalised extension's lexer again
Scintillua's detect() matches an extension by its case, and the fallback
e1960f0 dropped had lowercased it, so MAIN.C and SETUP.PY lost their
colour. The fake lexer now answers only for the suffixes it knows, as
Scintillua does, which is what lets the check see it.
Diffstat (limited to '')
| -rw-r--r-- | custom/extensions/syntax-highlight.lua | 6 | |
| -rw-r--r-- | tests/extensions/fake-lexers/lexer.lua | 13 | |
| -rw-r--r-- | tests/extensions/test-syntax-highlight.lua | 4 |
3 files changed, 16 insertions, 7 deletions
diff --git a/custom/extensions/syntax-highlight.lua b/custom/extensions/syntax-highlight.lua index 282b0cf..7414b63 100644 --- a/custom/extensions/syntax-highlight.lua +++ b/custom/extensions/syntax-highlight.lua @@ -153,9 +153,11 @@ local function load_scintillua() end -- The first line goes along to detect(), so a script with no extension is --- still known by its shebang. +-- still known by its shebang. detect() matches an extension by its case, so +-- a name in capitals such as MAIN.C is tried again in lower case. local function lexer_for(name, text) - local lang = scintillua.detect(name, string.match(text, "^[^\n]*")) + local first = string.match(text, "^[^\n]*") + local lang = scintillua.detect(name, first) or scintillua.detect(string.lower(name), first) return lang and scintillua.load(lang) end diff --git a/tests/extensions/fake-lexers/lexer.lua b/tests/extensions/fake-lexers/lexer.lua index dd7545f..bb869de 100644 --- a/tests/extensions/fake-lexers/lexer.lua +++ b/tests/extensions/fake-lexers/lexer.lua @@ -1,9 +1,10 @@ -- 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. 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. +-- real collection installed. detect() answers for the suffixes it knows, +-- matched by case as Scintillua's own does, 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 python, which detect() knows but +-- load() cannot find, as a collection missing a lexer would answer. local M = {} @@ -67,11 +68,13 @@ function M.load(name) return lexer end +local suffixes = { fake = "fake", short = "short", badlex = "badlex", py = "python" } + function M.detect(filename, line) if line ~= nil and line:match("^#!.*fake") then return "fake" end - return filename:match("%.([^.]+)$") + return suffixes[filename:match("%.([^.]+)$") or ""] end return M diff --git a/tests/extensions/test-syntax-highlight.lua b/tests/extensions/test-syntax-highlight.lua index 5fc6ea3..a0debc6 100644 --- a/tests/extensions/test-syntax-highlight.lua +++ b/tests/extensions/test-syntax-highlight.lua @@ -70,6 +70,10 @@ 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>") +out = render("HELLO.FAKE", "if 42") +h.contains("an extension in capitals finds its 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>") |
