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
|
#include "HelpPluginMessageDlg.h"
#include "HelpPluginSettings.h"
#include "HelpPluginSettingsDlg.h"
#include "clKeyboardManager.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileextmanager.h"
#include "fileutils.h"
#include "helpplugin.h"
#include <wx/msgdlg.h>
#include <wx/stc/stc.h>
#include <wx/uri.h>
#include <wx/xrc/xmlres.h>
static HelpPlugin* thePlugin = NULL;
// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
if(thePlugin == 0) { thePlugin = new HelpPlugin(manager); }
return thePlugin;
}
CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
static PluginInfo info;
info.SetAuthor(wxT("Eran Ifrah"));
info.SetName(wxT("HelpPlugin"));
info.SetDescription(wxT("Provide help based on selected words"));
info.SetVersion(wxT("v1.0"));
return &info;
}
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
HelpPlugin::HelpPlugin(IManager* manager)
: IPlugin(manager)
{
m_longName = _("Provide help based on selected words");
m_shortName = _("HelpPlugin");
wxTheApp->Bind(wxEVT_MENU, &HelpPlugin::OnHelp, this, XRCID("ID_ZEAL_HELP"));
Bind(wxEVT_MENU, &HelpPlugin::OnHelpSettings, this, XRCID("ID_ZEAL_SETTINGS"));
EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_EDITOR, &HelpPlugin::OnEditorContextMenu, this);
clKeyboardManager::Get()->AddGlobalAccelerator("ID_ZEAL_HELP", "F1", "Help::Search the docs for selected text");
}
HelpPlugin::~HelpPlugin() {}
void HelpPlugin::CreateToolBar(clToolBar* toolbar) { wxUnusedVar(toolbar); }
void HelpPlugin::CreatePluginMenu(wxMenu* pluginsMenu)
{
wxMenu* menu = new wxMenu;
menu->Append(XRCID("ID_ZEAL_SETTINGS"), _("Settings..."));
pluginsMenu->Append(wxID_ANY, _("Help Plugin"), menu);
menu->SetNextHandler(this);
this->SetPreviousHandler(menu);
}
void HelpPlugin::UnPlug()
{
wxTheApp->Unbind(wxEVT_MENU, &HelpPlugin::OnHelp, this, XRCID("ID_ZEAL_HELP"));
Unbind(wxEVT_MENU, &HelpPlugin::OnHelpSettings, this, XRCID("ID_ZEAL_SETTINGS"));
EventNotifier::Get()->Unbind(wxEVT_CONTEXT_MENU_EDITOR, &HelpPlugin::OnEditorContextMenu, this);
}
void HelpPlugin::OnEditorContextMenu(clContextMenuEvent& event)
{
event.Skip();
IEditor* editor = m_mgr->GetActiveEditor();
CHECK_PTR_RET(editor);
if(!editor->GetCtrl()->HasSelection()) return;
wxString selection = editor->GetCtrl()->GetSelectedText();
wxString modSelection = selection.BeforeFirst('\n');
modSelection.Trim().Trim(false);
if(modSelection.IsEmpty()) return;
// Ensure we only use 15 chars of the selected text, otherwise the menu label
// will overflow
if(modSelection.length() > 15) { modSelection = modSelection.Mid(0, 15); }
if(selection.Contains("\n")) { modSelection << "..."; }
// Get the context menu
wxMenu* menu = event.GetMenu();
wxBitmap helpBitmap = wxXmlResource::Get()->LoadBitmap("svn_info");
menu->AppendSeparator();
wxString label;
label << _("Search the docs for '") << modSelection << "'";
menu->Append(XRCID("ID_ZEAL_HELP"), label)->SetBitmap(helpBitmap);
menu->AppendSeparator();
}
void HelpPlugin::OnHelp(wxCommandEvent& event)
{
wxUnusedVar(event);
CallAfter(&HelpPlugin::DoHelp);
}
wxString HelpPlugin::DoBuildQueryString()
{
IEditor* editor = m_mgr->GetActiveEditor();
CHECK_PTR_RET_EMPTY_STRING(editor);
// if no selection is available, just launch the help with an empty query
// so Zeal will come to front
if(!editor->GetCtrl()->HasSelection()) return "dash-plugin://";
wxString selection = editor->GetCtrl()->GetSelectedText();
HelpPluginSettings settings;
settings.Load();
// Auto detect the language
wxString language;
wxString label;
FileExtManager::FileType type = FileExtManager::GetType(editor->GetFileName().GetFullName());
switch(type) {
case FileExtManager::TypeCMake:
language << settings.GetCmakeDocset();
break;
case FileExtManager::TypeHeader:
case FileExtManager::TypeSourceC:
case FileExtManager::TypeSourceCpp:
language << settings.GetCxxDocset();
break;
case FileExtManager::TypeHtml:
language << settings.GetHtmlDocset();
break;
case FileExtManager::TypeCSS:
language << settings.GetCssDocset();
break;
case FileExtManager::TypeJS:
language << settings.GetJsDocset();
break;
case FileExtManager::TypePhp:
language << settings.GetPhpDocset();
break;
case FileExtManager::TypeJava:
language << settings.GetJavaDocset();
break;
default:
break;
}
wxString q;
if(!language.IsEmpty()) {
// Build context aware search string
q << "dash-plugin://keys=" << language << "&query=" << selection;
} else {
q << "dash-plugin://query=" << selection;
}
q = FileUtils::EncodeURI(q);
return q;
}
void HelpPlugin::OnHelpSettings(wxCommandEvent& event)
{
wxUnusedVar(event);
HelpPluginSettingsDlg dlg(EventNotifier::Get()->TopFrame());
dlg.ShowModal();
}
void HelpPlugin::DoHelp()
{
wxString query = DoBuildQueryString();
if(query.IsEmpty()) return;
#ifdef __WXGTK__
wxFileName fnZeal("/usr/bin", "zeal");
if(!fnZeal.Exists()) {
HelpPluginMessageDlg dlg(EventNotifier::Get()->TopFrame());
dlg.ShowModal();
}
wxString command;
command << fnZeal.GetFullPath() << " "
<< "\"" << query << "\"";
clDEBUG() << "Help Plugin:" << command << clEndl;
::wxExecute(command);
#else
clDEBUG() << "Help Plugin:" << query << clEndl;
if(!::wxLaunchDefaultBrowser(query)) {
HelpPluginMessageDlg dlg(EventNotifier::Get()->TopFrame());
dlg.ShowModal();
}
#endif
}
|