summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2017-01-22 18:35:50 +0100
committerGeorg Brandl <georg@python.org>2017-01-22 18:35:50 +0100
commit6af1485becae0a9847c1f49e7dd0e6040a412c9c (patch)
tree4c16d6d77da6d7c3ba271805cdcc862a2881698a
parent304ab38c9a49800c86c5ccc97e77f37f17c1480f (diff)
downloadpygments-6af1485becae0a9847c1f49e7dd0e6040a412c9c.tar.gz
modeline: work for the first N lines and add a test to keep it that way
-rw-r--r--pygments/modeline.py9
-rw-r--r--tests/test_modeline.py26
2 files changed, 31 insertions, 4 deletions
diff --git a/pygments/modeline.py b/pygments/modeline.py
index 2200f1cf..43c1784f 100644
--- a/pygments/modeline.py
+++ b/pygments/modeline.py
@@ -35,9 +35,10 @@ def get_filetype_from_buffer(buf, max_lines=5):
ret = get_filetype_from_line(l)
if ret:
return ret
- for l in lines[max_lines:-1:-1]:
- ret = get_filetype_from_line(l)
- if ret:
- return ret
+ for i in range(max_lines, -1, -1):
+ if i < len(lines):
+ ret = get_filetype_from_line(lines[i])
+ if ret:
+ return ret
return None
diff --git a/tests/test_modeline.py b/tests/test_modeline.py
new file mode 100644
index 00000000..d3a0c915
--- /dev/null
+++ b/tests/test_modeline.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+"""
+ Tests for the vim modeline feature
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+from __future__ import print_function
+
+from pygments import modeline
+
+
+def test_lexer_classes():
+ def verify(buf):
+ assert modeline.get_filetype_from_buffer(buf) == 'python'
+
+ for buf in [
+ 'vi: ft=python' + '\n' * 8,
+ 'vi: ft=python' + '\n' * 8,
+ '\n\n\n\nvi=8: syntax=python' + '\n' * 8,
+ '\n' * 8 + 'ex: filetype=python',
+ '\n' * 8 + 'vim: some,other,syn=python\n\n\n\n'
+ ]:
+ yield verify, buf