Skip to content

Commit

Permalink
support $cursor in replace_text spell
Browse files Browse the repository at this point in the history
  • Loading branch information
mreq committed Jan 7, 2017
1 parent b232ddc commit 376f949
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). Changelog muster from [olivierlacan/keep-a-changelog](https://github.com/olivierlacan/keep-a-changelog).

## [0.2.1] - 2016-01-07
### Added
- support `$cursor` in `replace_text` spell

## [0.2.0] - 2016-12-26
### Added
- default `alt+space` keymap
Expand Down
2 changes: 1 addition & 1 deletion docs/spells/replace_text.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A spell for replacing text inside/after delimiters.
## args

- `delimiter` - regexp pattern to be matched
- `replacement` - text used for replacement - when `$clipboard`, the clipboard content is used
- `replacement` - text used for replacement - `$clipboard` is replaced with the clipboard content; `$cursor` will be replaced by the cursor after the replacement
- `where`
- `inside` - replace text inside/between the delimiters
- `after` - replace text after the delimiter
Expand Down
23 changes: 18 additions & 5 deletions spells/replace_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def cast(self):
delimiter_length = len(delimiter)
re_delimiter = re.compile(delimiter)

self.replacement = self.spell.get('args').get('replacement')
if self.replacement == '$clipboard':
self.replacement = sublime.get_clipboard()

for sel in self.view.sel():
line = self.view.line(sel.a)

Expand All @@ -44,5 +40,22 @@ def cast(self):
raise AttributeError('Unknown value for "where": ' + where)

def replace(self, start, end):
replacement = self.spell.get('args').get('replacement')

clipboard = sublime.get_clipboard()
replacement = replacement.replace('$clipboard', clipboard)

cursor_position = replacement.find('$cursor')
if cursor_position is not -1:
replacement = replacement.replace('$cursor', '')

region = sublime.Region(start, end)
self.view.replace(self.edit, region, self.replacement)

self.view.replace(self.edit, region, replacement)

if cursor_position != -1:
new_cursor_position = start + cursor_position
self.view.sel().clear()
self.view.sel().add(sublime.Region(new_cursor_position))
if self.view.settings().get('vintage'):
self.view.run_command("_enter_insert_mode")

0 comments on commit 376f949

Please sign in to comment.