diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Find a script's lexer from its shebang line
Scintillua's detect reads the first line as well as the name, and
without it hooks, bin scripts and anything else lacking an extension
stayed plain.
Diffstat (limited to '')
| -rw-r--r-- | custom/extensions/syntax-highlight.lua | 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -rw-r--r-- | tests/extensions/fake-lexers/lexer.lua | 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -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 2bff263..544fce1 100644 --- a/custom/extensions/syntax-highlight.lua +++ b/custom/extensions/syntax-highlight.lua @@ -162,9 +162,11 @@ local function load_lexer_name(name) return nil end -local function lexer_for(name) +local function lexer_for(name, text) if type(scintillua.detect) == "function" then - local ok, lang = pcall(scintillua.detect, name) + -- The first line goes along, so a script with no extension is still + -- known by its shebang. + local ok, lang = pcall(scintillua.detect, name, text:match("^[^\n]*")) if ok and lang then local lexer = load_lexer_name(lang) if lexer then @@ -181,7 +183,7 @@ local function lexer_for(name) end local function highlight(text) - local lexer = lexer_for(filename) + local lexer = lexer_for(filename, text) if not lexer then return nil end diff --git a/tests/extensions/fake-lexers/lexer.lua b/tests/extensions/fake-lexers/lexer.lua index adf7d5b..0632744 100644 --- a/tests/extensions/fake-lexers/lexer.lua +++ b/tests/extensions/fake-lexers/lexer.lua @@ -1,9 +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, 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 +-- 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. local M = {} @@ -67,10 +67,13 @@ function M.load(name) return lexer end -function M.detect(filename) +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 end diff --git a/tests/extensions/test-syntax-highlight.lua b/tests/extensions/test-syntax-highlight.lua index a9e8b36..a87cd80 100644 --- a/tests/extensions/test-syntax-highlight.lua +++ b/tests/extensions/test-syntax-highlight.lua @@ -60,6 +60,10 @@ h.equals("a lexer that raises falls back to escaped text", out, "some <code&g 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>") + out = render("tool.py", "import <x>") h.equals("an extension with no lexer serves escaped text", out, "import <x>") |
