Skip to content

Commit

Permalink
Make regex for vim modeline more lenient
Browse files Browse the repository at this point in the history
This change allows the filetype/language to be retrieved from more complex vim modelines. The current regex strictly allows a set line which contains only the filetype/ft parameter and nothing else
  • Loading branch information
ammaraskar committed Aug 10, 2015
1 parent ffcc970 commit 4650368
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/linguist/strategy/modeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Linguist
module Strategy
class Modeline
EmacsModeline = /-\*-\s*(?:(?!mode)[\w-]+\s*:\s*(?:[\w+-]+)\s*;?\s*)*(?:mode\s*:)?\s*([\w+-]+)\s*(?:;\s*(?!mode)[\w-]+\s*:\s*[\w+-]+\s*)*;?\s*-\*-/i
VimModeline = /vim:\s*set\s*(?:ft|filetype)=(\w+):/i
VimModeline = /vim:\s*set.*\s(?:ft|filetype)=(\w+)\s?.*:/i

# Public: Detects language based on Vim and Emacs modelines
#
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/Data/Modelines/ruby2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* vim: set ts=8 sw=4 filetype=ruby tw=0: */

# Please help how do I into setting vim modlines
3 changes: 3 additions & 0 deletions test/fixtures/Data/Modelines/ruby3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* vim: set ft=ruby ts=8 sw=4 tw=0: */

# I am not good at humor
4 changes: 4 additions & 0 deletions test/test_modelines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def assert_modeline(language, blob)

def test_modeline_strategy
assert_modeline Language["Ruby"], fixture_blob("Data/Modelines/ruby")
assert_modeline Language["Ruby"], fixture_blob("Data/Modelines/ruby2")
assert_modeline Language["Ruby"], fixture_blob("Data/Modelines/ruby3")
assert_modeline Language["C++"], fixture_blob("Data/Modelines/seeplusplus")
assert_modeline Language["C++"], fixture_blob("Data/Modelines/seeplusplusEmacs1")
assert_modeline Language["C++"], fixture_blob("Data/Modelines/seeplusplusEmacs2")
Expand All @@ -27,6 +29,8 @@ def test_modeline_strategy

def test_modeline_languages
assert_equal Language["Ruby"], fixture_blob("Data/Modelines/ruby").language
assert_equal Language["Ruby"], fixture_blob("Data/Modelines/ruby2").language
assert_equal Language["Ruby"], fixture_blob("Data/Modelines/ruby3").language
assert_equal Language["C++"], fixture_blob("Data/Modelines/seeplusplus").language
assert_equal Language["C++"], fixture_blob("Data/Modelines/seeplusplusEmacs1").language
assert_equal Language["C++"], fixture_blob("Data/Modelines/seeplusplusEmacs2").language
Expand Down

0 comments on commit 4650368

Please sign in to comment.