Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
},
{
"keys": ["j"],
"command": "find_next",
"command": "find_in_files_find_next",
"context": [
{"key": "selector", "operator": "equal", "operand": "text.find-in-files" }
]
},
{
"keys": ["k"],
"command": "find_prev",
"command": "find_in_files_find_next",
"args": { "forward": false },
"context": [
{"key": "selector", "operator": "equal", "operand": "text.find-in-files" }
Expand Down
108 changes: 101 additions & 7 deletions find_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,84 @@
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):

Copy link
Copy Markdown
Author

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.

""" 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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
if len(view.sel()) == 1:
line_text = view.substr(view.line(view.sel()[0]))
print("line_text: %s" % line_text)
match = re.match(r"\s*(\d+).+", line_text)
if match:
return match.group(1)
Expand All @@ -36,9 +98,28 @@ 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):
print("BetterFind: jumping")
v = self.view
files = v.find_by_selector("entity.name.filename.find-in-files")
caret = v.sel()[0]
Expand All @@ -54,6 +135,19 @@ 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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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):
print("BetterFind: find next: %s " % forward)
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):
Expand Down