diff options
| author | Thomas Aglassinger <roskakori@users.sourceforge.net> | 2020-08-23 16:36:14 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-23 16:36:14 +0200 |
| commit | 2a762c5fe655a3b3ecc9c52422300b9430110758 (patch) | |
| tree | f3497d0b95cb2f699182c67ea83dd73bb77d107b | |
| parent | 90555f7e73ccf9858d0cda7cb31b9fb727bf5adf (diff) | |
| download | pygments-git-2a762c5fe655a3b3ecc9c52422300b9430110758.tar.gz | |
Fix cmake header (#1491)
* Fixed guessing of CMake by header.
* Version number can have multiple digits.
* Tabs are handled as white space.
* Trailing comments are ignored.
* Cleaned up regex to detect CMake header.
| -rw-r--r-- | pygments/lexers/make.py | 7 | ||||
| -rw-r--r-- | tests/test_make.py | 29 |
2 files changed, 35 insertions, 1 deletions
diff --git a/pygments/lexers/make.py b/pygments/lexers/make.py index 121db1c2..5f65afcc 100644 --- a/pygments/lexers/make.py +++ b/pygments/lexers/make.py @@ -196,7 +196,12 @@ class CMakeLexer(RegexLexer): } def analyse_text(text): - exp = r'^ *CMAKE_MINIMUM_REQUIRED *\( *VERSION *\d(\.\d)* *( FATAL_ERROR)? *\) *$' + exp = ( + r'^[ \t]*CMAKE_MINIMUM_REQUIRED[ \t]*' + r'\([ \t]*VERSION[ \t]*\d+(\.\d+)*[ \t]*' + r'([ \t]FATAL_ERROR)?[ \t]*\)[ \t]*' + r'(#[^\n]*)?$' + ) if re.search(exp, text, flags=re.MULTILINE | re.IGNORECASE): return 0.8 return 0.0 diff --git a/tests/test_make.py b/tests/test_make.py new file mode 100644 index 00000000..b311850c --- /dev/null +++ b/tests/test_make.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +""" + CMake Tests + ~~~~~~~~~~~ + + :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from pygments.lexers import CMakeLexer, guess_lexer + + +def test_guess_cmake_lexer_from_header(): + headers = [ + "CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)", + "cmake_minimum_required(version 3.13) # CMake version check", + " CMAKE_MINIMUM_REQUIRED\t( VERSION 2.6 FATAL_ERROR ) ", + ] + for header in headers: + code = '\n'.join([ + header, + 'project(example)', + 'set(CMAKE_CXX_STANDARD 14)', + 'set(SOURCE_FILES main.cpp)', + 'add_executable(example ${SOURCE_FILES})', + ]) + lexer = guess_lexer(code) + assert isinstance(lexer, CMakeLexer), \ + "header must be detected as CMake: %r" % header |
