forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcompletion.cpp
162 lines (138 loc) · 5.13 KB
/
wordcompletion.cpp
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
#include "wordcompletion.h"
#include <wx/xrc/xmlres.h>
#include "WordCompletionThread.h"
#include <wx/stc/stc.h>
#include "clKeyboardManager.h"
#include <wx/app.h>
#include "WordCompletionSettingsDlg.h"
#include "event_notifier.h"
#include "cl_command_event.h"
#include "wxCodeCompletionBoxManager.h"
static WordCompletionPlugin* thePlugin = NULL;
// Define the plugin entry point
extern "C" EXPORT IPlugin* CreatePlugin(IManager* manager)
{
if(thePlugin == 0) {
thePlugin = new WordCompletionPlugin(manager);
}
return thePlugin;
}
extern "C" EXPORT PluginInfo GetPluginInfo()
{
PluginInfo info;
info.SetAuthor(wxT("Eran Ifrah"));
info.SetName(wxT("Word Completion"));
info.SetDescription(wxT("Suggest completion based on words typed in the editor"));
info.SetVersion(wxT("v1.0"));
return info;
}
extern "C" EXPORT int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
WordCompletionPlugin::WordCompletionPlugin(IManager* manager)
: IPlugin(manager)
, m_thread(NULL)
{
m_longName = wxT("Suggest completion based on words typed in the editor");
m_shortName = wxT("Word Completion");
m_thread = new WordCompletionThread(this);
m_thread->Start();
wxTheApp->Connect(XRCID("text_word_complete"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(WordCompletionPlugin::OnWordComplete),
NULL,
this);
wxTheApp->Connect(XRCID("text_word_complete_settings"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(WordCompletionPlugin::OnSettings),
NULL,
this);
clKeyboardManager::Get()->AddGlobalAccelerator(
"text_word_complete", "Ctrl-ENTER", "Plugins::Word Completion::Show word completion");
}
WordCompletionPlugin::~WordCompletionPlugin() {}
clToolBar* WordCompletionPlugin::CreateToolBar(wxWindow* parent)
{
wxUnusedVar(parent);
clToolBar* tb(NULL);
return tb;
}
void WordCompletionPlugin::CreatePluginMenu(wxMenu* pluginsMenu)
{
wxMenu* menu = new wxMenu;
menu->Append(XRCID("text_word_complete"), _("Show Word Completion"));
menu->AppendSeparator();
menu->Append(XRCID("text_word_complete_settings"), _("Settings"));
pluginsMenu->Append(wxID_ANY, GetShortName(), menu);
}
void WordCompletionPlugin::HookPopupMenu(wxMenu* menu, MenuType type)
{
wxUnusedVar(menu);
wxUnusedVar(type);
}
void WordCompletionPlugin::UnHookPopupMenu(wxMenu* menu, MenuType type)
{
wxUnusedVar(menu);
wxUnusedVar(type);
}
void WordCompletionPlugin::UnPlug()
{
m_thread->Stop(); // Stop the thread
wxDELETE(m_thread); // Delete it
wxTheApp->Disconnect(XRCID("text_word_complete"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(WordCompletionPlugin::OnWordComplete),
NULL,
this);
wxTheApp->Disconnect(XRCID("text_word_complete_settings"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(WordCompletionPlugin::OnSettings),
NULL,
this);
}
void WordCompletionPlugin::OnSuggestThread(const WordCompletionThreadReply& reply)
{
IEditor* activeEditor = m_mgr->GetActiveEditor();
CHECK_PTR_RET(activeEditor);
// File has been modified since we triggered the completion request
if(activeEditor->GetFileName() != reply.filename) return;
// Build the suggetsion list
wxString suggestString;
wxCodeCompletionBoxEntry::Vec_t entries;
wxCodeCompletionBox::BmpVec_t bitmaps;
bitmaps.push_back(m_images.Bitmap("m_bmpWord"));
for(wxStringSet_t::iterator iter = reply.suggest.begin(); iter != reply.suggest.end(); ++iter) {
entries.push_back(wxCodeCompletionBoxEntry::New(*iter, 0));
}
wxCodeCompletionBoxManager::Get().ShowCompletionBox(
activeEditor->GetSTC(), entries, bitmaps, wxCodeCompletionBox::kInsertSingleMatch);
}
void WordCompletionPlugin::OnWordComplete(wxCommandEvent& event)
{
event.Skip();
IEditor* activeEditor = m_mgr->GetActiveEditor();
CHECK_PTR_RET(activeEditor);
wxStyledTextCtrl* stc = activeEditor->GetSTC();
int curPos = stc->GetCurrentPos();
int start = stc->WordStartPosition(stc->GetCurrentPos(), true);
if(curPos <= start) return;
wxString filter = stc->GetTextRange(start, curPos);
// Invoke the thread to parse and suggets words for this file
WordCompletionThreadRequest* req = new WordCompletionThreadRequest;
req->buffer = stc->GetText();
req->filename = activeEditor->GetFileName();
req->filter = filter;
m_thread->Add(req);
}
void WordCompletionPlugin::OnSettings(wxCommandEvent& event)
{
WordCompletionSettingsDlg dlg(EventNotifier::Get()->TopFrame());
dlg.ShowModal();
}
// void WordCompletionPlugin::OnEditorHandler(clCommandEvent& event)
//{
// event.Skip();
// wxStyledTextCtrl* editor = reinterpret_cast<wxStyledTextCtrl*>(event.GetEventObject());
// if(editor) {
// editor->RegisterImage(1, m_images.Bitmap("m_bmpWord"));
// }
//}
//