Skip to content

Commit f87b1a2

Browse files
committedSep 8, 2016
Replace Character: Fix positioning of caret
Before this change the command “Replace Character” would sometimes move the caret one position to the left. For example, if you insert the text a|bc – `|` represents the position of the caret – and use “Replace Character (←)”, then the result would look like this |👾bc . Now the result after the steps above is correct: 👾|bc. .
1 parent 1ea5776 commit f87b1a2

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
 

‎Commands/Replace Character (←).tmCommand

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ convert_single_character(reverse_direction = true)
2424
<key>name</key>
2525
<string>Replace Character (←)</string>
2626
<key>outputCaret</key>
27-
<string>interpolateByLine</string>
27+
<string>afterOutput</string>
2828
<key>outputFormat</key>
29-
<string>text</string>
29+
<string>snippet</string>
3030
<key>outputLocation</key>
3131
<string>replaceInput</string>
3232
<key>uuid</key>

‎Commands/Replace Character (→).tmCommand

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ convert_single_character
2424
<key>name</key>
2525
<string>Replace Character (→)</string>
2626
<key>outputCaret</key>
27-
<string>interpolateByLine</string>
27+
<string>afterOutput</string>
2828
<key>outputFormat</key>
29-
<string>text</string>
29+
<string>snippet</string>
3030
<key>outputLocation</key>
3131
<string>replaceInput</string>
3232
<key>uuid</key>

‎Support/lib/support.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def char_before(position)
7979
# REPLACEMENT.[] or REPLACEMENT.previous should be used as
8080
# replacement.
8181
#
82+
# [add_null] Add a null byte after the replaced character. The null byte is
83+
# useful as marker. If you want to insert text after the character
84+
# replacement, just activate this option and use something like
85+
# +text.sub("\0", add_after_replacement)+ afterwards.
86+
#
8287
# = Examples
8388
#
8489
# doctest: Replace the character at a certain position of a string
@@ -96,14 +101,18 @@ def char_before(position)
96101
# => "haha"
97102
# >> 'test'.replace_character(2, true)
98103
# => "t∉st"
99-
def replace_character(position, reverse = false)
104+
# >> 'test'.replace_character(2, true, true)
105+
# => "t∉\u0000st"
106+
#
107+
def replace_character(position, reverse = false, add_null = false)
100108
character = if reverse
101109
REPLACEMENT.previous(char_before(position))
102110
else
103111
REPLACEMENT[char_before(position)]
104112
end
105113
character.nil? ? self : (byteslice(0, position).characters[0..-2].join +
106-
character + byteslice(position..-1))
114+
character + ("\0" if add_null).to_s +
115+
byteslice(position..-1))
107116
end
108117
end
109118

@@ -248,6 +257,7 @@ def convert_single_character(reverse = false)
248257
if line_index <= 0
249258
TextMate.exit_show_tool_tip 'No character on the left side of the caret!'
250259
else
251-
print STDIN.read.replace_character(line_index, reverse).to_s
260+
snippet = e_sn(STDIN.read.replace_character(line_index, reverse, true))
261+
print(snippet.sub("\0", '$0'))
252262
end
253263
end

0 commit comments

Comments
 (0)
Please sign in to comment.