forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabbreviation.cpp
260 lines (217 loc) · 8.09 KB
/
abbreviation.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
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : abbreviation.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <wx/textdlg.h>
#include "abbreviationentry.h"
#include "abbreviationssettingsdlg.h"
#include <wx/bitmap.h>
#include <wx/menu.h>
#include "abbreviation.h"
#include <wx/xrc/xmlres.h>
#include <wx/app.h>
#include <wx/log.h>
#include <wx/regex.h>
static AbbreviationPlugin* thePlugin = NULL;
//Define the plugin entry point
extern "C" EXPORT IPlugin *CreatePlugin(IManager *manager)
{
if (thePlugin == 0) {
thePlugin = new AbbreviationPlugin(manager);
}
return thePlugin;
}
extern "C" EXPORT PluginInfo GetPluginInfo()
{
PluginInfo info;
info.SetAuthor(wxT("Eran Ifrah"));
info.SetName(wxT("abbreviation"));
info.SetDescription(_("Abbreviation plugin"));
info.SetVersion(wxT("v1.0"));
return info;
}
extern "C" EXPORT int GetPluginInterfaceVersion()
{
return PLUGIN_INTERFACE_VERSION;
}
AbbreviationPlugin::AbbreviationPlugin(IManager *manager)
: IPlugin(manager)
, m_topWindow(NULL)
{
m_longName = _("Abbreviation plugin");
m_shortName = wxT("abbreviation");
m_topWindow = m_mgr->GetTheApp();
m_topWindow->Connect(wxEVT_CCBOX_SELECTION_MADE, wxCommandEventHandler(AbbreviationPlugin::OnAbbrevSelected), NULL, this);
InitDefaults();
}
AbbreviationPlugin::~AbbreviationPlugin()
{
}
clToolBar *AbbreviationPlugin::CreateToolBar(wxWindow *parent)
{
wxUnusedVar(parent);
return NULL;
}
void AbbreviationPlugin::CreatePluginMenu(wxMenu *pluginsMenu)
{
wxMenu *menu = new wxMenu();
wxMenuItem *item( NULL );
item = new wxMenuItem(menu, XRCID("abbrev_show"), _( "Show Abbreviations" ), _( "Show Abbreviations" ), wxITEM_NORMAL );
menu->Append( item );
menu->AppendSeparator();
item = new wxMenuItem(menu, XRCID("abbrev_settings"), _( "Settings..." ), _( "Settings..." ), wxITEM_NORMAL );
menu->Append( item );
pluginsMenu->Append(wxID_ANY, wxT("Abbreviation"), menu);
m_topWindow->Connect( XRCID("abbrev_settings"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AbbreviationPlugin::OnSettings ), NULL, this );
m_topWindow->Connect( XRCID("abbrev_show"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AbbreviationPlugin::OnAbbreviations), NULL, this );
}
void AbbreviationPlugin::HookPopupMenu(wxMenu *menu, MenuType type)
{
wxUnusedVar(menu);
wxUnusedVar(type);
}
void AbbreviationPlugin::UnPlug()
{
m_topWindow->Disconnect(wxEVT_CCBOX_SELECTION_MADE, wxCommandEventHandler(AbbreviationPlugin::OnAbbrevSelected), NULL, this);
m_topWindow->Disconnect( XRCID("abbrev_settings"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AbbreviationPlugin::OnSettings ), NULL, this );
m_topWindow->Disconnect( XRCID("abbrev_show"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AbbreviationPlugin::OnAbbreviations), NULL, this );
}
void AbbreviationPlugin::OnSettings(wxCommandEvent& e)
{
AbbreviationsSettingsDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
dlg.ShowModal();
}
void AbbreviationPlugin::OnAbbreviations(wxCommandEvent& e)
{
static wxBitmap bmp = LoadBitmapFile(wxT("abbrev.png")) ;
IEditor *editor = m_mgr->GetActiveEditor();
if ( editor && bmp.IsOk() ) {
editor->RegisterImageForKind(wxT("Abbreviation"), bmp);
std::vector<TagEntryPtr> tags;
// prepate list of abbreviations
AbbreviationEntry data;
m_mgr->GetConfigTool()->ReadObject(wxT("AbbreviationsData"), &data);
// search for the old item
std::map<wxString, wxString> entries = data.GetEntries();
std::map<wxString, wxString>::iterator iter = entries.begin();
for (; iter != entries.end(); iter ++) {
TagEntryPtr t(new TagEntry());
t->SetName(iter->first);
t->SetKind(wxT("Abbreviation"));
tags.push_back(t);
}
editor->ShowCompletionBox(tags, editor->GetWordAtCaret(), this);
}
}
void AbbreviationPlugin::OnAbbrevSelected(wxCommandEvent& e)
{
// get the active editor
IEditor *editor = m_mgr->GetActiveEditor();
// Note that we do not delete str!
wxString *str = (wxString*)e.GetClientData();
if (!editor || !str)
return;
// hide the completion box
editor->HideCompletionBox();
// search for abbreviation that matches str
// prepate list of abbreviations
AbbreviationEntry data;
m_mgr->GetConfigTool()->ReadObject(wxT("AbbreviationsData"), &data);
// search for the old item
std::map<wxString, wxString> entries = data.GetEntries();
std::map<wxString, wxString>::iterator iter = entries.find(*str);
if (iter != entries.end()) {
wxString text = iter->second;
int selStart = editor->WordStartPos(editor->GetCurrentPosition(), true);
int selEnd = editor->WordEndPos(editor->GetCurrentPosition(), true);
int curPos = editor->GetCurrentPosition();
int typedWordLen = curPos - selStart;
if (typedWordLen < 0) {
typedWordLen = 0;
}
// format the text to insert
bool appendEol(false);
if (text.EndsWith(wxT("\r")) || text.EndsWith(wxT("\n"))) {
appendEol = true;
}
text = editor->FormatTextKeepIndent(text, selStart);
// remove the first line indenation that might have been placed by CL
text.Trim(false).Trim();
if (appendEol) {
wxString eol;
switch (editor->GetEOL()) {
case 1:
eol = wxT("\r");
break;
case 0:
eol = wxT("\r\n");
break;
case 2:
eol = wxT("\n");
break;
}
text << eol;
}
//--------------------------------------------
// replace any place holders
//--------------------------------------------
static wxRegEx reVarPattern(wxT("\\$\\(( *)([a-zA-Z0-9_]+)( *)\\)"));
while (reVarPattern.Matches(text)) {
// add result
wxString v = reVarPattern.GetMatch(text);
wxString replaceWith = wxGetTextFromUser(wxString::Format(_("Enter replacement for '%s':"), v.c_str()), _("CodeLite"));
text.Replace(v, replaceWith);
}
// locate the caret
int where = text.Find(wxT("|"));
if (where == wxNOT_FOUND) {
where = text.length();
}
// remove the pipe (|) character
text.Replace(wxT("|"), wxT(""));
if (selEnd - selStart >= 0) {
editor->SelectText(selStart, selEnd - selStart);
editor->ReplaceSelection(text);
editor->SetCaretAt(curPos + where - typedWordLen);
}
}
}
void AbbreviationPlugin::InitDefaults()
{
// check to see if there are any abbreviations configured
AbbreviationEntry data;
m_mgr->GetConfigTool()->ReadObject(wxT("AbbreviationsData"), &data);
// search for the old item
if (data.GetEntries().empty()) {
// fill some default abbreviations
std::map<wxString, wxString> entries;
entries[wxT("main")] = wxT("int main(int argc, char **argv)\n{\n\t|\n}\n");
entries[wxT("while")] = wxT("while(|)\n{\n\t\n}\n");
entries[wxT("dowhile")] = wxT("do\n{\n\t\n}while(|)\n");
entries[wxT("tryblock")] = wxT("try\n{\n\t|\n}\ncatch($(ExceptionType) e)\n{\n}\n");
entries[wxT("for_size")] = wxT("for(size_t i=0; i<|; i++)\n{\n}\n");
entries[wxT("for_int")] = wxT("for(int i=0; i<|; i++)\n{\n}\n");
data.SetEntries(entries);
m_mgr->GetConfigTool()->WriteObject(wxT("AbbreviationsData"), &data);
}
}