Skip to content

Commit

Permalink
git: do not run any commands on the gitconsol unless the view is shown
Browse files Browse the repository at this point in the history
Signed-off-by: Eran Ifrah <[email protected]>
  • Loading branch information
eranif committed Dec 16, 2024
1 parent f6e9e1e commit 1238958
Show file tree
Hide file tree
Showing 3 changed files with 411 additions and 201 deletions.
75 changes: 53 additions & 22 deletions Plugin/clCodeLiteRemoteProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ clCodeLiteRemoteProcess::~clCodeLiteRemoteProcess()
wxDELETE(m_process);
}

void clCodeLiteRemoteProcess::StartInteractive(const wxString& account, const wxString& scriptPath,
void clCodeLiteRemoteProcess::StartInteractive(const wxString& account,
const wxString& scriptPath,
const wxString& contextString)
{
auto ssh_account = SSHAccountInfo::LoadAccount(account);
Expand Down Expand Up @@ -203,7 +204,8 @@ void clCodeLiteRemoteProcess::StartIfNotRunning()
m_process = ::CreateAsyncProcess(this, command, IProcessCreateDefault | IProcessRawOutput);
}

void clCodeLiteRemoteProcess::StartInteractive(const SSHAccountInfo& account, const wxString& scriptPath,
void clCodeLiteRemoteProcess::StartInteractive(const SSHAccountInfo& account,
const wxString& scriptPath,
const wxString& contextString)
{
if (m_process) {
Expand Down Expand Up @@ -306,7 +308,12 @@ void clCodeLiteRemoteProcess::ProcessOutput()
}

auto p = m_completionCallbacks.front();
if (p.handler) {
if (p.user_callback != nullptr) {
p.aggregated_output << buffer;
if (is_completed) {
p.user_callback(p.aggregated_output);
}
} else if (p.handler) {
auto handler = static_cast<CodeLiteRemoteProcess*>(p.handler);
handler->PostOutputEvent(buffer);
if (is_completed) {
Expand Down Expand Up @@ -341,7 +348,7 @@ void clCodeLiteRemoteProcess::ListLSPs()
m_process->Write(item.format(false) + "\n");

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListLSPsOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListLSPsOutput, nullptr, nullptr });
}

void clCodeLiteRemoteProcess::ListFiles(const wxString& root_dir, const wxString& extensions)
Expand All @@ -360,11 +367,11 @@ void clCodeLiteRemoteProcess::ListFiles(const wxString& root_dir, const wxString
m_process->Write(item.format(false) + "\n");

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListFilesOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListFilesOutput, nullptr, nullptr });
}

void clCodeLiteRemoteProcess::Search(const wxString& root_dir, const wxString& extensions, const wxString& find_what,
bool whole_word, bool icase)
void clCodeLiteRemoteProcess::Search(
const wxString& root_dir, const wxString& extensions, const wxString& find_what, bool whole_word, bool icase)
{
if (!m_process) {
return;
Expand All @@ -385,10 +392,12 @@ void clCodeLiteRemoteProcess::Search(const wxString& root_dir, const wxString& e
LOG_IF_TRACE { clDEBUG1() << command << endl; }

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindOutput, nullptr, nullptr });
}

void clCodeLiteRemoteProcess::Locate(const wxString& path, const wxString& name, const wxString& ext,
void clCodeLiteRemoteProcess::Locate(const wxString& path,
const wxString& name,
const wxString& ext,
const std::vector<wxString>& versions)
{
if (!m_process) {
Expand Down Expand Up @@ -418,7 +427,7 @@ void clCodeLiteRemoteProcess::Locate(const wxString& path, const wxString& name,
LOG_IF_TRACE { clDEBUG1() << command << endl; }

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnLocateOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnLocateOutput, nullptr, nullptr });
}

void clCodeLiteRemoteProcess::FindPath(const wxString& path)
Expand All @@ -438,7 +447,7 @@ void clCodeLiteRemoteProcess::FindPath(const wxString& path)
LOG_IF_TRACE { clDEBUG1() << command << endl; }

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindPathOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindPathOutput, nullptr, nullptr });
}

void clCodeLiteRemoteProcess::ResetStates()
Expand All @@ -447,8 +456,8 @@ void clCodeLiteRemoteProcess::ResetStates()
m_fif_files_scanned = 0;
}

