Skip to content

Commit

Permalink
Better completion display
Browse files Browse the repository at this point in the history
  • Loading branch information
BojanStipic committed Jun 25, 2019
1 parent 3bc4030 commit d4b7b65
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ void lsp_hover(int id, const cJSON *params_json) {
char *text = strdup(buffer.content);
truncate_string(text, document.line, document.character);
const char *symbol_name = extract_last_symbol(text);
char *contents = symbol_info(symbol_name, text);
cJSON *contents = symbol_info(symbol_name, text);
free(text);

if(contents == NULL) {
lsp_send_response(id, NULL);
return;
}
cJSON *result = cJSON_CreateObject();
cJSON_AddStringToObject(result, "contents", contents);
cJSON_AddItemToObject(result, "contents", contents);
lsp_send_response(id, result);
}

Expand Down
28 changes: 7 additions & 21 deletions minic.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void yy_delete_buffer(YY_BUFFER_STATE buffer);

char char_buffer[CHAR_BUFFER_LENGTH];
int severity = ERROR;
const char *types_str[] = { "void", "int", "unsigned int" };

cJSON *_diagnostics = NULL;

Expand Down Expand Up @@ -51,30 +50,15 @@ void parse(cJSON *diagnostics, const char *text) {
_diagnostics = NULL;
}

char* symbol_info(const char *symbol_name, const char *text) {
cJSON* symbol_info(const char *symbol_name, const char *text) {
parse(NULL, text);
int idx = lookup_symbol(symbol_name, VAR|PAR|FUN);
if(idx == -1) {
return NULL;
}
const char *type = types_str[get_type(idx)];
const char *name = get_name(idx);
const char *par_type = "";
if(get_kind(idx) == FUN) {
if(get_atr1(idx) == 1) {
par_type = types_str[get_atr2(idx)];
}
}

char *info = malloc(strlen(type) + strlen(name) + strlen(par_type) + 4);
strcpy(info, type);
strcat(info, " ");
strcat(info, name);
if(get_kind(idx) == FUN) {
strcat(info, "(");
strcat(info, par_type);
strcat(info, ")");
}
char *display = get_display(idx);
cJSON *info = cJSON_CreateString(display);
free(display);
return info;
}

Expand All @@ -93,7 +77,9 @@ cJSON* symbol_completion(const char *symbol_name_part, const char *text) {
for(int i = 0; i < indices_num; i++) {
cJSON *item = cJSON_CreateObject();
cJSON_AddStringToObject(item, "label", get_name(indices[i]));
//cJSON_AddStringToObject(item, "detail", "");
char *detail = get_display(indices[i]);
cJSON_AddStringToObject(item, "detail", detail);
free(detail);
cJSON_AddItemToArray(results, item);
}
return results;
Expand Down
4 changes: 1 addition & 3 deletions minic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ void parse(cJSON *diagnostics, const char *text);

/*
* Parse the `text` string and return info about the specified symbol.
*
* WARNING: Caller is responsible to free the result.
*/
char* symbol_info(const char *symbol_name, const char *text);
cJSON* symbol_info(const char *symbol_name, const char *text);

/*
* Parse the `text` string and return definition location of the specified symbol.
Expand Down
27 changes: 25 additions & 2 deletions symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ int lookup_symbol(const char *name, unsigned kind) {

int lookup_starts_with(int *results, const char *name_part) {
int found_num = 0;
int i;
for(i = first_empty - 1; i > FUN_REG; i--) {
for(int i = first_empty - 1; i > FUN_REG; i--) {
const char *symbol_name = symbol_table[i].name;
if(strstr(symbol_name, name_part) == symbol_name) {
results[found_num] = i;
Expand Down Expand Up @@ -145,6 +144,30 @@ int get_lineno(int index) {
return NO_LINENO;
}

char* get_display(int index) {
const char *types_str[] = { "void", "int", "unsigned int" };

const char *type = types_str[get_type(index)];
const char *name = get_name(index);
const char *par_type = "";
if(get_kind(index) == FUN) {
if(get_atr1(index) == 1) {
par_type = types_str[get_atr2(index)];
}
}

char *display = malloc(strlen(type) + strlen(name) + strlen(par_type) + 4);
strcpy(display, type);
strcat(display, " ");
strcat(display, name);
if(get_kind(index) == FUN) {
strcat(display, "(");
strcat(display, par_type);
strcat(display, ")");
}
return display;
}

void clear_symbols(int begin_index) {
int i;
if(begin_index == first_empty) // Already empty
Expand Down
6 changes: 6 additions & 0 deletions symtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ void set_atr2(int index, unsigned atr2);
unsigned get_atr2(int index);
void set_lineno(int index, int lineno);
int get_lineno(int index);
/*
* Returns display string of a symbol.
*
* WARNING: Caller is responsible to free the result.
*/
char* get_display(int index);

// Removes elements beginning with the specified index.
void clear_symbols(int begin_index);
Expand Down

0 comments on commit d4b7b65

Please sign in to comment.