Skip to content

Commit

Permalink
- Rust plugin: change the default clippy command to: `cargo clippy --…
Browse files Browse the repository at this point in the history
…color always --all-features --all-targets -- -D warnings`

- LSP plugin: when a broken LSP is found, in addition to the "Attempt to fix" button, add a "Cancel" button
  • Loading branch information
eranif committed May 31, 2024
1 parent 1181aaf commit bc0c391
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 88 deletions.
38 changes: 19 additions & 19 deletions CodeFormatter/CodeFormatterManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ CodeFormatterManager::~CodeFormatterManager() { clear(); }
std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatter(const wxString& filepath) const
{
auto type = FileExtManager::GetType(filepath);
for(auto f : m_formatters) {
if(f->IsEnabled() && f->CanHandle(type)) {
for (auto f : m_formatters) {
if (f->IsEnabled() && f->CanHandle(type)) {
return f;
}
}
Expand Down Expand Up @@ -54,12 +54,12 @@ void CodeFormatterManager::push_back(GenericFormatter* formatter)

size_t CodeFormatterManager::GetAllNames(wxArrayString* names) const
{
if(!names) {
if (!names) {
return 0;
}

names->reserve(m_formatters.size());
for(auto f : m_formatters) {
for (auto f : m_formatters) {
names->Add(f->GetName());
}
return names->size();
Expand All @@ -70,20 +70,20 @@ void CodeFormatterManager::Load()
wxFileName config_file{ clStandardPaths::Get().GetUserDataDir(), "code-formatters.json" };
config_file.AppendDir("config");

if(!config_file.FileExists()) {
if (!config_file.FileExists()) {
return;
}

JSON root{ config_file };
if(!root.isOk() || !root.toElement().isArray()) {
if (!root.isOk() || !root.toElement().isArray()) {
initialize_defaults();
return;
}

clear();
auto arr = root.toElement();
int count = arr.arraySize();
for(int i = 0; i < count; ++i) {
for (int i = 0; i < count; ++i) {
GenericFormatter* fmtr = new GenericFormatter();
fmtr->FromJSON(arr[i]);
push_back(fmtr);
Expand All @@ -96,16 +96,16 @@ void CodeFormatterManager::Save()
config_file.AppendDir("config");
JSON root{ cJSON_Array };
auto arr = root.toElement();
for(auto fmtr : m_formatters) {
for (auto fmtr : m_formatters) {
arr.arrayAppend(fmtr->ToJSON());
}
root.save(config_file);
}

std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatterByName(const wxString& name) const
{
for(auto f : m_formatters) {
if(f->GetName() == name) {
for (auto f : m_formatters) {
if (f->GetName() == name) {
return f;
}
}
Expand All @@ -115,8 +115,8 @@ std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatterByName(const
bool CodeFormatterManager::CanFormat(const wxString& filepath) const
{
auto file_type = FileExtManager::GetType(filepath);
for(auto f : m_formatters) {
if(f->IsEnabled() && f->CanHandle(file_type)) {
for (auto f : m_formatters) {
if (f->IsEnabled() && f->CanHandle(file_type)) {
return true;
}
}
Expand All @@ -126,12 +126,12 @@ bool CodeFormatterManager::CanFormat(const wxString& filepath) const
bool CodeFormatterManager::CanFormatByContent(const wxString& content) const
{
FileExtManager::FileType file_type;
if(!FileExtManager::GetContentType(content, file_type)) {
if (!FileExtManager::GetContentType(content, file_type)) {
return false;
}

for(auto f : m_formatters) {
if(f->IsEnabled() && f->CanHandle(file_type)) {
for (auto f : m_formatters) {
if (f->IsEnabled() && f->CanHandle(file_type)) {
return true;
}
}
Expand All @@ -146,20 +146,20 @@ void CodeFormatterManager::RestoreDefaults()

void CodeFormatterManager::ClearRemoteCommands()
{
for(auto f : m_formatters) {
for (auto f : m_formatters) {
f->SetRemoteCommand(wxEmptyString, wxEmptyString, {});
}
}

std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatterByContent(const wxString& content) const
{
FileExtManager::FileType type;
if(!FileExtManager::GetContentType(content, type)) {
if (!FileExtManager::GetContentType(content, type)) {
return nullptr;
}

for(auto f : m_formatters) {
if(f->IsEnabled() && f->CanHandle(type)) {
for (auto f : m_formatters) {
if (f->IsEnabled() && f->CanHandle(type)) {
return f;
}
}
Expand Down
72 changes: 37 additions & 35 deletions LanguageServer/languageserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static LanguageServerPlugin* thePlugin = NULL;
// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
if(thePlugin == NULL) {
if (thePlugin == NULL) {
thePlugin = new LanguageServerPlugin(manager);
}
return thePlugin;
Expand Down Expand Up @@ -89,20 +89,22 @@ LanguageServerPlugin::~LanguageServerPlugin() { wxDELETE(m_servers); }
void LanguageServerPlugin::CheckServers()
{
auto broken_lsps = GetBrokenLSPs();
if(broken_lsps.empty()) {
if (broken_lsps.empty()) {
return;
}

clSYSTEM() << "The following LSPs contain paths to a non existing locations:" << broken_lsps << endl;
// Show a notification message with a suggestion for a fix
wxString message;
message << "The following LSPs contain paths to a non existing locations: [";
for(const auto& name : broken_lsps) {
for (const auto& name : broken_lsps) {
message << name << ", ";
}
message.RemoveLast(2);
message << "]";

clGetManager()->DisplayMessage(message, wxICON_WARNING, { { XRCID("lsp-fix-paths"), _("Attempt to fix") } });
clGetManager()->DisplayMessage(message, wxICON_WARNING,
{ { wxID_CANCEL, _("Cancel") }, { XRCID("lsp-fix-paths"), _("Attempt to fix") } });
}

void LanguageServerPlugin::CreateToolBar(clToolBarGeneric* toolbar)
Expand Down Expand Up @@ -142,7 +144,7 @@ void LanguageServerPlugin::UnPlug()
LanguageServerConfig::Get().Save();

// before this plugin is un-plugged we must remove the tab we added
if(!m_mgr->BookDeletePage(PaneId::BOTTOM_BAR, m_logView)) {
if (!m_mgr->BookDeletePage(PaneId::BOTTOM_BAR, m_logView)) {
m_logView->Destroy();
}
m_logView = nullptr;
Expand All @@ -152,10 +154,10 @@ void LanguageServerPlugin::UnPlug()
void LanguageServerPlugin::OnSettings(wxCommandEvent& e)
{
LanguageServerSettingsDlg dlg(EventNotifier::Get()->TopFrame(), false);
if(dlg.ShowModal() == wxID_OK) {
if (dlg.ShowModal() == wxID_OK) {
// restart all language servers
dlg.Save();
if(m_servers) {
if (m_servers) {
// Lets assume that we fixed something in the settings
// and clear all the restart counters
m_servers->ClearRestartCounters();
Expand All @@ -167,7 +169,7 @@ void LanguageServerPlugin::OnSettings(wxCommandEvent& e)
void LanguageServerPlugin::OnRestartLSP(wxCommandEvent& e)
{
wxUnusedVar(e);
if(m_servers) {
if (m_servers) {
m_servers->Reload();
}
}
Expand All @@ -179,22 +181,22 @@ void LanguageServerPlugin::OnInitDone(wxCommandEvent& event)

bool force = false;
const auto& servers = LanguageServerConfig::Get().GetServers();
for(auto& server : servers) {
if(server.second.GetCommand().Contains(".codelite/lsp/clang-tools")) {
for (auto& server : servers) {
if (server.second.GetCommand().Contains(".codelite/lsp/clang-tools")) {
force = true;
break;
}
}

if(LanguageServerConfig::Get().GetServers().empty() || force) {
if (LanguageServerConfig::Get().GetServers().empty() || force) {
LSP_DEBUG() << "Scanning..." << endl;
std::thread thr(
[=](LanguageServerPlugin* plugin) {
std::vector<LSPDetector::Ptr_t> matches;
LSPDetectorManager detector;
LSP_DEBUG() << "***"
<< "Scanning for LSPs... ***" << endl;
if(detector.Scan(matches)) {
if (detector.Scan(matches)) {
LSP_DEBUG() << " ******"
<< "found!" << endl;
}
Expand Down Expand Up @@ -224,17 +226,17 @@ void LanguageServerPlugin::OnEditorContextMenu(clContextMenuEvent& event)
bool add_rename_symbol = lsp->IsRenameSupported();

// nothing to be done here
if(!add_find_symbol && !add_find_references && !add_rename_symbol) {
if (!add_find_symbol && !add_find_references && !add_rename_symbol) {
return;
}

wxMenu* menu = event.GetMenu();
if(add_find_references) {
if (add_find_references) {
menu->PrependSeparator();
menu->Prepend(XRCID("lsp_find_references"), _("Find references"));
}
menu->PrependSeparator();
if(add_rename_symbol) {
if (add_rename_symbol) {
menu->Prepend(XRCID("lsp_rename_symbol"), _("Rename symbol"));
}
menu->Prepend(XRCID("lsp_find_symbol"), _("Find symbol"));
Expand Down Expand Up @@ -289,40 +291,40 @@ void LanguageServerPlugin::ConfigureLSPs(const std::vector<LSPDetector::Ptr_t>&
{
LSP_DEBUG() << " ******"
<< "ConfigureLSPs is called!" << endl;
if(lsps.empty()) {
if (lsps.empty()) {
LSP_DEBUG() << "ConfigureLSPs: no LSPs found. Nothing to be done here" << endl;
return;
}

LanguageServerConfig& config = LanguageServerConfig::Get();
// remove clangd installed under ~/.codelite/lsp/clang-tools
wxArrayString serversToRemove;
for(const auto& server : config.GetServers()) {
if(server.second.GetCommand().Contains(".codelite/lsp/clang-tools")) {
for (const auto& server : config.GetServers()) {
if (server.second.GetCommand().Contains(".codelite/lsp/clang-tools")) {
serversToRemove.Add(server.first);
}
}

bool force = !serversToRemove.IsEmpty();
// remove all old entries
for(const auto& name : serversToRemove) {
for (const auto& name : serversToRemove) {
LSP_SYSTEM() << "Removing broken LSP server:" << name << endl;
config.RemoveServer(name);
}

LSP_DEBUG() << "ConfigureLSPs: there are currently" << config.GetServers().size() << "LSPs configured" << endl;
if(config.GetServers().empty() || force) {
if (config.GetServers().empty() || force) {
LSP_DEBUG() << "No LSPs configured - auto configuring" << endl;
// Only if the user did not configure LSP before, we configure it for him
for(auto lsp : lsps) {
for (auto lsp : lsps) {
LanguageServerEntry entry;
lsp->GetLanguageServerEntry(entry);
config.AddServer(entry);
LSP_DEBUG() << "Adding LSP:" << entry.GetName() << endl;
}
config.SetEnabled(true);
config.Save();
if(m_servers) {
if (m_servers) {
m_servers->Reload();
}
}
Expand Down Expand Up @@ -380,7 +382,7 @@ void LanguageServerPlugin::OnLSPConfigure(clLanguageServerEvent& event)
LanguageServerEntry* pentry = &entry;

auto d = LanguageServerConfig::Get().GetServer(event.GetLspName());
if(!d.IsNull()) {
if (!d.IsNull()) {
LSP_DEBUG() << "an LSP with the same name:" << event.GetLspName() << "already exists. updating it" << endl;
pentry = &d;
}
Expand Down Expand Up @@ -415,7 +417,7 @@ wxString LanguageServerPlugin::GetEditorFilePath(IEditor* editor) const { return
void LanguageServerPlugin::OnLSPEnableServer(clLanguageServerEvent& event)
{
auto& lsp_config = LanguageServerConfig::Get().GetServer(event.GetLspName());
if(lsp_config.IsNull()) {
if (lsp_config.IsNull()) {
return;
}
lsp_config.SetEnabled(true);
Expand All @@ -424,7 +426,7 @@ void LanguageServerPlugin::OnLSPEnableServer(clLanguageServerEvent& event)
void LanguageServerPlugin::OnLSPDisableServer(clLanguageServerEvent& event)
{
auto& lsp_config = LanguageServerConfig::Get().GetServer(event.GetLspName());
if(lsp_config.IsNull()) {
if (lsp_config.IsNull()) {
return;
}
lsp_config.SetEnabled(false);
Expand All @@ -436,7 +438,7 @@ void LanguageServerPlugin::LogMessage(const wxString& server_name, const wxStrin

int ansi_colour_code = AnsiColours::NormalText();
wxString label = "T "; // trace
switch(log_leve) {
switch (log_leve) {
case 1:
ansi_colour_code = AnsiColours::Red(); // error
label = "E ";
Expand Down Expand Up @@ -474,35 +476,35 @@ void LanguageServerPlugin::OnFixLSPPaths(wxCommandEvent& event)

wxUnusedVar(event);
auto broken_lsps = GetBrokenLSPs();
if(broken_lsps.empty()) {
if (broken_lsps.empty()) {
return;
}

wxBusyCursor bc;
std::vector<LSPDetector::Ptr_t> matches;
LSPDetectorManager detector;
if(detector.Scan(matches)) {
if (detector.Scan(matches)) {
wxArrayString fixed;
for(const wxString& broken_lsp : broken_lsps) {
for (const wxString& broken_lsp : broken_lsps) {
auto& lsp = LanguageServerConfig::Get().GetServer(broken_lsp);
if(lsp.IsNull()) {
if (lsp.IsNull()) {
// Could not find it
continue;
}

// Check to see if
for(size_t i = 0; i < matches.size(); ++i) {
for (size_t i = 0; i < matches.size(); ++i) {
LanguageServerEntry entry;
matches[i]->GetLanguageServerEntry(entry);
if(entry.GetName() == broken_lsp) {
if (entry.GetName() == broken_lsp) {
lsp = entry;
fixed.Add(broken_lsp);
break;
}
}
}

if(!fixed.empty()) {
if (!fixed.empty()) {
LanguageServerConfig::Get().Save();
m_servers->Reload();
}
Expand All @@ -513,10 +515,10 @@ wxArrayString LanguageServerPlugin::GetBrokenLSPs() const
{
wxArrayString broken_lsps;
const auto& servers = LanguageServerConfig::Get().GetServers();
for(const auto& [name, server] : servers) {
for (const auto& [name, server] : servers) {
auto argv = StringUtils::BuildArgv(server.GetCommand());
// Check that the first argument (the executable path) exists
if(server.IsEnabled() && argv.empty() || !wxFileName::FileExists(argv[0])) {
if (server.IsEnabled() && argv.empty() || !wxFileName::FileExists(argv[0])) {
broken_lsps.push_back(name);
}
}
Expand Down
Loading

0 comments on commit bc0c391

Please sign in to comment.