• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoni56 / c-xrefactory / 1712

19 Jan 2026 10:56AM UTC coverage: 81.304% (-3.6%) from 84.87%
1712

push

travis-ci

thoni56
[tests] Fix include path for test_c-xref now we have 3pp'd cJSON

14490 of 17822 relevant lines covered (81.3%)

16115974.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.73
src/lsp_handler.c
1
#include "lsp_handler.h"
2

3
#include <stdlib.h>
4
#include <string.h>
5

6
#include "editor.h"
7
#include "editorbuffer.h"
8
#include "editorbuffertable.h"
9
#include "filetable.h"
10
#include "json_utils.h"
11
#include "log.h"
12
#include "lsp_adapter.h"
13
#include "lsp_sender.h"
14
#include "parsing.h"
15
#include "reference_database.h"
16

17

18
/* Global reference database for the LSP session */
19
static ReferenceDatabase *referenceDatabase = NULL;
20

21
ReferenceDatabase *getReferenceDatabase(void) {
2✔
22
    return referenceDatabase;
2✔
23
}
24

25
void setReferenceDatabase(ReferenceDatabase *db) {
×
26
    referenceDatabase = db;
×
27
}
28

29
static char *filename_from_uri(const char *uri) {
4✔
30
    char *uri_prefix = "file://";
4✔
31
    if (strncmp(uri, uri_prefix, strlen(uri_prefix)) == 0)
4✔
32
        return (char *)&uri[strlen(uri_prefix)];
4✔
33
    else
34
        return NULL;
×
35
}
36

37
void handle_initialize(JSON *request) {
6✔
38
    log_trace("LSP: Handling 'initialize'");
6✔
39

40
    JSON *response = create_lsp_message_with_id(id_of_request(request));
6✔
41

42
    JSON *result = add_json_item(response, "result");
6✔
43
    JSON *capabilities = add_json_item(result, "capabilities");
6✔
44
    //add_json_bool(capabilities, "codeActionProvider", true);
45
    add_json_bool(capabilities, "definitionProvider", true);
6✔
46
    add_json_string(response, "positionEncoding", "utf-8");
6✔
47

48
    initFileTable(100);
6✔
49
    initEditorBufferTable();
6✔
50

51
    /* Initialize parsing subsystem */
52
    initializeParsingSubsystem();
6✔
53

54
    /* Create the reference database for this LSP session */
55
    referenceDatabase = createReferenceDatabase();
6✔
56
    log_trace("LSP: Reference database created");
6✔
57

58
    send_response_and_delete(response);
6✔
59
}
6✔
60

61
void handle_initialized(JSON *notification) {
4✔
62
    /* Just ignore it for now... */
63
}
4✔
64

65
/* What code actions are available at this location? */
66
void handle_code_action(JSON *request) {
1✔
67
    log_trace("LSP: Handling 'textDocument/codeAction'");
1✔
68

69
    JSON *response = create_lsp_message_with_id(id_of_request(request));
1✔
70

71
    JSON *actions = add_json_array_as(response, "result");
1✔
72

73
    add_lsp_action(actions, "Noop Action", "quickfix");
1✔
74

75
    JSON *insert_dummy_text_action = add_lsp_action(actions, "Insert Dummy Text", "quickfix");
1✔
76

77
    JSON *edit = add_json_item(insert_dummy_text_action, "edit");
1✔
78
    JSON *changes = add_json_item(edit, "changes");
1✔
79

80
    JSON *uri = add_json_array_as(changes, get_lsp_uri_string_from_request(request));
1✔
81
    JSON *change = add_json_object_to_array(uri);
1✔
82

83
    JSON *params = get_json_item(request, "params");
1✔
84

85
    int start_line, start_character, end_line, end_character;
86
    get_lsp_range_positions(params, &start_line, &start_character, &end_line, &end_character);
1✔
87

88
    add_lsp_range(change, start_line, start_character, end_line, end_character);
1✔
89

90
    add_lsp_new_text(change, "Dummy Text");
1✔
91

92
    send_response_and_delete(response);
1✔
93
}
1✔
94

