summaryrefslogtreecommitdiff
path: root/ide-format.c
blob: 70103c7939663719520ebf97eaef9350552e535b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ted-internal.h"

struct Formatting {
	LSPServerRequestID last_request_id;
};

void format_init(Ted *ted) {
	ted->formatting = calloc(1, sizeof (Formatting));
}

static void format_common(Ted *ted, bool selection) {
	Formatting *formatting = ted->formatting;
	ted_cancel_lsp_request(ted, &formatting->last_request_id);
	TextBuffer *buffer = ted_active_buffer(ted);
	if (!buffer) return;
	if (selection && !buffer_has_selection(buffer)) return;
	LSP *lsp = buffer_lsp(buffer);
	if (!lsp) return;
	Settings *settings = buffer_settings(buffer);
	LSPRequest request = {
		.type = selection ? LSP_REQUEST_RANGE_FORMATTING : LSP_REQUEST_FORMATTING
	};
	LSPRequestFormatting *req_data = &request.data.formatting;
	req_data->document = buffer_lsp_document_id(buffer);
	req_data->indent_with_spaces = settings->indent_with_spaces;
	req_data->tab_width = settings->tab_width;
	if (selection) {
		req_data->use_range = true;
		req_data->range = buffer_selection_as_lsp_range(buffer);
	}
	formatting->last_request_id = lsp_send_request(lsp, &request);
}

void format_selection(Ted *ted) {
	format_common(ted, true);
}

void format_file(Ted *ted) {
	format_common(ted, false);
}

void format_cancel_request(Ted *ted) {
	Formatting *formatting = ted->formatting;
	ted_cancel_lsp_request(ted, &formatting->last_request_id);
}

void format_process_lsp_response(Ted *ted, const LSPResponse *response) {
	Formatting *formatting = ted->formatting;
	const LSPRequest *request = &response->request;
	if (request->id != formatting->last_request_id.id)
		return;
	if (!(request->type == LSP_REQUEST_RANGE_FORMATTING
		|| request->type == LSP_REQUEST_FORMATTING)) {
		return;
	}
	TextBuffer *buffer = ted->active_buffer;
	if (!buffer) return;
	if (buffer_lsp_document_id(buffer) != request->data.formatting.document)
		return; // switched document

	const LSPResponseFormatting *f = &response->data.formatting;
	arr_foreach_ptr(f->edits, const LSPTextEdit, edit) {
		buffer_apply_lsp_text_edit(buffer, response, edit);
	}
}

void format_quit(Ted *ted) {
	free(ted->formatting);
	ted->formatting = NULL;
}