bool clCodeLiteRemoteProcess::DoExec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env,
IProcess* handler)
bool clCodeLiteRemoteProcess::DoExec(
const wxString& cmd, const wxString& working_directory, const clEnvList_t& env, IProcess* handler, UserCallback cb)
{
if (!m_process) {
return false;
Expand All @@ -472,7 +481,7 @@ bool clCodeLiteRemoteProcess::DoExec(const wxString& cmd, const wxString& workin
m_process->Write(command + "\n");

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnExecOutput, handler });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnExecOutput, handler, cb });
return true;
}

Expand All @@ -485,6 +494,18 @@ void clCodeLiteRemoteProcess::Exec(const wxArrayString& args, const wxString& wo
DoExec(cmdstr, working_directory, env);
}

void clCodeLiteRemoteProcess::ExecWithCallback(const wxArrayString& args,
UserCallback cb,
const wxString& working_directory,
const clEnvList_t& env)
{
wxString cmdstr = GetCmdString(args);
if (cmdstr.empty()) {
return;
}
DoExec(cmdstr, working_directory, env, nullptr, std::move(cb));
}

void clCodeLiteRemoteProcess::Exec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env)
{
DoExec(cmd, working_directory, env);
Expand All @@ -502,8 +523,10 @@ void clCodeLiteRemoteProcess::Write(const wxString& str)
}
}

