summaryrefslogtreecommitdiff
path: root/lsp.c
diff options
context:
space:
mode:
Diffstat (limited to 'lsp.c')
-rw-r--r--lsp.c58
1 files changed, 43 insertions, 15 deletions
diff --git a/lsp.c b/lsp.c
index 1b6d214..4499cfe 100644
--- a/lsp.c
+++ b/lsp.c
@@ -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;
}