-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict Vim modeline parsing to a single line #4138
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! 👍 Well done.
I've left some feedback.
lib/linguist/strategy/modeline.rb
Outdated
@@ -49,20 +49,20 @@ class Modeline | |||
# serves as a terminator for an option sequence, delimited by whitespace. | |||
(?= | |||
# So we have to ensure the modeline ends with a colon | |||
: (?=\s* set? \s [^\n:]+ :) | | |||
: (?=[[:blank:]]* set? [[:blank:]] [^\n:]+ :) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend the use of [ \t]
instead, as it's arguably clearer than a POSIX character class.
lib/linguist/strategy/modeline.rb
Outdated
| | ||
\s* : \s* # Note that whitespace around colons is accepted too: | ||
[[:blank:]]* : [[:blank:]]* # Note that whitespace around colons is accepted too: | ||
) # vim: noai : ft=ruby:noexpandtab |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should indent this line so the comment matches the line above it.
lib/linguist/strategy/modeline.rb
Outdated
|
||
# Option's value. Might be blank; `vim: ft= ` says "use no filetype". | ||
(?: | ||
[^\\\s] # Beware of escaped characters: titlestring=\ ft=ruby | ||
[^\\[[:blank:]]] # Beware of escaped characters: titlestring=\ ft=ruby | ||
| # will be read by Vim as { titlestring: " ft=ruby" }. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent needed here too.
LGTM. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! Nice improvement @jatoben 🙇♂️
better late than never :D |
Description
Files which have a similar syntax to Vim modelines, such as YAML, can trigger regex matching behavior that makes filetype detection very slow. In this pull request, I've replaced
\s
in the Vim modeline regex (which considers newlines as whitespace) with the POSIX bracket expression[[:blank:]]
, which doesn't ([ \t]
should also work, if that's preferred). This constrains the parsing of each modeline to a single line and avoids the pathological case.This short YAML file can be used to reproduce the behavior:
Before the change, using Ruby 2.4.2 on Linux:
After:
Checklist:
I am associating a language with a new file extension.
I am adding a new language.
I am fixing a misclassified language
I am changing the source of a syntax highlighting grammar
I am adding new or changing current functionality
I have not added any tests here, as I think the existing coverage in fixtures like test/fixtures/Data/Modelines/iamjs2.pl already covers the desired behavior. If I've missed anything, please let me know!