// retoor <retoor@molodetz.nl>
#include "tool.h"
#include "r_config.h"
#include "bash_executor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct json_object *code_grep_get_description(void);
static char *code_grep_execute(tool_t *self, struct json_object *args);
static void code_grep_print_action(const char *name, struct json_object *args);
static struct json_object *code_symbol_find_get_description(void);
static char *code_symbol_find_execute(tool_t *self, struct json_object *args);
static void code_symbol_find_print_action(const char *name, struct json_object *args);
static const tool_vtable_t code_grep_vtable = {
.get_description = code_grep_get_description,
.execute = code_grep_execute,
.print_action = code_grep_print_action
};
static const tool_vtable_t code_symbol_find_vtable = {
.get_description = code_symbol_find_get_description,
.execute = code_symbol_find_execute,
.print_action = code_symbol_find_print_action
};
static tool_t code_grep_tool = { .vtable = &code_grep_vtable, .name = "code_grep" };
static tool_t code_symbol_find_tool = { .vtable = &code_symbol_find_vtable, .name = "code_symbol_find" };
tool_t *tool_code_grep_create(void) { return &code_grep_tool; }
tool_t *tool_code_symbol_find_create(void) { return &code_symbol_find_tool; }
static void code_grep_print_action(const char *name, struct json_object *args) {
(void)name;
struct json_object *pattern;
if (json_object_object_get_ex(args, "pattern", &pattern)) {
fprintf(stderr, " -> Grepping for: %s\n", json_object_get_string(pattern));
}
}
static char *code_grep_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *pattern_obj, *path_obj;
if (!json_object_object_get_ex(args, "pattern", &pattern_obj)) {
return strdup("Error: missing 'pattern' argument");
}
const char *pattern = json_object_get_string(pattern_obj);
const char *path = ".";
if (json_object_object_get_ex(args, "path", &path_obj)) {
path = json_object_get_string(path_obj);
}
char command[4096];
snprintf(command, sizeof(command), "grep -rnH --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=build -E \"%s\" %s | head -n 100", pattern, path);
return r_bash_execute(command, false, 300);
}
static void code_symbol_find_print_action(const char *name, struct json_object *args) {
(void)name;
struct json_object *symbol;
if (json_object_object_get_ex(args, "symbol", &symbol)) {
fprintf(stderr, " -> Finding symbol: %s\n", json_object_get_string(symbol));
}
}
static char *code_symbol_find_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *symbol_obj;
if (!json_object_object_get_ex(args, "symbol", &symbol_obj)) {
return strdup("Error: missing 'symbol' argument");
}
const char *symbol = json_object_get_string(symbol_obj);
// Search for common definition patterns
char command[4096];
snprintf(command, sizeof(command),
"grep -rnH --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=build "
"-E \"(function|class|def|struct|enum)[[:space:]]+%s[[:space:]]*[(:{]|^[[:space:]]*%s[[:space:]]*=[[:space:]]*[(]|%s[[:space:]]*\\(\" . | head -n 50",
symbol, symbol, symbol);
return r_bash_execute(command, false, 300);
}
static struct json_object *code_grep_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("code_grep"));
json_object_object_add(function, "description", json_object_new_string("Search for a regex pattern in the codebase."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *pattern = json_object_new_object();
json_object_object_add(pattern, "type", json_object_new_string("string"));
json_object_object_add(pattern, "description", json_object_new_string("Regex pattern to search for."));
json_object_object_add(properties, "pattern", pattern);
struct json_object *path = json_object_new_object();
json_object_object_add(path, "type", json_object_new_string("string"));
json_object_object_add(path, "description", json_object_new_string("Directory to search in (defaults to current)."));
json_object_object_add(properties, "path", path);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("pattern"));
json_object_array_add(required, json_object_new_string("path"));
json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
json_object_object_add(root, "function", function);
return root;
}
static struct json_object *code_symbol_find_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("code_symbol_find"));
json_object_object_add(function, "description", json_object_new_string("Find definitions of a symbol (function, class, etc) in the codebase."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *symbol = json_object_new_object();
json_object_object_add(symbol, "type", json_object_new_string("string"));
json_object_object_add(symbol, "description", json_object_new_string("Name of the symbol to find."));
json_object_object_add(properties, "symbol", symbol);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("symbol"));
json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
json_object_object_add(root, "function", function);
return root;
}