-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycoding.c
311 lines (292 loc) · 10.8 KB
/
pycoding.c
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
/*
* demoplugin.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
* Tools menu.
*
* Note: This is not installed by default, but (on *nix) you can build it as follows:
* cd plugins
* make demoplugin.so
*
* Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
* - it will be loaded at next startup.
*/
#include "geanyplugin.h" /* plugin API, always comes first */
#include <libsoup/soup.h>
#define BLACKD_URL "http://127.0.0.1:45484"
#define JEDI_URL "http://127.0.0.1:45484/jedi/"
enum {
KB_FORMAT_PYCODE,
KB_COUNT
};
static GtkWidget *main_menu_item = NULL;
SoupSession *http_session;
gboolean check_doc(GeanyDocument *doc){
if(!DOC_VALID(doc)){
return FALSE;
}
if(doc->file_type->id != GEANY_FILETYPES_PYTHON){
return FALSE;
}
}
static void
format_callback (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
gint pos;
ScintillaObject *sci = user_data;
switch(msg->status_code){
case SOUP_STATUS_OK:
pos = sci_get_current_position(sci);
sci_set_text(sci, msg->response_body->data);
sci_set_current_position(sci, pos, TRUE);
break;
case SOUP_STATUS_NO_CONTENT:
break;
case SOUP_STATUS_INTERNAL_SERVER_ERROR:
case SOUP_STATUS_BAD_REQUEST:
break;
default:
dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Server seems to be not running ?");
}
keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_LINK);
}
static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if(!check_doc(doc)){
return;
}
msgwin_clear_tab(MSG_MESSAGE);
msgwin_clear_tab(MSG_COMPILER);
GeanyPlugin *plugin = user_data;
gchar *line_length;
gint len = sci_get_length(doc->editor->sci);
SoupMessage *msg;
msg = soup_message_new (SOUP_METHOD_POST, BLACKD_URL);
line_length = g_strdup_printf("%i", MAX(plugin->geany_data->editor_prefs->long_line_column, plugin->geany_data->editor_prefs->line_break_column));
GString *content_type = g_string_sized_new(40);
g_string_append(content_type, "text/plain; charset=");
g_string_append(content_type, doc->encoding);
soup_message_headers_append(msg->request_headers, "X-Line-Length", line_length);
soup_message_headers_append(msg->request_headers, "X-Fast-Or-Safe", "fast");
soup_message_set_request(msg, content_type->str, SOUP_MEMORY_STATIC, sci_get_contents(doc->editor->sci, len+1), len);
soup_session_queue_message(http_session, msg, format_callback, doc->editor->sci);
g_string_free(content_type, TRUE);
g_free(line_length);
}
static void on_document_action(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if(!check_doc(doc)){
return;
}
keybindings_send_command(GEANY_KEY_GROUP_BUILD, GEANY_KEYS_BUILD_LINK);
}
static void show_autocomplete(ScintillaObject *sci, gsize rootlen, const gchar *str, gsize len)
{
// copied as is from geany
/* hide autocompletion if only option is already typed */
if (rootlen >= len ||
(str[rootlen] == '?' && rootlen >= len - 2))
{
sci_send_command(sci, SCI_AUTOCCANCEL);
return;
}
scintilla_send_message(sci, SCI_AUTOCSHOW, rootlen, (sptr_t) str);
}
static void complete_python(GeanyEditor *editor, int ch, const gchar *text, GeanyData *geany_data){
g_return_if_fail(editor != NULL);
gint line, pos, rootlen, start;
gboolean import_check = FALSE;
ScintillaObject *sci;
gchar *word_at_pos;
if (text == NULL){
switch(ch){
case '\r':
case '\n':
case ' ':
case '\t':
case '\v':
case '\f':
case '>':
case '/':
case '(':
case ')':
case '{':
case '[':
case '"':
case '\'':
case '}':
case ':':
return;
}
}
sci = editor->sci;
pos = sci_get_current_position(sci);
line = sci_get_current_line(sci)+1;
word_at_pos = g_strchug(sci_get_line(sci, line-1));
g_return_if_fail(word_at_pos != NULL);
if(g_str_has_prefix(word_at_pos, "import") || g_str_has_prefix(word_at_pos, "from")){
start = sci_get_position_from_line(sci, line-1);
import_check = TRUE;
}
else{
start = 0;
}
g_free(word_at_pos);
word_at_pos = editor_get_word_at_pos(editor, pos, GEANY_WORDCHARS".");
g_return_if_fail(word_at_pos != NULL);
rootlen = strlen(word_at_pos);
if (strstr(word_at_pos, ".") != NULL){
g_free(word_at_pos);
word_at_pos = editor_get_word_at_pos(editor, pos, NULL);
if(word_at_pos == NULL){
rootlen = 0;
}
else{
rootlen = strlen(word_at_pos);
g_free(word_at_pos);
}
}
else if((!import_check && rootlen < 2) || rootlen == 0 ){
g_free(word_at_pos);
return;
}
msgwin_clear_tab(MSG_COMPILER);
msgwin_clear_tab(MSG_MESSAGE);
SoupMessage *msg;
gchar *line_length;
msg = soup_message_new (SOUP_METHOD_POST, JEDI_URL);
line_length = g_strdup_printf("%i", geany_data->editor_prefs->autocompletion_max_entries);
GString *content_type = g_string_sized_new(40);
g_string_append(content_type, "text/plain; charset=");
g_string_append(content_type, editor->document->encoding);
soup_message_headers_append(msg->request_headers, "X-Line-Length", line_length);
soup_message_headers_append(msg->request_headers, "X-File-Path", (editor->document->real_path==NULL)?editor->document->file_name:editor->document->real_path);
if(geany_data->app->project != NULL){
soup_message_headers_append(msg->request_headers, "X-Project-Path", geany_data->app->project->base_path);
}
if(text != NULL){
soup_message_headers_append(msg->request_headers, "X-Doc-Text", text);
}
gchar *buffer = sci_get_contents_range(sci, start, pos);
gint len = g_utf8_strlen(buffer, -1);
soup_message_set_request(msg, content_type->str, SOUP_MEMORY_STATIC, buffer, len);
gint status = soup_session_send_message(http_session, msg);
if(status == SOUP_STATUS_OK){
if(text == NULL){
show_autocomplete(editor->sci, rootlen, msg->response_body->data, msg->response_body->length);
}
else{
if(msg->response_body->length > 6){
msgwin_msg_add_string(COLOR_BLACK, line-1, editor->document, msg->response_body->data);
msgwin_switch_tab(MSG_MESSAGE, FALSE);
}
}
}
g_string_free(content_type, TRUE);
g_free(line_length);
g_free(buffer);
g_object_unref(msg);
}
static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
SCNotification *nt, gpointer data)
{
gboolean ret = FALSE;
if(!check_doc(editor->document)){
return ret;
}
gint lexer, pos, style;
/* For detailed documentation about the SCNotification struct, please see
* http://www.scintilla.org/ScintillaDoc.html#Notifications. */
pos = sci_get_current_position(editor->sci);
if (G_UNLIKELY(pos < 2))
return ret;
lexer = sci_get_lexer(editor->sci);
style = sci_get_style_at(editor->sci, pos - 2);
/* don't autocomplete in comments and strings */
if (!highlighting_is_code_style(lexer, style))
return ret;
GeanyPlugin *plugin = data;
switch (nt->nmhdr.code)
{
case SCN_CHARADDED:
complete_python(editor, nt->ch, NULL, plugin->geany_data);
break;
case SCN_AUTOCSELECTION:
complete_python(editor, nt->ch, nt->text, plugin->geany_data);
break;
}
return ret;
}
static PluginCallback demo_callbacks[] =
{
/* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
* If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
* can prevent Geany from processing the notification. Use this with care. */
{"document-open", (GCallback) & on_document_action, FALSE, NULL},
{"document-activate", (GCallback) & on_document_action, FALSE, NULL},
//{"document-save", (GCallback) & on_document_action, FALSE, NULL},
{"document-save", (GCallback) & on_document_save, FALSE, NULL},
{ "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
{ NULL, NULL, FALSE, NULL }
};
static void menu_item_action(G_GNUC_UNUSED GtkMenuItem *menuitem, gpointer gdata)
{
GeanyDocument *doc = document_get_current();
document_save_file(doc, TRUE);
}
/* Called by Geany to initialize the plugin */
static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
{
GeanyData *geany_data = plugin->geany_data;
geany_plugin_set_data(plugin, plugin, NULL);
main_menu_item = gtk_menu_item_new_with_label(_("Format Python Code"));
gtk_widget_show(main_menu_item);
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu_item);
g_signal_connect(main_menu_item, "activate", G_CALLBACK(menu_item_action), NULL);
GeanyKeyGroup *group;
group = plugin_set_key_group (plugin, _("Format Python Code"), KB_COUNT, NULL);
keybindings_set_item (group, KB_FORMAT_PYCODE, NULL,
0, 0, "format_pycode", _("Format Python Code"), main_menu_item);
http_session = soup_session_new();
return TRUE;
}
/* Called by Geany before unloading the plugin.
* Here any UI changes should be removed, memory freed and any other finalization done.
* Be sure to leave Geany as it was before demo_init(). */
static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
{
g_object_unref(http_session);
gtk_widget_destroy(main_menu_item);
}
void geany_load_module(GeanyPlugin *plugin)
{
/* main_locale_init() must be called for your package before any localization can be done */
main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
plugin->info->name = _("Python Coding.");
plugin->info->description = _("Python Completion, Checker and Formatter.");
plugin->info->version = "0.1";
plugin->info->author = _("Sagar Chalise");
plugin->funcs->init = demo_init;
plugin->funcs->help = NULL; /* This demo has no help but it is an option */
plugin->funcs->cleanup = demo_cleanup;
plugin->funcs->callbacks = demo_callbacks;
GEANY_PLUGIN_REGISTER(plugin, 225);
}