ecode: LSP minor improvements.

This commit is contained in:
Martín Lucas Golini
2023-03-30 12:40:01 -03:00
parent d0099f2538
commit 19d7d4a06e
5 changed files with 49 additions and 24 deletions

View File

@@ -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<std::string, std::string>& environment = {},
const std::unordered_map<std::string, std::string>& 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<std::string, std::string>& environment = {},
const std::unordered_map<std::string, std::string>& environment = {},
const std::string& workingDirectory = "" );
/** @brief Starts a new thread to receive all stdout and stderr data */

View File

@@ -27,7 +27,7 @@ namespace EE { namespace System {
Process::Process() {}
Process::Process( const std::string& command, const Uint32& options,
const std::map<std::string, std::string>& environment,
const std::unordered_map<std::string, std::string>& 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<std::string, std::string>& environment,
const std::unordered_map<std::string, std::string>& 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<char* const>( &buffer[0] ),
mBufferSize );
mBufferSize );
if ( n == 0 )
break;
if ( n < static_cast<long>( mBufferSize - 1 ) )

View File

@@ -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<std::string>();
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<LSPDefinition>& lsps, const std::string& path,
bool updateConfigFile ) {
std::string data;
@@ -809,6 +825,7 @@ void LSPClientPlugin::loadLSPConfig( std::vector<LSPDefinition>& 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<LSPDefinition>& 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 );

View File

@@ -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<std::string>();
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<std::string>();
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;

View File

@@ -20,6 +20,7 @@ struct LSPDefinition {
std::vector<std::string> rootIndicationFileNames;
std::string url;
std::string host;
std::unordered_map<std::string, std::string> env;
int port{ 0 };
nlohmann::json initializationOptions;