1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
#include "NodeDebugger.h"
#include "NodeJSDebuggerDlg.h"
#include "NodeJSExecutable.h"
#include "NodeJSNewWorkspaceDlg.h"
#include "NodeJSWorkspaceConfiguration.h"
#include "NodeJSWorkspaceView.h"
#include "NoteJSWorkspace.h"
#include "asyncprocess.h"
#include "clWorkspaceManager.h"
#include "clWorkspaceView.h"
#include "codelite_events.h"
#include "ctags_manager.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"
#include "imanager.h"
#include "processreaderthread.h"
#include <wx/dir.h>
#include <wx/dirdlg.h>
#include <wx/msgdlg.h>
NodeJSWorkspace* NodeJSWorkspace::ms_workspace = NULL;
NodeJSWorkspace::NodeJSWorkspace(bool dummy)
{
m_dummy = true;
SetWorkspaceType("Node.js");
}
NodeJSWorkspace::NodeJSWorkspace()
: m_clangOldFlag(false)
, m_showWelcomePage(false)
{
SetWorkspaceType("Node.js");
m_view = new NodeJSWorkspaceView(clGetManager()->GetWorkspaceView()->GetBook(), GetWorkspaceType());
clGetManager()->GetWorkspaceView()->AddPage(m_view, GetWorkspaceType());
EventNotifier::Get()->Bind(wxEVT_CMD_CLOSE_WORKSPACE, &NodeJSWorkspace::OnCloseWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &NodeJSWorkspace::OnNewWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_OPEN_WORKSPACE, &NodeJSWorkspace::OnOpenWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_ALL_EDITORS_CLOSED, &NodeJSWorkspace::OnAllEditorsClosed, this);
EventNotifier::Get()->Bind(wxEVT_SAVE_SESSION_NEEDED, &NodeJSWorkspace::OnSaveSession, this);
EventNotifier::Get()->Bind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &NodeJSWorkspace::OnExecute, this);
EventNotifier::Get()->Bind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &NodeJSWorkspace::OnStopExecute, this);
EventNotifier::Get()->Bind(wxEVT_CMD_IS_PROGRAM_RUNNING, &NodeJSWorkspace::OnIsExecuteInProgress, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_START, &NodeJSWorkspace::OnDebugStart, this);
m_terminal.Bind(wxEVT_TERMINAL_COMMAND_EXIT, &NodeJSWorkspace::OnProcessTerminated, this);
m_terminal.Bind(wxEVT_TERMINAL_COMMAND_OUTPUT, &NodeJSWorkspace::OnProcessOutput, this);
}
NodeJSWorkspace::~NodeJSWorkspace()
{
if(!m_dummy) {
EventNotifier::Get()->Unbind(wxEVT_CMD_CLOSE_WORKSPACE, &NodeJSWorkspace::OnCloseWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &NodeJSWorkspace::OnNewWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_OPEN_WORKSPACE, &NodeJSWorkspace::OnOpenWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_ALL_EDITORS_CLOSED, &NodeJSWorkspace::OnAllEditorsClosed, this);
EventNotifier::Get()->Unbind(wxEVT_SAVE_SESSION_NEEDED, &NodeJSWorkspace::OnSaveSession, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &NodeJSWorkspace::OnExecute, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &NodeJSWorkspace::OnStopExecute, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_IS_PROGRAM_RUNNING, &NodeJSWorkspace::OnIsExecuteInProgress, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_START, &NodeJSWorkspace::OnDebugStart, this);
m_debugger.reset(NULL);
m_terminal.Unbind(wxEVT_TERMINAL_COMMAND_EXIT, &NodeJSWorkspace::OnProcessTerminated, this);
m_terminal.Unbind(wxEVT_TERMINAL_COMMAND_OUTPUT, &NodeJSWorkspace::OnProcessOutput, this);
m_terminal.Terminate();
}
}
bool NodeJSWorkspace::IsBuildSupported() const { return false; }
bool NodeJSWorkspace::IsProjectSupported() const { return false; }
void NodeJSWorkspace::Free()
{
if(ms_workspace) { delete ms_workspace; }
ms_workspace = NULL;
}
NodeJSWorkspace* NodeJSWorkspace::Get()
{
if(!ms_workspace) { ms_workspace = new NodeJSWorkspace(); }
return ms_workspace;
}
bool NodeJSWorkspace::IsOpen() const { return m_filename.IsOk() && m_filename.Exists(); }
bool NodeJSWorkspace::Create(const wxFileName& filename)
{
if(IsOpen()) return false;
if(filename.Exists()) return false;
DoClear();
m_filename = filename;
// By default add the workspace path
m_folders.Add(m_filename.GetPath());
Save();
// We dont load the workspace
DoClear();
return true;
}
bool NodeJSWorkspace::Open(const wxFileName& filename)
{
if(IsOpen()) return false;
m_filename = filename;
return DoOpen(m_filename);
}
void NodeJSWorkspace::Close()
{
if(!IsOpen()) return;
// Store the session
clGetManager()->StoreWorkspaceSession(m_filename);
Save();
DoClear();
// disable clang for NodeJS
clGetManager()->EnableClangCodeCompletion(m_clangOldFlag);
// Clear the UI
GetView()->Clear();
// Notify workspace closed event
wxCommandEvent event(wxEVT_WORKSPACE_CLOSED);
EventNotifier::Get()->ProcessEvent(event);
m_debugger.reset(NULL);
// notify codelite to close the currently opened workspace
wxCommandEvent eventClose(wxEVT_MENU, wxID_CLOSE_ALL);
eventClose.SetEventObject(EventNotifier::Get()->TopFrame());
EventNotifier::Get()->TopFrame()->GetEventHandler()->ProcessEvent(eventClose);
m_showWelcomePage = true;
}
void NodeJSWorkspace::DoClear()
{
m_filename.Clear();
m_folders.Clear();
}
void NodeJSWorkspace::Save()
{
NodeJSWorkspaceConfiguration conf(GetFileName());
conf.SetFolders(m_folders);
conf.Save();
}
void NodeJSWorkspace::OnCloseWorkspace(clCommandEvent& e)
{
e.Skip();
if(IsOpen()) {
e.Skip(false);
Close();
}
}
void NodeJSWorkspace::OnNewWorkspace(clCommandEvent& e)
{
e.Skip();
if(e.GetString() == GetWorkspaceType()) {
e.Skip(false);
// Create a new NodeJS workspace
NodeJSNewWorkspaceDlg dlg(NULL);
if(dlg.ShowModal() != wxID_OK) return;
wxFileName workspaceFile = dlg.GetWorkspaceFilename();
if(!workspaceFile.GetDirCount()) {
::wxMessageBox(_("Can not create workspace in the root folder"), _("New Workspace"),
wxICON_ERROR | wxOK | wxCENTER);
return;
}
// Ensure that the path the workspace exists
workspaceFile.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
if(!Create(workspaceFile)) {
::wxMessageBox(_("Failed to create workspace\nWorkspace already exists"), _("New Workspace"),
wxICON_ERROR | wxOK | wxCENTER);
return;
}
Open(workspaceFile);
}
}
bool NodeJSWorkspace::DoOpen(const wxFileName& filename)
{
NodeJSWorkspaceConfiguration conf(filename);
conf.Load();
if(!conf.IsOk()) {
DoClear();
return false;
}
m_folders = conf.GetFolders();
GetView()->Clear();
GetView()->ShowHiddenFiles(conf.IsShowHiddenFiles());
const wxArrayString& folders = GetFolders();
for(size_t i = 0; i < folders.size(); ++i) {
GetView()->AddFolder(folders.Item(i));
}
// Notify codelite that NodeJS workspace is opened
clGetManager()->GetWorkspaceView()->SelectPage(GetWorkspaceType());
clWorkspaceManager::Get().SetWorkspace(this);
// Keep the old clang state before we disable it
const TagsOptionsData& options = TagsManagerST::Get()->GetCtagsOptions();
m_clangOldFlag = (options.GetClangOptions() & CC_CLANG_ENABLED);
clGetManager()->EnableClangCodeCompletion(false);
// Notify that the a new workspace is loaded
wxCommandEvent event(wxEVT_WORKSPACE_LOADED);
event.SetString(filename.GetFullPath());
EventNotifier::Get()->AddPendingEvent(event);
// and finally, request codelite to keep this workspace in the recently opened workspace list
clGetManager()->AddWorkspaceToRecentlyUsedList(m_filename);
// Load the workspace session (if any)
CallAfter(&NodeJSWorkspace::RestoreSession);
// Create new debugger for this workspace
DoAllocateDebugger();
return true;
}
void NodeJSWorkspace::OnOpenWorkspace(clCommandEvent& event)
{
event.Skip();
wxFileName workspaceFile(event.GetFileName());
// Test that this is our workspace
NodeJSWorkspaceConfiguration conf(workspaceFile);
conf.Load();
if(!conf.IsOk()) { return; }
// This is a NodeJS workspace, stop event processing by calling
// event.Skip(false)
event.Skip(false);
// Check if this is a PHP workspace
if(IsOpen()) { Close(); }
Open(workspaceFile);
}
void NodeJSWorkspace::OnAllEditorsClosed(wxCommandEvent& event)
{
event.Skip();
if(m_showWelcomePage) {
m_showWelcomePage = false;
// Show the 'Welcome Page'
wxFrame* frame = EventNotifier::Get()->TopFrame();
wxCommandEvent eventShowWelcomePage(wxEVT_MENU, XRCID("view_welcome_page"));
eventShowWelcomePage.SetEventObject(frame);
frame->GetEventHandler()->AddPendingEvent(eventShowWelcomePage);
}
}
void NodeJSWorkspace::RestoreSession()
{
if(IsOpen()) { clGetManager()->LoadWorkspaceSession(m_filename); }
}
void NodeJSWorkspace::OnSaveSession(clCommandEvent& event)
{
event.Skip();
if(IsOpen()) {
// Call event.Skip(false) so no other session are kept beside ours
event.Skip(false);
clGetManager()->StoreWorkspaceSession(m_filename);
}
}
wxString NodeJSWorkspace::GetFilesMask() const
{
return "*.js;*.html;*.css;*.scss;*.json;*.xml;*.ini;*.md;*.txt;*.text;*.javascript";
}
void NodeJSWorkspace::OnExecute(clExecuteEvent& event)
{
event.Skip();
if(IsOpen()) {
if(m_terminal.IsRunning()) {
::wxMessageBox(_("Another instance is already running. Please stop it before executing another one"),
"CodeLite", wxICON_WARNING | wxCENTER | wxOK);
return;
}
event.Skip(false);
NodeJSDebuggerDlg dlg(EventNotifier::Get()->TopFrame(), NodeJSDebuggerDlg::kExecute);
if(dlg.ShowModal() != wxID_OK) { return; }
wxString command;
wxString command_args;
dlg.GetCommand(command, command_args);
m_terminal.ExecuteConsole(command, true, command_args, dlg.GetWorkingDirectory(), command + " " + command_args);
}
}
void NodeJSWorkspace::OnProcessOutput(clCommandEvent& event)
{
clGetManager()->AppendOutputTabText(kOutputTab_Output, event.GetString());
}
void NodeJSWorkspace::OnProcessTerminated(clCommandEvent& event)
{
wxUnusedVar(event);
EventNotifier::Get()->TopFrame()->Raise();
}
void NodeJSWorkspace::OnIsExecuteInProgress(clExecuteEvent& event)
{
event.Skip();
if(IsOpen()) {
event.Skip(false);
event.SetAnswer(m_terminal.IsRunning());
}
}
void NodeJSWorkspace::OnStopExecute(clExecuteEvent& event)
{
event.Skip();
if(IsOpen()) {
event.Skip(false);
m_terminal.Terminate();
}
}
wxString NodeJSWorkspace::GetProjectFromFile(const wxFileName& filename) const
{
// projects are not supported in NodeJS
return "";
}
void NodeJSWorkspace::GetProjectFiles(const wxString& projectName, wxArrayString& files) const
{
wxUnusedVar(files);
wxUnusedVar(projectName);
}
void NodeJSWorkspace::GetWorkspaceFiles(wxArrayString& files) const
{
// Return all the files
wxDir::GetAllFiles(GetFilename().GetPath(), &files);
}
wxString NodeJSWorkspace::GetActiveProjectName() const { return wxEmptyString; }
wxFileName NodeJSWorkspace::GetProjectFileName(const wxString& projectName) const
{
wxUnusedVar(projectName);
return wxFileName();
}
wxArrayString NodeJSWorkspace::GetWorkspaceProjects() const { return wxArrayString(); }
int NodeJSWorkspace::GetNodeJSMajorVersion() const
{
NodeJSExecutable nodeJS;
int nodeVersion = nodeJS.GetMajorVersion();
clDEBUG() << "NodeJS major version is:" << nodeVersion;
return nodeVersion;
}
void NodeJSWorkspace::DoAllocateDebugger()
{
if((GetNodeJSMajorVersion() >= 8)) {
clDEBUG() << "Successfully allocated new JS debugger";
m_debugger.reset(new NodeDebugger());
} else {
m_debugger.reset();
clWARNING() << "Your Nodejs version is lower than v8, unable to allocate debugger";
}
}
void NodeJSWorkspace::OnDebugStart(clDebugEvent& event)
{
if(IsOpen()) {
if(m_debugger) {
// let our debugger handle it
event.Skip();
} else {
// We don't have a proper debugger, however, we dont want CodeLite to start the wrong debugger
::wxMessageBox(_("Could not instantiate a debugger for your NodeJS version!"), "CodeLite", wxICON_WARNING);
event.Skip(false);
}
} else {
event.Skip();
}
}
NodeDebugger::Ptr_t NodeJSWorkspace::GetDebugger() { return m_debugger; }
void NodeJSWorkspace::AllocateDebugger() { DoAllocateDebugger(); }
|