From 540557e9984ce198bf081511258547bfc48a0802 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Mon, 11 Mar 2019 11:20:23 +0900 Subject: [PATCH] Detect C++ language from all C++ file extensions --- tests/samples/codefiles/c_and_cxx/cpp.h | 0 tests/samples/codefiles/c_and_cxx/empty.h | 0 tests/samples/codefiles/c_and_cxx/empty.m | 0 tests/samples/codefiles/c_and_cxx/empty.mm | 0 tests/samples/codefiles/c_and_cxx/foo.cxx | 0 tests/samples/codefiles/c_and_cxx/non_empty.cxx | 8 ++++++++ tests/samples/codefiles/c_and_cxx/non_empty.h | 0 tests/samples/codefiles/c_and_cxx/objective-c.h | 0 tests/samples/codefiles/c_and_cxx/objective-c.m | 0 tests/samples/codefiles/c_and_cxx/objective-cpp.h | 0 tests/samples/codefiles/c_and_cxx/objective-cpp.mm | 0 tests/samples/codefiles/c_and_cxx/see.c | 0 tests/samples/codefiles/c_and_cxx/see.h | 0 tests/test_languages.py | 6 ++++++ wakatime/stats.py | 9 +++++++-- 15 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/samples/codefiles/c_and_cxx/cpp.h create mode 100644 tests/samples/codefiles/c_and_cxx/empty.h create mode 100644 tests/samples/codefiles/c_and_cxx/empty.m create mode 100644 tests/samples/codefiles/c_and_cxx/empty.mm create mode 100644 tests/samples/codefiles/c_and_cxx/foo.cxx create mode 100644 tests/samples/codefiles/c_and_cxx/non_empty.cxx create mode 100644 tests/samples/codefiles/c_and_cxx/non_empty.h create mode 100644 tests/samples/codefiles/c_and_cxx/objective-c.h create mode 100644 tests/samples/codefiles/c_and_cxx/objective-c.m create mode 100644 tests/samples/codefiles/c_and_cxx/objective-cpp.h create mode 100644 tests/samples/codefiles/c_and_cxx/objective-cpp.mm create mode 100644 tests/samples/codefiles/c_and_cxx/see.c create mode 100644 tests/samples/codefiles/c_and_cxx/see.h diff --git a/tests/samples/codefiles/c_and_cxx/cpp.h b/tests/samples/codefiles/c_and_cxx/cpp.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/empty.h b/tests/samples/codefiles/c_and_cxx/empty.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/empty.m b/tests/samples/codefiles/c_and_cxx/empty.m new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/empty.mm b/tests/samples/codefiles/c_and_cxx/empty.mm new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/foo.cxx b/tests/samples/codefiles/c_and_cxx/foo.cxx new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/non_empty.cxx b/tests/samples/codefiles/c_and_cxx/non_empty.cxx new file mode 100644 index 0000000..9c0368d --- /dev/null +++ b/tests/samples/codefiles/c_and_cxx/non_empty.cxx @@ -0,0 +1,8 @@ +#include +#include + +main() +{ + printf("Hello World\n"); + return 0; +} diff --git a/tests/samples/codefiles/c_and_cxx/non_empty.h b/tests/samples/codefiles/c_and_cxx/non_empty.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/objective-c.h b/tests/samples/codefiles/c_and_cxx/objective-c.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/objective-c.m b/tests/samples/codefiles/c_and_cxx/objective-c.m new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/objective-cpp.h b/tests/samples/codefiles/c_and_cxx/objective-cpp.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/objective-cpp.mm b/tests/samples/codefiles/c_and_cxx/objective-cpp.mm new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/see.c b/tests/samples/codefiles/c_and_cxx/see.c new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/c_and_cxx/see.h b/tests/samples/codefiles/c_and_cxx/see.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_languages.py b/tests/test_languages.py index 6486a64..c94e317 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -69,6 +69,12 @@ class LanguagesTestCase(utils.TestCase): entity='c_and_cpp/cpp.h', ) + def test_cpp_language_detected_for_header_with_c_and_cxx_files_in_folder(self): + self.shared( + expected_language='C++', + entity='c_and_cxx/cpp.h', + ) + def test_c_not_detected_for_non_header_with_c_files_in_folder(self): self.shared( expected_language='Python', diff --git a/wakatime/stats.py b/wakatime/stats.py index 258e343..3b36e0d 100644 --- a/wakatime/stats.py +++ b/wakatime/stats.py @@ -25,6 +25,7 @@ from .packages.pygments.lexers import ( _fn_matches, basename, ClassNotFound, + CppLexer, find_lexer_class, get_lexer_by_name, ) @@ -181,8 +182,12 @@ def get_language_from_extension(file_name): return 'Objective-C++' available_extensions = extensions_in_same_folder(file_name) - if '.cpp' in available_extensions: - return 'C++' + + for ext in CppLexer.filenames: + ext = ext.lstrip('*') + if ext in available_extensions: + return 'C++' + if '.c' in available_extensions: return 'C'