Skip to content

Commit

Permalink
🎨 make case check an option
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed May 9, 2024
1 parent 90444cf commit 4287d6b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.o
*.a
.d
gwfmt
3 changes: 2 additions & 1 deletion include/gwfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ typedef struct GwfmtState {
bool header;
bool use_tabs;
bool error;
bool fix;
bool fix_case;
bool check_case;
} GwfmtState;

void gwfmt_state_init(GwfmtState *ls);
Expand Down
21 changes: 17 additions & 4 deletions src/gwfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ enum {
// MARK,
EXPAND,
MINIFY,
CASING,
FIX_CASING,
CHECK_CASING,
CONFIG,
COLOR,
NOPTIONS
Expand All @@ -110,7 +111,9 @@ static void setup_options(cmdapp_t *app, cmdopt_t *opt) {
cmdapp_set(app, 'm', "minify", CMDOPT_TAKESARG, NULL, "minimize input",
"bool", &opt[MINIFY]);
cmdapp_set(app, 'f', "fix", CMDOPT_TAKESARG, NULL, "fix casing",
"bool", &opt[CASING]);
"bool", &opt[FIX_CASING]);
cmdapp_set(app, 'w', "fix", CMDOPT_TAKESARG, NULL, "check casing",
"bool", &opt[CHECK_CASING]);
cmdapp_set(app, 'C', "config", CMDOPT_TAKESARG, NULL, "config file",
"filename", &opt[CONFIG]);
cmdapp_set(app, 'c', "color", CMDOPT_TAKESARG, NULL, "enable or disable {R}c{G}o{B}l{M}o{Y}r{C}s{0}",
Expand Down Expand Up @@ -179,7 +182,10 @@ static void myproc(void *data, cmdopt_t *option, const char *arg) {
ls->minimize = arg2bool(option->value);
break;
case 'f':
ls->fix = arg2bool(option->value);
ls->fix_case = arg2bool(option->value);
break;
case 'w':
ls->check_case = arg2bool(option->value);
break;
case 'C':
run_config(ls, option->value);
Expand Down Expand Up @@ -221,7 +227,14 @@ int main(int argc, char **argv) {
SymTable * st = new_symbol_table(mp, 65347); // could be smaller
PPArg ppa = {.fmt = 1};
pparg_ini(mp, &ppa);
struct GwfmtState ls = {.color = isatty(1) ? COLOR_AUTO : COLOR_NEVER, .show_line = true, .header = true, .nindent = 2, .ppa = &ppa, .minimize = false };
struct GwfmtState ls = {
.color = isatty(1) ? COLOR_AUTO : COLOR_NEVER,
.show_line = true,
.header = true,
.nindent = 2,
.ppa = &ppa,
.minimize = false,
.check_case = true};
tcol_override_color_checks(ls.color);
gwfmt_state_init(&ls);
text_init(&ls.text, mp);
Expand Down
6 changes: 4 additions & 2 deletions src/gwion_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ runtime_bool_func(pretty, pretty);
runtime_bool_func(show_lines, show_line);
runtime_bool_func(header, header);
runtime_bool_func(use_tabs, use_tabs);
runtime_bool_func(fix, fix);
runtime_bool_func(fix_case, fix_case);
runtime_bool_func(check_case, check_case);

static MFUN(fmt_color) {
struct GwfmtState *ls = **(struct GwfmtState***)MEM(0);
Expand Down Expand Up @@ -93,7 +94,8 @@ GWION_IMPORT(GwFmt) {
import_bool_fun(header);
import_bool_fun(show_lines);
import_bool_fun(use_tabs);
import_bool_fun(fix);
import_bool_fun(fix_case);
import_bool_fun(check_case);

GWI_B(gwi_func_ini(gwi, "void", "color"));
GWI_B(gwi_func_arg(gwi, "ColorWhen", "arg"));
Expand Down
7 changes: 4 additions & 3 deletions src/lint.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ void flow(Gwfmt *a, const char *kw) {
ANN static void check_tag(Gwfmt *a, const Tag * tag, const CaseType ct,
char* color) {
const Casing casing = a->ls->config->cases[ct];
if(!casing.check(s_name(tag->sym))) {
if(a->ls->check_case && !casing.check(s_name(tag->sym))) {
a->ls->error = true;
if(!a->ls->fix) {
if(!a->ls->fix_case) {
char main[256];
char info[256];
snprintf(main, sizeof(main) - 1, "invalid {Y+}%s{0} casing", ct_name[ct]);
snprintf(info, sizeof(info) - 1, "should be {W+}%s{0} case", casing.name);
gwlog_error(main, info, a->filename, tag->loc, 0);
printf("filename: %s\n", a->filename);
gwlog_error(main, info, a->filename ?: "/dev/null", tag->loc, 0);
} else {
char buf[256];
const char *name = s_name(tag->sym);
Expand Down

0 comments on commit 4287d6b

Please sign in to comment.