-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlignAmount.py
112 lines (81 loc) · 3.12 KB
/
AlignAmount.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
"""
Provide a command to auto-align amounts in ledger file.
See README.md for details.
@author: Etienne Monier <etienne.monier@enseeiht.fr>
@license: CC-BY-NC-SA
@since: 2020-07-24
"""
import sublime
import sublime_plugin
import re
import threading
from . import utils
class LedgerAlignAmountsCommand(sublime_plugin.TextCommand):
def run(self, edit):
# if not utils.is_ledger_file(self.view):
# # Current view is not a ledger file.
# return
dot_pos = utils.get_settings().get('dot_pos')
# Get the whole document as a region.
region = sublime.Region(0, self.view.size())
# Get a list of all lines
lines = self.view.lines(region)
# Set match pattern.
p = re.compile(
r'^\s+([\[\]\w:\s_-]+)\s+([-$£¥€¢\d,_]+)(?:.\d*)?.*$')
for line in reversed(lines):
# Get line content
line_content = self.view.substr(line)
# Catch amount
res = p.search(line_content)
# If the line has an amount, correct it.
if res:
# Get the position of the dot in the line.
line_pos_dot = line_content.find(res.group(2)) + \
len(res.group(2))
# Get number of spaces to add or to remove
# If >=0, spaces should be added
# If <0, spaces should be removed
num = dot_pos - line_pos_dot
# Get position of the beggining of ammount in the line
line_pos_amount = line_content.find(res.group(2))
# Same in the view
view_pos_amount = line.begin() + line_pos_amount
if num > 0:
self.view.insert(edit, view_pos_amount, ' '*num)
elif num < 0:
self.view.erase(
edit,
sublime.Region(view_pos_amount+num, view_pos_amount))
class alignOnModified(sublime_plugin.EventListener):
# align thread
thread = None
# listen actions
registered_actions = ["insert", "left_delete", "right_delete",
"delete_word", "paste", "cut"]
def on_modified(self, view):
# If not a ledger file
if not utils.is_ledger_file(view):
return
if not utils.get_settings().get('automatic_amount_alignment'):
return
# Do not process scratch or widget files
if view.is_scratch() or view.settings().get('is_widget'):
return
# Get the last executed command
cmdhist = view.command_history(0)
# If that' not a command to listen, return.
if cmdhist[0] not in self.registered_actions:
return
# Default delay
delay = 0.2
# if cmdhist[0] == "insert" and cmdhist[1]['characters'].strip() == "":
# delay = 1
# We need to define a timer to add delay.
if self.thread:
self.thread.cancel()
self.thread = threading.Timer(
delay,
lambda: view.run_command('ledger_align_amounts')
)
self.thread.start()