-
Notifications
You must be signed in to change notification settings - Fork 31
Add support for opening in split layouts and using transient previews #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
62f1ec0
9a2ebfd
f0ea82e
b6a752b
6d5edbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,78 @@ | |
| import sublime_plugin | ||
| import re, os, shutil | ||
|
|
||
| class FindInFilesOpenFileCommand(sublime_plugin.TextCommand): | ||
| def run(self, edit): | ||
| view = self.view | ||
| if view.name() == "Find Results": | ||
| line_no = self.get_line_no() | ||
|
|
||
| class FileOpeningSupport(object): | ||
| """ Base class for plugins that need file opening support. """ | ||
| def open_file(self, transient = False): | ||
| view = self.view | ||
| window = self.view.window() | ||
|
|
||
| # If we have multiple groups, then select the next one in layout | ||
| # to open the new file. We only do transient if we have a | ||
| # split layout. | ||
| # todo: move this to a config option | ||
| use_transient_preview = True | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to add this to a configuration option. Unfortunately I don't know how to do this. If anyone can point me to how to add an option like "use_transient_preview" to the config for this plugin, that would be awesome. |
||
| if transient and not use_transient_preview: | ||
| return | ||
|
|
||
| start_group = window.active_group() | ||
| dest_group = (start_group + 1) % window.num_groups() | ||
| should_open = (not transient) or (start_group != dest_group) | ||
|
|
||
| if (view.name() == "Find Results") and should_open: | ||
| line_no = self.get_line_no() | ||
| file_name = self.get_file() | ||
| find_term = self.get_find_term() | ||
| open_flags = sublime.TRANSIENT if transient else 0 | ||
|
|
||
| print ("BetterFind: file: [%s] trans: [%s] term: [%s]" % (file_name, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug code still in here for now. |
||
| transient, | ||
| find_term)) | ||
| new_view = None | ||
| window.focus_group(dest_group) | ||
| if line_no is not None and file_name is not None: | ||
| open_flags = open_flags | sublime.ENCODED_POSITION | ||
| file_loc = "%s:%s" % (file_name, line_no) | ||
| view.window().open_file(file_loc, sublime.ENCODED_POSITION) | ||
| new_view = view.window().open_file(file_loc, open_flags) | ||
| elif file_name is not None: | ||
| view.window().open_file(file_name) | ||
| new_view = view.window().open_file(file_name, open_flags) | ||
| window.focus_group(start_group) | ||
|
|
||
| # Transient views don't respect the focus group, move explicitly | ||
| # XXX: Not sure if we can do this before view is loaded. | ||
| if new_view and transient: | ||
| window.set_view_index(new_view, dest_group, 0) | ||
|
|
||
| def finish_open(): | ||
| # delay until view is done loading | ||
| if new_view.is_loading(): | ||
| sublime.set_timeout(finish_open, 50) | ||
|
|
||
| # Find and highlight the current result in transient window | ||
| new_view.erase_regions("bfb_key") | ||
| if transient: | ||
| row_num = 0 if (line_no is None) else int(line_no) - 1 | ||
| line_start_pos = new_view.text_point(row_num, 0) | ||
|
|
||
| ##print("line: %s pos: %s" % (line_no, line_start_pos)) | ||
| ##print(" : %s" % new_view.substr(line_start_pos)) | ||
| ##print(" : %s" % new_view.substr(new_view.line(line_start_pos))) | ||
|
|
||
| # XXX: Don't have active find settings, so use most generic options | ||
| region = new_view.find(find_term, line_start_pos, flags = sublime.IGNORECASE) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could make this find much better if I could determine what the current settings are for the find tool. Namely is it case sensitive, is it using regex, etc. For now I am just using the most flexible options to make it match in most cases. |
||
| if region is not None: | ||
| new_view.add_regions("bfb_key", [region], | ||
| scope="string", flags = sublime.DRAW_NO_OUTLINE) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite like the coloring of the highlight here but I don't know how to change it. The scope setting influences it, but I don't know enough about plugins to do better. |
||
|
|
||
| # Transient views don't respect the focus group, move explicitly | ||
| window.set_view_index(new_view, dest_group, 0) | ||
|
|
||
| # Adding regions can change group focus, so set back | ||
| window.focus_group(start_group) | ||
|
|
||
| if new_view: | ||
| finish_open() | ||
|
|
||
| def get_line_no(self): | ||
| view = self.view | ||
|
|
@@ -27,7 +88,7 @@ def get_file(self): | |
| view = self.view | ||
| if len(view.sel()) == 1: | ||
| line = view.line(view.sel()[0]) | ||
| while line.begin() > 0: | ||
| while line.begin() >= 0: | ||
| line_text = view.substr(line) | ||
| match = re.match(r"(.+):$", line_text) | ||
| if match: | ||
|
|
@@ -36,6 +97,24 @@ def get_file(self): | |
| line = view.line(line.begin() - 1) | ||
| return None | ||
|
|
||
| def get_find_term(self): | ||
| # Scan back in view for current search term for selected result | ||
| view = self.view | ||
| if len(view.sel()) == 1: | ||
| line = view.line(view.sel()[0]) | ||
| while line.begin() >= 0: | ||
| line_text = view.substr(line) | ||
| match = re.match(r"^Searching.*?\"(.*)\".*?$", line_text) | ||
| if match: | ||
| term = match.group(1) | ||
| return term | ||
| line = view.line(line.begin() - 1) | ||
| return None | ||
|
|
||
| class FindInFilesOpenFileCommand(sublime_plugin.TextCommand, FileOpeningSupport): | ||
| def run(self, edit): | ||
| self.open_file(transient = False) | ||
|
|
||
|
|
||
| class FindInFilesJumpFileCommand(sublime_plugin.TextCommand): | ||
| def run(self, edit, forward=True): | ||
|
|
@@ -54,6 +133,18 @@ def run(self, edit, forward=True): | |
| top_offset = v.text_to_layout(region.begin())[1] - v.line_height() | ||
| v.set_viewport_position((0, top_offset), True) | ||
|
|
||
| class FindInFilesFindNextCommand(sublime_plugin.TextCommand, FileOpeningSupport): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new command that simply calls through to find_nex and find_prev but has the hook to add the transient view file preview. |
||
| def run(self, edit, forward=True): | ||
| v = self.view | ||
| window = v.window() | ||
|
|
||
| if forward: | ||
| window.run_command("find_next",) | ||
| else: | ||
| window.run_command("find_prev", {"forward": False}) | ||
|
|
||
| # If we want to support transient preview | ||
| self.open_file(transient = True) | ||
|
|
||
| class FindInFilesSetReadOnly(sublime_plugin.EventListener): | ||
| def is_find_results(self, view): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a common base class for the plugins. If there is a better way to do this please let me know.