Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 19 additions & 7 deletions pkg/highlight/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"errors"
"fmt"
"regexp"

"gopkg.in/yaml.v2"
"path/filepath"
"regexp"
)

// A Group represents a syntax group
Expand Down Expand Up @@ -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 {
Comment on lines +157 to +159
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really convinced, that all of this is really needed.
Using header.MatchFileName(b.AbsPath) to check against header.FileNameRegex seems feasible combined with your syntax files changes, unless proven wrong.
Did I oversee something?

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 {
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/apacheconf.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
filetype: apacheconf

detect:
filename: "httpd\\.conf|mime\\.types|vhosts\\.d\\\\*|\\.htaccess"
filename: "httpd\\.conf|mime\\.types|vhosts\\.d(/|\\\\).*|\\.htaccess"
Copy link
Copy Markdown
Author

@questionmarkexclamationpoint questionmarkexclamationpoint Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vhosts\\.d\\\\* previously would've matched:

  • vhosts.d (as in a file named vhosts.d)
  • vhosts.d\
  • vhosts.deez
  • vhosts.d\eez
  • vhosts.d\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\eez

With the addition of .* onto the end of the path, the matched substring of this regex extends into the name of the file, so it passes the new function definition.


rules:
- identifier: "(AcceptMutex|AcceptPathInfo|AccessFileName|Action|AddAlt|AddAltByEncoding|AddAltByType|AddCharset|AddDefaultCharset|AddDescription|AddEncoding)"
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/groff.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
filetype: groff

detect:
filename: "\\.m[ems]$|\\.rof|\\.tmac$|^tmac."
filename: "\\.m[ems]$|\\.rof|\\.tmac$|(^|/|\\\\)tmac."
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This situation covers most of the issues addressed in this commit: start of string. Previously this regex also would've failed if you opened dir/.tmac. I hadn't considered start-of-string based matching, so, there's a few more previously-failing-now-fixed regexes with this patch.


rules:
- statement: "^\\.(ds|nr) [^[[:space:]]]*"
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/groovy.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/ini.yaml
Original file line number Diff line number Diff line change
@@ -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$"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: fixing for DOS users.


rules:
- constant.bool.true: "\\btrue\\b"
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/justfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
filetype: 'justfile'

detect:
filename: "(^\\.?[Jj]ustfile|\\.just)$"
filename: "((^|/|\\\\)\\.?[Jj]ustfile|\\.just)$"
header: "^#!.*/(env +)?[bg]?just --justfile"

rules:
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/ledger.yaml
Original file line number Diff line number Diff line change
@@ -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}|[=~]) .*"
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/mail.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
filetype: mail

detect:
filename: "(.*/mutt-.*|\\.eml)$"
filename: "(.*(/|\\\\)mutt-.*|\\.eml)$"
header: "^From .* \\d+:\\d+:\\d+ \\d+"

rules:
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/nftables.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
filetype: nftables

detect:
filename: "(nftables\\.(conf|rules)$|nftables(\\.rules)?\\.d/)"
filename: "(nftables\\.(conf|rules)$|nftables(\\.rules)?\\.d(/|\\\\).+)"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, note the need for the .+ (.* also fine) so that it picks up part of the filename.

header: "^(#!.*/(env +)?nft( |$)|flush +ruleset)"

rules:
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/privoxy-config.yaml
Original file line number Diff line number Diff line change
@@ -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:]]"
Expand Down
2 changes: 1 addition & 1 deletion runtime/syntax/vi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
filetype: vi

detect:
filename: "(^|/|\\.)(ex|vim)rc$|\\.vim"
filename: "(^|/|\\\\)\\.?(ex|vim)rc$|\\.vim"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what the intention here was, but with this change this no longer matches dir/something.vimrc etc. I don't know if people name their vimrcs that way. I use micro.


rules:
- identifier: "[A-Za-z_][A-Za-z0-9_]*[(]+[A-Za-z0-9_:.,\\s]*[)]+"
Expand Down