detect non-C languages in folders with C/C++ files

Improves on 2da75aa119.
This commit is contained in:
Alan Hamlett 2017-02-26 16:24:43 -08:00
parent d85448b563
commit 9ce9d528fd
11 changed files with 39 additions and 40 deletions

View file

@ -42,7 +42,7 @@ class DependenciesTestCase(utils.TestCase):
def test_token_parser(self): def test_token_parser(self):
with self.assertRaises(NotYetImplemented): with self.assertRaises(NotYetImplemented):
source_file = 'tests/samples/codefiles/see.h' source_file = 'tests/samples/codefiles/c_only/non_empty.h'
parser = TokenParser(source_file) parser = TokenParser(source_file)
parser.parse() parser.parse()
@ -458,7 +458,7 @@ class DependenciesTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/see.c' entity = 'tests/samples/codefiles/c_only/non_empty.c'
shutil.copy(entity, os.path.join(tempdir, 'see.c')) shutil.copy(entity, os.path.join(tempdir, 'see.c'))
entity = os.path.realpath(os.path.join(tempdir, 'see.c')) entity = os.path.realpath(os.path.join(tempdir, 'see.c'))
@ -510,9 +510,9 @@ class DependenciesTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/seeplusplus.cpp' entity = 'tests/samples/codefiles/c_and_cpp/non_empty.cpp'
shutil.copy(entity, os.path.join(tempdir, 'seeplusplus.cpp')) shutil.copy(entity, os.path.join(tempdir, 'non_empty.cpp'))
entity = os.path.realpath(os.path.join(tempdir, 'seeplusplus.cpp')) entity = os.path.realpath(os.path.join(tempdir, 'non_empty.cpp'))
now = u(int(time.time())) now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg' config = 'tests/samples/configs/good_config.cfg'
@ -562,7 +562,7 @@ class DependenciesTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/seesharp.cs' entity = 'tests/samples/codefiles/csharp/seesharp.cs'
shutil.copy(entity, os.path.join(tempdir, 'seesharp.cs')) shutil.copy(entity, os.path.join(tempdir, 'seesharp.cs'))
entity = os.path.realpath(os.path.join(tempdir, 'seesharp.cs')) entity = os.path.realpath(os.path.join(tempdir, 'seesharp.cs'))

View file

@ -23,31 +23,6 @@ class LanguagesTestCase(utils.TestCase):
['wakatime.session_cache.SessionCache.connect', None], ['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_language_detected_for_header_file(self):
response = Response()
response.status_code = 500
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
entity = 'tests/samples/codefiles/see.h'
args = ['--file', entity, '--config', config, '--time', now]
retval = execute(args)
self.assertEquals(retval, 102)
language = u('C')
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
entity = 'tests/samples/codefiles/seeplusplus.h'
args[1] = entity
retval = execute(args)
self.assertEquals(retval, 102)
language = u('C++')
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
def test_c_language_detected_for_header_with_c_files_in_folder(self): def test_c_language_detected_for_header_with_c_files_in_folder(self):
response = Response() response = Response()
response.status_code = 500 response.status_code = 500
@ -80,6 +55,22 @@ class LanguagesTestCase(utils.TestCase):
language = u('C++') language = u('C++')
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language) self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
def test_c_not_detected_for_non_header_with_c_files_in_folder(self):
response = Response()
response.status_code = 500
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
entity = 'tests/samples/codefiles/c_and_python/see.py'
args = ['--file', entity, '--config', config, '--time', now]
retval = execute(args)
self.assertEquals(retval, 102)
language = u('Python')
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
def test_guess_language(self): def test_guess_language(self):
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer: with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
mock_guess_lexer.return_value = None mock_guess_lexer.return_value = None

View file

@ -11,6 +11,7 @@
import logging import logging
import os import os
import re
import sys import sys
from .compat import u, open from .compat import u, open
@ -64,13 +65,18 @@ def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
def guess_language(file_name): def guess_language(file_name):
"""Guess lexer and language for a file. """Guess lexer and language for a file.
Returns (language, lexer) tuple where language is a unicode string. Returns a tuple of (language_str, lexer_obj).
""" """
lexer = None
language = get_language_from_extension(file_name) language = get_language_from_extension(file_name)
lexer = smart_guess_lexer(file_name) if language:
if language is None and lexer is not None: lexer = get_lexer(language)
language = u(lexer.name) else:
lexer = smart_guess_lexer(file_name)
if lexer:
language = u(lexer.name)
return language, lexer return language, lexer
@ -151,15 +157,17 @@ def guess_lexer_using_modeline(text):
def get_language_from_extension(file_name): def get_language_from_extension(file_name):
"""Returns a matching language for the given file extension. """Returns a matching language for the given file extension.
When guessed_language is 'C', does not restrict to known file extensions.
""" """
filepart, extension = os.path.splitext(file_name) filepart, extension = os.path.splitext(file_name)
if os.path.exists(u('{0}{1}').format(u(filepart), u('.c'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.C'))): if re.match(r'\.h.*', extension, re.IGNORECASE) or re.match(r'\.c.*', extension, re.IGNORECASE):
return 'C'
if os.path.exists(u('{0}{1}').format(u(filepart), u('.c'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.C'))):
return 'C'
extension = extension.lower()
if extension == '.h':
directory = os.path.dirname(file_name) directory = os.path.dirname(file_name)
available_files = os.listdir(directory) available_files = os.listdir(directory)
available_extensions = list(zip(*map(os.path.splitext, available_files)))[1] available_extensions = list(zip(*map(os.path.splitext, available_files)))[1]
@ -191,7 +199,7 @@ def number_lines_in_file(file_name):
def standardize_language(language, plugin): def standardize_language(language, plugin):
"""Maps a string to the equivalent Pygments language. """Maps a string to the equivalent Pygments language.
Returns a tuple of (language_name, lexer_object). Returns a tuple of (language_str, lexer_obj).
""" """
if not language: if not language: