Skip to content

Commit

Permalink
Replace Character: Fix positioning of caret
Browse files Browse the repository at this point in the history
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.

.
  • Loading branch information
sanssecours committed Sep 8, 2016
1 parent 1ea5776 commit f87b1a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Commands/Replace Character (←).tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ convert_single_character(reverse_direction = true)
<key>name</key>
<string>Replace Character (←)</string>
<key>outputCaret</key>
<string>interpolateByLine</string>
<string>afterOutput</string>
<key>outputFormat</key>
<string>text</string>
<string>snippet</string>
<key>outputLocation</key>
<string>replaceInput</string>
<key>uuid</key>
Expand Down
4 changes: 2 additions & 2 deletions Commands/Replace Character (→).tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ convert_single_character
<key>name</key>
<string>Replace Character (→)</string>
<key>outputCaret</key>
<string>interpolateByLine</string>
<string>afterOutput</string>
<key>outputFormat</key>
<string>text</string>
<string>snippet</string>
<key>outputLocation</key>
<string>replaceInput</string>
<key>uuid</key>
Expand Down
16 changes: 13 additions & 3 deletions Support/lib/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def char_before(position)
# REPLACEMENT.[] or REPLACEMENT.previous should be used as
# replacement.
#
# [add_null] Add a null byte after the replaced character. The null byte is
# useful as marker. If you want to insert text after the character
# replacement, just activate this option and use something like
# +text.sub("\0", add_after_replacement)+ afterwards.
#
# = Examples
#
# doctest: Replace the character at a certain position of a string
Expand All @@ -96,14 +101,18 @@ def char_before(position)
# => "haha"
# >> 'test'.replace_character(2, true)
# => "t∉st"
def replace_character(position, reverse = false)
# >> 'test'.replace_character(2, true, true)
# => "t∉\u0000st"
#
def replace_character(position, reverse = false, add_null = false)
character = if reverse
REPLACEMENT.previous(char_before(position))
else
REPLACEMENT[char_before(position)]
end
character.nil? ? self : (byteslice(0, position).characters[0..-2].join +
character + byteslice(position..-1))
character + ("\0" if add_null).to_s +
byteslice(position..-1))
end
end

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

0 comments on commit f87b1a2

Please sign in to comment.