diff options
author | pommicket <pommicket@gmail.com> | 2022-12-06 12:05:32 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-06 12:05:32 -0500 |
commit | ac3a7a0c40864339b4c94ab3f58f8dd5d144ed2e (patch) | |
tree | 351ddd93af391171a4052bcdb4a11d2f98ba7488 | |
parent | 05862b802237df8bc61b0b45ef1ba4e70b0773f5 (diff) |
more lsp
-rw-r--r-- | json.c | 113 | ||||
-rw-r--r-- | lsp.c | 58 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | util.c | 16 |
4 files changed, 161 insertions, 29 deletions
@@ -63,35 +63,34 @@ static inline bool json_is_space(char c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t'; } -static void json_debug_print_value(const JSON *json, u32 idx) { - JSONValue *value = &json->values[idx]; +static void json_debug_print_value(const JSON *json, const JSONValue *value) { switch (value->type) { case JSON_NULL: printf("null"); break; case JSON_FALSE: printf("false"); break; case JSON_TRUE: printf("true"); break; - case JSON_NUMBER: printf("%f", value->val.number); break; + case JSON_NUMBER: printf("%g", value->val.number); break; case JSON_STRING: { - JSONString *string = &value->val.string; + const JSONString *string = &value->val.string; printf("\"%.*s\"", (int)string->len, json->text + string->pos); } break; case JSON_ARRAY: { - JSONArray *array = &value->val.array; + const JSONArray *array = &value->val.array; printf("["); for (u32 i = 0; i < array->len; ++i) { - json_debug_print_value(json, array->elements + i); + json_debug_print_value(json, &json->values[array->elements + i]); printf(", "); } printf("]"); } break; case JSON_OBJECT: { - JSONObject *obj = &value->val.object; + const JSONObject *obj = &value->val.object; printf("{"); for (u32 i = 0; i < obj->len; ++i) { - json_debug_print_value(json, obj->items + i); + json_debug_print_value(json, &json->values[obj->items + i]); printf(": "); - json_debug_print_value(json, obj->items + obj->len + i); + json_debug_print_value(json, &json->values[obj->items + obj->len + i]); printf(", "); } printf("}"); @@ -355,7 +354,6 @@ static bool json_parse_value(JSON *json, u32 *p_index, JSONValue *val) { static bool json_parse(JSON *json, const char *text) { memset(json, 0, sizeof *json); json->text = text; - // @TODO: is this a good capacity? arr_reserve(json->values, strlen(text) / 8); arr_addp(json->values); // add root JSONValue val = {0}; @@ -377,6 +375,101 @@ static void json_free(JSON *json) { memset(json, 0, sizeof *json); } +static bool json_streq(const JSON *json, const JSONString *string, const char *name) { + const char *p = &json->text[string->pos]; + const char *end = p + string->len; + for (; p < end; ++p, ++name) { + if (*name != *p) + return false; + } + return *name == '\0'; +} + +// returns null if the property `name` does not exist. +static JSONValue json_object_get(const JSON *json, const JSONObject *object, const char *name) { + const JSONValue *items = &json->values[object->items]; + for (u32 i = 0; i < object->len; ++i) { + const JSONValue *this_name = items++; + if (this_name->type == JSON_STRING && json_streq(json, &this_name->val.string, name)) { + return json->values[object->items + object->len + i]; + } + } + return (JSONValue){0}; +} + +// e.g. if json is { "a" : { "b": 3 }}, then json_get(json, "a.b") = 3. +// returns null if there is no such property +static JSONValue json_get(const JSON *json, const char *path) { + char segment[128]; + const char *p = path; + if (!json->values) { + return (JSONValue){0}; + } + JSONValue curr_value = json->values[0]; + while (*p) { + size_t segment_len = strcspn(p, "."); + strn_cpy(segment, sizeof segment, p, segment_len); + if (curr_value.type != JSON_OBJECT) { + return (JSONValue){0}; + } + curr_value = json_object_get(json, &curr_value.val.object, segment); + p += segment_len; + if (*p == '.') ++p; + } + return curr_value; +} + +// turn a json string into a null terminated string. +// this won't be nice if the json string includes \u0000 but that's rare. +// if buf_sz > string->len, the string will fit. +static void json_string_get(const JSON *json, const JSONString *string, char *buf, size_t buf_sz) { + const char *text = json->text; + if (buf_sz == 0) { + assert(0); + return; + } + char *buf_end = buf + buf_sz - 1; + for (u32 i = string->pos, end = string->pos + string->len; i < end && buf < buf_end; ++i) { + if (text[i] != '\\') { + *buf++ = text[i]; + } else { + ++i; + if (i >= end) break; + // escape sequence + switch (text[i]) { + case 'n': *buf++ = '\n'; break; + case 'r': *buf++ = '\r'; break; + case 'b': *buf++ = '\b'; break; + case 't': *buf++ = '\t'; break; + case 'f': *buf++ = '\f'; break; + case '\\': *buf++ = '\\'; break; + case '/': *buf++ = '/'; break; + case '"': *buf++ = '"'; break; + case 'u': { + if ((buf_end - buf) < 4 || i + 5 > end) + goto brk; + ++i; + + char hex[5] = {0}; + hex[0] = text[i++]; + hex[1] = text[i++]; + hex[2] = text[i++]; + hex[3] = text[i++]; + unsigned code_point=0; + sscanf(hex, "%04x", &code_point); + // technically this won't deal with people writing out UTF-16 surrogate halves + // using \u. i dont care. + size_t n = unicode_utf32_to_utf8(buf, code_point); + if (n <= 4) buf += n; + } break; + } + } + } + brk: + *buf = '\0'; +} + + #if __unix__ static void json_test_time_large(const char *filename) { struct timespec start={0},end={0}; @@ -1,5 +1,6 @@ typedef struct { Process process; + char error[256]; } LSP; @@ -11,7 +12,42 @@ static void send_request(LSP *lsp, const char *content) { process_write(&lsp->process, content, content_size); } -void lsp_create(LSP *lsp, const char *analyzer_command) { +static bool recv_response(LSP *lsp, JSON *json) { + static char response_data[500000]; + // i think this should always read all the response data. + long long bytes_read = process_read(&lsp->process, response_data, sizeof response_data); + if (bytes_read < 0) { + strbuf_printf(lsp->error, "Read error."); + return false; + } + response_data[bytes_read] = '\0'; + + char *body_start = strstr(response_data, "\r\n\r\n"); + if (body_start) { + body_start += 4; + if (!json_parse(json, body_start)) { + strbuf_cpy(lsp->error, json->error); + return false; + } + JSONValue result = json_get(json, "result"); + if (result.type == JSON_NULL) { + // uh oh + JSONValue error = json_get(json, "error.message"); + if (error.type == JSON_STRING) { + json_string_get(json, &error.val.string, lsp->error, sizeof lsp->error); + } else { + strbuf_printf(lsp->error, "Server error (no message)"); + } + } + } else { + strbuf_printf(lsp->error, "No response body."); + return false; + } + + return true; +} + +bool lsp_create(LSP *lsp, const char *analyzer_command) { ProcessSettings settings = { .stdin_blocking = true, .stdout_blocking = true @@ -25,19 +61,11 @@ void lsp_create(LSP *lsp, const char *analyzer_command) { "}}", process_get_id()); send_request(lsp, init_request); - // @TODO: recv_response - char response_text[4096] = {0}; - process_read(&lsp->process, response_text, sizeof response_text); - char *rnrn = strstr(response_text, "\r\n\r\n"); - if (!rnrn) { - //@TODO delete me - printf("no analyzer ):\n"); - exit(0); - } JSON json = {0}; - printf("%s\n",rnrn+4); - if (!json_parse(&json, rnrn + 4)) - printf("fail : %s\n",json.error); -// json_debug_print(&json);printf("\n"); - + if (!recv_response(lsp, &json)) { + return false; + } + JSONValue response = json_get(&json, "result.capabilities.textDocumentSync.openClose"); + json_debug_print_value(&json, &response);printf("\n"); + return true; } @@ -284,7 +284,8 @@ int main(int argc, char **argv) { // @TODO TEMPORARY { LSP lsp={0}; - lsp_create(&lsp, "rust-analyzer"); + if (!lsp_create(&lsp, "rust-analyzer")) + printf("%s\n",lsp.error); exit(0); } @@ -134,9 +134,14 @@ static void str_cat(char *dst, size_t dst_sz, char const *src) { } // safer version of strncpy. dst_sz includes a null terminator. -static void str_cpy(char *dst, size_t dst_sz, char const *src) { - size_t srclen = strlen(src); - size_t n = srclen; // number of bytes to copy +static void strn_cpy(char *dst, size_t dst_sz, char const *src, size_t src_len) { + size_t n = src_len; // number of bytes to copy + for (size_t i = 0; i < n; ++i) { + if (src[i] == '\0') { + n = i; + break; + } + } if (dst_sz == 0) { assert(0); @@ -149,6 +154,11 @@ static void str_cpy(char *dst, size_t dst_sz, char const *src) { dst[n] = 0; } +// safer version of strcpy. dst_sz includes a null terminator. +static void str_cpy(char *dst, size_t dst_sz, char const *src) { + strn_cpy(dst, dst_sz, src, SIZE_MAX); +} + #define strbuf_cpy(dst, src) str_cpy(dst, sizeof dst, src) #define strbuf_cat(dst, src) str_cat(dst, sizeof dst, src) |