From 376f949afbdfedaccdf9bf9836c50fdbdb537201 Mon Sep 17 00:00:00 2001 From: Petr Marek Date: Sat, 7 Jan 2017 23:36:17 +0100 Subject: [PATCH] support $cursor in replace_text spell --- CHANGELOG.md | 4 ++++ docs/spells/replace_text.md | 2 +- spells/replace_text.py | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a5ebc7..3d25580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/spells/replace_text.md b/docs/spells/replace_text.md index dc34538..c661ab9 100644 --- a/docs/spells/replace_text.md +++ b/docs/spells/replace_text.md @@ -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 diff --git a/spells/replace_text.py b/spells/replace_text.py index fa3ab55..a98df2d 100644 --- a/spells/replace_text.py +++ b/spells/replace_text.py @@ -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) @@ -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")