Skip to content

Commit

Permalink
LSP & Source code formatter changes
Browse files Browse the repository at this point in the history
- Remote config file "codelite-remote.json" now supports the LSP & Source Code Formatter "command" property in both array and string formats
- Added 'cmake-format' to the default formatter tools
- LSP: expand command macros

Signed-off-by: Eran Ifrah <eran@codelite.org>
  • Loading branch information
eranif committed Dec 1, 2024
1 parent baf7836 commit 1fc7117
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 38 deletions.
20 changes: 15 additions & 5 deletions CodeFormatter/codeformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ void inc_save_count(const wxString& filepath)

// Allocate the code formatter on the heap, it will be freed by
// the application
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
return new CodeFormatter(manager);
}
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager) { return new CodeFormatter(manager); }

CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
Expand Down Expand Up @@ -478,7 +475,20 @@ void CodeFormatter::OnWorkspaceLoaded(clWorkspaceEvent& e)
continue;
}

wxString cmd = json["command"].toString();
// Try both array and string name
wxArrayString cmdArray;
wxString cmd;

if (json.hasNamedObject("command") && json["command"].isArray()) {
cmdArray = json["command"].toArrayString();
} else {
cmd = json["command"].toString();
}

if (!cmdArray.empty()) {
cmd = StringUtils::BuildCommandStringFromArray(cmdArray);
}

wxString wd = json["working_directory"].toString();

wxString remote_cmd;
Expand Down
42 changes: 33 additions & 9 deletions LanguageServer/LanguageServerCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ wxString json_get_server_config_command(JSON* root, const wxString& server_name)
return json["command"].toString();
}

wxArrayString json_get_server_config_command_array(JSON* root, const wxString& server_name)
{
auto json = json_get_server_config(root, server_name);
if (!json.isOk()) {
return {};
}

if (json.hasNamedObject("command") && json["command"].isArray()) {
return json["command"].toArrayString();
}
return {};
}

wxString json_get_server_config_working_directory(JSON* root, const wxString& server_name)
{
auto json = json_get_server_config(root, server_name);
Expand Down Expand Up @@ -619,6 +632,8 @@ void LanguageServerCluster::StartServer(const LanguageServerEntry& entry)
wxString working_directory;
wxString project;
clEnvList_t env_list;
// Expand the working directory (which is later used as the rootUri)
wxArrayString lspCommandArray;
if (is_remote) {
// remote workspace
// read the commands from the codelite-remote.json file (the m_remoteHelper does that for us)
Expand All @@ -628,7 +643,11 @@ void LanguageServerCluster::StartServer(const LanguageServerEntry& entry)
return;
}

command = json_get_server_config_command(root, entry.GetName());
// Try to new way
lspCommandArray = json_get_server_config_command_array(root, entry.GetName());
if (lspCommandArray.empty()) {
command = json_get_server_config_command(root, entry.GetName());
}
working_directory = json_get_server_config_working_directory(root, entry.GetName());
working_directory = m_remoteHelper->ReplaceMacros(working_directory);
env_list = json_get_server_config_env(root, entry.GetName());
Expand All @@ -643,15 +662,20 @@ void LanguageServerCluster::StartServer(const LanguageServerEntry& entry)
working_directory = MacroManager::Instance()->Expand(working_directory, clGetManager(), project);
}

// Expand the working directory (which is later used as the rootUri)
wxArrayString lspCommand;
lspCommand = StringUtils::BuildCommandArrayFromString(command);
if (lspCommandArray.empty()) {
lspCommandArray = StringUtils::BuildCommandArrayFromString(command);
}

// Expand any command macro
for (auto& cmd : lspCommandArray) {
cmd = MacroManager::Instance()->Expand(cmd, clGetManager(), project);
}