95
void handle_execute_command(JSON *request) {
×
96
    log_trace("LSP: Handling 'workspace/executeCommand'");
×
97

98
    JSON *params = get_json_item(request, "params");
×
99
    const JSON *command = get_json_item(params, "command");
×
100
    const char *cmd = command->valuestring;
×
101

102
    if (command && strcmp(cmd, "dummyCommand") == 0) {
×
103
        log_info("LSP: Executing dummy command");
×
104
        // Perform some action or return a result
105
    }
106

107
    JSON *response = cJSON_CreateNull();
×
108
    send_response_and_delete(response);
×
109
}
110

111
void handle_did_open(JSON *notification) {
4✔
112
    log_trace("LSP: Handling 'textDocument/didOpen'");
4✔
113

114
    JSON *params = get_json_item(notification, "params");
4✔
115

116
    JSON *textDocument = get_json_item(params, "textDocument");
4✔
117
    const char *uri = get_json_string_item(textDocument, "uri");
4✔
118
    const char *text = get_json_string_item(textDocument, "text");
4✔
119

120
    log_trace("LSP: Opened file '%s', text = '%s'", uri, text);
4✔
121

122
    char *fileName = filename_from_uri(uri);
4✔
123
    EditorBuffer *buffer = createNewEditorBuffer(fileName, NULL, time(NULL), strlen(text));
4✔
124
    loadTextIntoEditorBuffer(buffer, time(NULL), text);
4✔
125
    buffer->textLoaded = true;  /* Mark text as loaded for getOpenedAndLoadedEditorBuffer() */
4✔
126

127
    /* Parse the file to populate the reference database.
128
     * Language is derived from filename extension by parseToCreateReferences(). */
129
    log_trace("LSP: Parsing file '%s'", buffer->fileName);
4✔
130
    parseToCreateReferences(buffer->fileName);
4✔
131
    log_trace("LSP: Finished parsing file '%s'", buffer->fileName);
4✔
132
}
4✔
133

134

135
void handle_definition_request(JSON *request) {
3✔
136
    log_trace("LSP: Handling 'textDocument/definition'");
3✔
137

138
    JSON *params = get_json_item(request, "params");
3✔
139
    JSON *textDocument = get_json_item(params, "textDocument");
3✔
140
    char *uri = get_json_string_item(textDocument, "uri");
3✔
141
    JSON *position = get_json_item(params, "position");
3✔
142

143
    (void)uri;
144
    (void)position;
145

146
    JSON *definition = findDefinition(uri, position);
3✔
147

148
    JSON *response = create_lsp_response(id_of_request(request), definition);
3✔
149

150
    send_response_and_delete(response);
3✔
151
}
3✔
152

153
void handle_cancel(JSON *notification) {
×
154
    // For now there is nothing that can be cancelled
155
}
156

157
void handle_shutdown(JSON *request) {
4✔
158
    log_trace("LSP: Handling 'shutdown'");
4✔
159

160
    /* Destroy the reference database */
161
    if (referenceDatabase != NULL) {
4✔
162
        destroyReferenceDatabase(referenceDatabase);
4✔
163
        referenceDatabase = NULL;
4✔
164
        log_trace("LSP: Reference database destroyed");
4✔
165
    }
166

167
    JSON *response = create_lsp_message_with_id(id_of_request(request));
4✔
168
    cJSON_AddNullToObject(response, "result");
4✔
169

170
    send_response_and_delete(response);
4✔
171
}
4✔
172

173
void handle_exit(JSON *request) {
5✔
174
    log_trace("LSP: Handling 'exit'");
5✔
175
}
5✔
176

177
void handle_method_not_found(JSON *request) {
1✔
178
    log_trace("LSP: Handling 'method not found'");
1✔
179

180
    JSON *response = create_lsp_message_with_id(id_of_request(request));
1✔
181

182
    JSON *error = cJSON_CreateObject();
1✔
183
    cJSON_AddNumberToObject(error, "code", -32601);
1✔
184
    cJSON_AddStringToObject(error, "message", "Method not found");
1✔
185
    cJSON_AddItemToObject(response, "error", error);
1✔
186

187
    send_response_and_delete(response);
1✔
188
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc