Added file-header pattern support to `syntax`
This commit is contained in:
parent
db471c0554
commit
ba6c14846b
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ local syntax = require "core.syntax"
|
|||
|
||||
syntax.add {
|
||||
files = "%.lua$",
|
||||
headers = "^#!.*[ /]lua",
|
||||
comment = "--",
|
||||
patterns = {
|
||||
{ pattern = { '"', '"', '\\' }, type = "string" },
|
||||
|
|
|
@ -2,6 +2,7 @@ local syntax = require "core.syntax"
|
|||
|
||||
syntax.add {
|
||||
files = "%.py$",
|
||||
headers = "^#!.*[ /]python",
|
||||
comment = "#",
|
||||
patterns = {
|
||||
{ pattern = { "#", "\n" }, type = "comment" },
|
||||
|
|
|
@ -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" },
|
||||
|
|
Loading…
Reference in New Issue