IProcess* clCodeLiteRemoteProcess::CreateAsyncProcess(wxEvtHandler* handler, const wxString& cmd,
const wxString& working_directory, const clEnvList_t& env)
IProcess* clCodeLiteRemoteProcess::CreateAsyncProcess(wxEvtHandler* handler,
const wxString& cmd,
const wxString& working_directory,
const clEnvList_t& env)
{
CodeLiteRemoteProcess* p = new CodeLiteRemoteProcess(handler, this);
if (DoExec(cmd, working_directory, env, p)) {
Expand All @@ -513,8 +536,10 @@ IProcess* clCodeLiteRemoteProcess::CreateAsyncProcess(wxEvtHandler* handler, con
return nullptr;
}

void clCodeLiteRemoteProcess::CreateAsyncProcessCB(const wxString& cmd, std::function<void(const wxString&)> callback,
const wxString& working_directory, const clEnvList_t& env)
void clCodeLiteRemoteProcess::CreateAsyncProcessCB(const wxString& cmd,
std::function<void(const wxString&)> callback,
const wxString& working_directory,
const clEnvList_t& env)
{
CodeLiteRemoteProcess* p = new CodeLiteRemoteProcess(nullptr, this);
p->SetCallback(std::move(callback));
Expand Down Expand Up @@ -694,7 +719,9 @@ void clCodeLiteRemoteProcess::OnExecOutput(const wxString& buffer, bool is_compl
}
}

bool clCodeLiteRemoteProcess::SyncExec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env,
bool clCodeLiteRemoteProcess::SyncExec(const wxString& cmd,
const wxString& working_directory,
const clEnvList_t& env,
wxString* output)
{
if (!m_completionCallbacks.empty()) {
Expand Down Expand Up @@ -745,8 +772,12 @@ bool clCodeLiteRemoteProcess::SyncExec(const wxString& cmd, const wxString& work
return false;
}

void clCodeLiteRemoteProcess::Replace(const wxString& root_dir, const wxString& extensions, const wxString& find_what,
const wxString& replace_with, bool whole_word, bool icase)
void clCodeLiteRemoteProcess::Replace(const wxString& root_dir,
const wxString& extensions,
const wxString& find_what,
const wxString& replace_with,
bool whole_word,
bool icase)
{
if (!m_process) {
return;
Expand All @@ -768,5 +799,5 @@ void clCodeLiteRemoteProcess::Replace(const wxString& root_dir, const wxString&
LOG_IF_TRACE { clDEBUG1() << command << endl; }

// push a callback
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnReplaceOutput, nullptr });
m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnReplaceOutput, nullptr, nullptr });
}
65 changes: 47 additions & 18 deletions Plugin/clCodeLiteRemoteProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ class WXDLLIMPEXP_SDK clCodeLiteRemoteProcess : public wxEvtHandler
{
protected:
typedef void (clCodeLiteRemoteProcess::*CallbackFunc)(const wxString&, bool);
struct callback_pair {
typedef std::function<void(const wxString&)> UserCallback;
struct CallbackOptions {
CallbackFunc func = nullptr;
IProcess* handler = nullptr;
callback_pair(CallbackFunc func, IProcess* handler)
UserCallback user_callback = nullptr;

// When user_callback is used, we aggregate the output here until "is_completed"
// is true, only then we call the user_callback
wxString aggregated_output;
CallbackOptions(CallbackFunc func, IProcess* handler, UserCallback user_callback)
{
this->func = func;
this->handler = handler;
this->user_callback = user_callback;
}
};

protected:
IProcess* m_process = nullptr;
std::deque<callback_pair> m_completionCallbacks;
std::deque<CallbackOptions> m_completionCallbacks;
wxString m_outputRead;
size_t m_fif_matches_count = 0;
size_t m_fif_files_scanned = 0;
Expand All @@ -55,18 +62,22 @@ class WXDLLIMPEXP_SDK clCodeLiteRemoteProcess : public wxEvtHandler
void OnLocateOutput(const wxString& buffer, bool is_completed);
void OnFindPathOutput(const wxString& buffer, bool is_completed);
void OnExecOutput(const wxString& buffer, bool is_completed);
bool DoExec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env,
IProcess* handler = nullptr);
bool DoExec(const wxString& cmd,
const wxString& working_directory,
const clEnvList_t& env,
IProcess* handler = nullptr,
UserCallback cb = nullptr);

template <typename Container> wxString GetCmdString(const Container& args) const
template <typename Container>
wxString GetCmdString(const Container& args) const
{
if(args.empty()) {
if (args.empty()) {
return wxEmptyString;
}

wxString cmdstr;
for(auto arg : args) {
if(arg.Contains(" ")) {
for (auto arg : args) {
if (arg.Contains(" ")) {
// escape any " before we start escaping
arg.Replace("\"", "\\\"");
// now wrap with double quotes
Expand Down Expand Up @@ -116,20 +127,32 @@ class WXDLLIMPEXP_SDK clCodeLiteRemoteProcess : public wxEvtHandler
/**
* @brief find in files on a remote machine
*/
void Search(const wxString& root_dir, const wxString& extensions, const wxString& find_what, bool whole_word,
bool icase);
void Search(
const wxString& root_dir, const wxString& extensions, const wxString& find_what, bool whole_word, bool icase);

/**
* @brief replace in file on a remote machine
*/
void Replace(const wxString& root_dir, const wxString& extensions, const wxString& find_what,
const wxString& replace_with, bool whole_word, bool icase);
void Replace(const wxString& root_dir,
const wxString& extensions,
const wxString& find_what,
const wxString& replace_with,
bool whole_word,
bool icase);

/**
* @brief execute a command on the remote machine
*/
void Exec(const wxArrayString& args, const wxString& working_directory, const clEnvList_t& env);

/**
* @brief execute a command on the remote machine trigger "cb" when output arrives
*/
void ExecWithCallback(const wxArrayString& args,
UserCallback cb,
const wxString& working_directory = wxEmptyString,
const clEnvList_t& env = {});

/**
* @brief attempt to locate a file on the remote machine with possible version number
*/
Expand All @@ -147,24 +170,30 @@ class WXDLLIMPEXP_SDK clCodeLiteRemoteProcess : public wxEvtHandler
void FindPath(const wxString& path);

/**
* @brief call 'exec' and return an instance of IProcess. This method is for compatability with the
* @brief call 'exec' and return an instance of IProcess. This method is for compatibility with the
* CreateAsyncProcess family of functions
* @note it is up to the caller to delete the return process object
*/
IProcess* CreateAsyncProcess(wxEvtHandler* handler, const wxString& cmd, const wxString& working_directory,
IProcess* CreateAsyncProcess(wxEvtHandler* handler,
const wxString& cmd,
const wxString& working_directory,
const clEnvList_t& env);
/**
* @brief call 'exec' with callback
*/
void CreateAsyncProcessCB(const wxString& cmd, std::function<void(const wxString&)> callback,
const wxString& working_directory, const clEnvList_t& env);
void CreateAsyncProcessCB(const wxString& cmd,
std::function<void(const wxString&)> callback,
const wxString& working_directory,
const clEnvList_t& env);
/**
* @brief call 'exec' and return an instance of IProcess. This method is for compatability with the
* CreateAsyncProcess family of functions
* @note it is up to the caller to delete the return process object
*/
template <typename Container>
IProcess* CreateAsyncProcess(wxEvtHandler* handler, const Container& cmd, const wxString& working_directory,
IProcess* CreateAsyncProcess(wxEvtHandler* handler,
const Container& cmd,
const wxString& working_directory,
const clEnvList_t& env)
{
wxString cmdstr = GetCmdString(cmd);
Expand Down
Loading

0 comments on commit 1238958

Please sign in to comment.