| 1 |
#include "LanguageServerCluster.h" |
| 2 |
|
| 3 |
#include "CompileCommandsGenerator.h" |
| 4 |
#include "LSP/LSPEvent.h" |
| 5 |
#include "LSPOutlineViewDlg.h" |
| 6 |
#include "LanguageServerConfig.h" |
| 7 |
#include "PathConverterDefault.hpp" |
| 8 |
#include "StringUtils.h" |
| 9 |
#include "clEditorBar.h" |
| 10 |
#include "clFileSystemWorkspace.hpp" |
| 11 |
#include "clSFTPEvent.h" |
| 12 |
#include "clSelectSymbolDialog.h" |
| 13 |
#include "clWorkspaceManager.h" |
| 14 |
#include "cl_calltip.h" |
| 15 |
#include "cl_standard_paths.h" |
| 16 |
#include "codelite_events.h" |
| 17 |
#include "ctags_manager.h" |
| 18 |
#include "event_notifier.h" |
| 19 |
#include "file_logger.h" |
| 20 |
#include "globals.h" |
| 21 |
#include "ieditor.h" |
| 22 |
#include "imanager.h" |
| 23 |
#include "languageserver.h" |
| 24 |
#include "macromanager.h" |
| 25 |
#include "macros.h" |
| 26 |
#include "wxCodeCompletionBoxManager.h" |
| 27 |
#include "wxStringHash.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <thread> |
| 31 |
#include <wx/arrstr.h> |
| 32 |
#include <wx/stc/stc.h> |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
struct SymbolClientData : public wxClientData { |
| 37 |
SymbolClientData(const LSP::Location& loc) { location = loc; } |
| 38 |
LSP::Location location; |
| 39 |
}; |
| 40 |
|
| 41 |
// JSON config helpers |
| 42 |
JSONItem json_get_server_config(JSON* root, const wxString& server_name) |
| 43 |
{ |
| 44 |
auto json = root->toElement(); |
| 45 |
if(!json.hasNamedObject("servers")) { |
| 46 |
return JSONItem{ nullptr }; |
| 47 |
} |
| 48 |
auto servers = json["servers"]; |
| 49 |
int count = servers.arraySize(); |
| 50 |
for(int i = 0; i < count; ++i) { |
| 51 |
auto server = servers[i]; |
| 52 |
if(server["name"].toString() != server_name) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
return server; |
| 56 |
} |
| 57 |
return JSONItem{ nullptr }; |
| 58 |
} |
| 59 |
|
| 60 |
wxString json_get_server_config_command(JSON* root, const wxString& server_name) |
| 61 |
{ |
| 62 |
auto json = json_get_server_config(root, server_name); |
| 63 |
if(!json.isOk()) { |
| 64 |
return wxEmptyString; |
| 65 |
} |
| 66 |
return json["command"].toString(); |
| 67 |
} |
| 68 |
|
| 69 |
wxString json_get_server_config_working_directory(JSON* root, const wxString& server_name) |
| 70 |
{ |
| 71 |
auto json = json_get_server_config(root, server_name); |
| 72 |
if(!json.isOk()) { |
| 73 |
return wxEmptyString; |
| 74 |
} |
| 75 |
return json["working_directory"].toString(); |
| 76 |
} |
| 77 |
|
| 78 |
clEnvList_t json_get_server_config_env(JSON* root, const wxString& server_name) |
| 79 |
{ |
| 80 |
auto json = json_get_server_config(root, server_name); |
| 81 |
if(!json.isOk()) { |
| 82 |
return {}; |
| 83 |
} |
| 84 |
|
| 85 |
auto env = json["env"]; |
| 86 |
int env_size = env.arraySize(); |
| 87 |
clEnvList_t env_list; |
| 88 |
for(int n = 0; n < env_size; ++n) { |
| 89 |
auto env_entry = env[n]; |
| 90 |
wxString env_name = env_entry["name"].toString(); |
| 91 |
wxString env_value = env_entry["value"].toString(); |
| 92 |
env_list.push_back({ env_name, env_value }); |
| 93 |
} |
| 94 |
return env_list; |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace |
| 98 |
|
| 99 |
LanguageServerCluster::LanguageServerCluster(LanguageServerPlugin* plugin) |
| 100 |
: m_plugin(plugin) |
| 101 |
{ |
| 102 |
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &LanguageServerCluster::OnWorkspaceClosed, this); |
| 103 |
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_LOADED, &LanguageServerCluster::OnWorkspaceOpen, this); |
| 104 |
EventNotifier::Get()->Bind(wxEVT_FILE_CLOSED, &LanguageServerCluster::OnEditorClosed, this); |
| 105 |
EventNotifier::Get()->Bind(wxEVT_ACTIVE_EDITOR_CHANGED, &LanguageServerCluster::OnActiveEditorChanged, this); |
| 106 |
|
| 107 |
EventNotifier::Get()->Bind(wxEVT_COMPILE_COMMANDS_JSON_GENERATED, |
| 108 |
&LanguageServerCluster::OnCompileCommandsGenerated, this); |
| 109 |
EventNotifier::Get()->Bind(wxEVT_BUILD_ENDED, &LanguageServerCluster::OnBuildEnded, this); |
| 110 |
EventNotifier::Get()->Bind(wxEVT_CMD_OPEN_RESOURCE, &LanguageServerCluster::OnOpenResource, this); |
| 111 |
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_FILES_SCANNED, &LanguageServerCluster::OnWorkspaceScanCompleted, this); |
| 112 |
Bind(wxEVT_LSP_DEFINITION, &LanguageServerCluster::OnSymbolFound, this); |
| 113 |
Bind(wxEVT_LSP_COMPLETION_READY, &LanguageServerCluster::OnCompletionReady, this); |
| 114 |
Bind(wxEVT_LSP_REPARSE_NEEDED, &LanguageServerCluster::OnReparseNeeded, this); |
| 115 |
Bind(wxEVT_LSP_RESTART_NEEDED, &LanguageServerCluster::OnRestartNeeded, this); |
| 116 |
Bind(wxEVT_LSP_INITIALIZED, &LanguageServerCluster::OnLSPInitialized, this); |
| 117 |
Bind(wxEVT_LSP_METHOD_NOT_FOUND, &LanguageServerCluster::OnMethodNotFound, this); |
| 118 |
Bind(wxEVT_LSP_SIGNATURE_HELP, &LanguageServerCluster::OnSignatureHelp, this); |
| 119 |
Bind(wxEVT_LSP_HOVER, &LanguageServerCluster::OnHover, this); |
| 120 |
Bind(wxEVT_LSP_SET_DIAGNOSTICS, &LanguageServerCluster::OnSetDiagnostics, this); |
| 121 |
Bind(wxEVT_LSP_CLEAR_DIAGNOSTICS, &LanguageServerCluster::OnClearDiagnostics, this); |
| 122 |
Bind(wxEVT_LSP_SHOW_QUICK_OUTLINE_DLG, &LanguageServerCluster::OnShowQuickOutlineDlg, this); |
| 123 |
Bind(wxEVT_LSP_DOCUMENT_SYMBOLS_QUICK_OUTLINE, &LanguageServerCluster::OnQuickOutlineView, this); |
| 124 |
Bind(wxEVT_LSP_DOCUMENT_SYMBOLS_OUTLINE_VIEW, &LanguageServerCluster::OnOulineViewSymbols, this); |
| 125 |
Bind(wxEVT_LSP_DOCUMENT_SYMBOLS_FOR_HIGHLIGHT, &LanguageServerCluster::OnDocumentSymbolsForHighlight, this); |
| 126 |
Bind(wxEVT_LSP_SEMANTICS, &LanguageServerCluster::OnSemanticTokens, this); |
| 127 |
Bind(wxEVT_LSP_LOGMESSAGE, &LanguageServerCluster::OnLogMessage, this); |
| 128 |
m_remoteHelper.reset(new CodeLiteRemoteHelper); |
| 129 |
} |
| 130 |
|
| 131 |
LanguageServerCluster::~LanguageServerCluster() |
| 132 |
{ |
| 133 |
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &LanguageServerCluster::OnWorkspaceClosed, this); |
| 134 |
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_LOADED, &LanguageServerCluster::OnWorkspaceOpen, this); |
| 135 |
EventNotifier::Get()->Unbind(wxEVT_FILE_CLOSED, &LanguageServerCluster::OnEditorClosed, this); |
| 136 |
EventNotifier::Get()->Unbind(wxEVT_ACTIVE_EDITOR_CHANGED, &LanguageServerCluster::OnActiveEditorChanged, this); |
| 137 |
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_FILES_SCANNED, &LanguageServerCluster::OnWorkspaceScanCompleted, this); |
| 138 |
EventNotifier::Get()->Unbind(wxEVT_COMPILE_COMMANDS_JSON_GENERATED, |
| 139 |
&LanguageServerCluster::OnCompileCommandsGenerated, this); |
| 140 |
EventNotifier::Get()->Unbind(wxEVT_BUILD_ENDED, &LanguageServerCluster::OnBuildEnded, this); |
| 141 |
|
| 142 |
EventNotifier::Get()->Unbind(wxEVT_CMD_OPEN_RESOURCE, &LanguageServerCluster::OnOpenResource, this); |
| 143 |
|
| 144 |
Unbind(wxEVT_LSP_SHOW_QUICK_OUTLINE_DLG, &LanguageServerCluster::OnShowQuickOutlineDlg, this); |
| 145 |
Unbind(wxEVT_LSP_DEFINITION, &LanguageServerCluster::OnSymbolFound, this); |
| 146 |
Unbind(wxEVT_LSP_COMPLETION_READY, &LanguageServerCluster::OnCompletionReady, this); |
| 147 |
Unbind(wxEVT_LSP_REPARSE_NEEDED, &LanguageServerCluster::OnReparseNeeded, this); |
| 148 |
Unbind(wxEVT_LSP_RESTART_NEEDED, &LanguageServerCluster::OnRestartNeeded, this); |
| 149 |
Unbind(wxEVT_LSP_INITIALIZED, &LanguageServerCluster::OnLSPInitialized, this); |
| 150 |
Unbind(wxEVT_LSP_METHOD_NOT_FOUND, &LanguageServerCluster::OnMethodNotFound, this); |
| 151 |
Unbind(wxEVT_LSP_SIGNATURE_HELP, &LanguageServerCluster::OnSignatureHelp, this); |
| 152 |
Unbind(wxEVT_LSP_HOVER, &LanguageServerCluster::OnHover, this); |
| 153 |
Unbind(wxEVT_LSP_SET_DIAGNOSTICS, &LanguageServerCluster::OnSetDiagnostics, this); |
| 154 |
Unbind(wxEVT_LSP_CLEAR_DIAGNOSTICS, &LanguageServerCluster::OnClearDiagnostics, this); |
| 155 |
Unbind(wxEVT_LSP_DOCUMENT_SYMBOLS_QUICK_OUTLINE, &LanguageServerCluster::OnQuickOutlineView, this); |
| 156 |
Unbind(wxEVT_LSP_DOCUMENT_SYMBOLS_OUTLINE_VIEW, &LanguageServerCluster::OnOulineViewSymbols, this); |
| 157 |
Unbind(wxEVT_LSP_SEMANTICS, &LanguageServerCluster::OnSemanticTokens, this); |
| 158 |
Unbind(wxEVT_LSP_LOGMESSAGE, &LanguageServerCluster::OnLogMessage, this); |
| 159 |
Unbind(wxEVT_LSP_DOCUMENT_SYMBOLS_FOR_HIGHLIGHT, &LanguageServerCluster::OnDocumentSymbolsForHighlight, this); |
| 160 |
if(m_quick_outline_dlg) { |
| 161 |
m_quick_outline_dlg->Destroy(); |
| 162 |
m_quick_outline_dlg = nullptr; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void LanguageServerCluster::Reload(const std::unordered_set<wxString>& languages) |
| 167 |
{ |
| 168 |
StopAll(languages); |
| 169 |
|
| 170 |
// If we are not enabled, stop here |
| 171 |
if(!LanguageServerConfig::Get().IsEnabled()) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
StartAll(languages); |
| 176 |
} |
| 177 |
|
| 178 |
LanguageServerProtocol::Ptr_t LanguageServerCluster::GetServerForEditor(IEditor* editor) |
| 179 |
{ |
| 180 |
LanguageServerProtocol::Ptr_t bestServer(nullptr); |
| 181 |
for(const auto& vt : m_servers) { |
| 182 |
const auto& thisServer = vt.second; |
| 183 |
if(bestServer && thisServer->GetPriority() <= bestServer->GetPriority()) { |
| 184 |
// this LSP has lower priority, skip |
| 185 |
continue; |
| 186 |
} |
| 187 |
if(thisServer->CanHandle(editor)) { |
| 188 |
bestServer = thisServer; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return bestServer; |
| 193 |
} |
| 194 |
|
| 195 |
void LanguageServerCluster::OnSymbolFound(LSPEvent& event) |
| 196 |
{ |
| 197 |
// if we have more than one location - prompt the user |
| 198 |
if(event.GetLocations().empty()) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// for now, use the first location |
| 203 |
LSP::Location location; |
| 204 |
if(event.GetLocations().size() > 1) { |
| 205 |
// multiple matches |
| 206 |
clSelectSymbolDialogEntry::List_t entries; |
| 207 |
entries.reserve(event.GetLocations().size()); |
| 208 |
for(const auto& loc : event.GetLocations()) { |
| 209 |
entries.emplace_back(); |
| 210 |
auto& entry = entries.back(); |
| 211 |
entry.name = loc.GetPattern(); |
| 212 |
entry.clientData = new SymbolClientData(loc); |
| 213 |
} |
| 214 |
|
| 215 |
clSelectSymbolDialog dlg(EventNotifier::Get()->TopFrame(), entries); |
| 216 |
if(dlg.ShowModal() != wxID_OK) { |
| 217 |
return; |
| 218 |
} |
| 219 |
location = static_cast<SymbolClientData*>(dlg.GetSelection())->location; |
| 220 |
} else { |
| 221 |
location = event.GetLocations()[0]; |
| 222 |
} |
| 223 |
|
| 224 |
// let someone else try and open this file first, as it might be a remote file |
| 225 |
LSPEvent open_event(wxEVT_LSP_OPEN_FILE); |
| 226 |
open_event.SetLocation(location); |
| 227 |
open_event.SetFileName(location.GetPath()); |
| 228 |
open_event.SetLineNumber(location.GetRange().GetStart().GetLine()); |
| 229 |
if(EventNotifier::Get()->ProcessEvent(open_event)) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
wxFileName fn(location.GetPath()); |
| 234 |
clDEBUG() << "LSP: Opening file:" << fn << "(" << location.GetRange().GetStart().GetLine() << ":" |
| 235 |
<< location.GetRange().GetStart().GetCharacter() << ")"; |
| 236 |
|
| 237 |
// Manage the browser (BACK and FORWARD) ourself |
| 238 |
BrowseRecord from; |
| 239 |
IEditor* oldEditor = clGetManager()->GetActiveEditor(); |
| 240 |
if(oldEditor) { |
| 241 |
from = oldEditor->CreateBrowseRecord(); |
| 242 |
} |
| 243 |
|
| 244 |
auto cb = [=](IEditor* editor) { |
| 245 |
editor->GetCtrl()->ClearSelections(); |
| 246 |
// try selecting using a location first |
| 247 |
if(!editor->SelectLocation(location)) { |
| 248 |
// try the range |
| 249 |
editor->SelectRange(location.GetRange()); |
| 250 |
} |
| 251 |
NavMgr::Get()->StoreCurrentLocation(from, editor->CreateBrowseRecord()); |
| 252 |
}; |
| 253 |
clGetManager()->OpenFileAndAsyncExecute(fn.GetFullPath(), std::move(cb)); |
| 254 |
} |
| 255 |
|
| 256 |
void LanguageServerCluster::OnLSPInitialized(LSPEvent& event) |
| 257 |
{ |
| 258 |
// Now that the workspace is loaded, parse the active file |
| 259 |
IEditor* editor = clGetManager()->GetActiveEditor(); |
| 260 |
CHECK_PTR_RET(editor); |
| 261 |
|
| 262 |
LanguageServerProtocol::Ptr_t lsp = GetServerByName(event.GetServerName()); |
| 263 |
if(lsp) { |
| 264 |
lsp->OpenEditor(editor); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void LanguageServerCluster::OnSignatureHelp(LSPEvent& event) |
| 269 |
{ |
| 270 |
IEditor* editor = clGetManager()->GetActiveEditor(); |
| 271 |
CHECK_PTR_RET(editor); |
| 272 |
|
| 273 |
// Signature help results are ready, display them in the editor |
| 274 |
const LSP::SignatureHelp& sighelp = event.GetSignatureHelp(); |
| 275 |
|
| 276 |
TagEntryPtrVector_t tags; |
| 277 |
LSPSignatureHelpToTagEntries(tags, sighelp); |
| 278 |
|
| 279 |
if(tags.empty()) { |
| 280 |
return; |
| 281 |
} |
| 282 |
editor->ShowCalltip(new clCallTip(tags)); |
| 283 |
} |
| 284 |
|
| 285 |
void LanguageServerCluster::OnHover(LSPEvent& event) |
| 286 |
{ |
| 287 |
IEditor* editor = clGetManager()->GetActiveEditor(); |
| 288 |
CHECK_PTR_RET(editor); |
| 289 |
|
| 290 |
const LSP::Hover& hover = event.GetHover(); |
| 291 |
const LSP::MarkupContent& contents = hover.GetContents(); |
| 292 |
const LSP::Range& range = hover.GetRange(); |
| 293 |
|
| 294 |
if(contents.GetValue().IsEmpty()) { |
| 295 |
// No available tooltip |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
// Sanity check for range (if available) |
| 300 |
if(range.IsOk()) { |
| 301 |
// Is the mouse pointer still under the hovered position? |
| 302 |
int pos = editor->GetPosAtMousePointer(); |
| 303 |
if(pos == wxNOT_FOUND) { |
| 304 |
return; |
| 305 |
} |
| 306 |
LSP::Position cur(editor->LineFromPos(pos), editor->GetColumnInChars(pos)); |
| 307 |
if(cur < range.GetStart() || cur > range.GetEnd()) { |
| 308 |
// The hover is no longer valid |
| 309 |
return; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if(contents.GetKind() == "markdown") { |
| 314 |
editor->ShowTooltip(contents.GetValue()); |
| 315 |
} else { |
| 316 |
wxString fixedTip = contents.GetValue(); |
| 317 |
StringUtils::DisableMarkdownStyling(fixedTip); |
| 318 |
editor->ShowTooltip(fixedTip); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
void LanguageServerCluster::OnMethodNotFound(LSPEvent& event) { wxUnusedVar(event); } |
| 323 |
|
| 324 |
void LanguageServerCluster::OnCompletionReady(LSPEvent& event) |
| 325 |
{ |
| 326 |
const LSP::CompletionItem::Vec_t& items = event.GetCompletions(); |
| 327 |
|
| 328 |
IEditor* editor = clGetManager()->GetActiveEditor(); |
| 329 |
CHECK_PTR_RET(editor); |
| 330 |
|
| 331 |
wxCodeCompletionBoxManager::Get().ShowCompletionBox(clGetManager()->GetActiveEditor()->GetCtrl(), items); |
| 332 |
} |
| 333 |
|
| 334 |
void LanguageServerCluster::OnReparseNeeded(LSPEvent& event) |
| 335 |
{ |
| 336 |
LanguageServerProtocol::Ptr_t server = GetServerByName(event.GetServerName()); |
| 337 |
if(!server) { |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
IEditor* editor = clGetManager()->GetActiveEditor(); |
| 342 |
CHECK_PTR_RET(editor); |
| 343 |
|
| 344 |
server->CloseEditor(editor); |
| 345 |
server->OpenEditor(editor); |
| 346 |
} |
| 347 |
|
| 348 |
void LanguageServerCluster::OnSemanticTokens(LSPEvent& event) |
| 349 |
{ |
| 350 |
LanguageServerProtocol::Ptr_t server = GetServerByName(event.GetServerName()); |
| 351 |
CHECK_PTR_RET(server); |
| 352 |
|
| 353 |
clDEBUG() << "Processing semantic tokens from server:" << server->GetName() << "file:" << event.GetFileName() |
| 354 |
<< endl; |
| 355 |
IEditor* editor = clGetManager()->FindEditor(event.GetFileName()); |
| 356 |
if(!editor) { |
| 357 |
clDEBUG() << "Could not locate file:" << event.GetFileName() << endl; |
| 358 |
// maybe the given path is on a remote machine, try again, using the remote path this time |
| 359 |
IEditor::List_t all_editors; |
| 360 |
clGetManager()->GetAllEditors(all_editors); |
| 361 |
for(auto e : all_editors) { |
| 362 |
if(e->IsRemoteFile() && (e->GetRemotePath() == event.GetFileName())) { |
| 363 |
editor = e; |
| 364 |
clDEBUG() << "Found remote file:" << e->GetRemotePath() << endl; |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
CHECK_PTR_RET(editor); |
| 370 |
clDEBUG() << "Found the editor!" << endl; |
| 371 |
const auto& semanticTokens = event.GetSemanticTokens(); |
| 372 |
|
| 373 |
wxStringSet_t variables_tokens = { "variable", "parameter", "typeParameter", "property" }; |
| 374 |
wxStringSet_t classes_tokens = { "class", "enum", "namespace", "type", "struct", "trait", "interface" }; |
| 375 |
wxStringSet_t method_tokens = { "function", "method" }; |
| 376 |
|
| 377 |
wxStringSet_t classes_set; |
| 378 |
wxStringSet_t variables_set; |
| 379 |
wxStringSet_t methods_set; |
| 380 |
|
| 381 |
wxString classes_str; |
| 382 |
wxString variabls_str; |
| 383 |
wxString method_str; |
| 384 |
|
| 385 |
clDEBUG() << "Going over" << semanticTokens.size() << "tokens" << endl; |
| 386 |
for(const auto& token : semanticTokens) { |
| 387 |
// is this an interesting token? |
| 388 |
wxString token_type = server->GetSemanticToken(token.token_type); |
| 389 |
bool is_class = classes_tokens.count(token_type) > 0; |
| 390 |
bool is_variable = variables_tokens.count(token_type) > 0; |
| 391 |
bool is_method = method_tokens.count(token_type) > 0; |
| 392 |
|
| 393 |
// read its name |
| 394 |
int start_pos = editor->GetCtrl()->PositionFromLine(token.line) + token.column; |
| 395 |
int end_pos = start_pos + token.length; |
| 396 |
wxString token_name = editor->GetTextRange(start_pos, end_pos); |
| 397 |
if(!is_class && !is_variable && !is_method) { |
| 398 |
continue; |
| 399 |
} |
| 400 |
|
| 401 |
if(is_class && classes_set.count(token_name) == 0) { |
| 402 |
classes_set.insert(token_name); |
| 403 |
classes_str << token_name << " "; |
| 404 |
} else if(is_variable && variables_set.count(token_name) == 0) { |
| 405 |
variables_set.insert(token_name); |
| 406 |
variabls_str << token_name << " "; |
| 407 |
} else if(is_method && methods_set.count(token_name) == 0) { |
| 408 |
methods_set.insert(token_type); |
| 409 |
method_str << token_name << " "; |
| 410 |
} |
| 411 |
} |
| 412 |
clDEBUG() << "Done" << endl; |
| 413 |
|
| 414 |
clDEBUG() << "Calling editor->SetSemanticTokens" << endl; |
| 415 |
editor->SetSemanticTokens(classes_str, variabls_str, method_str, wxEmptyString); |
| 416 |
clDEBUG() << "Success" << endl; |
| 417 |
} |
| 418 |
|
| 419 |
void LanguageServerCluster::OnRestartNeeded(LSPEvent& event) |
| 420 |
{ |
| 421 |
// Don't keep restarting a crashing LSP |
| 422 |
// We consider a "crashing LSP" as: |
| 423 |
// 1. Crashed more than 10 times |
| 424 |
// 2. The interval between crashes is less than 1 minute |
| 425 |
|
| 426 |
clDEBUG() << "LSP:" << event.GetServerName() << "needs to be restarted" << clEndl; |
| 427 |
auto iter = m_restartCounters.find(event.GetServerName()); |
| 428 |
if(iter == m_restartCounters.end()) { |
| 429 |
iter = m_restartCounters.insert({ event.GetServerName(), {} }).first; |
| 430 |
} |
| 431 |
|
| 432 |
time_t curtime = time(nullptr); |
| 433 |
CrashInfo& crash_info = iter->second; |
| 434 |
if((curtime - crash_info.last_crash) >= 60) { |
| 435 |
// if the last crash occured over 1 min ago, reset the crash counters |
| 436 |
crash_info.times = 0; |
| 437 |
} |
| 438 |
|
| 439 |
crash_info.times++; // increase the restart counter |
| 440 |
crash_info.last_crash = curtime; // remember when the crash occured |
| 441 |
if(crash_info.times > 3) { |
| 442 |
clWARNING() << "Too many restart failures for LSP:" << event.GetServerName() << ". Will not restart it again" |
| 443 |
<< clEndl; |
| 444 |
return; |
| 445 |
} |
| 446 |
RestartServer(event.GetServerName()); |
| 447 |
} |
| 448 |
|
| 449 |
LanguageServerProtocol::Ptr_t LanguageServerCluster::GetServerByName(const wxString& name) |
| 450 |
{ |
| 451 |
if(m_servers.count(name) == 0) { |
| 452 |
return LanguageServerProtocol::Ptr_t(nullptr); |
| 453 |
} |
| 454 |
return m_servers[name]; |
| 455 |
} |
| 456 |
|
| 457 |
void LanguageServerCluster::RestartServer(const wxString& name) |
| 458 |
{ |
| 459 |
{ |
| 460 |
// Incase a server terminated, remove it from the list of servers |
| 461 |
// We do this in an inner block because 'server' (line below) will have |
| 462 |
// ref-count of 2 to make sure it is destroyed (i.e. unregister itself from |
| 463 |
// the service provider manager) the ref count needs to get to 0 |
| 464 |
// Hence the inner block |
| 465 |
LanguageServerProtocol::Ptr_t server = GetServerByName(name); |
| 466 |
if(!server) { |
| 467 |
return; |
| 468 |
} |
| 469 |
clDEBUG() << "Restarting LSP server:" << name << clEndl; |
| 470 |
server->Stop(); |
| 471 |
|
| 472 |
// Remove the old instance |
| 473 |
m_servers.erase(name); |
| 474 |
} |
| 475 |
|
| 476 |
// Create new instance |
| 477 |
if(LanguageServerConfig::Get().GetServers().count(name) == 0) { |
| 478 |
return; |
| 479 |
} |
| 480 |
const LanguageServerEntry& entry = LanguageServerConfig::Get().GetServers().at(name); |
| 481 |
StartServer(entry); |
| 482 |
} |
| 483 |
|
| 484 |
void LanguageServerCluster::StartServer(const LanguageServerEntry& entry) |
| 485 |
{ |
| 486 |
if(!entry.IsEnabled()) { |
| 487 |
LOG_IF_TRACE { clDEBUG1() << "LSP" << entry.GetName() << "is not enabled" << clEndl; } |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
// we can only start "remote" servers that use the "stdio" connection |
| 492 |
if(entry.GetConnectionString() != "stdio" && m_remoteHelper->IsRemoteWorkspaceOpened()) { |
| 493 |
clDEBUG() << "can't start remote LSP server:" << entry.GetName() << ". Not an stdio connection" << endl; |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
LOG_IF_TRACE { clDEBUG1() << "Connecting to LSP server:" << entry.GetName(); } |
| 498 |
|
| 499 |
if(!entry.IsValid()) { |
| 500 |
clWARNING() << "LSP Server" << entry.GetName() |
| 501 |
<< "is not valid and it will not be started (one of the specified paths do not " |
| 502 |
"exist)"; |
| 503 |
LanguageServerConfig::Get().GetServers()[entry.GetName()].SetEnabled(false); |
| 504 |
LanguageServerConfig::Get().Save(); |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
if(m_servers.count(entry.GetName())) { |
| 509 |
clDEBUG() << "LSP" << entry.GetName() << "is already running" << endl; |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
LanguageServerProtocol::Ptr_t lsp(new LanguageServerProtocol(entry.GetName(), entry.GetNetType(), this)); |
| 514 |
lsp->SetPriority(entry.GetPriority()); |
| 515 |
lsp->SetDisaplayDiagnostics(entry.IsDisaplayDiagnostics()); |
| 516 |
|
| 517 |
if(lsp->GetName() == "ctagsd") { |
| 518 |
// set startup callback |
| 519 |
auto cb = [=]() { |
| 520 |
if(!clWorkspaceManager::Get().IsWorkspaceOpened()) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
wxFileName fn(clWorkspaceManager::Get().GetWorkspace()->GetDir(), wxEmptyString); |
| 525 |
fn.AppendDir(".ctagsd"); |
| 526 |
fn.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL); |
| 527 |
|
| 528 |
wxFileName settings_json(fn.GetPath(), "ctagsd.json"); |
| 529 |
{ |
| 530 |
if(!settings_json.FileExists()) { |
| 531 |
// create an empty json object file |
| 532 |
FileUtils::WriteFileContent(settings_json, "{}"); |
| 533 |
} else { |
| 534 |
// the file exists, ensure its a valid json |
| 535 |
JSON root(settings_json); |
| 536 |
if(!root.isOk()) { |
| 537 |
FileUtils::WriteFileContent(settings_json, "{}"); |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
JSON root(settings_json); |
| 543 |
JSONItem json = root.toElement(); |
| 544 |
|
| 545 |
if(json.hasNamedObject("limit_results")) { |
| 546 |
json.removeProperty("limit_results"); |
| 547 |
clDEBUG() << "ctagsd: found limit_results -> removing it" << endl; |
| 548 |
} |
| 549 |
|
| 550 |
// update the entries |
| 551 |
if(json.hasNamedObject("codelite_indexer")) { |
| 552 |
json.removeProperty("codelite_indexer"); |
| 553 |
clDEBUG() << "ctagsd: found codelite_indexer -> removing it" << endl; |
| 554 |
} |
| 555 |
|
| 556 |
json.addProperty("codelite_indexer", clStandardPaths::Get().GetBinaryFullPath("codelite-ctags")); |
| 557 |
json.addProperty("limit_results", TagsManagerST::Get()->GetCtagsOptions().GetCcNumberOfDisplayItems()); |
| 558 |
root.save(settings_json); |
| 559 |
clDEBUG() << "ctagsd: writing new file:" << settings_json << endl; |
| 560 |
|
| 561 |
// create the file_list.txt file |
| 562 |
wxFileName file_list(fn.GetPath(), "file_list.txt"); |
| 563 |
wxArrayString files; |
| 564 |
clWorkspaceManager::Get().GetWorkspace()->GetWorkspaceFiles(files); |
| 565 |
|
| 566 |
wxString file_list_content; |
| 567 |
for(const auto& filepath : files) { |
| 568 |
file_list_content << filepath << "\n"; |
| 569 |
} |
| 570 |
FileUtils::WriteFileContent(file_list, file_list_content); |
| 571 |
}; |
| 572 |
lsp->SetStartedCallback(std::move(cb)); |
| 573 |
} |
| 574 |
|
| 575 |
wxString command; |
| 576 |
wxString working_directory; |
| 577 |
wxString project; |
| 578 |
if(m_remoteHelper->IsRemoteWorkspaceOpened()) { |
| 579 |
// remote workspace |
| 580 |
// read the commands from the codelite-remote.json file (the m_remoteHelper does that for us) |
| 581 |
JSON* root = m_remoteHelper->GetPluginConfig("Language Server Plugin"); |
| 582 |
if(!root || !json_get_server_config(root, entry.GetName()).isOk()) { |
| 583 |
clDEBUG() << "No remote entry found for server:" << entry.GetName() << endl; |
| 584 |
return; |
| 585 |
} |
| 586 |
|
| 587 |
wxString remote_command = json_get_server_config_command(root, entry.GetName()); |
| 588 |
working_directory = json_get_server_config_working_directory(root, entry.GetName()); |
| 589 |
auto env_list = json_get_server_config_env(root, entry.GetName()); |
| 590 |
if(remote_command.empty() || |
| 591 |
!m_remoteHelper->BuildRemoteCommand(remote_command, env_list, working_directory, &command)) { |
| 592 |
clWARNING() << "LSP: failed to build remote command for server:" << entry.GetName() << endl; |
| 593 |
return; |
| 594 |
} |
| 595 |
} else { |
| 596 |
// local workspace |
| 597 |
command = entry.GetCommand(); |
| 598 |
if(clCxxWorkspaceST::Get()->IsOpen()) { |
| 599 |
project = clCxxWorkspaceST::Get()->GetActiveProjectName(); |
| 600 |
} |
| 601 |
command = MacroManager::Instance()->Expand(command, clGetManager(), project); |
| 602 |
working_directory = entry.GetWorkingDirectory(); |
| 603 |
} |
| 604 |
|
| 605 |
// Expand the working directory (which is later used as the rootUri) |
| 606 |
working_directory = MacroManager::Instance()->Expand(working_directory, clGetManager(), project); |
| 607 |
|
| 608 |
wxArrayString lspCommand; |
| 609 |
lspCommand = StringUtils::BuildCommandArrayFromString(command); |
| 610 |
|
| 611 |
if(!lspCommand.empty() && !wxFileName::FileExists(lspCommand[0])) { |
| 612 |
clERROR() << "Disabling lsp:" << entry.GetName() << endl; |
| 613 |
clERROR() << lspCommand[0] << ". No such file" << endl; |
| 614 |
LanguageServerConfig::Get().GetServer(entry.GetName()).SetEnabled(false); |
| 615 |
LanguageServerConfig::Get().Save(); |
| 616 |
return; |
| 617 |
} |
| 618 |
clDEBUG() << "Starting lsp:"; |
| 619 |
clDEBUG() << "Connection string:" << entry.GetConnectionString(); |
| 620 |
|
| 621 |
if(entry.IsAutoRestart()) { |
| 622 |
clDEBUG() << "lspCommand:" << lspCommand; |
| 623 |
clDEBUG() << "entry.GetWorkingDirectory():" << working_directory; |
| 624 |
} |
| 625 |
clDEBUG() << "working_directory:" << working_directory; |
| 626 |
clDEBUG() << "entry.GetLanguages():" << entry.GetLanguages(); |
| 627 |
|
| 628 |
size_t flags = 0; |
| 629 |
if(entry.IsAutoRestart()) { |
| 630 |
flags |= LSPStartupInfo::kAutoStart; |
| 631 |
} |
| 632 |
|
| 633 |
LSPStartupInfo startup_info; |
| 634 |
startup_info.SetConnectioString(entry.GetConnectionString()); |
| 635 |
startup_info.SetLspServerCommand(lspCommand); |
| 636 |
startup_info.SetFlags(flags); |
| 637 |
startup_info.SetWorkingDirectory(working_directory); |
| 638 |
|
| 639 |
// apply the environment |
| 640 |
clEnvList_t env_list; |
| 641 |
if(clWorkspaceManager::Get().IsWorkspaceOpened()) { |
| 642 |
env_list = clWorkspaceManager::Get().GetWorkspace()->GetEnvironment(); |
| 643 |
} |
| 644 |
|
| 645 |
if(!env_list.empty()) { |
| 646 |
clDEBUG() << "Creating LSP with env:" << endl; |
| 647 |
for(const auto& p : env_list) { |
| 648 |
clDEBUG() << p.first << "=" << p.second << endl; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if(lsp->Start(startup_info, env_list, wxEmptyString, working_directory, entry.GetLanguages())) { |
| 653 |
m_servers.insert({ entry.GetName(), lsp }); |
| 654 |
} else { |
| 655 |
clERROR() << "Failed to start language server:" << entry.GetName() << endl; |
| 656 |
clERROR() << "Language Server:" << entry.GetName() << "has been disabled until the problem is fixed" << endl; |
| 657 |
LanguageServerConfig::Get().GetServer(entry.GetName()).SetEnabled(false); |
| 658 |
LanguageServerConfig::Get().Save(); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
void LanguageServerCluster::OnWorkspaceClosed(clWorkspaceEvent& event) |
| 663 |
{ |
| 664 |
event.Skip(); |
| 665 |
|
| 666 |
// let the helper process this event *before* us |
| 667 |
m_remoteHelper->ProcessEvent(event); |
| 668 |
|
| 669 |
clDEBUG() << "LSP: workspace CLOSED event" << endl; |
| 670 |
LanguageServerProtocol::workspace_file_type = FileExtManager::TypeOther; |
| 671 |
this->StopAll(); |
| 672 |
m_symbols_to_file_cache.clear(); |
| 673 |
} |
| 674 |
|
| 675 |
void LanguageServerCluster::OnWorkspaceOpen(clWorkspaceEvent& event) |
| 676 |
{ |
| 677 |
event.Skip(); |
| 678 |
|
| 679 |
// let the helper process this event *before* us |
| 680 |
m_remoteHelper->ProcessEvent(event); |
| 681 |
|
| 682 |
clDEBUG() << "LSP: workspace OPEN event" << endl; |
| 683 |
this->Reload(); |
| 684 |
m_symbols_to_file_cache.clear(); |
| 685 |
DiscoverWorkspaceType(); |
| 686 |
} |
| 687 |
|
| 688 |
void LanguageServerCluster::SetWorkspaceType(FileExtManager::FileType type) |
| 689 |
{ |
| 690 |
LanguageServerProtocol::workspace_file_type = type; |
| 691 |
clDEBUG() << "*** LSP: workspace type is set:" << LanguageServerProtocol::workspace_file_type << "***" << endl; |
| 692 |
} |
| 693 |
|
| 694 |
void LanguageServerCluster::StopAll(const std::unordered_set<wxString>& languages) |
| 695 |
{ |
| 696 |
clDEBUG() << "LSP: Stopping all servers" << endl; |
| 697 |
if(languages.empty()) { |
| 698 |
// stop all |
| 699 |
for(const std::unordered_map<wxString, LanguageServerProtocol::Ptr_t>::value_type& vt : m_servers) { |
| 700 |
// stop all current processes |
| 701 |
LanguageServerProtocol::Ptr_t server = vt.second; |
| 702 |
server.reset(nullptr); |
| 703 |
} |
| 704 |
m_servers.clear(); |
| 705 |
} else { |
| 706 |
for(const auto& lang : languages) { |
| 707 |
clDEBUG() << "Stopping server for language:" << lang << endl; |
| 708 |
auto server = GetServerForLanguage(lang); |
| 709 |
if(server) { |
| 710 |
// this will stop and remove the server from the list |
| 711 |
StopServer(server->GetName()); |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
clDEBUG() << "LSP: Success" << endl; |
| 716 |
|
| 717 |
// Clear all markers |
| 718 |
ClearAllDiagnostics(); |
| 719 |
} |
| 720 |
|
| 721 |
void LanguageServerCluster::StartAll(const std::unordered_set<wxString>& languages) |
| 722 |
{ |
| 723 |
// create a new list |
| 724 |
ClearAllDiagnostics(); |
| 725 |
|
| 726 |
clDEBUG() << "LSP: Staring all servers..." << endl; |
| 727 |
if(languages.empty()) { |
| 728 |
const LanguageServerEntry::Map_t& servers = LanguageServerConfig::Get().GetServers(); |
| 729 |
for(const LanguageServerEntry::Map_t::value_type& vt : servers) { |
| 730 |
const LanguageServerEntry& entry = vt.second; |
| 731 |
StartServer(entry); |
| 732 |
} |
| 733 |
} else { |
| 734 |
for(const wxString& lang : languages) { |
| 735 |
const LanguageServerEntry::Map_t& servers = LanguageServerConfig::Get().GetServers(); |
| 736 |
for(const auto& vt : servers) { |
| 737 |
// start all the LSPs for the current language |
| 738 |
if(vt.second.IsEnabled() && vt.second.GetLanguages().Index(lang) != wxNOT_FOUND) { |
| 739 |
StartServer(vt.second); |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
clDEBUG() << "LSP: Success" << endl; |
| 745 |
} |
| 746 |
|
| 747 |
void LanguageServerCluster::LSPSignatureHelpToTagEntries(TagEntryPtrVector_t& tags, const LSP::SignatureHelp& sighelp) |
| 748 |
{ |
| 749 |
if(sighelp.GetSignatures().empty()) { |
| 750 |
return; |
| 751 |
} |
| 752 |
const LSP::SignatureInformation::Vec_t& sigs = sighelp.GetSignatures(); |
| 753 |
for(const LSP::SignatureInformation& si : sigs) { |
| 754 |
TagEntryPtr tag(new TagEntry()); |
| 755 |
wxString sig = si.GetLabel().BeforeFirst('-'); |
| 756 |
sig.Trim().Trim(false); |
| 757 |
wxString returnValue = si.GetLabel().AfterFirst('-'); |
| 758 |
if(!returnValue.IsEmpty()) { |
| 759 |
returnValue.Remove(0, 1); // remove ">" |
| 760 |
returnValue.Trim().Trim(false); |
| 761 |
} |
| 762 |
|
| 763 |
tag->SetSignature(sig); |
| 764 |
tag->SetTypename(returnValue); |
| 765 |
tag->SetKind("function"); |
| 766 |
tag->SetFlags(TagEntry::Tag_No_Signature_Format); |
| 767 |
tags.push_back(tag); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
void LanguageServerCluster::OnCompileCommandsGenerated(clCommandEvent& event) |
| 772 |
{ |
| 773 |
event.Skip(); |
| 774 |
clGetManager()->SetStatusMessage(_("Restarting Language Servers...")); |
| 775 |
this->Reload({ "c", "cpp" }); // restart c/c++ the servers |
| 776 |
clGetManager()->SetStatusMessage(_("Ready")); |
| 777 |
} |
| 778 |
|
| 779 |
void LanguageServerCluster::OnOulineViewSymbols(LSPEvent& event) |
| 780 |
{ |
| 781 |
event.Skip(); |
| 782 |
// we use it for outline + editor bar |
| 783 |
if(m_symbols_to_file_cache.count(event.GetFileName())) { |
| 784 |
m_symbols_to_file_cache.erase(event.GetFileName()); |
| 785 |
} |
| 786 |
m_symbols_to_file_cache.insert({ event.GetFileName(), event.GetSymbolsInformation() }); |
| 787 |
clDEBUG() << "LSP: cached symbols for file" << event.GetFileName() << endl; |
| 788 |
UpdateNavigationBar(); |
| 789 |
} |
| 790 |
|
| 791 |
void LanguageServerCluster::OnShowQuickOutlineDlg(LSPEvent& event) |
| 792 |
{ |
| 793 |
wxUnusedVar(event); |
| 794 |
if(m_quick_outline_dlg == nullptr) { |
| 795 |
m_quick_outline_dlg = new LSPOutlineViewDlg(EventNotifier::Get()->TopFrame()); |
| 796 |
} |
| 797 |
if(!m_quick_outline_dlg->IsShown()) { |
| 798 |
m_quick_outline_dlg->Show(); |
| 799 |
m_quick_outline_dlg->CenterOnParent(); |
| 800 |
} |
| 801 |
m_quick_outline_dlg->SetSymbols({}); |
| 802 |
} |
| 803 |
|
| 804 |
void LanguageServerCluster::OnQuickOutlineView(LSPEvent& event) |
| 805 |
{ |
| 806 |
if(m_quick_outline_dlg && m_quick_outline_dlg->IsShown()) { |
| 807 |
m_quick_outline_dlg->SetSymbols(event.GetSymbolsInformation()); |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
void LanguageServerCluster::OnSetDiagnostics(LSPEvent& event) |
| 812 |
{ |
| 813 |
event.Skip(); |
| 814 |
IEditor* editor = FindEditor(event); |
| 815 |
if(editor) { |
| 816 |
editor->DelAllCompilerMarkers(); |
| 817 |
for(const LSP::Diagnostic& d : event.GetDiagnostics()) { |
| 818 |
// LSP uses 1 based line numbers |
| 819 |
editor->SetErrorMarker(d.GetRange().GetStart().GetLine(), d.GetMessage()); |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
void LanguageServerCluster::OnClearDiagnostics(LSPEvent& event) |
| 825 |
{ |
| 826 |
event.Skip(); |
| 827 |
IEditor* editor = FindEditor(event); |
| 828 |
if(editor) { |
| 829 |
editor->DelAllCompilerMarkers(); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
void LanguageServerCluster::ClearAllDiagnostics() |
| 834 |
{ |
| 835 |
IEditor::List_t editors; |
| 836 |
clGetManager()->GetAllEditors(editors); |
| 837 |
for(IEditor* editor : editors) { |
| 838 |
editor->DelAllCompilerMarkers(); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void LanguageServerCluster::ClearRestartCounters() { m_restartCounters.clear(); } |
| 843 |
|
| 844 |
void LanguageServerCluster::OnBuildEnded(clBuildEvent& event) { event.Skip(); } |
| 845 |
|
| 846 |
void LanguageServerCluster::StopServer(const wxString& name) |
| 847 |
{ |
| 848 |
LanguageServerProtocol::Ptr_t server = GetServerByName(name); |
| 849 |
if(!server) { |
| 850 |
return; |
| 851 |
} |
| 852 |
|
| 853 |
clDEBUG() << "Stopping LSP server:" << name << clEndl; |
| 854 |
server->Stop(); |
| 855 |
// Remove the old instance |
| 856 |
m_servers.erase(name); |
| 857 |
} |
| 858 |
|
| 859 |
void LanguageServerCluster::DeleteServer(const wxString& name) |
| 860 |
{ |
| 861 |
StopServer(name); // stop any server that goes by that name |
| 862 |
// Delete it's configuration entry |
| 863 |
LanguageServerConfig::Get().RemoveServer(name); |
| 864 |
LanguageServerConfig::Get().Save(); |
| 865 |
} |
| 866 |
|
| 867 |
void LanguageServerCluster::StartServer(const wxString& name) |
| 868 |
{ |
| 869 |
auto entry = LanguageServerConfig::Get().GetServer(name); |
| 870 |
if(!entry.IsValid()) { |
| 871 |
return; |
| 872 |
} |
| 873 |
StartServer(entry); |
| 874 |
} |
| 875 |
|
| 876 |
wxString LanguageServerCluster::GetEditorFilePath(IEditor* editor) const |
| 877 |
{ |
| 878 |
if(editor->IsRemoteFile()) { |
| 879 |
return editor->GetRemotePath(); |
| 880 |
} else { |
| 881 |
return editor->GetFileName().GetFullPath(); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
IEditor* LanguageServerCluster::FindEditor(const LSPEvent& event) const |
| 886 |
{ |
| 887 |
return FindEditor(event.GetLocation().GetPath()); |
| 888 |
} |
| 889 |
|
| 890 |
IEditor* LanguageServerCluster::FindEditor(const wxString& path) const |
| 891 |
{ |
| 892 |
IEditor::List_t all_editors; |
| 893 |
clGetManager()->GetAllEditors(all_editors); |
| 894 |
for(IEditor* editor : all_editors) { |
| 895 |
if(editor->GetFileName().GetFullPath() == path || editor->GetRemotePath() == path) { |
| 896 |
return editor; |
| 897 |
} |
| 898 |
} |
| 899 |
return nullptr; |
| 900 |
} |
| 901 |
|
| 902 |
LanguageServerProtocol::Ptr_t LanguageServerCluster::GetServerForLanguage(const wxString& lang) |
| 903 |
{ |
| 904 |
LanguageServerProtocol::Ptr_t bestServer(nullptr); |
| 905 |
for(const auto& vt : m_servers) { |
| 906 |
const auto& thisServer = vt.second; |
| 907 |
if(bestServer && thisServer->GetPriority() <= bestServer->GetPriority()) { |
| 908 |
// this LSP has lower priority, skip |
| 909 |
continue; |
| 910 |
} |
| 911 |
if(thisServer->IsRunning() && thisServer->IsLanguageSupported(lang)) { |
| 912 |
bestServer = thisServer; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
if(bestServer) { |
| 917 |
clDEBUG() << "Using server" << bestServer->GetName() << "for language" << lang << endl; |
| 918 |
} |
| 919 |
return bestServer; |
| 920 |
} |
| 921 |
|
| 922 |
void LanguageServerCluster::OnLogMessage(LSPEvent& event) |
| 923 |
{ |
| 924 |
event.Skip(); |
| 925 |
m_plugin->LogMessage(event.GetServerName(), event.GetMessage(), event.GetLogMessageSeverity()); |
| 926 |
} |
| 927 |
|
| 928 |
void LanguageServerCluster::OnDocumentSymbolsForHighlight(LSPEvent& event) |
| 929 |
{ |
| 930 |
clDEBUG() << "LanguageServerCluster::OnDocumentSymbolsForHighlight called for file:" << event.GetFileName() << endl; |
| 931 |
IEditor* editor = FindEditor(event.GetFileName()); |
| 932 |
CHECK_PTR_RET(editor); |
| 933 |
|
| 934 |
const auto& symbols = event.GetSymbolsInformation(); |
| 935 |
wxString classes, variables, methods, others; |
| 936 |
for(const auto& si : symbols) { |
| 937 |
switch(si.GetKind()) { |
| 938 |
case LSP::kSK_Module: |
| 939 |
case LSP::kSK_Namespace: |
| 940 |
case LSP::kSK_Package: |
| 941 |
case LSP::kSK_Class: |
| 942 |
case LSP::kSK_Enum: |
| 943 |
case LSP::kSK_Interface: |
| 944 |
case LSP::kSK_Struct: |
| 945 |
case LSP::kSK_Object: |
| 946 |
classes << si.GetName() << " "; |
| 947 |
break; |
| 948 |
case LSP::kSK_Method: |
| 949 |
case LSP::kSK_Function: |
| 950 |
methods << si.GetName() << " "; |
| 951 |
break; |
| 952 |
case LSP::kSK_TypeParameter: |
| 953 |
others << si.GetName() << " "; |
| 954 |
break; |
| 955 |
case LSP::kSK_EnumMember: |
| 956 |
case LSP::kSK_Property: |
| 957 |
case LSP::kSK_Field: |
| 958 |
case LSP::kSK_Variable: |
| 959 |
case LSP::kSK_Constant: |
| 960 |
variables << si.GetName() << " "; |
| 961 |
default: |
| 962 |
break; |
| 963 |
} |
| 964 |
} |
| 965 |
clDEBUG() << "Setting semantic highlight (using DocumentSymbolsRequest):" << endl; |
| 966 |
clDEBUG() << "Classes :" << classes << endl; |
| 967 |
clDEBUG() << "Variables:" << variables << endl; |
| 968 |
clDEBUG() << "Methods:" << methods << endl; |
| 969 |
editor->SetSemanticTokens(classes, variables, methods, others); |
| 970 |
} |
| 971 |
|
| 972 |
void LanguageServerCluster::OnOpenResource(wxCommandEvent& event) |
| 973 |
{ |
| 974 |
event.Skip(); |
| 975 |
DiscoverWorkspaceType(); |
| 976 |
} |
| 977 |
|
| 978 |
void LanguageServerCluster::DiscoverWorkspaceType() |
| 979 |
{ |
| 980 |
if(LanguageServerProtocol::workspace_file_type != FileExtManager::TypeOther) { |
| 981 |
return; |
| 982 |
} |
| 983 |
|
| 984 |
wxArrayString files; |
| 985 |
CHECK_PTR_RET(clWorkspaceManager::Get().GetWorkspace()); |
| 986 |
|
| 987 |
clWorkspaceManager::Get().GetWorkspace()->GetWorkspaceFiles(files); |
| 988 |
if(files.empty()) { |
| 989 |
clWARNING() << "Workspace contains no files" << endl; |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
// try to determine the workspace type by checking |
| 994 |
// for common files |
| 995 |
wxStringSet_t S; |
| 996 |
S.reserve(files.size()); |
| 997 |
|
| 998 |
for(const auto& str : files) { |
| 999 |
wxFileName fn(str); |
| 1000 |
S.insert(fn.GetFullName()); |
| 1001 |
} |
| 1002 |
|
| 1003 |
if(S.count("Cargo.toml")) { |
| 1004 |
LanguageServerProtocol::workspace_file_type = FileExtManager::TypeRust; |
| 1005 |
clDEBUG() << "*** LSP: workspace type is set to Rust (found Cargo.toml)" << endl; |
| 1006 |
|
| 1007 |
} else if(S.count("Rakefile")) { |
| 1008 |
LanguageServerProtocol::workspace_file_type = FileExtManager::TypeRuby; |
| 1009 |
clDEBUG() << "*** LSP: workspace type is set to Ruby (found Rakefile)" << endl; |
| 1010 |
|
| 1011 |
} else if(S.count("CMakeLists.txt")) { |
| 1012 |
LanguageServerProtocol::workspace_file_type = FileExtManager::TypeSourceCpp; |
| 1013 |
clDEBUG() << "*** LSP: workspace type is set to C++ (found CMakeLists.txt)" << endl; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if(LanguageServerProtocol::workspace_file_type != FileExtManager::TypeOther) { |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
|
| 1020 |
std::thread thr( |
| 1021 |
[=](const wxArrayString& files, wxEvtHandler* owner) { |
| 1022 |
// create a table for all the known file types |
| 1023 |
int table[FileExtManager::TypeLast + 1]; |
| 1024 |
memset(table, 0, sizeof(table)); |
| 1025 |
|
| 1026 |
// holds the index to the biggest file type found so far |
| 1027 |
int cur_max = wxNOT_FOUND; |
| 1028 |
FileExtManager::FileType cur_type = FileExtManager::TypeOther; |
| 1029 |
|
| 1030 |
std::unordered_set<int> acceptable_file_types = { |
| 1031 |
FileExtManager::TypeSQL, FileExtManager::TypeShellScript, FileExtManager::TypeSourceC, |
| 1032 |
FileExtManager::TypeSourceCpp, FileExtManager::TypeCSS, FileExtManager::TypeHeader, |
| 1033 |
FileExtManager::TypeHtml, FileExtManager::TypeJS, FileExtManager::TypeJava, |
| 1034 |
FileExtManager::TypeLua, FileExtManager::TypeRuby, FileExtManager::TypeRust, |
| 1035 |
FileExtManager::TypePython, FileExtManager::TypeDart, |
| 1036 |
}; |
| 1037 |
for(const wxString& file : files) { |
| 1038 |
auto file_type = FileExtManager::GetType(file); |
| 1039 |
if(file_type != FileExtManager::TypeOther && |
| 1040 |
acceptable_file_types.count(file_type)) { // TypeOther is -1 |
| 1041 |
table[file_type]++; |
| 1042 |
if(table[file_type] > cur_max) { |
| 1043 |
cur_type = file_type; |
| 1044 |
// update the current max |
| 1045 |
cur_max = table[file_type]; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
owner->CallAfter(&LanguageServerCluster::SetWorkspaceType, cur_type); |
| 1050 |
}, |
| 1051 |
files, this); |
| 1052 |
thr.detach(); |
| 1053 |
} |
| 1054 |
|
| 1055 |
void LanguageServerCluster::OnEditorClosed(clCommandEvent& event) |
| 1056 |
{ |
| 1057 |
event.Skip(); |
| 1058 |
// clear the cache for the closed file |
| 1059 |
m_symbols_to_file_cache.erase(event.GetFileName()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
void LanguageServerCluster::OnActiveEditorChanged(wxCommandEvent& event) |
| 1063 |
{ |
| 1064 |
event.Skip(); |
| 1065 |
UpdateNavigationBar(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
void LanguageServerCluster::UpdateNavigationBar() |
| 1069 |
{ |
| 1070 |
auto editor = clGetManager()->GetActiveEditor(); |
| 1071 |
CHECK_PTR_RET(editor); |
| 1072 |
|
| 1073 |
wxString fullpath = editor->IsRemoteFile() ? editor->GetRemotePath() : editor->GetFileName().GetFullPath(); |
| 1074 |
if(m_symbols_to_file_cache.count(fullpath) == 0) { |
| 1075 |
return; |
| 1076 |
} |
| 1077 |
|
| 1078 |
auto symbols = m_symbols_to_file_cache.find(fullpath)->second; |
| 1079 |
|
| 1080 |
// prepare list of scopes and send them to the navigation bar |
| 1081 |
clEditorBar::ScopeEntry::vec_t scopes; |
| 1082 |
scopes.reserve(symbols.size()); |
| 1083 |
|
| 1084 |
for(const LSP::SymbolInformation& symbol : symbols) { |
| 1085 |
// only collect methdos |
| 1086 |
if(symbol.GetKind() != LSP::kSK_Function && symbol.GetKind() != LSP::kSK_Method && |
| 1087 |
symbol.GetKind() != LSP::kSK_Constructor) |
| 1088 |
continue; |
| 1089 |
clEditorBar::ScopeEntry scope_entry; |
| 1090 |
const LSP::Location& location = symbol.GetLocation(); |
| 1091 |
scope_entry.line_number = location.GetRange().GetStart().GetLine(); |
| 1092 |
|
| 1093 |
wxString display_string; |
| 1094 |
if(!symbol.GetContainerName().empty()) { |
| 1095 |
display_string << symbol.GetContainerName() << "."; |
| 1096 |
} |
| 1097 |
|
| 1098 |
wxString short_name = symbol.GetName(); |
| 1099 |
short_name = short_name.BeforeFirst('('); |
| 1100 |
short_name += "()"; |
| 1101 |
display_string << short_name; |
| 1102 |
|
| 1103 |
scope_entry.display_string.swap(display_string); |
| 1104 |
scopes.push_back(scope_entry); |
| 1105 |
} |
| 1106 |
clGetManager()->GetNavigationBar()->SetScopes(fullpath, scopes); |
| 1107 |
} |
| 1108 |
|
| 1109 |
void LanguageServerCluster::OnWorkspaceScanCompleted(clWorkspaceEvent& event) |
| 1110 |
{ |
| 1111 |
event.Skip(); |
| 1112 |
clDEBUG() << "==> LanguageServerCluster: workspace file scanned completed." << endl; |
| 1113 |
|
| 1114 |
LanguageServerProtocol::workspace_file_type = FileExtManager::TypeOther; |
| 1115 |
DiscoverWorkspaceType(); |
| 1116 |
Reload(); |
| 1117 |
} |