/
format.py
387 lines (347 loc) · 17.1 KB
/
format.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
#
# Kate/Pâté plugins to work with C++ code formatting
# Copyright 2010-2012 by Alex Trubov <i.zaufi@gmail.com>
#
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
# Here is a short list of plugins in this file:
#
#
# Boost-like Format Params (Meta+F)
# Format function's/template's parameters list (or `for`'s) in a boost-like style
# I.e. when 2nd and the rest parameters has leading comma/semicolon
# and closing ')' or '>' on a separate line.
# THIS IS REALLY BETTER TO HAVE SUCH STYLE WHEN U HAVE A LONG PARAMETERS LIST!
#
# Unformat Function Params (Meta+Shift+F)
# merge everything between '(' and ')' into a single line
#
#
import kate
from PyKDE4.ktexteditor import KTextEditor
from libkatepate import ui
from libkatepate.decorators import *
from libkatepate import selection
def getLeftNeighbour(lineStr, column):
if column:
return lineStr[column - 1]
return None
def getRightNeighbour(lineStr, column):
if (column + 1) < len(lineStr):
return lineStr[column + 1]
return None
def looksLikeTemplateAngelBracket(lineStr, column):
""" Check if a symbol at given position looks like a template angel bracket
"""
assert(lineStr[column] in '<>')
print("?LLTAB: ch='" + lineStr[column] + "'")
ln = getLeftNeighbour(lineStr, column)
print("?LLTAB: ln='" + str(ln) + "'")
rn = getRightNeighbour(lineStr, column)
print("?LLTAB: rn='" + str(rn) + "'")
# Detect possible template
if lineStr[column] == '<': # --[current char is '<']-------
if ln == '<' or rn == '<': # "<<" in any place on a line...
return False # ... can't be a template!
if ln == ' ' and rn == '=': # " <="
return False # operator<=()
if lineStr[0:column].strip().startswith('template'):# template declaration at the start of line
return True # ... possible smth like `template < typename ...'
if ln == ' ' and rn == ' ': # " < "
return False # operator<()
return True
if lineStr[column] == '>': # --[current char is '>']-------
if lineStr.strip().startswith('>'): # line starts w/ one or more '>'
return True # ... can be end of formatted `typedef <...\n> type;' for example
if ln == ' ' and rn == ' ': # " > "
return False # operator>()
if ln == ' ' and rn == '=': # ">="
return False # operator>=()
if ln == '-':
return False # operator->()
return True
pass
#
# TODO Probably decorators may help to simplify this code ???
#
def getRangeTopology(breakChars):
"""Get range opened w/ `openCh' and closed w/ `closeCh'
@return tuple w/ current range, list of nested ranges
and list of positions of break characters
@note Assume cursor positioned whithin that range already.
"""
document = kate.activeDocument()
view = kate.activeView()
pos = view.cursorPosition()
stack = list()
nestedRanges = list()
breakPositions = list()
firstIteration = True
found = False
# Iterate from the current line towards a document start
for cl in range(pos.line(), -1, -1):
lineStr = str(document.line(cl))
if not firstIteration: # skip first iteration
pos.setColumn(len(lineStr)) # set current column to the end of current line
else:
firstIteration = False # do nothing on first iteration
# Iterate from the current column to a line start
for cc in range(pos.column() - 1, -1, -1):
#print("c: current position" + str(cl) + "," + str(cc) + ",ch='" + lineStr[cc] + "'")
# Check open/close brackets
if lineStr[cc] == ')': # found closing char: append its position to the stack
stack.append((cl, cc, False))
print("o( Add position: " + str(stack[-1]))
continue
if lineStr[cc] == '(': # found open char...
if len(stack): # if stack isn't empty (i.e. there are some closing chars met)
print("o( Pop position: " + str(stack[-1]))
nrl, nrc, isT = stack.pop() # remove last position from the stack
if not isT:
nestedRanges.append( # and append a nested range
KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
"Misbalanced brackets: '(' @" + str(cl + 1) + ',' + str(cc + 1) +
" and '>' @ " + str(nrl + 1) + ',' + str(nrc + 1)
)
else: # otherwise,
openPos = (cl, cc + 1, False) # remember range start (exclude an open char)
print("o( Found position: " + str(openPos))
found = True
break
continue
# Check for template angel brackets
if lineStr[cc] == '>':
if looksLikeTemplateAngelBracket(lineStr, cc):
stack.append((cl, cc, True))
print("o< Add position: " + str(stack[-1]))
else:
print("o< Doesn't looks like template: " + str(cl) + "," + str(cc))
continue
if lineStr[cc] == '<':
if not looksLikeTemplateAngelBracket(lineStr, cc):
print("o< Doesn't looks like template: " + str(cl) + "," + str(cc + 1))
elif len(stack): # if stack isn't empty (i.e. there are some closing chars met)
print("o< Pop position: " + str(stack[-1]))
nrl, nrc, isT = stack.pop() # remove last position from the stack
if isT:
nestedRanges.append( # and append a nested range
KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
"Misbalanced brackets: '<' @" + str(cl + 1) + ',' + str(cc + 1) +
" and ')' @ " + str(nrl + 1) + ',' + str(nrc + 1)
)
raise LookupError("Misbalanced brackets")
else:
openPos = (cl, cc + 1, True) # remember range start (exclude an open char)
print("o< Found position: " + str(openPos))
found = True
break
continue
if lineStr[cc] in breakChars and len(stack) == 0:
breakPositions.append(KTextEditor.Cursor(cl, cc))
# Did we found smth on the current line?
if found:
break # Yep! Break the outer loop
if not found:
return (KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
assert(len(stack) == 0) # stack expected to be empty!
breakPositions.reverse() # reverse breakers list required cuz we found 'em in a reverse order :)
# Iterate from the current position towards the end of a document
pos = view.cursorPosition() # get current cursor position again
firstIteration = True
found = False
for cl in range(pos.line(), document.lines()):
lineStr = str(document.line(cl))
if not firstIteration: # skip first iteration
pos.setColumn(0) # set current column to the start of current line
else:
firstIteration = False # do nothing on first iteration
for cc in range(pos.column(), len(lineStr)):
#print("c: current position" + str(cl) + "," + str(cc) + ",ch='" + lineStr[cc] + "'")
# Check open/close brackets
if lineStr[cc] == '(':
stack.append((cl, cc, False))
print("c) Add position: " + str(stack[-1]))
continue
if lineStr[cc] == ')':
if len(stack):
print("c) Pop position: " + str(stack[-1]))
nrl, nrc, isT = stack.pop() # remove a last position from the stack
if not isT:
nestedRanges.append( # and append a nested range
KTextEditor.Range(nrl, nrc, cl, cc)
)
else:
raise LookupError(
"Misbalanced brackets: '<' @" + str(nrl + 1) + ',' + str(nrc + 1) +
" and ')' @ " + str(cl + 1) + ',' + str(cc + 1)
)
else:
closePos = (cl, cc, False) # remember the range end
print("c) Found position: " + str(closePos))
found = True
break
continue
# Check for template angel brackets
if lineStr[cc] == '<':
if looksLikeTemplateAngelBracket(lineStr, cc):
stack.append((cl, cc, True))
print("c> Add position: " + str(stack[-1]))
else:
print("c> Doesn't looks like template: " + str(cl) + "," + str(cc))
continue
if lineStr[cc] == '>':
if not looksLikeTemplateAngelBracket(lineStr, cc):
print("c> Doesn't looks like template: " + str(cl) + "," + str(cc))
elif len(stack): # if stack isn't empty (i.e. there are some closing chars met)
print("c> Pop position: " + str(stack[-1]))
nrl, nrc, isT = stack.pop() # remove last position from the stack
if isT:
nestedRanges.append( # and append a nested range
KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
"Misbalanced brackets: '(' @" + str(nrl + 1) + ',' + str(nrc + 1) +
" and '>' @ " + str(cl + 1) + ',' + str(cc + 1)
)
else:
closePos = (cl, cc, True) # remember the range end
print("c> Found position: " + str(closePos))
found = True
break
continue
if lineStr[cc] in breakChars and len(stack) == 0:
breakPositions.append(KTextEditor.Cursor(cl, cc))
# Did we found smth on the current line?
if found:
break # Yep! Break the outer loop
if not found:
return (KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
assert(len(stack) == 0) # stack expected to be empty!
if openPos[2] != closePos[2]:
raise LookupError(
"Misbalanced brackets: at " + str(openPos[0] + 1) + ',' + str(openPos[1] + 1) +
" and " + str(closePos[0] + 1) + ',' + str(closePos[1] + 1)
)
return (KTextEditor.Range(openPos[0], openPos[1], closePos[0], closePos[1]), nestedRanges, breakPositions)
def boostFormatText(textRange, indent, breakPositions):
document = kate.activeDocument()
originalText = document.text(textRange)
print("Original text:\n'" + originalText + "'")
# Slice text whithin a given range into pieces to be realigned
ranges = list()
prevPos = textRange.start()
breakCh = None
indentStr = ' ' * (indent + 2);
breakPositions.append(textRange.end())
for b in breakPositions:
print("* prev pos: " + str(prevPos.line()) + ", " + str(prevPos.column()))
print("* current pos: " + str(b.line()) + ", " + str(b.column()))
chunk = (document.text(KTextEditor.Range(prevPos, b))).strip()
print("* current chunk:\n'" + chunk + "'")
t = ('\n ').join(chunk.splitlines())
print("* current line:\n'" + t + "'")
if breakCh:
outText += indentStr + breakCh + ' ' + t + '\n'
else:
outText = '\n' + indentStr + ' ' + t + '\n'
breakCh = document.character(b)
prevPos = KTextEditor.Cursor(b.line(), b.column() + 1)
outText += indentStr
print("Out text:\n'" + outText + "'")
if outText != originalText:
document.startEditing()
document.replaceText(textRange, outText)
document.endEditing()
@kate.action('Boost-like Format Params', shortcut='Meta+F')
@check_constraints
@has_selection(False)
@selection_mode(selection.NORMAL)
def boostFormat():
document = kate.activeDocument()
view = kate.activeView()
try:
r, nestedRanges, breakPositions = getRangeTopology(',')
except LookupError as error:
ui.popup("Failed to parse C++ expression", str(error), "face-sad")
return
if r.isEmpty(): # Is range empty?
ui.popup(
"Failed to parse C++ expression"
, "Didn't found anything to format. Sorry..."
, "face-sad"
)
return # Nothing interesting wasn't found...
# Rescan the range w/ ';' as breaker added if current range is a `for` statement
if document.line(r.start().line())[0:r.start().column() - 1].rstrip().endswith('for'):
try:
r, nestedRanges, breakPositions = getRangeTopology(',;')
except LookupError as error:
ui.popup("Failed to parse C++ expression", str(error), "face-sad")
return
# Going to format a text whithin a selected range
lineStr = document.line(r.start().line())
lineStrStripped = lineStr.lstrip()
indent = len(lineStr) - len(lineStrStripped)
if lineStrStripped.startswith(', '):
indent += 2
text = boostFormatText(r, indent, breakPositions)
def boostUnformatText(textRange, breakPositions):
document = kate.activeDocument()
originalText = document.text(textRange)
#print("Original text:\n'" + originalText + "'")
# Join text within a selected range
prevPos = textRange.start()
outText = ''.join([line.strip() for line in originalText.splitlines()])
#print("Out text:\n'" + outText + "'")
if outText != originalText:
document.startEditing()
document.replaceText(textRange, outText)
document.endEditing()
@kate.action('Unformat Function Params', shortcut='Meta+Shift+F')
@check_constraints
@has_selection(False)
@selection_mode(selection.NORMAL)
def boostFormat():
document = kate.activeDocument()
view = kate.activeView()
try:
r, nestedRanges, breakPositions = getRangeTopology(',')
except LookupError as error:
ui.popup("Failed to parse C++ expression", str(error), "face-sad")
return
if r.isEmpty(): # Is range empty?
ui.popup(
"Failed to parse C++ expression"
, "Didn't found anything to format. Sorry"
, "face-sad"
)
return # Nothing interesting wasn't found...
# Rescan the range w/ ';' as breaker added if current range is a `for` statement
if document.line(r.start().line())[0:r.start().column() - 1].rstrip().endswith('for'):
try:
r, nestedRanges, breakPositions = getRangeTopology(',;')
except LookupError as error:
ui.popup("Failed to parse C++ expression", str(error), "face-sad")
return
# Going to unformat a text whithin a selected range
text = boostUnformatText(r, breakPositions)