Debian Codegraph @

CodeLite/LSP/DocumentSymbolsRequest.cpp

raw
1 #include "LSP/DocumentSymbolsRequest.hpp"
2
3 #include "LSP/LSPEvent.h"
4 #include "event_notifier.h"
5 #include "file_logger.h"
6 #include "json_rpc_params.h"
7
8 #include <algorithm>
9 #include <thread>
10
11 namespace
12 {
13 FileLogger& operator<<(FileLogger& logger, const std::vector<LSP::SymbolInformation>& symbols)
14 {
15 wxString s;
16 s << "\n[\n";
17 for(const LSP::SymbolInformation& d : symbols) {
18 s << " " << d.GetContainerName() << "." << d.GetName() << ",\n";
19 }
20 s << "]\n";
21 logger.Append(s, logger.GetRequestedLogLevel());
22 return logger;
23 }
24 } // namespace
25
26 LSP::DocumentSymbolsRequest::DocumentSymbolsRequest(const wxString& filename, size_t context)
27 : m_context(context)
28 {
29 SetMethod("textDocument/documentSymbol");
30 // set the params
31 m_params.reset(new DocumentSymbolParams());
32 m_params->As<DocumentSymbolParams>()->SetTextDocument(TextDocumentIdentifier(filename));
33 }
34
35 LSP::DocumentSymbolsRequest::~DocumentSymbolsRequest() {}
36
37 void LSP::DocumentSymbolsRequest::OnResponse(const LSP::ResponseMessage& const_response, wxEvtHandler* owner)
38 {
39 clDEBUG() << "LSP::DocumentSymbolsRequest::OnResponse() is called!" << endl;
40 LSP::ResponseMessage& response = const_cast<LSP::ResponseMessage&>(const_response);
41 auto json = std::move(response.take());
42 if(!json->toElement().hasNamedObject("result")) {
43 clWARNING() << "LSP::DocumentSymbolsRequest::OnResponse(): invalid 'result' object";
44 return;
45 }
46
47 auto result = json->toElement().namedObject("result");
48 if(result.isArray()) {
49 int size = result.arraySize();
50 if(size == 0) {
51 return;
52 }
53
54 wxString filename = m_params->As<DocumentSymbolParams>()->GetTextDocument().GetPath();
55 auto context = m_context;
56 if(result[0].hasNamedObject("location")) {
57 auto result = json->toElement().namedObject("result");
58 std::vector<LSP::SymbolInformation> symbols;
59 symbols.reserve(size);
60 for(int i = 0; i < size; ++i) {
61 SymbolInformation si;
62 si.FromJSON(result[i]);
63 symbols.push_back(si);
64 }
65
66 // sort the items by line position
67 std::sort(symbols.begin(), symbols.end(),
68 [=](const LSP::SymbolInformation& a, const LSP::SymbolInformation& b) -> int {
69 return a.GetLocation().GetRange().GetStart().GetLine() <
70 b.GetLocation().GetRange().GetStart().GetLine();
71 });
72
73 LOG_IF_TRACE {
74 clDEBUG1() << symbols << endl;
75 }
76
77 // fire event per context
78 if(context & CONTEXT_SEMANTIC_HIGHLIGHT) {
79 QueueEvent(owner, symbols, filename, wxEVT_LSP_DOCUMENT_SYMBOLS_FOR_HIGHLIGHT);
80 }
81 if(context & CONTEXT_QUICK_OUTLINE) {
82 QueueEvent(owner, symbols, filename, wxEVT_LSP_DOCUMENT_SYMBOLS_QUICK_OUTLINE);
83 }
84 if(context & CONTEXT_OUTLINE_VIEW) {
85 QueueEvent(owner, symbols, filename, wxEVT_LSP_DOCUMENT_SYMBOLS_OUTLINE_VIEW);
86 }
87
88 // always fire the wxEVT_LSP_DOCUMENT_SYMBOLS_QUICK_OUTLINE for the EventNotifier
89 // so it might be used by other plugins as well, e.g. "Outline"
90 QueueEvent(EventNotifier::Get(), symbols, filename, wxEVT_LSP_DOCUMENT_SYMBOLS_QUICK_OUTLINE);
91 } else {
92 std::vector<DocumentSymbol> symbols;
93 symbols.reserve(size);
94 for(int i = 0; i < size; ++i) {
95 DocumentSymbol ds;
96 ds.FromJSON(result[i]);
97 symbols.push_back(ds);
98 }
99 wxUnusedVar(symbols);
100 }
101 }
102 }
103
104 void LSP::DocumentSymbolsRequest::QueueEvent(wxEvtHandler* owner, const std::vector<LSP::SymbolInformation>& symbols,
105 const wxString& filename, const wxEventType& event_type)
106 {
107 LSPEvent event{ event_type };
108 event.GetSymbolsInformation().reserve(symbols.size());
109 event.GetSymbolsInformation().insert(event.GetSymbolsInformation().end(), symbols.begin(), symbols.end());
110 event.SetFileName(filename);
111 owner->QueueEvent(event.Clone());
112 }