This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
table_plugin.py
496 lines (396 loc) · 17.8 KB
/
table_plugin.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# table_plugin.py - sublime plugins for pretty print text table
# Copyright (C) 2012 Free Software Foundation, Inc.
# Author: Valery Kocubinsky
# Package: SublimeTableEditor
# Homepage: https://github.com/vkocubinsky/SublimeTableEditor
# This file is part of SublimeTableEditor.
# SublimeTableEditor 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 3 of the License, or
# (at your option) any later version.
# SublimeTableEditor 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 SublimeTableEditor. If not, see <http://www.gnu.org/licenses/>.
import sublime
import sublime_plugin
import re
try:
from . import table_lib as tlib
from . import table_base as tbase
except ValueError:
import table_lib as tlib
import table_base as tbase
class TableContext:
def __init__(self, view, sel, syntax):
self.view = view
(sel_row, sel_col) = self.view.rowcol(sel.begin())
self.syntax = syntax
self.first_table_row = self._get_first_table_row(sel_row, sel_col)
self.last_table_row = self._get_last_table_row(sel_row, sel_col)
self.table_text = self._get_table_text(self.first_table_row, self.last_table_row)
self.visual_field_num = self._visual_field_num(sel_row, sel_col)
self.row_num = sel_row - self.first_table_row
self.table_pos = tbase.TablePos(self.row_num, self.visual_field_num)
self.table = self.syntax.table_parser.parse_text(self.table_text)
self.table_driver = self.syntax.table_driver
self.field_num = self.table_driver.visual_to_internal_index(self.table, self.table_pos).field_num
def _get_table_text(self, first_table_row, last_table_row):
begin_point = self.view.line(self.view.text_point(first_table_row, 0)
).begin()
end_point = self.view.line(self.view.text_point(last_table_row, 0)
).end()
return self.view.substr(sublime.Region(begin_point, end_point))
def _get_last_table_row(self, sel_row, sel_col):
row = sel_row
last_table_row = sel_row
last_line = self.view.rowcol(self.view.size())[0]
while (row <= last_line and self._is_table_row(row)):
last_table_row = row
row = row + 1
return last_table_row
def _get_first_table_row(self, sel_row, sel_col):
row = sel_row
first_table_row = sel_row
while (row >= 0 and self._is_table_row(row)):
first_table_row = row
row = row - 1
return first_table_row
def _is_table_row(self, row):
text = self._get_text(row)
return self.syntax.table_parser.is_table_row(text)
def _visual_field_num(self, sel_row, sel_col):
line_text = self._get_text(sel_row)
line = self.syntax.line_parser.parse(line_text)
return line.field_num(sel_col)
def _get_text(self, row):
point = self.view.text_point(row, 0)
region = self.view.line(point)
text = self.view.substr(region)
return text
class AbstractTableCommand(sublime_plugin.TextCommand):
def detect_syntax(self):
if self.view.settings().has("table_editor_syntax"):
syntax_name = self.view.settings().get("table_editor_syntax")
else:
syntax_name = self.auto_detect_syntax_name()
table_configuration = tbase.TableConfiguration()
border_style = (self.view.settings().get("table_editor_border_style", None)
or self.view.settings().get("table_editor_style", None))
if border_style == "emacs":
table_configuration.hline_out_border = '|'
table_configuration.hline_in_border = '+'
elif border_style == "grid":
table_configuration.hline_out_border = '+'
table_configuration.hline_in_border = '+'
elif border_style == "simple":
table_configuration.hline_out_border = '|'
table_configuration.hline_in_border = '|'
if self.view.settings().has("table_editor_custom_column_alignment"):
table_configuration.custom_column_alignment = self.view.settings().get("table_editor_custom_column_alignment")
if self.view.settings().has("table_editor_keep_space_left"):
table_configuration.keep_space_left = self.view.settings().get("table_editor_keep_space_left")
if self.view.settings().has("table_editor_align_number_right"):
table_configuration.align_number_right = self.view.settings().get("table_editor_align_number_right")
if self.view.settings().has("table_editor_detect_header"):
table_configuration.detect_header = self.view.settings().get("table_editor_detect_header")
if self.view.settings().has("table_editor_intelligent_formatting"):
table_configuration.intelligent_formatting = self.view.settings().get("table_editor_intelligent_formatting")
syntax = tlib.create_syntax(syntax_name, table_configuration)
return syntax
def auto_detect_syntax_name(self):
view_syntax = self.view.settings().get('syntax')
if (view_syntax == 'Packages/Markdown/MultiMarkdown.tmLanguage' or
view_syntax == 'Packages/Markdown/Markdown.tmLanguage'):
return "MultiMarkdown"
elif view_syntax == 'Packages/Textile/Textile.tmLanguage':
return "Textile"
elif (view_syntax == 'Packages/RestructuredText/reStructuredText.tmLanguage'):
return "reStructuredText"
else:
return "Simple"
def merge(self, edit, ctx):
table = ctx.table
new_lines = table.render_lines()
first_table_row = ctx.first_table_row
last_table_row = ctx.last_table_row
rows = range(first_table_row, last_table_row + 1)
for row, new_text in zip(rows, new_lines):
region = self.view.line(self.view.text_point(row, 0))
old_text = self.view.substr(region)
if old_text != new_text:
self.view.replace(edit, region, new_text)
#case 1: some lines inserted
if len(rows) < len(new_lines):
row = last_table_row
for new_text in new_lines[len(rows):]:
end_point = self.view.line(self.view.text_point(row, 0)).end()
self.view.insert(edit, end_point, "\n" + new_text)
row = row + 1
#case 2: some lines deleted
elif len(rows) > len(new_lines):
for row in rows[len(new_lines):]:
region = self.view.line(self.view.text_point(row, 0))
self.view.erase(edit, region)
def create_context(self, sel):
return TableContext(self.view, sel, self.detect_syntax())
def run(self, edit):
new_sels = []
for sel in self.view.sel():
new_sel = self.run_one_sel(edit, sel)
new_sels.append(new_sel)
self.view.sel().clear()
for sel in new_sels:
self.view.sel().add(sel)
self.view.show(sel, False)
def run_one_sel(self, edit, sel):
ctx = self.create_context(sel)
try:
msg, table_pos = self.run_operation(ctx)
self.merge(edit, ctx)
sublime.status_message("Table Editor: {0}".format(msg))
return self.table_pos_sel(ctx, table_pos)
except tbase.TableException as err:
sublime.status_message("Table Editor: {0}".format(err))
return self.table_pos_sel(ctx, ctx.table_pos)
def visual_field_sel(self, ctx, row_num, visual_field_num):
if ctx.table.empty():
pt = self.view.text_point(ctx.first_table_row, 0)
else:
pos = tbase.TablePos(row_num, visual_field_num)
col = ctx.table_driver.get_cursor(ctx.table, pos)
pt = self.view.text_point(ctx.first_table_row + row_num, col)
return sublime.Region(pt, pt)
def table_pos_sel(self, ctx, table_pos):
return self.visual_field_sel(ctx, table_pos.row_num,
table_pos.field_num)
def field_sel(self, ctx, row_num, field_num):
if ctx.table.empty():
visual_field_num = 0
else:
pos = tbase.TablePos(row_num, field_num)
visual_field_num = ctx.table_driver.internal_to_visual_index(ctx.table, pos).field_num
return self.visual_field_sel(ctx, row_num, visual_field_num)
class TableEditorAlignCommand(AbstractTableCommand):
"""
Key: ctrl+shift+a
Re-align the table without change the current table field.
Move cursor to begin of the current table field.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_align(ctx.table, ctx.table_pos)
class TableEditorNextField(AbstractTableCommand):
"""
Key: tab
Re-align the table, move to the next field.
Creates a new row if necessary.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_next_field(ctx.table, ctx.table_pos)
class TableEditorPreviousField(AbstractTableCommand):
"""
Key: shift+tab
Re-align, move to previous field.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_previous_field(ctx.table, ctx.table_pos)
class TableEditorNextRow(AbstractTableCommand):
"""
Key: enter
Re-align the table and move down to next row.
Creates a new row if necessary.
At the beginning or end of a line, enter still does new line.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_next_row(ctx.table, ctx.table_pos)
class TableEditorMoveColumnLeft(AbstractTableCommand):
"""
Key: alt+left
Move the current column left.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_move_column_left(ctx.table,
ctx.table_pos)
class TableEditorMoveColumnRight(AbstractTableCommand):
"""
Key: alt+right
Move the current column right.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_move_column_right(ctx.table,
ctx.table_pos)
class TableEditorDeleteColumn(AbstractTableCommand):
"""
Key: alt+shift+left
Kill the current column.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_delete_column(ctx.table,
ctx.table_pos)
class TableEditorInsertColumn(AbstractTableCommand):
"""
Keys: alt+shift+right
Insert a new column to the left of the cursor position.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_insert_column(ctx.table,
ctx.table_pos)
class TableEditorKillRow(AbstractTableCommand):
"""
Key : alt+shift+up
Kill the current row.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_kill_row(ctx.table, ctx.table_pos)
class TableEditorInsertRow(AbstractTableCommand):
"""
Key: alt+shift+down
Insert a new row above the current row.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_insert_row(ctx.table, ctx.table_pos)
class TableEditorMoveRowUp(AbstractTableCommand):
"""
Key: alt+up
Move the current row up.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_move_row_up(ctx.table, ctx.table_pos)
class TableEditorMoveRowDown(AbstractTableCommand):
"""
Key: alt+down
Move the current row down.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_move_row_down(ctx.table,
ctx.table_pos)
class TableEditorInsertSingleHline(AbstractTableCommand):
"""
Key: ctrl+k,-
Insert single horizontal line below current row.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_insert_single_hline(ctx.table,
ctx.table_pos)
class TableEditorInsertDoubleHline(AbstractTableCommand):
"""
Key: ctrl+k,=
Insert double horizontal line below current row.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_insert_double_hline(ctx.table,
ctx.table_pos)
class TableEditorHlineAndMove(AbstractTableCommand):
"""
Key: ctrl+k, enter
Insert a horizontal line below current row,
and move the cursor into the row below that line.
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_insert_hline_and_move(ctx.table,
ctx.table_pos)
class TableEditorSplitColumnDown(AbstractTableCommand):
"""
Key: alt+enter
Split rest of cell down from current cursor position,
insert new line bellow if current row is last row in the table
or if next line is hline
"""
def remove_rest_line(self, edit, sel):
end_region = self.view.find("\|",
sel.begin())
rest_region = sublime.Region(sel.begin(), end_region.begin())
rest_data = self.view.substr(rest_region)
self.view.replace(edit, rest_region, "")
return rest_data.strip()
def run_one_sel(self, edit, sel):
ctx = self.create_context(sel)
field_num = ctx.field_num
row_num = ctx.row_num
if (ctx.table[row_num].is_separator() or
ctx.table[row_num].is_header_separator()):
sublime.status_message("Table Editor: Split column is not "
"permitted for separator or header "
"separator line")
return self.table_pos_sel(ctx, ctx.table_pos)
if row_num + 1 < len(ctx.table):
if len(ctx.table[row_num + 1]) - 1 < field_num:
sublime.status_message("Table Editor: Split column is not "
"permitted for short line")
return self.table_pos_sel(ctx, ctx.table_pos)
elif ctx.table[row_num + 1][field_num].pseudo():
sublime.status_message("Table Editor: Split column is not "
"permitted to colspan column")
return self.table_pos_sel(ctx, ctx.table_pos)
(sel_row, sel_col) = self.view.rowcol(sel.begin())
rest_data = self.remove_rest_line(edit, sel)
ctx = self.create_context(sel)
field_num = ctx.field_num
row_num = ctx.row_num
if row_num + 1 == len(ctx.table) or ctx.table[row_num + 1].is_separator():
ctx.table.insert_empty_row(row_num + 1)
row_num = row_num + 1
ctx.table[row_num][field_num].data = rest_data + " " + ctx.table[row_num][field_num].data.strip()
ctx.table.pack()
self.merge(edit, ctx)
sublime.status_message("Table Editor: Column splitted down")
return self.field_sel(ctx, row_num, field_num)
class TableEditorJoinLines(AbstractTableCommand):
"""
Key: ctrl+j
Join current row and next row into one if next row is not hline
"""
def run_operation(self, ctx):
return ctx.table_driver.editor_join_lines(ctx.table, ctx.table_pos)
class TableEditorCsvToTable(AbstractTableCommand):
"""
Command: table_csv_to_table
Key: ctrl+k, |
Convert selected CSV region into table
"""
def run_one_sel(self, edit, sel):
if sel.empty():
return sel
else:
syntax = self.detect_syntax()
text = self.view.substr(sel)
table = syntax.table_driver.parse_csv(text)
self.view.replace(edit, sel, table.render())
first_row = self.view.rowcol(sel.begin())[0]
pt = self.view.text_point(first_row, syntax.table_driver.get_cursor(table, tbase.TablePos(0, 0)))
sublime.status_message("Table Editor: Table created from CSV")
return sublime.Region(pt, pt)
class TableEditorDisableForCurrentView(sublime_plugin.TextCommand):
def run(self, args, prop):
self.view.settings().set(prop, False)
class TableEditorEnableForCurrentView(sublime_plugin.TextCommand):
def run(self, args, prop):
self.view.settings().set(prop, True)
class TableEditorDisableForCurrentSyntax(sublime_plugin.TextCommand):
def run(self, edit):
syntax = self.view.settings().get('syntax')
if syntax is not None:
m = re.search("([^/]+)[.]tmLanguage$", syntax)
if m:
base_name = m.group(1) + ".sublime-settings"
settings = sublime.load_settings(base_name)
settings.erase("enable_table_editor")
sublime.save_settings(base_name)
class TableEditorEnableForCurrentSyntax(sublime_plugin.TextCommand):
def run(self, edit):
syntax = self.view.settings().get('syntax')
if syntax is not None:
m = re.search("([^/]+)[.]tmLanguage$", syntax)
if m:
base_name = m.group(1) + ".sublime-settings"
settings = sublime.load_settings(base_name)
settings.set("enable_table_editor", True)
sublime.save_settings(base_name)
class TableEditorSetSyntax(sublime_plugin.TextCommand):
def run(self, edit, syntax):
self.view.settings().set("enable_table_editor", True)
self.view.settings().set("table_editor_syntax", syntax)
sublime.status_message("Table Editor: set syntax to '{0}'"
.format(syntax))