-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcalc.py
111 lines (90 loc) · 3.66 KB
/
calc.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
import sublime
import sublime_plugin
import xml.etree.ElementTree as ET
import xml.sax.saxutils as SAX
settings = sublime.load_settings("FileMaker.sublime-settings")
def get_selection(view, select_all=False):
""" Returns regions of view to operate on
"""
if select_all and not view.has_non_empty_selection_region():
selection = [sublime.Region(0, view.size())]
else:
selection = view.sel()
return selection
def syntax_to_filemaker(self):
"""Changes syntax to FileMaker if its in plain text
"""
if "Plain text" in self.view.settings().get('syntax'):
self.view.set_syntax_file("Packages/FileMaker/FileMaker.tmLanguage")
def syntax_to_clipboard(self):
"""Changes syntax to FileMaker Clipboard
"""
self.view.set_syntax_file("Packages/FileMaker/FileMaker Clipboard.tmLanguage")
def quote(text):
text = text.replace('\\', '\\\\')
text = text.replace('"', '\\"')
text = text.replace('¶', '\\¶')
return '"' + text + '"'
def quote_and_append(text):
textNew = ""
for line in text.splitlines():
textNew += quote(line) + ' & ¶ &' + "\n"
return textNew + "\n" + '""'
def extract_table_field_names(text):
root = ET.fromstring(text)
textNew = ""
for elem in root.iterfind('Field'):
textNew += elem.get('name') + "\n"
return textNew
def convert_to_script_comments(text):
template = '<Step enable="True" id="" name="Comment"><Text>{0}</Text></Step>'
textNew = '<fmxmlsnippet type="FMObjectList">' + "\n"
for line in text.splitlines():
lineNew = SAX.escape(line)
textNew += template.format(' ' + lineNew + "\n")
textNew += '</fmxmlsnippet>'
return textNew
class FilemakerQuoteCommand(sublime_plugin.TextCommand):
""" Quotes calculation as FileMaker string, escaping as necessary
"""
def run(self, edit):
view = self.view
select_all = settings.get("use_entire_file_if_no_selection", True)
selection = get_selection(view, select_all)
for sel in selection:
view.replace(edit, sel, quote(view.substr(sel)))
if select_all:
self.syntax_to_filemaker()
class FilemakerQuoteAndAppendCommand(sublime_plugin.TextCommand):
""" Quotes each line in calculation as FileMaker string, appending each line of text with carriage return
"""
def run(self, edit):
view = self.view
select_all = settings.get("use_entire_file_if_no_selection", True)
selection = get_selection(view, select_all)
for sel in selection:
view.replace(edit, sel, quote_and_append(view.substr(sel)))
if select_all:
self.syntax_to_filemaker()
class FilemakerExtractTableFieldNamesCommand(sublime_plugin.TextCommand):
""" Extracts list of field names from FileMaker clipboard object
"""
def run(self, edit):
view = self.view
select_all = settings.get("use_entire_file_if_no_selection", True)
selection = get_selection(view, select_all)
for sel in selection:
view.replace(edit, sel, extract_table_field_names(view.substr(sel)))
if select_all:
extract_table_field_names(view.substr(selection))
class FilemakerConvertToScriptComments(sublime_plugin.TextCommand):
""" Converts each line of text to a script comment to be pasted into FileMaker
"""
def run(self, edit):
view = self.view
select_all = settings.get("use_entire_file_if_no_selection", True)
selection = get_selection(view, select_all)
for sel in selection:
view.replace(edit, sel, convert_to_script_comments(view.substr(sel)))
if select_all:
self.syntax_to_clipboard()