From ba6c14846b97512254901357392d5a649fe130dd Mon Sep 17 00:00:00 2001 From: rxi Date: Mon, 8 Jun 2020 09:44:51 +0100 Subject: [PATCH] Added file-header pattern support to `syntax` --- data/core/doc/init.lua | 3 ++- data/core/syntax.lua | 11 ++++++++--- data/plugins/language_lua.lua | 1 + data/plugins/language_python.lua | 1 + data/plugins/language_xml.lua | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index bb09ce2b..83b5fc01 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -56,7 +56,8 @@ end function Doc:reset_syntax() - local syn = syntax.get(self.filename or "") + local header = self:get_text(1, 1, self:position_offset(1, 1, 128)) + local syn = syntax.get(self.filename or "", header) if self.syntax ~= syn then self.syntax = syn self.highlighter:reset() diff --git a/data/core/syntax.lua b/data/core/syntax.lua index fe8cc862..a763ac78 100644 --- a/data/core/syntax.lua +++ b/data/core/syntax.lua @@ -11,14 +11,19 @@ function syntax.add(t) end -function syntax.get(filename) +local function find(string, field) for i = #syntax.items, 1, -1 do local t = syntax.items[i] - if common.match_pattern(filename, t.files) then + if common.match_pattern(string, t[field] or {}) then return t end end - return plain_text_syntax +end + +function syntax.get(filename, header) + return find(filename, "files") + or find(header, "headers") + or plain_text_syntax end diff --git a/data/plugins/language_lua.lua b/data/plugins/language_lua.lua index 79e2fce0..c577a28f 100644 --- a/data/plugins/language_lua.lua +++ b/data/plugins/language_lua.lua @@ -2,6 +2,7 @@ local syntax = require "core.syntax" syntax.add { files = "%.lua$", + headers = "^#!.*[ /]lua", comment = "--", patterns = { { pattern = { '"', '"', '\\' }, type = "string" }, diff --git a/data/plugins/language_python.lua b/data/plugins/language_python.lua index ef79f234..4a6ada33 100644 --- a/data/plugins/language_python.lua +++ b/data/plugins/language_python.lua @@ -2,6 +2,7 @@ local syntax = require "core.syntax" syntax.add { files = "%.py$", + headers = "^#!.*[ /]python", comment = "#", patterns = { { pattern = { "#", "\n" }, type = "comment" }, diff --git a/data/plugins/language_xml.lua b/data/plugins/language_xml.lua index 9400e778..5240bdc4 100644 --- a/data/plugins/language_xml.lua +++ b/data/plugins/language_xml.lua @@ -2,6 +2,7 @@ local syntax = require "core.syntax" syntax.add { files = { "%.xml$", "%.html?$" }, + headers = "<%?xml", patterns = { { pattern = { "" }, type = "comment" }, { pattern = { '%f[^>][^<]', '%f[<]' }, type = "normal" },