diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index d1f8db4b2b..08666e304b 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -866,7 +866,7 @@ func (b *Buffer) UpdateRules() { matchedFileHeader := false if ft == "unknown" || ft == "" { - if header.MatchFileName(b.Path) { + if header.MatchFilePath(b.AbsPath) { matchedFileName = true } if len(fnameMatches) == 0 && header.MatchFileHeader(b.lines[0].data) { @@ -920,7 +920,7 @@ func (b *Buffer) UpdateRules() { } if ft == "unknown" || ft == "" { - if header.MatchFileName(b.Path) { + if header.MatchFilePath(b.AbsPath) { fnameMatches = append(fnameMatches, syntaxFileInfo{header, f.Name(), nil}) } if len(fnameMatches) == 0 && header.MatchFileHeader(b.lines[0].data) { diff --git a/pkg/highlight/parser.go b/pkg/highlight/parser.go index 804e6bc0bd..7d7e1480b3 100644 --- a/pkg/highlight/parser.go +++ b/pkg/highlight/parser.go @@ -4,9 +4,9 @@ import ( "bytes" "errors" "fmt" - "regexp" - "gopkg.in/yaml.v2" + "path/filepath" + "regexp" ) // A Group represents a syntax group @@ -154,13 +154,25 @@ func MakeHeaderYaml(data []byte) (*Header, error) { return header, nil } -// MatchFileName will check the given file name with the stored regex -func (header *Header) MatchFileName(filename string) bool { - if header.FileNameRegex != nil { - return header.FileNameRegex.MatchString(filename) +// MatchFilePath will check the given file's absolute path with the stored regex; +// Only matches which include at least part of the filename are considered. +func (header *Header) MatchFilePath(absoluteFilePath string) bool { + if header.FileNameRegex == nil { + return false } - return false + matchBounds := header.FileNameRegex.FindAllStringIndex(absoluteFilePath, -1) + if matchBounds == nil { + return false + } + + baseName := filepath.Base(absoluteFilePath) + lastMatchEnd := matchBounds[len(matchBounds)-1][1] + + // If a match occurs on the absolute path, make sure the match includes part of the + // filename (i.e. the match extends beyond the length of the file's dir path) to + // avoid having just the dir path of the file trigger a match. + return lastMatchEnd > (len(absoluteFilePath) - len(baseName)) } func (header *Header) MatchFileHeader(firstLine []byte) bool { diff --git a/runtime/syntax/apacheconf.yaml b/runtime/syntax/apacheconf.yaml index a6f89140df..27bf921a12 100644 --- a/runtime/syntax/apacheconf.yaml +++ b/runtime/syntax/apacheconf.yaml @@ -1,7 +1,7 @@ filetype: apacheconf detect: - filename: "httpd\\.conf|mime\\.types|vhosts\\.d\\\\*|\\.htaccess" + filename: "httpd\\.conf|mime\\.types|vhosts\\.d(/|\\\\).*|\\.htaccess" rules: - identifier: "(AcceptMutex|AcceptPathInfo|AccessFileName|Action|AddAlt|AddAltByEncoding|AddAltByType|AddCharset|AddDefaultCharset|AddDescription|AddEncoding)" diff --git a/runtime/syntax/groff.yaml b/runtime/syntax/groff.yaml index 3eaea1f940..b6df048b6f 100644 --- a/runtime/syntax/groff.yaml +++ b/runtime/syntax/groff.yaml @@ -1,7 +1,7 @@ filetype: groff detect: - filename: "\\.m[ems]$|\\.rof|\\.tmac$|^tmac." + filename: "\\.m[ems]$|\\.rof|\\.tmac$|(^|/|\\\\)tmac." rules: - statement: "^\\.(ds|nr) [^[[:space:]]]*" diff --git a/runtime/syntax/groovy.yaml b/runtime/syntax/groovy.yaml index 1830f8b5e3..a5b56a1fa3 100644 --- a/runtime/syntax/groovy.yaml +++ b/runtime/syntax/groovy.yaml @@ -1,7 +1,7 @@ filetype: groovy detect: - filename: "(\\.(groovy|gy|gvy|gsh|gradle)$|^[Jj]enkinsfile$)" + filename: "(\\.(groovy|gy|gvy|gsh|gradle)$|(^|/|\\\\)[Jj]enkinsfile$)" header: "^#!.*/(env +)?groovy *$" rules: diff --git a/runtime/syntax/ini.yaml b/runtime/syntax/ini.yaml index 902d66a3b7..51222bf9fe 100644 --- a/runtime/syntax/ini.yaml +++ b/runtime/syntax/ini.yaml @@ -1,7 +1,7 @@ filetype: ini detect: - filename: "\\.(ini|desktop|lfl|override|tscn|tres)$|(mimeapps\\.list|pinforc|setup\\.cfg|project\\.godot)$|weechat/.+\\.conf$" + filename: "\\.(ini|desktop|lfl|override|tscn|tres)$|(mimeapps\\.list|pinforc|setup\\.cfg|project\\.godot)$|weechat(/|\\\\).+\\.conf$" rules: - constant.bool.true: "\\btrue\\b" diff --git a/runtime/syntax/justfile.yaml b/runtime/syntax/justfile.yaml index 926edb21e2..42c4564e0e 100644 --- a/runtime/syntax/justfile.yaml +++ b/runtime/syntax/justfile.yaml @@ -2,7 +2,7 @@ filetype: 'justfile' detect: - filename: "(^\\.?[Jj]ustfile|\\.just)$" + filename: "((^|/|\\\\)\\.?[Jj]ustfile|\\.just)$" header: "^#!.*/(env +)?[bg]?just --justfile" rules: diff --git a/runtime/syntax/ledger.yaml b/runtime/syntax/ledger.yaml index 0121ae0a1e..29a966bac7 100644 --- a/runtime/syntax/ledger.yaml +++ b/runtime/syntax/ledger.yaml @@ -1,7 +1,7 @@ filetype: ledger detect: - filename: "(^|\\.|/)(ledger|ldgr|beancount|bnct)$" + filename: "(^|/|\\\\)\\.?(ledger|ldgr|beancount|bnct)$" rules: - special: "^([0-9]{4}(/|-)[0-9]{2}(/|-)[0-9]{2}|[=~]) .*" diff --git a/runtime/syntax/mail.yaml b/runtime/syntax/mail.yaml index 6ffe8733fd..86894026eb 100644 --- a/runtime/syntax/mail.yaml +++ b/runtime/syntax/mail.yaml @@ -1,7 +1,7 @@ filetype: mail detect: - filename: "(.*/mutt-.*|\\.eml)$" + filename: "(.*(/|\\\\)mutt-.*|\\.eml)$" header: "^From .* \\d+:\\d+:\\d+ \\d+" rules: diff --git a/runtime/syntax/nftables.yaml b/runtime/syntax/nftables.yaml index 489fdb0b29..56e2c56b02 100644 --- a/runtime/syntax/nftables.yaml +++ b/runtime/syntax/nftables.yaml @@ -1,7 +1,7 @@ filetype: nftables detect: - filename: "(nftables\\.(conf|rules)$|nftables(\\.rules)?\\.d/)" + filename: "(nftables\\.(conf|rules)$|nftables(\\.rules)?\\.d(/|\\\\).+)" header: "^(#!.*/(env +)?nft( |$)|flush +ruleset)" rules: diff --git a/runtime/syntax/privoxy-config.yaml b/runtime/syntax/privoxy-config.yaml index 7eb0898950..ac6fd67df9 100644 --- a/runtime/syntax/privoxy-config.yaml +++ b/runtime/syntax/privoxy-config.yaml @@ -1,7 +1,7 @@ filetype: privoxy-config detect: - filename: "privoxy/config$" + filename: "privoxy(/|\\\\)config$" rules: - statement: "(accept-intercepted-requests|actionsfile|admin-address|allow-cgi-request-crunching|buffer-limit|compression-level|confdir|connection-sharing|debug|default-server-timeout|deny-access|enable-compression|enable-edit-actions|enable-remote-http-toggle|enable-remote-toggle|enforce-blocks|filterfile|forward|forwarded-connect-retries|forward-socks4|forward-socks4a|forward-socks5|handle-as-empty-doc-returns-ok|hostname|keep-alive-timeout|listen-address|logdir|logfile|max-client-connections|permit-access|proxy-info-url|single-threaded|socket-timeout|split-large-forms|templdir|toggle|tolerate-pipelining|trustfile|trust-info-url|user-manual)[[:space:]]" diff --git a/runtime/syntax/vi.yaml b/runtime/syntax/vi.yaml index 2a828dcd6d..f83c69f195 100644 --- a/runtime/syntax/vi.yaml +++ b/runtime/syntax/vi.yaml @@ -1,7 +1,7 @@ filetype: vi detect: - filename: "(^|/|\\.)(ex|vim)rc$|\\.vim" + filename: "(^|/|\\\\)\\.?(ex|vim)rc$|\\.vim" rules: - identifier: "[A-Za-z_][A-Za-z0-9_]*[(]+[A-Za-z0-9_:.,\\s]*[)]+"