summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-28 11:45:07 -0500
committerpommicket <pommicket@gmail.com>2022-12-28 11:45:07 -0500
commit11df4f10197d67e5b61898bd98cdfccc1159dd26 (patch)
tree7639b03aa348183eb563f6179c2435b5ea28a106
parent47adb1651d35dcc545850916c4a16b747901dba5 (diff)
parsing signature help
-rw-r--r--lsp-parse.c52
-rw-r--r--lsp.c2
-rw-r--r--lsp.h1
-rw-r--r--main.c9
-rw-r--r--signature-help.c46
-rw-r--r--ted.h13
-rw-r--r--unicode.h42
7 files changed, 158 insertions, 7 deletions
diff --git a/lsp-parse.c b/lsp-parse.c
index c340d33..2932302 100644
--- a/lsp-parse.c
+++ b/lsp-parse.c
@@ -201,7 +201,7 @@ static bool parse_completion(LSP *lsp, const JSON *json, LSPResponse *response)
};
double kind = json_object_get_number(json, item_object, "kind");
- if (isnormal(kind) && kind >= LSP_COMPLETION_KIND_MIN && kind <= LSP_COMPLETION_KIND_MAX) {
+ if (isfinite(kind) && kind >= LSP_COMPLETION_KIND_MIN && kind <= LSP_COMPLETION_KIND_MAX) {
item->kind = (LSPCompletionKind)kind;
}
@@ -302,7 +302,7 @@ static bool parse_signature_help(LSP *lsp, const JSON *json, LSPResponse *respon
u32 active_signature = 0;
double active_signature_dbl = json_object_get_number(json, result, "activeSignature");
- if (isnormal(active_signature_dbl))
+ if (isfinite(active_signature_dbl))
active_signature = (u32)active_signature_dbl;
double active_signature_active_parameter = json_object_get_number(json, result, "activeParameter");
@@ -315,6 +315,12 @@ static bool parse_signature_help(LSP *lsp, const JSON *json, LSPResponse *respon
JSONObject signature_in = json_array_get_object(json, signatures, s);
JSONString label = json_object_get_string(json, signature_in, "label");
signature_out->label = lsp_response_add_json_string(response, json, label);
+ size_t label_len_utf16 = unicode_utf16_len(lsp_response_string(response, signature_out->label));
+ if (label_len_utf16 == (size_t)-1) {
+ lsp_set_error(lsp, "Bad UTF-8 in SignatureInformation.label");
+ return false;
+ }
+
JSONString documentation = get_markup_content(json,
json_object_get(json, signature_in, "documentation"));
if (documentation.len)
@@ -323,16 +329,52 @@ static bool parse_signature_help(LSP *lsp, const JSON *json, LSPResponse *respon
JSONArray parameters = json_object_get_array(json, signature_in, "parameters");
u32 active_parameter = U32_MAX;
double active_parameter_dbl = json_object_get_number(json, signature_in, "activeParameter");
- if (isnormal(active_parameter_dbl)) {
+ if (isfinite(active_parameter_dbl)) {
active_parameter = (u32)active_parameter_dbl;
}
if (s == active_signature && active_parameter == U32_MAX &&
- isnormal(active_signature_active_parameter)) {
+ isfinite(active_signature_active_parameter)) {
active_parameter = (u32)active_parameter_dbl;
}
if (active_parameter < parameters.len) {
JSONObject parameter_info = json_array_get_object(json, parameters, active_parameter);
- @TODO
+ JSONValue parameter_label_value = json_object_get(json, parameter_info, "label");
+ u16 start = 0, end = 0;
+ if (parameter_label_value.type == JSON_ARRAY) {
+ JSONArray parameter_label = parameter_label_value.val.array;
+ double start_dbl = json_array_get_number(json, parameter_label, 0);
+ double end_dbl = json_array_get_number(json, parameter_label, 1);
+ if (isfinite(start_dbl) && isfinite(end_dbl)) {
+ lsp_set_error(lsp, "Bad contents of ParameterInfo.label array.");
+ return false;
+ }
+ start = (u16)start_dbl;
+ end = (u16)end_dbl;
+ } else if (parameter_label_value.type == JSON_STRING) {
+ JSONString parameter_label = parameter_label_value.val.string;
+ // this is a substring within the label.
+ char *sig_lbl = json_string_get_alloc(json, label);
+ char *param_lbl = json_string_get_alloc(json, parameter_label);
+ const char *pos = strstr(sig_lbl, param_lbl);
+ if (pos) {
+ start = (u16)(pos - sig_lbl);
+ end = (u16)(start + strlen(param_lbl));
+ }
+ free(sig_lbl);
+ free(param_lbl);
+ } else {
+ lsp_set_error(lsp, "Bad type for ParameterInfo.label");
+ return false;
+ }
+
+
+ if (start > end || end > label_len_utf16) {
+ lsp_set_error(lsp, "Bad range for ParameterInfo.label: %u-%u within signature label of length %u", start, end, label.len);
+ return false;
+ }
+
+ signature_out->active_start = start;
+ signature_out->active_end = end;
}
}
diff --git a/lsp.c b/lsp.c
index bd6348a..f2729f5 100644
--- a/lsp.c
+++ b/lsp.c
@@ -1,5 +1,5 @@
// print server-to-client communication
-#define LSP_SHOW_S2C 1
+#define LSP_SHOW_S2C 0
// print client-to-server communication
#define LSP_SHOW_C2S 0
diff --git a/lsp.h b/lsp.h
index 4491cd2..711db2a 100644
--- a/lsp.h
+++ b/lsp.h
@@ -273,6 +273,7 @@ typedef struct {
// to avoid doing a ton of allocations+frees,
// they're all stored here.
char *string_data;
+ // one of these is filled based on request.type
union {
LSPResponseCompletion completion;
LSPResponseSignatureHelp signature_help;
diff --git a/main.c b/main.c
index 3468577..ffc8610 100644
--- a/main.c
+++ b/main.c
@@ -9,7 +9,7 @@
- JSON syntax highlighting
- separate signature-help setting (dont use trigger-characters)
- check if there are any other non-optional/nice-to-have-support-for server-to-client requests
-- do something with lsp->error
+- better non-error window/showMessage(Request)
- document lsp.h and lsp.c.
- maximum queue size for requests/responses just in case?
- idea: configurable timeout
@@ -905,6 +905,7 @@ int main(int argc, char **argv) {
case LSP_RESPONSE: {
LSPResponse *r = &message.u.response;
autocomplete_process_lsp_response(ted, r);
+ signature_help_process_lsp_response(ted, r);
} break;
}
lsp_message_free(&message);
@@ -1038,6 +1039,12 @@ int main(int argc, char **argv) {
buffer_clearerr(buffer);
}
}
+ for (int i = 0; ted->lsps[i]; ++i) {
+ LSP *lsp = ted->lsps[i];
+ if (lsp_get_error(lsp, NULL, 0, false)) {
+ lsp_get_error(lsp, ted->error, sizeof ted->error, true);
+ }
+ }
// check if there's a new error
if (ted_haserr(ted)) {
diff --git a/signature-help.c b/signature-help.c
index df8c564..b6a1105 100644
--- a/signature-help.c
+++ b/signature-help.c
@@ -10,3 +10,49 @@ void signature_help_open(Ted *ted, char32_t trigger) {
s->position = buffer_cursor_pos_as_lsp_document_position(buffer);
lsp_send_request(lsp, &request);
}
+
+static void signature_help_clear(SignatureHelp *help) {
+ for (int i = 0; i < help->signature_count; ++i) {
+ Signature sig = help->signatures[i];
+ free(sig.label_pre);
+ free(sig.label_active);
+ free(sig.label_post);
+ }
+ memset(help->signatures, 0, sizeof help->signatures);
+}
+
+void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response) {
+ if (response->request.type != LSP_REQUEST_SIGNATURE_HELP)
+ return;
+ SignatureHelp *help = &ted->signature_help;
+ const LSPResponseSignatureHelp *lsp_help = &response->data.signature_help;
+ u32 signature_count = arr_len(lsp_help->signatures);
+ if (signature_count > SIGNATURE_HELP_MAX)
+ signature_count = SIGNATURE_HELP_MAX;
+
+ signature_help_clear(help);
+ for (u32 s = 0; s < signature_count; ++s) {
+ Signature *signature = &help->signatures[s];
+ LSPSignatureInformation *lsp_signature = &lsp_help->signatures[s];
+
+ const char *label = lsp_response_string(response, lsp_signature->label);
+ size_t start = unicode_utf16_to_utf8_offset(label, lsp_signature->active_start);
+ size_t end = unicode_utf16_to_utf8_offset(label, lsp_signature->active_end);
+ if (start == (size_t)-1) {
+ assert(0);
+ start = 0;
+ }
+ if (end == (size_t)-1) {
+ assert(0);
+ end = 0;
+ }
+ u32 active_start = (u32)start;
+ u32 active_end = (u32)end;
+ signature->label_pre = strn_dup(label, active_start);
+ signature->label_active = strn_dup(label + active_start, active_end - active_start);
+ signature->label_post = str_dup(label + active_end);
+ printf("%s*%s*%s\n",signature->label_pre,signature->label_active,signature->label_post);
+ }
+
+ help->signature_count = (u16)signature_count;
+}
diff --git a/ted.h b/ted.h
index 7cfdbd4..aa475f8 100644
--- a/ted.h
+++ b/ted.h
@@ -417,9 +417,22 @@ typedef struct {
Rect rect; // rectangle where the autocomplete menu is (needed to avoid interpreting autocomplete clicks as other clicks)
} Autocomplete;
+typedef struct {
+ // displayed normal
+ char *label_pre;
+ // displayed bold
+ char *label_active;
+ // displayed normal
+ char *label_post;
+} Signature;
+
+#define SIGNATURE_HELP_MAX 5
+
// "signature help" (LSP) is thing that shows the current parameter, etc.
typedef struct {
bool open;
+ u16 signature_count;
+ Signature signatures[SIGNATURE_HELP_MAX];
} SignatureHelp;
diff --git a/unicode.h b/unicode.h
index 25ccd6f..1def401 100644
--- a/unicode.h
+++ b/unicode.h
@@ -152,4 +152,46 @@ static size_t unicode_utf32_to_utf8(char *s, char32_t c32) {
return (size_t)-1;
}
}
+
+
+// get the number of UTF-16 codepoints needed to encode `str`.
+// returns (size_t)-1 on bad UTF-8
+static size_t unicode_utf16_len(const char *str) {
+ size_t len = 0;
+ char32_t c = 0;
+ while (*str) {
+ size_t n = unicode_utf8_to_utf32(&c, str, 4);
+ if (n >= (size_t)-2)
+ return (size_t)-1;
+ if (c >= 0x10000)
+ len += 2;
+ else
+ len += 1;
+ str += n;
+ }
+ return len;
+}
+
+// returns the UTF-8 offset from `str` which corresponds to a UTF-16 offset of utf16_offset (rounds down if utf16_offset is in the middle of a codepoint).
+// returns strlen(str) if utf16_offset == unicode_utf16_len(str)
+// returns (size_t)-1 on bad UTF-8, or if utf16_offset > unicode_utf16_len(str)
+static size_t unicode_utf16_to_utf8_offset(const char *str, size_t utf16_offset) {
+ size_t offset = 0;
+ char32_t c = 0;
+ while (*str) {
+ size_t n = unicode_utf8_to_utf32(&c, str, 4);
+ if (n >= (size_t)-2)
+ return (size_t)-1;
+ size_t u = c >= 0x10000 ? 2 : 1;
+ if (utf16_offset < u)
+ return offset;
+ utf16_offset -= u;
+ offset += n;
+ str += n;
+ }
+ if (utf16_offset == 0)
+ return offset;
+ return SIZE_MAX;
+}
+
#endif // UNICODE_H_