Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
similar_function_finder.cpp
Go to the documentation of this file.
1#include <iostream>
2
7
8using fm = FormatterManager;
9
10void SimilarFunctionFinder::find_path_that_meets_pattern() {
11 std::vector<Path> paths = similarity_table->get_path_list();
12 for (const auto& _path : paths) {
13 if (_path.contains_given_pattern(function_pattern)) {
14 path = _path;
15 }
16 }
17}
18
19void SimilarFunctionFinder::print_empty_path_message() const {
20 fm::write(TEMPLATE_EMPTY_FUNCTION, SimilarFunctionEmptyEntry{
21 function_pattern
22 }, Format::TEXT);
23}
24
25Function SimilarFunctionFinder::get_reference_function(const Path& path) const {
26 Function function(path);
27 function.load();
28 return function;
29}
30
31void SimilarFunctionFinder::print_reference_path() {
32 fm::write(REFERENCE_PATH_MESSAGE);
33 auto function = get_reference_function(path);
34 function.print_basic_info();
35}
36
37void SimilarFunctionFinder::print_similar_functions(const std::vector<Path>& similar_paths) {
39 similar_paths.size()
40 }, Format::TEXT);
41 fm::write("");
42 for (const auto& similar_path : similar_paths) {
43 auto function = get_reference_function(similar_path);
45 function.print_basic_info();
47 fm::write("");
48 }
49}
50
51void SimilarFunctionFinder::print_similar_functions() {
52 std::vector<Path> similar_paths = similarity_table->get_similar_path_to_the_reference(path);
53
55 print_reference_path();
57 if (open_folder) {
59 fs::path full_path = fs::path{params[0]} / fs::path{path.build_relative_path()};
60 fs::path dir_path = full_path.parent_path();
61 Utils::open_folder(dir_path);
62 return;
63 }
64 print_similar_functions(similar_paths);
65}
66
67void SimilarFunctionFinder::handle_show_mode() {
68 Function ref = get_reference_function(path);
69 ref.print_basic_info();
70 fm::write("");
71 ref.print_code(no_numbers);
72}
73
75 similarity_table = _similarity_table;
76 function_pattern = "";
77}
78
80 open_folder = options.args.count("open") > 0;
81 show_mode = options.args.count("show") > 0;
82 no_numbers = options.args.count("no-numbers") > 0;
83
84 if (options.args.count("help") > 0) {
85 return true;
86 }
87
88 if (options.extra_args.empty()) {
89 throw CLIError("Similar Function Finder Command expect one parameter, but none was given");
90 return false;
91 }
92
93 return true;
94}
95
97 function_pattern = options.extra_args[0];
98
99 find_path_that_meets_pattern();
100
101 if (path.is_empty()) {
102 print_empty_path_message();
103 return true;
104 }
105
106 if (show_mode) {
107 handle_show_mode();
108 return true;
109 }
110
111 print_similar_functions();
112
113 return true;
114}
Base class for CLI-related errors.
Definition cli_error.hpp:10
const CliOption * options() const final
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
Represents a code function with its content and metadata.
Definition function.hpp:30
void print_code(bool no_numbers=false)
Definition function.cpp:80
void print_basic_info()
Prints formatted function information to console.
Definition function.cpp:65
Path manipulation class for tool-specific directory structure.
Definition path.hpp:27
std::string build_relative_path() const
Builds relative path portion.
Definition path.cpp:101
bool is_empty() const
Checks if path is empty.
Definition path.cpp:29
static std::vector< std::string > read_current_run_params()
read preprocessing parameters runs
SimilarFunctionFinder(Similarity_Table *_similarity_table)
Constructs finder and initiates search.
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
bool run(const ParsedOptions &options) override
Handles similar function finding command.
Represents a similarity graph between functions (paths).
const std::vector< Path > & get_path_list() const
Gets list of all known paths.
std::vector< Path > get_similar_path_to_the_reference(const Path &reference)
Gets paths similar to reference path.
void open_folder(const std::string &path)
Definition utils.cpp:183
const std::string LIMITER_PRINT
Constant string used as a visual delimiter/separator in prints.
Definition utils.hpp:33
Similar function locator interface.