summaryrefslogtreecommitdiff
path: root/ide-document-link.c
blob: 9d4803f2d8a0cfb790d67be3b28cef9697bd7115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "ted.h"

void document_link_frame(Ted *ted) {
	DocumentLink *document_link = &ted->document_link;
	
	bool key_down = ted_is_ctrl_down(ted);
	if (!key_down) {
		ted_cancel_lsp_request(ted, &document_link->last_request);
		return;
	}
	
	TextBuffer *buffer = ted->active_buffer;
	if (!buffer)
		return;
	
	LSP *lsp = buffer_lsp(buffer);
	if (!lsp)
		return;
	
	if (!document_link->last_request.id) {
		// send the request
		LSPRequest request = {.type = LSP_REQUEST_DOCUMENT_LINK};
		LSPRequestDocumentLink *lnk = &request.data.document_link;
		lnk->document = buffer_lsp_document_id(buffer);
		document_link->last_request = lsp_send_request(lsp, &request);
	}
}

void document_link_process_lsp_response(Ted *ted, const LSPResponse *response) {
	DocumentLink *document_link = &ted->document_link;
	if (response->request.type != LSP_REQUEST_DOCUMENT_LINK)
		return;
	if (!document_link->last_request.id)
		return; // request was cancelled
	
	bool key_down = ted_is_ctrl_down(ted);
	if (!key_down)
		return;
	TextBuffer *buffer = ted->active_buffer;
	if (!buffer)
		return;
	if (buffer_lsp_document_id(buffer) != response->request.data.document_link.document)
		return; // request was for a different document
	
	const LSPResponseDocumentLink *response_data = &response->data.document_link;
	arr_foreach_ptr(response_data->links, const LSPDocumentLink, link) {
		BufferPos start = buffer_pos_from_lsp(buffer, link->range.start);
		BufferPos end = buffer_pos_from_lsp(buffer, link->range.end);
		printf("%d:%d — %d:%d\t: %s\n",
			start.line, start.index, end.line, end.index,
			lsp_response_string(response, link->target));
		
	}
}