Debian Codegraph @

CodeLite/LSP/SemanticTokensRquest.cpp

raw
1 #include "SemanticTokensRquest.hpp"
2
3 #include "LSP/LSPEvent.h"
4 #include "LSP/basic_types.h"
5 #include "file_logger.h"
6 #include "json_rpc_params.h"
7 #include "wx/vector.h"
8
9 #include <thread>
10 #include <vector>
11
12 LSP::SemanticTokensRquest::SemanticTokensRquest(const wxString& filename)
13 : m_filename(filename)
14 {
15 SetMethod("textDocument/semanticTokens/full");
16 m_params.reset(new SemanticTokensParams());
17 m_params->As<SemanticTokensParams>()->SetTextDocument(filename);
18 }
19
20 LSP::SemanticTokensRquest::~SemanticTokensRquest() {}
21
22 void LSP::SemanticTokensRquest::OnResponse(const LSP::ResponseMessage& response, wxEvtHandler* owner)
23 {
24 // build set of classes, locals so we can colour them
25 if(!owner) {
26 return;
27 }
28
29 std::vector<int> encoded_types;
30 LOG_IF_TRACE { clTRACE() << "OnResponse for SemanticTokensRquest is called" << endl; }
31
32 encoded_types = response["result"]["data"].toIntArray();
33
34 LOG_IF_TRACE { clTRACE() << "Parsing semantic tokens array (" << encoded_types.size() << ")" << endl; }
35
36 // since this is CPU heavy processing, spawn a thread to do the job
37 wxString filename = m_filename;
38 wxString server_name = GetServerName();
39
40 // sanity: each token is represented by a set of 5 integers
41 // { line, startChar, length, tokenType, tokenModifiers}
42 if(encoded_types.size() % 5 != 0) {
43 return;
44 }
45
46 int last_line = 0;
47 int last_column = 0;
48 std::vector<LSP::SemanticTokenRange> semantic_tokens;
49 semantic_tokens.reserve(encoded_types.size() / 5);
50
51 for(size_t i = 0; i < encoded_types.size() / 5; i++) {
52 size_t base_index = 5 * i;
53 LSP::SemanticTokenRange t;
54 // calculate the token line
55 t.line = last_line + encoded_types[base_index];
56
57 // did we change lines?
58 bool changed_line = t.line != last_line;
59
60 // incase we are on a different line, the start_col is relative to 0, otherwise
61 // it is relative to the previous item column
62 t.column = changed_line ? encoded_types[base_index + 1] : encoded_types[base_index + 1] + last_column;
63 t.length = encoded_types[base_index + 2];
64 t.token_type = encoded_types[base_index + 3];
65
66 last_column = t.column;
67 last_line = t.line;
68 semantic_tokens.emplace_back(t);
69 }
70
71 LSPEvent event(wxEVT_LSP_SEMANTICS);
72 event.SetSemanticTokens(semantic_tokens);
73 event.SetFileName(filename);
74 event.SetServerName(GetServerName());
75 owner->AddPendingEvent(event);
76 LOG_IF_DEBUG { clDEBUG() << "Colouring file:" << filename << endl; }
77 }