From 7640ecce499fc49cad3d9b9f391cce0b74e435c9 Mon Sep 17 00:00:00 2001
From: pommicket <pommicket@gmail.com>
Date: Sat, 7 Jan 2023 13:01:09 -0500
Subject: improve request cancellation, fix hover bug

---
 ide-hover.c             |  5 ++++-
 lsp.c                   | 35 +++++++++++++++++++++++++++++------
 test/lsp/python/test.py | 10 ++++++++++
 3 files changed, 43 insertions(+), 7 deletions(-)
 create mode 100644 test/lsp/python/test.py

diff --git a/ide-hover.c b/ide-hover.c
index f99c2e7..2be04ca 100644
--- a/ide-hover.c
+++ b/ide-hover.c
@@ -46,7 +46,10 @@ void hover_process_lsp_response(Ted *ted, LSPResponse *response) {
 	TextBuffer *buffer=0;
 	LSPDocumentPosition pos={0};
 	LSP *lsp=0;
-	get_hover_position(ted, &pos, &buffer, &lsp);
+	if (!get_hover_position(ted, &pos, &buffer, &lsp)) {
+		free(hover->text); hover->text = NULL;
+		return;
+	}
 	
 	if (hover->text // we already have hover text
 		&& (
diff --git a/lsp.c b/lsp.c
index 1cbe9e6..34f56ae 100644
--- a/lsp.c
+++ b/lsp.c
@@ -492,8 +492,8 @@ LSP *lsp_create(const char *root_dir, const char *command, const char *configura
 	lsp->log = log;
 	
 	#if DEBUG
-		printf("Starting up LSP %p `%s` in %s\n",
-			(void *)lsp, command, root_dir);
+		printf("Starting up LSP %p (ID %u) `%s` in %s\n",
+			(void *)lsp, (unsigned)lsp->id, command, root_dir);
 	#endif
 	
 	str_hash_table_create(&lsp->document_ids, sizeof(u32));
@@ -657,8 +657,31 @@ bool lsp_covers_path(LSP *lsp, const char *path) {
 void lsp_cancel_request(LSP *lsp, LSPRequestID id) {
 	if (!id) return;
 	if (!lsp) return;
-	
-	LSPRequest request = {.type = LSP_REQUEST_CANCEL};
-	request.data.cancel.id = id;
-	lsp_send_request(lsp, &request);
+	bool sent = false;
+	SDL_LockMutex(lsp->messages_mutex);
+		for (u32 i = 0; i < arr_len(lsp->requests_sent); ++i) {
+			LSPRequest *req = &lsp->requests_sent[i];
+			if (req->id == id) {
+				// we sent this request but haven't received a response
+				sent = true;
+				arr_remove(lsp->requests_sent, i);
+				break;
+			}
+		}
+		
+		for (u32 i = 0; i < arr_len(lsp->messages_client2server); ++i) {
+			LSPMessage *message = &lsp->messages_client2server[i];
+			if (message->type == LSP_REQUEST && message->u.request.id == id) {
+				// we haven't sent this request yet
+				arr_remove(lsp->messages_client2server, i);
+				break;
+			}
+		
+		}
+	SDL_UnlockMutex(lsp->messages_mutex);
+	if (sent) {
+		LSPRequest request = {.type = LSP_REQUEST_CANCEL};
+		request.data.cancel.id = id;
+		lsp_send_request(lsp, &request);
+	}
 }
diff --git a/test/lsp/python/test.py b/test/lsp/python/test.py
new file mode 100644
index 0000000..7b23309
--- /dev/null
+++ b/test/lsp/python/test.py
@@ -0,0 +1,10 @@
+def funciton() -> str:
+	''' this function does things '''
+	print('here')
+	print('there')
+	return 'something something'
+
+funciton()
+funciton()
+funciton()
+funciton()
-- 
cgit v1.2.3