Skip to content

Commit

Permalink
feat: Tabled rule view in cli
Browse files Browse the repository at this point in the history
Fixes #68
  • Loading branch information
lyxell committed Jul 6, 2021
1 parent 525b8e9 commit fdbd100
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
7 changes: 4 additions & 3 deletions scripts/generate_rule_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
function generate {
echo "#include <vector>"
echo "#include <tuple>"
echo "#include <unordered_map>"
echo "#include <string>"
echo ""
echo "std::vector<std::tuple<std::string, std::string, std::string, std::string>> rule_data = {"
echo "std::unordered_map<std::string, std::tuple<std::string, std::string, std::string>> rule_data = {"

for f in ../src/rules/*; do
json_file="$f/data.json"
name=$(basename "$f")
if [ -f "$json_file" ]; then

printf " {\"$name\","
printf " {\"$name\",{"

jq -c '.sonar.id, .pmd.id, .description' $json_file | tr '\n' ','

echo "},"
echo "}},"

fi
done
Expand Down
36 changes: 20 additions & 16 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace fs = std::filesystem;

using rewrite_collection = std::vector<std::tuple<std::string, std::string, std::string, bool>>;

extern std::vector<std::tuple<std::string, std::string, std::string, std::string>> rule_data;
extern std::unordered_map<std::string, std::tuple<std::string, std::string, std::string>> rule_data;

struct options_t {
bool accept_all;
Expand Down Expand Up @@ -592,27 +592,31 @@ int main(int argc, char** argv) {
rules[std::get<1>(rewrite)].emplace_back(rewrite);
}
std::vector<std::string> keys;
std::vector<std::string> options;
std::vector<std::tuple<std::string,std::string,std::string>> columns;
for (auto& [rule, rws] : rules) {
size_t accepted = 0;
for (auto rw : rws) {
if (std::get<3>(rw)) accepted++;
}
keys.emplace_back(rule);
std::string description = rule;
for (auto [id, squid, pmdid, desc] : rule_data) {
if (id == rule) {
description = fmt::format("{} • {}", desc, squid);
break;
}
}
auto description = std::get<2>(rule_data[rule]);
size_t accepted = std::count_if(rws.begin(), rws.end(), [](auto rw) {
return std::get<3>(rw);
});
std::string status;
if (accepted > 0) {
status = fmt::format(fg(fmt::terminal_color::green), " ({}/{})", accepted, rws.size());
columns.emplace_back(description,
std::get<0>(rule_data[rule]),
fmt::format(fg(fmt::terminal_color::green), "{}/{}", accepted, rws.size()));
} else {
status = fmt::format(" ({}/{})", accepted, rws.size());
columns.emplace_back(description,
std::get<0>(rule_data[rule]),
fmt::format("{}/{}", accepted, rws.size()));
}
options.emplace_back(description + status);
}
size_t left_column_width = 0;
for (auto [l, m, r] : columns) {
left_column_width = std::max(left_column_width, l.size());
}
std::vector<std::string> options;
for (auto [l, m, r] : columns) {
options.emplace_back(fmt::format("{0:<{3}} {1:5} {2}", l, m, r, left_column_width));
}
auto rule_selection = multi_choice("Which rule would you like to review?", options, true);
if (rule_selection == -1) break;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/fix_raw_use_of_generic_class/data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "Fix raw usage of generic class",
"description": "Fix raw use of generic class",
"sonar": {
"id": "S3740",
"url": "https://rules.sonarsource.com/java/RSPEC-3740"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* return [:id]; */

/* Post return [:initializer]; */
rewrite("remove_unnecessary_variable_declarations_before_return_statement", filename, start, end, cat("return ", initializer_str, ";")) :-
rewrite("remove_unnecessary_declarations_above_return_statements", filename, start, end, cat("return ", initializer_str, ";")) :-
local_variable_declaration_statement(id, declaration),
filename_of(id, filename),
starts_at(id, start),
Expand Down
2 changes: 1 addition & 1 deletion src/rules/simplify_code_using_collection_isempty/data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "Simplify code using Collection.isEmpty",
"description": "Simplify code using Collection::isEmpty",
"sonar": {
"id": "S1155",
"url": "https://rules.sonarsource.com/java/RSPEC-1155"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "Simplify code using Map.computeIfAbsent",
"description": "Simplify code using Map::computeIfAbsent",
"sonar": {
"id": "S3824",
"url": "https://rules.sonarsource.com/java/RSPEC-3824"
Expand Down

0 comments on commit fdbd100

Please sign in to comment.