Skip to content

Commit

Permalink
Added command history
Browse files Browse the repository at this point in the history
  • Loading branch information
tarwich committed Mar 31, 2014
1 parent a5647b0 commit 700110a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[

{
"keys": ["up"], "command": "select_by_regex_history",
"args": {
"backwards": true
},
"context":
[
{ "key": "selector", "operator": "equal", "operand": "text.selectbyregex" }
]
},

{
"keys": ["down"], "command": "select_by_regex_history",
"context":
[
{ "key": "selector", "operator": "equal", "operand": "text.selectbyregex" }
]

},

{
"keys": ["escape"], "command": "select_by_regex_clear",
"context":
[
{ "key": "align_tab_mode", "operator": "equal", "operand": "true" }
]

}


]
12 changes: 12 additions & 0 deletions SelectByRegex.hidden-tmLanguage
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>SelectByRegex</string>
<key>scopeName</key>
<string>text.selectbyregex</string>
<key>uuid</key>
<string>d1f80e34-d8aa-4039-b321-9ec9c0873c09</string>
</dict>
</plist>
26 changes: 23 additions & 3 deletions SelectByRegexCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
ST3 = sublime.version() >= '3000'

class SelectByRegexCommand(sublime_plugin.TextCommand):
previous_regex = ""
original_regions = []
history = [ "" ]
history_index = 0

def description():
"""
Expand Down Expand Up @@ -44,7 +45,9 @@ def on_done(self, text):
# Perform select by regex
self.update_selection(text, draw_borders=False)
# Save the selection for next time
SelectByRegexCommand.previous_regex = text
SelectByRegexCommand.history.append(text)
# Reset the history to the end (technically beginning)
SelectByRegexCommand.history_index = 0

def restore_selection(self):
"""
Expand All @@ -71,14 +74,19 @@ def run(self, edit):
for region in self.view.sel():
SelectByRegexCommand.original_regions += [ sublime.Region(region.a, region.b) ]
# Show the user input prompt asking for a regex
SelectByRegexCommand.input_view = self.view.window().show_input_panel('Regex:', SelectByRegexCommand.previous_regex,
SelectByRegexCommand.input_view = self.view.window().show_input_panel('Regex:', '',
# On Done
self.on_done,
# On Change
self.on_change,
# On Cancel
self.on_cancel
)
# Set a TMLanguage to give the input view a context, keybindings, etc.
SelectByRegexCommand.input_view.set_syntax_file('Packages/SelectByRegex/SelectByRegex.hidden-tmLanguage')
SelectByRegexCommand.input_view.settings().set("is_widget", True);
SelectByRegexCommand.input_view.settings().set("gutter", False);
SelectByRegexCommand.input_view.settings().set("rulers", []);

def update_selection(self, regex, draw_borders=True):
"""
Expand Down Expand Up @@ -133,3 +141,15 @@ def update_selection(self, regex, draw_borders=True):
else: view.sel().add_all(SelectByRegexCommand.original_regions)
# Erase the borders and select the regions
view.erase_regions("SelectByRegexCommand")

class SelectByRegexHistoryCommand(sublime_plugin.TextCommand):
def run(self, edit, backwards=False):
# Either go forwards or backwards
increment = [1, -1][backwards]
# Increment the history
SelectByRegexCommand.history_index += increment
# Wrap the history
SelectByRegexCommand.history_index %= len(SelectByRegexCommand.history)
# Replace the text with the item from history
self.view.replace(edit, sublime.Region(0, self.view.size()), SelectByRegexCommand.history[SelectByRegexCommand.history_index])

14 changes: 14 additions & 0 deletions messages/1.1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Changes in 1.1.0
================

- Added history navigation and fixed prior text issue

Features
========

* Added history navigation with up/down arrow keys

Fixes
=====

* Fixed issue where prior text was always inserted and unselected

0 comments on commit 700110a

Please sign in to comment.