From 19d7d4a06ed7ab4a3f938017453ba924122a6280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 30 Mar 2023 12:40:01 -0300 Subject: [PATCH] ecode: LSP minor improvements. --- include/eepp/system/process.hpp | 4 +- src/eepp/system/process.cpp | 6 +-- .../ecode/plugins/lsp/lspclientplugin.cpp | 19 ++++++++ .../ecode/plugins/lsp/lspclientserver.cpp | 43 +++++++++++-------- src/tools/ecode/plugins/lsp/lspdefinition.hpp | 1 + 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/include/eepp/system/process.hpp b/include/eepp/system/process.hpp index 3a59b5d1e..34e86b259 100644 --- a/include/eepp/system/process.hpp +++ b/include/eepp/system/process.hpp @@ -49,7 +49,7 @@ class EE_API Process { ** @param command Command line to execute for this process. ** @param options A bit field of Options's to pass. */ Process( const std::string& command, const Uint32& options = getDefaultOptions(), - const std::map& environment = {}, + const std::unordered_map& environment = {}, const std::string& workingDirectory = "", const size_t& bufferSize = 132072 ); ~Process(); @@ -59,7 +59,7 @@ class EE_API Process { ** @param options A bit field of Options's to pass. ** @return On success true is returned. */ bool create( const std::string& command, const Uint32& options = getDefaultOptions(), - const std::map& environment = {}, + const std::unordered_map& environment = {}, const std::string& workingDirectory = "" ); /** @brief Starts a new thread to receive all stdout and stderr data */ diff --git a/src/eepp/system/process.cpp b/src/eepp/system/process.cpp index d80978085..6b67c131e 100644 --- a/src/eepp/system/process.cpp +++ b/src/eepp/system/process.cpp @@ -27,7 +27,7 @@ namespace EE { namespace System { Process::Process() {} Process::Process( const std::string& command, const Uint32& options, - const std::map& environment, + const std::unordered_map& environment, const std::string& workingDirectory, const size_t& bufferSize ) : mBufferSize( bufferSize ) { create( command, options, environment, workingDirectory ); @@ -47,7 +47,7 @@ Process::~Process() { } bool Process::create( const std::string& command, const Uint32& options, - const std::map& environment, + const std::unordered_map& environment, const std::string& workingDirectory ) { if ( mProcess ) return false; @@ -199,7 +199,7 @@ void Process::startAsyncRead( ReadFn readStdOut, ReadFn readStdErr ) { buffer.resize( mBufferSize ); while ( !mShuttingDown ) { n = subprocess_read_stdout( PROCESS_PTR, static_cast( &buffer[0] ), - mBufferSize ); + mBufferSize ); if ( n == 0 ) break; if ( n < static_cast( mBufferSize - 1 ) ) diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index 5c63d7fcd..b1447bf5a 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -704,6 +704,22 @@ static std::string parseCommand( nlohmann::json cmd ) { return command; } +static void tryAddEnv( const json& obj, LSPDefinition& lsp ) { + if ( obj.contains( "env" ) && obj.is_array() ) { + for ( const auto& obje : obj ) { + if ( !obje.is_string() ) + continue; + std::string envStr = obje.get(); + if ( !envStr.empty() && envStr.find_first_of( "=" ) != std::string::npos ) { + auto envp = String::split( envStr, '=' ); + if ( envp.size() == 2 ) { + lsp.env[envp[0]] = envp[1]; + } + } + } + } +} + void LSPClientPlugin::loadLSPConfig( std::vector& lsps, const std::string& path, bool updateConfigFile ) { std::string data; @@ -809,6 +825,7 @@ void LSPClientPlugin::loadLSPConfig( std::vector& lsps, const std lspR.host = obj.value( "host", "" ); lspR.port = obj.value( "port", 0 ); } + tryAddEnv( obj, lspR ); } } } @@ -877,6 +894,8 @@ void LSPClientPlugin::loadLSPConfig( std::vector& lsps, const std sanitizeCommand( lsp.command ); sanitizeCommand( lsp.commandParameters ); + tryAddEnv( obj, lsp ); + // If the file pattern is repeated, we will overwrite the previous LSP. // The previous LSP should be the "default" LSP that comes with ecode. size_t pos = lspFilePatternPosition( lsps, lsp.filePatterns ); diff --git a/src/tools/ecode/plugins/lsp/lspclientserver.cpp b/src/tools/ecode/plugins/lsp/lspclientserver.cpp index 880660f2c..0b8882d87 100644 --- a/src/tools/ecode/plugins/lsp/lspclientserver.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientserver.cpp @@ -1156,8 +1156,8 @@ bool LSPClientServer::start() { cmd += mLSP.commandParameters; } if ( !cmd.empty() ) { - bool ret = mProcess.create( cmd, Process::getDefaultOptions() | Process::EnableAsync, {}, - mRootPath ); + bool ret = mProcess.create( cmd, Process::getDefaultOptions() | Process::EnableAsync, + mLSP.env, mRootPath ); if ( ret && mProcess.isAlive() ) { mUsingProcess = true; @@ -1271,27 +1271,32 @@ LSPClientServer::LSPRequestHandle LSPClientServer::write( const json& msg, ob[MEMBER_ID] = id; } - std::string sjson = ob.dump(); - sjson = String::format( "Content-Length: %lu\r\n\r\n%s", sjson.length(), sjson.c_str() ); + try { + std::string sjson = ob.dump(); + sjson = String::format( "Content-Length: %lu\r\n\r\n%s", sjson.length(), sjson.c_str() ); - if ( mReady || msg[MEMBER_METHOD] == "initialize" ) { - std::string method; - if ( msg.contains( MEMBER_METHOD ) ) - method = msg[MEMBER_METHOD].get(); - else if ( msg.contains( MEMBER_MESSAGE ) ) - method = msg[MEMBER_MESSAGE]; - Log::info( "LSPClientServer server %s calling %s", mLSP.name.c_str(), method.c_str() ); - Log::debug( "LSPClientServer server %s sending message:\n%s", mLSP.name.c_str(), - sjson.c_str() ); + if ( mReady || msg[MEMBER_METHOD] == "initialize" ) { + std::string method; + if ( msg.contains( MEMBER_METHOD ) ) + method = msg[MEMBER_METHOD].get(); + else if ( msg.contains( MEMBER_MESSAGE ) ) + method = msg[MEMBER_MESSAGE]; + Log::info( "LSPClientServer server %s calling %s", mLSP.name.c_str(), method.c_str() ); + Log::debug( "LSPClientServer server %s sending message:\n%s", mLSP.name.c_str(), + sjson.c_str() ); - if ( mSocket ) { - size_t sent = 0; - mSocket->send( sjson.c_str(), sjson.size(), sent ); + if ( mSocket ) { + size_t sent = 0; + mSocket->send( sjson.c_str(), sjson.size(), sent ); + } else { + mProcess.write( sjson ); + } } else { - mProcess.write( sjson ); + mQueuedMessages.push_back( { std::move( ob ), h, eh } ); } - } else { - mQueuedMessages.push_back( { std::move( ob ), h, eh } ); + } catch ( const json::exception& e ) { + Log::debug( "LSPClientServer::write server %s failed. Coudln't dump json err: %s", + mLSP.name.c_str(), e.what() ); } return ret; diff --git a/src/tools/ecode/plugins/lsp/lspdefinition.hpp b/src/tools/ecode/plugins/lsp/lspdefinition.hpp index 8ed4f7dbb..b1eb13f3f 100644 --- a/src/tools/ecode/plugins/lsp/lspdefinition.hpp +++ b/src/tools/ecode/plugins/lsp/lspdefinition.hpp @@ -20,6 +20,7 @@ struct LSPDefinition { std::vector rootIndicationFileNames; std::string url; std::string host; + std::unordered_map env; int port{ 0 }; nlohmann::json initializationOptions;