-
Notifications
You must be signed in to change notification settings - Fork 0
/
general_text_tools.py
117 lines (97 loc) · 3.71 KB
/
general_text_tools.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
# General tools for making it easier to work in Sublime Text
#
# Original Author: Joshua Leung
# Date: April 2017
import sublime
import sublime_plugin
###########################################
# Convert Camel-Case <-> Underscores
# Convert between Camel-Case and Underscores
class ConvertCamelCaseUnderscoresCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
# Figure out the mode for how we're going to do things
mode = args.get("mode", 'toggle')
# Find selected text to operate on
sel_regions = self.view.sel()
if (len(sel_regions) == 0) or (sel_regions[0].size() == 0):
sublime.message_dialog("No text selected")
return;
# Operate on each selected region (from last to first, to avoid errors)
for sel in reversed(sel_regions):
# Extract the text in question
txt = self.view.substr(sel)
if mode == "camel":
self.convert_to_camel(edit, sel, txt)
elif mode == "underscores":
self.convert_to_underscores(edit, sel, txt)
else:
self.toggle_case(edit, sel, txt)
# Check what to do with the given region
def toggle_case(self, edit, region, txt):
# If it has underscores, it is "underscores", otherwise "camel"
if "_" in txt:
self.convert_to_camel(edit, region, txt)
else:
self.convert_to_underscores(edit, region, txt)
# Convert to CamelCase
def convert_to_camel(self, edit, region, txt):
# Break into segments
segments = txt.split("_")
# Capitalise start letter of each segment, then join them back together
result = "".join([s.title() for s in segments])
self.view.replace(edit, region, result)
# Convert to under_scores_style
def convert_to_underscores(self, edit, region, txt):
# Break into segments -> Find the capitals
segments = []
current = []
for ch in txt:
if ch.isupper():
# Start new segment, but converted to lowercase
current = [ch.lower()]
segments.append(current)
else:
# Append to existing segment
current.append(ch)
# Join segments together (but first, we must convert the lists back to strings :)
result = "_".join([''.join(s) for s in segments])
self.view.replace(edit, region, result)
###########################################
# Toggle between arrow/dot accessors for C/C++
class ToggleArrowDotAccessorsCommand(sublime_plugin.TextCommand):
"""
Toggle between dot and arrow accessors for C/C++ code.
* Dot Example: test.data
* Arrow Example: test.data
Multiple Edit Test:
* Put multiple cursors: test.data.data2
* Select both regions: test->data().not_data
"""
def run(self, edit, **args):
for region in reversed(self.view.sel()):
if region.size() == 0:
# Try one character beside the region to see what we've got
next_char = self.view.substr(region.begin())
if next_char == ".":
# Replace with arrow
edit_region = sublime.Region(region.begin(), region.end() + 1)
self.view.replace(edit, edit_region, "->")
elif next_char == "-":
# Check if it's an arrow
edit_region = sublime.Region(region.begin(), region.end() + 2)
if self.view.substr(edit_region) == "->":
self.view.replace(edit, edit_region, ".")
# TODO: Switch back to behind the edit point
elif next_char == ">":
# Check if it's an arrow (we might be in the middle, e.g. after dot conversion)
edit_region = sublime.Region(region.begin() - 1, region.end() + 1)
if self.view.substr(edit_region) == "->":
self.view.replace(edit, edit_region, ".")
else:
# Try to see if the selected text is something we want to replace
sel_text = self.view.substr(region)
if sel_text == ".":
self.view.replace(edit, region, "->")
elif sel_text == "->":
self.view.replace(edit, region, ".")
###########################################