limit language detection to known file extensions, unless file contents has a vim modeline
This commit is contained in:
parent
368fecfcf1
commit
6591c4e5e9
1 changed files with 19 additions and 3 deletions
|
@ -20,7 +20,8 @@ if sys.version_info[0] == 2:
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
||||||
else:
|
else:
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
||||||
from pygments.lexers import guess_lexer, guess_lexer_for_filename
|
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
|
||||||
|
from pygments.modeline import get_filetype_from_buffer
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('WakaTime')
|
log = logging.getLogger('WakaTime')
|
||||||
|
@ -92,11 +93,11 @@ def smart_guess_lexer(file_name):
|
||||||
text = get_file_contents(file_name)
|
text = get_file_contents(file_name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
guess_1 = guess_lexer(text)
|
guess_1 = guess_lexer_for_filename(file_name, text)
|
||||||
except:
|
except:
|
||||||
guess_1 = None
|
guess_1 = None
|
||||||
try:
|
try:
|
||||||
guess_2 = guess_lexer_for_filename(file_name, text)
|
guess_2 = guess_lexer_using_modeline(text)
|
||||||
except:
|
except:
|
||||||
guess_2 = None
|
guess_2 = None
|
||||||
try:
|
try:
|
||||||
|
@ -132,6 +133,21 @@ def get_language_from_extension(file_name, extension_map):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def guess_lexer_using_modeline(text):
|
||||||
|
"""Guess lexer for given text using Vim modeline.
|
||||||
|
"""
|
||||||
|
|
||||||
|
file_type = get_filetype_from_buffer(text)
|
||||||
|
|
||||||
|
if file_type is not None:
|
||||||
|
try:
|
||||||
|
return get_lexer_by_name(file_type)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def translate_language(language):
|
def translate_language(language):
|
||||||
"""Turns Pygments lexer class name string into human-readable language.
|
"""Turns Pygments lexer class name string into human-readable language.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue