-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_indentation.py
138 lines (113 loc) · 5.24 KB
/
python_indentation.py
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
# -*- coding: utf-8 -*-
# Copyright (C) 2008 - Olivier Lauzanne
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""Gedit Plugin : smart indentation for python code
The code is indented when the previous line ends with ':' and un-indented if
the previous line starts with 'return', 'pass', 'continue' or 'break'. This
plugin will use your tab configuration for indentation. To respect PEP8 you
should set tab width to 4 and choose to insert spaces instead of tabs.
"""
from gtk import gdk
import gedit
class Plugin(gedit.Plugin):
"""The python indentation plugin
"""
def activate(self, window):
self.views = {}
def deactivate(self, window):
"""Disconnect all signals.
"""
for view, signal in self.views.items():
view.disconnect(signal)
def update_ui(self, window):
"""Connect the key-press-event signal of the current view to the
on_key_press method if we are editing a python file.
"""
buffer = window.get_active_document()
view = window.get_active_view()
if buffer == None or view == None:
return
language = buffer.get_language()
if language == None:
return
if language.get_name() == 'Python' and view not in self.views:
self.views[view] = (view.connect('key-press-event',
self.on_key_press, buffer))
def on_key_press(self, view, event, buffer):
"""Check if the key press is 'Return' or 'Backspace' and indent or
un-indent accordingly.
"""
key_name = gdk.keyval_name(event.keyval)
if key_name not in ('Return', 'BackSpace') or \
len(buffer.get_selection_bounds()) != 0:
# If some text is selected we want the default behavior of Return
# and Backspace so we do nothing
return
if view.get_insert_spaces_instead_of_tabs():
self.indent = ' ' * view.props.tab_width
else:
self.indent = '\t'
if key_name == 'Return':
line = self._get_current_line(buffer)
if line.endswith(':'):
old_indent = line[:len(line) - len(line.lstrip())]
indent = '\n' + old_indent + self.indent
# Use insert_interactive instead of insert, so that the
# undo manager knows what we are doing (or something like that).
# The True parameter is here because we are inserting into an
# editable view
buffer.insert_interactive_at_cursor(indent, True)
self._scroll_to_cursor(buffer, view)
return True
else:
stripped_line = line.strip()
n = len(line) - len(line.lstrip())
if (stripped_line.startswith('return')
or stripped_line.startswith('break')
or stripped_line.startswith('continue')
or stripped_line.startswith('pass')
or stripped_line.startswith('raise')):
n -= len(self.indent)
buffer.insert_interactive_at_cursor('\n' + line[:n], True)
self._scroll_to_cursor(buffer, view)
return True
if key_name == 'BackSpace':
line = self._get_current_line(buffer)
if line.strip() == '' and line != '':
length = len(self.indent)
nb_to_delete = len(line) % length or length
self._delete_before_cursor(buffer, nb_to_delete)
self._scroll_to_cursor(buffer, view)
return True
def _delete_before_cursor(self, buffer, nb_to_delete):
cursor_position = buffer.get_property('cursor-position')
iter_cursor = buffer.get_iter_at_offset(cursor_position)
iter_before = buffer.get_iter_at_offset(cursor_position - nb_to_delete)
buffer.delete_interactive(iter_before, iter_cursor, True)
def _get_current_line(self, buffer):
iter_cursor = self._get_iter_cursor(buffer)
iter_line = buffer.get_iter_at_line(iter_cursor.get_line())
return buffer.get_text(iter_line, iter_cursor)
def _get_current_line_nb(self, buffer):
iter_cursor = self._get_iter_cursor(buffer)
return iter_cursor.get_line()
def _get_iter_cursor(self, buffer):
cursor_position = buffer.get_property('cursor-position')
return buffer.get_iter_at_offset(cursor_position)
def _scroll_to_cursor(self, buffer, view):
lineno = self._get_current_line_nb(buffer) + 1
insert = buffer.get_insert()
view.scroll_mark_onscreen(insert)