bad attempt at language huristics

main
Brett 2024-05-25 15:28:24 -04:00
parent 91a01542df
commit 2dccaa154e
3 changed files with 83 additions and 2 deletions

View File

@ -3,5 +3,6 @@
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/blt" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/blt/libraries/parallel-hashmap" vcs="Git" />
</component>
</project>

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(floc VERSION 0.0.8)
project(floc VERSION 0.0.9)
include(FetchContent)
@ -19,6 +19,10 @@ FetchContent_Declare(ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
GIT_TAG v5.0.0
)
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
)
FetchContent_GetProperties(ftxui)
if(NOT ftxui_POPULATED)
@ -26,11 +30,12 @@ if(NOT ftxui_POPULATED)
add_subdirectory(${ftxui_SOURCE_DIR} ${ftxui_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
target_link_libraries(floc ${BLT_TARGET})
target_link_libraries(floc PRIVATE BLT)
target_link_libraries(floc
PRIVATE ftxui::screen
PRIVATE ftxui::dom
PRIVATE ftxui::component
PRIVATE json
)
target_compile_options(floc PRIVATE -Wall -Werror -Wpedantic -Wno-comment)

View File

@ -19,6 +19,13 @@
#ifndef BLT_FLOC_ANALYZER_H
#define BLT_FLOC_ANALYZER_H
#include <string>
#include <string_view>
#include <functional>
#include <vector>
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
/*
* This file provides an analyzer class for counting lines of code in a file along with a recursive parser for finding files
* it also provides a utility class to build a structure which mimics the file system directories alongside relevant code
@ -26,4 +33,72 @@
* maybe split this into a few header files?
*/
namespace blt::floc
{
static inline const constexpr auto EMPTY_FUNCTION = [](std::string_view contents) {
return true;
};
struct associated_file_t
{
std::string file_extension;
std::string file_name;
associated_file_t() = default;
associated_file_t(std::string_view file_extension, std::string_view file_name): file_extension(file_extension), file_name(file_name)
{}
};
class language_t
{
private:
// extension -> file name
blt::hashmap_t<std::string, std::string> language_files;
std::function<bool(std::string_view)> matches_func;
public:
language_t(): matches_func(EMPTY_FUNCTION)
{}
explicit language_t(std::function<bool(std::string_view)> matches_func): matches_func(std::move(matches_func))
{}
inline bool matches(std::string_view file_extension, std::string_view file_contents)
{
return language_files.find(std::string(file_extension)) != language_files.end() && matches_func(file_contents);
}
inline language_t& add(const associated_file_t& file)
{
language_files[file.file_extension] = file.file_name;
return *this;
}
inline language_t& add(std::string_view extension, std::string_view name)
{
return add(associated_file_t{extension, name});
}
inline language_t& add(const std::vector<std::string>& extensions, std::string_view name)
{
for (const auto& v : extensions)
add(v, name);
return *this;
}
};
static inline const std::vector<language_t> BASE_LANGUAGES = {
language_t().add("c", "C Source").add("h", "C/C++ Header"),
language_t().add({"cpp", "cxx", "cc"}, "C++ Source").add("h", "C/C++ Header").add("hpp", "C++ Header"),
language_t().add("java", "Java Source"),
language_t().add("rs", "Rust Source"),
language_t().add({"py", "pyo"}, "Python Source"),
language_t().add("rb", "Ruby Source"),
language_t().add("pl", "Perl Source"),
};
}
#endif // BLT_FLOC_ANALYZER_H