/
pyedit_wrap_paragraph.py
315 lines (244 loc) · 13.8 KB
/
pyedit_wrap_paragraph.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
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
312
313
314
315
from __future__ import nested_scopes # for Jython 2.1 compatibility
""" Wrap Paragraph by Don Taylor.
A Pydev script for rewrapping the current paragraph to fit inside the print
margin preference in Eclipse (defaults to 80 columns). A paragraph is a block
of lines with a common leading string such as '# ' or a number of spaces. The
lines in the newly wrapped paragraph will all have the same leading string as
the original paragraph.
Usage: Position cursor inside paragraph to be rewrapped and hit <ctrl+2>, w
Caveats: Embedded tabs are always replaced by single spaces.
Does not wrap if the cursor is within the first line of a docstring.
Wrap Paragraph makes simple assumptions about paragraphs. Check your
results, <ctrl-Z> will undo the last rewrap.
Note: Activates with 'w' by default. Edit the constants ACTIVATION_STRING
and WAIT_FOR_ENTER near the end of this file if this does not suit your
needs.
Version: 0.1.1 - alpha
Date: May 2006
License: Available under the same conditions as PyDev. See PyDev license for
details: http://pydev.sourceforge.net
Support: Contact the author for bug reports/feature requests via the Pydev
users list (or use the source).
History: 20 May 2006 - Initial release.
21 May 2006 - Changed no of columns wrapped from 80 to the Eclipse
setting for the print margin preference.
"""
#===============================================================================
# The following is a copy of textwrap.py from the CPython 2.4 standard library
# - slightly modified for Jython 2.1 compatibility. Included here directly
# instead of as an imported module so that the Wrap Paragraph Jython Pydev
# extension can consist of a single file. The extension code starts at around
# line 400.
#===============================================================================
"""Text wrapping and filling.
"""
# Copyright (C) 1999-2001 Gregory P. Ward.
# Copyright (C) 2002, 2003 Python Software Foundation.
# Written by Greg Ward <gward@python.net>
__revision__ = "$Id$"
import string, re, textwrap
# Do the right thing with boolean values for all known Python versions (so this
# module can be copied to projects that don't depend on Python 2.3, e.g. Optik
# and Docutils).
try:
True, False #@UndefinedVariable
except NameError:
(True, False) = (1, 0)
#===============================================================================
# Pydev Extensions in Jython code protocol
#===============================================================================
if False:
from org.python.pydev.editor import PyEdit #@UnresolvedImport
cmd = 'command string'
editor = PyEdit
#---------------------------- REQUIRED LOCALS-----------------------------------
# interface: String indicating which command will be executed As this script
# will be watching the PyEdit (that is the actual editor in Pydev), and this
# script will be listening to it, this string can indicate any of the methods of
# org.python.pydev.editor.IPyEditListener
assert cmd is not None
# interface: PyEdit object: this is the actual editor that we will act upon
assert editor is not None
if cmd == 'onCreateActions':
from org.eclipse.jface.action import Action #@UnresolvedImport
from org.python.pydev.core.docutils import PySelection #@UnresolvedImport
from org.eclipse.ui.texteditor import IEditorStatusLine #@UnresolvedImport
from org.eclipse.swt.widgets import Display #@UnresolvedImport
from java.lang import Runnable #@UnresolvedImport
from org.python.pydev.plugin.preferences import PydevPrefs #@UnresolvedImport
from org.eclipse.ui.texteditor import AbstractDecoratedTextEditorPreferenceConstants #@UnresolvedImport
#------------------------ HELPER TO RUN THINGS IN THE UI -----------------------
class RunInUi(Runnable):
'''Helper class that implements a Runnable (just so that we
can pass it to the Java side). It simply calls some callable.
'''
def __init__(self, c):
self.callable = c
def run(self):
self.callable ()
def runInUi(callable):
'''
@param callable: the callable that will be run in the UI
'''
Display.getDefault().asyncExec(RunInUi(callable))
#---------------------- END HELPER TO RUN THINGS IN THE UI ---------------------
#----------------------------------Paragrapher----------------------------------
class Paragrapher:
''' Provides tools to process a paragraph of text in the Pydev editor.
'''
def __init__(self):
self.selection = PySelection(editor)
self.document = editor.getDocument()
self.offset = self.selection.getAbsoluteCursorOffset()
self.currentLineNo = self.selection.getLineOfOffset(self.offset)
self.docDelimiter = self.selection.getDelimiter(self.document)
self.currentLine = self.selection.getLine(self.currentLineNo)
self.pattern = r'''(\s*#\s*|\s*"""\s*|''' \
+ r"""\s*'''\s*|""" \
+ r'''\s*"\s*|''' \
+ r"""\s*'\s*|\s*)"""
self.compiledRe = re.compile(self.pattern)
self.leadingString, self.mainText = \
self._splitLine(self.currentLine)
self.offsetOfOriginalParagraph = 0
self.lengthOfOriginalParagraph = 0
self.numberOfLinesInDocument = self.document.getNumberOfLines()
def _splitLine(self, line):
''' _splitLine(string: line) -> (string: leadingString,\
string: mainText)
Split the line into two parts - a leading string and the remaining
text.
'''
matched = self.compiledRe.match(line)
leadingString = line[0:matched.end()]
mainText = line[matched.end():]
return (leadingString, mainText)
def getCurrentLine(self):
''' getCurrentLine() -> string
Return the main part of the text of the current line as a string.
'''
self.currentLine = self.selection.getLine(self.currentLineNo)
self.mainText = self._splitLine(self.currentLine)[1]
return self.mainText
def previousLineIsInParagraph(self):
''' previousLineIsInParagraph() -> bool '''
previousLine = self.selection.getLine(self.currentLineNo - 1)
leadingStringOfPreviousLine, mainTextOfPreviousLine = \
self._splitLine(previousLine)
if (self.currentLineNo == 0) |\
(mainTextOfPreviousLine.strip() == "") | \
(leadingStringOfPreviousLine != self.leadingString): # diff para [1]
line = self.selection.getLine(self.currentLineNo)
lineEndsAt = self.selection.getEndLineOffset(self.currentLineNo)
self.offsetOfOriginalParagraph = lineEndsAt - len(line)
return False
else:
return True # same para
# [1] The current line is the first line of a paragraph. Calculate
# starting offset of the first character of the original paragraph.
def nextLineIsInParagraph(self):
''' nextLineIsInParagraph() -> bool '''
nextLine = self.selection.getLine(self.currentLineNo + 1)
leadingStringOfNextLine, mainTextOfNextLine = \
self._splitLine(nextLine)
if (self.currentLineNo + 1 == self.numberOfLinesInDocument) | \
(mainTextOfNextLine.strip() == "") | \
(leadingStringOfNextLine != self.leadingString): # diff para [1]
self.lengthOfOriginalParagraph = \
self.selection.getEndLineOffset(self.currentLineNo) - \
self.offsetOfOriginalParagraph
return False
else:
return True # same para
# [1] The current line is the last line of a paragraph. Calculate
# the length of the original paragraph.
#------------------------------end of Paragrapher-------------------------------
class wrapParagraph(Action):
''' Rewrap the text of the current paragraph.
wrapParagraph searches for the beginning and end of the paragraph that
contains the selection, rewraps it to fit into 79 character lines and
replaces the original paragraph with the newly wrapped paragraph.
The current paragraph is the text surrounding the current selection
point.
Only one paragraph at a time is wrapped.
A paragraph is a consecutive block of lines whose alphanumeric text all
begins at the same column position. Any constant leading string will be
retained in the newly wrapped paragraph. This handles indented
paragraphs and # comment blocks, and avoids wrapping indented code
examples - but not code samples that are not indented.
The first, or only, line of a docstring is handled as a special case and
is not wrapped at all.
'''
def run(self):
def displayStatusMessage():
''' Displays the message in 'statusMessage' in the Eclipse
status area.
Uses runInUi so that it runs in the Eclispe UI thread otherwise
it won't show up. Called thus:
statusMessage = "Cannot rewrap docstrings"
runInUi(displayStatusMessage)
'''
statusLine = editor.getAdapter(IEditorStatusLine)
if statusLine is not None:
statusLine.setMessage(False, statusMessage, None)
p = Paragrapher()
# Start building a list of lines of text in paragraph
paragraph = [p.getCurrentLine()]
isDocstring = (p.leadingString.find('"""') != -1) |\
(p.leadingString.find("'") != -1) |\
(p.leadingString.find('"') != -1)
if isDocstring:
statusMessage = "Cannot rewrap docstrings"
runInUi(displayStatusMessage)
# Don't wrap empty lines or docstrings.
if ((paragraph[0].strip() != "") & (not isDocstring)):
startingLineNo = p.currentLineNo
# Add the lines before the line containing the selection.
while p.previousLineIsInParagraph():
p.currentLineNo -= 1
paragraph.insert(0, p.getCurrentLine())
# Add the lines after the line containing the selection.
p.currentLineNo = startingLineNo
while p.nextLineIsInParagraph():
p.currentLineNo += 1
paragraph.append(p.getCurrentLine())
# paragraph now contains all of the lines so rewrap it [1].
noCols = PydevPrefs.getChainedPrefStore().\
getInt(AbstractDecoratedTextEditorPreferenceConstants.\
EDITOR_PRINT_MARGIN_COLUMN )
paragraph = [line.rstrip() + " " for line in paragraph]
paragraph = textwrap.wrap("".join(paragraph), \
width = noCols - len(p.leadingString), \
expand_tabs = False, \
)
# Add line terminators.
paragraph = map((lambda aLine: p.leadingString + aLine + \
p.docDelimiter), paragraph)
paragraph[-1] = paragraph[-1].replace(p.docDelimiter,"") # [2]
# Replace original paragraph.
p.document.replace(p.offsetOfOriginalParagraph, \
p.lengthOfOriginalParagraph, \
"".join(paragraph))
# and we are done.
# [1] paragraph now contains all of the lines of the paragraph to be
# rewrapped and the lines have all been stripped of their leading
# strings.
#
# Rewrap the paragraph allowing space to insert the leading strings back
# in again after the wrapping is done. But first we need to make sure
# that there is at least one space at the end of each line otherwise the
# wrap routine will combine the last word of one line with the first
# word of the next line. We cannot just add a space as this will be
# kept if there is one there already so strip off any trailing white
# space first and add back just a single space character.
#
# [2] Add line terminators to the end of every line in paragraph except
# the last line otherwise the new paragraph will have an extra line
# terminator at the end.
# Change these constants if the default does not suit your needs
ACTIVATION_STRING = 'w'
WAIT_FOR_ENTER = False
# Register the extension as an ActionListener.
editor.addOfflineActionListener(ACTIVATION_STRING, wrapParagraph(),\
'Wrap paragraph',\
WAIT_FOR_ENTER)