if (!is_remote && !lspCommand.empty()) {
wxString mainCommand = StringUtils::StripDoubleQuotes(lspCommand[0]);
if (!is_remote && !lspCommandArray.empty()) {
wxString mainCommand = StringUtils::StripDoubleQuotes(lspCommandArray[0]);
if (!wxFileExists(mainCommand)) {
LSP_WARNING() << "Disabling lsp:" << entry.GetName() << endl;
LSP_WARNING() << lspCommand[0] << ". No such file" << endl;
LSP_WARNING() << lspCommandArray[0] << ". No such file" << endl;
LanguageServerConfig::Get().GetServer(entry.GetName()).SetEnabled(false);
LanguageServerConfig::Get().Save();
return;
Expand All @@ -662,7 +686,7 @@ void LanguageServerCluster::StartServer(const LanguageServerEntry& entry)
LSP_DEBUG() << "Connection string:" << entry.GetConnectionString() << endl;

if (entry.IsAutoRestart()) {
LSP_DEBUG() << "lspCommand:" << lspCommand;
LSP_DEBUG() << "lspCommand:" << lspCommandArray;
LSP_DEBUG() << "entry.GetWorkingDirectory():" << working_directory;
}
LSP_DEBUG() << "working_directory:" << working_directory;
Expand All @@ -679,7 +703,7 @@ void LanguageServerCluster::StartServer(const LanguageServerEntry& entry)

LSPStartupInfo startup_info;
startup_info.SetConnectioString(entry.GetConnectionString());
startup_info.SetLspServerCommand(lspCommand);
startup_info.SetLspServerCommand(lspCommandArray);
startup_info.SetFlags(flags);
startup_info.SetWorkingDirectory(working_directory);

Expand Down
21 changes: 11 additions & 10 deletions Plugin/CodeLiteRemoteHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CodeLiteRemoteHelper::CodeLiteRemoteHelper()
{
Bind(wxEVT_WORKSPACE_LOADED, &CodeLiteRemoteHelper::OnWorkspaceLoaded, this);
Bind(wxEVT_WORKSPACE_CLOSED, &CodeLiteRemoteHelper::OnWorkspaceClosed, this);
if(ThePlatform->Which("ssh", &m_ssh_exe)) {
if (ThePlatform->Which("ssh", &m_ssh_exe)) {
::WrapWithQuotes(m_ssh_exe);
}
}
Expand All @@ -35,7 +35,7 @@ void CodeLiteRemoteHelper::OnWorkspaceLoaded(clWorkspaceEvent& event)
Clear();
m_isRemoteLoaded = event.IsRemote();

if(m_isRemoteLoaded) {
if (m_isRemoteLoaded) {
wxString path = event.GetFileName();
path.Replace("\\", "/");
path = path.BeforeLast('/');
Expand All @@ -45,12 +45,12 @@ void CodeLiteRemoteHelper::OnWorkspaceLoaded(clWorkspaceEvent& event)
}
m_remoteAccount = event.GetRemoteAccount();

if(m_isRemoteLoaded && m_codeliteRemoteJSONContent.empty()) {
if (m_isRemoteLoaded && m_codeliteRemoteJSONContent.empty()) {
// load codelite-remote.json file
wxString codelite_remote_json_path = m_workspacePath + "/.codelite/codelite-remote.json";
#if USE_SFTP
wxMemoryBuffer membuf;
if(clSFTPManager::Get().AwaitReadFile(codelite_remote_json_path, m_remoteAccount, &membuf)) {
if (clSFTPManager::Get().AwaitReadFile(codelite_remote_json_path, m_remoteAccount, &membuf)) {
wxString content((const char*)membuf.GetData(), wxConvUTF8, membuf.GetDataLen());
m_codeliteRemoteJSONContent.swap(content);
ProcessCodeLiteRemoteJSON(codelite_remote_json_path);
Expand All @@ -67,7 +67,7 @@ void CodeLiteRemoteHelper::OnWorkspaceClosed(clWorkspaceEvent& event)

JSON* CodeLiteRemoteHelper::GetPluginConfig(const wxString& plugin_name) const
{
if(m_plugins_configs.count(plugin_name) == 0) {
if (m_plugins_configs.count(plugin_name) == 0) {
return nullptr;
}
return m_plugins_configs.find(plugin_name)->second;
Expand All @@ -81,7 +81,7 @@ void CodeLiteRemoteHelper::Clear()
m_workspacePath.clear();
m_remoteAccount.clear();
m_codeliteRemoteJSONContent.clear();
for(auto& vt : m_plugins_configs) {
for (auto& vt : m_plugins_configs) {
wxDELETE(vt.second);
}
m_plugins_configs.clear();
Expand Down Expand Up @@ -110,22 +110,23 @@ void add_formatter_tool(JSONItem& tools_arr, const wxString& name, const wxStrin
void CodeLiteRemoteHelper::ProcessCodeLiteRemoteJSON(const wxString& filepath)
{
JSON root(m_codeliteRemoteJSONContent);
if(!root.isOk()) {
if (!root.isOk()) {
return;
}

auto json = root.toElement();
{
// upgrade code: ensure that source code formatter
// entries exist
if(!json.hasNamedObject("Source Code Formatter")) {
if (!json.hasNamedObject("Source Code Formatter")) {
// missing the newly added "Source Code Formatter" section
// add the default entry
auto json_code_formatter = json.AddObject("Source Code Formatter");
auto tools_arr = json_code_formatter.AddArray("tools");
add_formatter_tool(tools_arr, "jq", "jq . -S $(CurrentFileRelPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "yq", "yq . $(CurrentFileRelPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "clang-format", "clang-format $(CurrentFileRelPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "clang-format", "clang-format $(CurrentFileFullPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "cmake-format", "cmake-format -i $(CurrentFileFullPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "xmllint", "xmllint --format $(CurrentFileRelPath)", "$(WorkspacePath)");
add_formatter_tool(tools_arr, "rustfmt", "rustfmt --edition 2021 $(CurrentFileRelPath)",
"$(WorkspacePath)");
Expand All @@ -137,7 +138,7 @@ void CodeLiteRemoteHelper::ProcessCodeLiteRemoteJSON(const wxString& filepath)
}

auto child = json.firstChild();
while(child.isOk()) {
while (child.isOk()) {
const wxString& plugin_name = child.GetPropertyName();
auto p = new JSON(child.format(false));
m_plugins_configs.insert({ plugin_name, p });
Expand Down
60 changes: 46 additions & 14 deletions Remoty/sample_codelite_remote_json.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
#ifndef SAMPLE_CODELITE_REMOTE_JSON_HPP
#define SAMPLE_CODELITE_REMOTE_JSON_HPP

#include <wx/string.h>

const wxString DEFAULT_CODELITE_REMOTE_JSON = R"EOF(
{
const wxString DEFAULT_CODELITE_REMOTE_JSON = R"EOF({
"Language Server Plugin": {
"servers": [
{
"command": "/usr/bin/clangd --limit-results=500 --header-insertion-decorators=1",
"command": [
"clangd",
"--limit-results=500",
"--header-insertion-decorators=0",
"--compile-commands-dir=$(WorkspacePath)"
],
"env": [],
"name": "clangd",
"working_directory": "$(WorkspacePath)"
},
{
"command": "rust-analyzer",
"command": [
"rust-analyzer"
],
"env": [],
"name": "rust-analyzer",
"working_directory": "$(WorkspacePath)"
},
{
"command": "python3 -m pylsp",
"command": [
"python3",
"-m",
"pylsp"
],
"env": [
{
"name": "PYTHONPATH",
Expand All @@ -35,28 +42,53 @@ const wxString DEFAULT_CODELITE_REMOTE_JSON = R"EOF(
"Source Code Formatter": {
"tools": [
{
"command": "jq . -S $(CurrentFileRelPath)",
"command": [
"jq",
".",
"-S",
"$(CurrentFileRelPath)"
],
"name": "jq",
"working_directory": "$(WorkspacePath)"
},
{
"command": "clang-format $(CurrentFileRelPath)",
"command": [
"clang-format",
"$(CurrentFileRelPath)"
],
"name": "clang-format",
"working_directory": "$(WorkspacePath)"
},
{
"command": "xmllint --format $(CurrentFileRelPath)",
"command": [
"cmake-format",
"-i",
"$(CurrentFileFullPath)"
],
"name": "cmake-format",
"working_directory": "$(WorkspacePath)"
},
{
"command": [
"xmllint",
"--format",
"$(CurrentFileRelPath)"
],
"name": "xmllint",
"working_directory": "$(WorkspacePath)"
},
{
"command": "rustfmt --edition 2021 $(CurrentFileRelPath)",
"command": [
"rustfmt",
"--edition",
"2021",
"$(CurrentFileRelPath)"
],
"name": "rustfmt",
"working_directory": "$(WorkspacePath)"
}
]
}
}
)EOF";
#endif // SAMPLE_CODELITE_REMOTE_JSON_HPP
)EOF";

0 comments on commit 1fc7117

Please sign in to comment.