Sublime Forum

Perceived buggy behavior in search

#1

Steps to reproduce:

  1. Open a new tab in Sublime text and type “the” into the text buffer
  2. Press Cmd+F and type “thespian”
  3. Realize you actually want to search more than just your current file; press Cmd+Shift+F
  4. Notice how the search box has cut off “spian”, leaving only “the”.

This feels systematic enough to possibly be intentional, but it took me a few months of getting bit before realizing what the root cause of the problem was.

Is there a way to change this behavior via configuration? Sublime Text is otherwise fantastic and I’ve been a happy user for years but this gets me every time.

0 Likes

#2

You might want to see if you have the following preference turned on in your user settings. As the comment mentions the value defaults to false on OSX (which it looks like you’re using), but having it enabled causes this to happen so it’s probably worth a check anyway.

// If true, the selected text will be copied into the find panel when it's
// shown.
// On OS X, this value is overridden in the platform specific settings, so
// you'll need to place this line in your user settings to override it.
"find_selected_text": true,
0 Likes

#3

Hi @OdatNurd, thanks for the suggestion!

Unfortunately while disabling that option does indeed fix the issue, I actually quite like that feature — it’s just the interaction with the case when the search field is already focused that I’d class as a bug. When I’ve just typed something in to a normal search and want to “upgrade” it to a multifile search, the current behavior actively causes me to lose work by eating the rest of the just-typed search query.

What would fix this is if the buffer selection were ignored while the search query is being actively edited – it’s the transition to the expanded field that causes the just-written search query to be replaced with the selection that I find problematic.

0 Likes

#4

@wbond Sorry to trouble you, but I wanted to make sure you saw this issue — tl;dr is that the find_selected_text does something unintuitive when a find mode is triggered while another find mode is focused (e.g. changing from find-in-buffer to find-in-files).

0 Likes

#5

This may be an itch that you can scratch with a plugin, such as the example below.

This plugin implements an event listener that detects when a find panel is about to open while another find panel is already visible, and clears the selection in the current view so that the new find panel doesn’t slurp it.

In cases where there is not already a panel visible, or a panel is visible but it’s the same panel that is about to be opened again, the selection is left as-is.

That lets you still select text and find it in the buffer or in files, but also ensures that if a find panel is already open and you open another one, the search string remains the same.

This could be enhanced to save the selection before it is cleared, and then have it be restored in an on_post_window_command to be a more seamless operation, if desired.

You can give it a try by selecting Tools > Developer > New Plugin... from the menu, replacing the stub plugin with the code below, then saving as a .py file in the location that Sublime will default to (your User package).

import sublime
import sublime_plugin


class ClearSelectionCommand(sublime_plugin.TextCommand):
    """
    Clear all selections and return to a single caret in the current view.
    """
    def run(self, edit):
        if self.view.sel():
            first_sel = self.view.sel()[0]
            self.view.sel().clear()
            self.view.sel().add(sublime.Region(first_sel.b))


class MyListener(sublime_plugin.EventListener):
    """
    When a find panel opens in a window, clear the selection in its current
    view if there is another find panel open.
    """
    def on_window_command(self, window, command, args):
        if command == "show_panel" and window.active_panel() is not None:
            if args.get("panel", None) not in ("console", "output"):
                window.active_view().run_command("clear_selection")
3 Likes

#6

Wow, thank you for the suggestion and the example.

I went to try it out and discovered that I could no longer replicate the original issue… I’m going to keep my eye out for the next time it occurs and see if I can distill the principle behind it since it looks like I was incorrect in my diagnosis this time.

Given that this is possible to alleviate with a plugin, I suspect the same will apply to the actual cause, whatever it turns out to be. Thanks so much for the demonstration of the power of Sublime’s extensibility — this was very enlightening.

Yuri

2 Likes

#7

By the way, this is off-topic but since this is my topic I figure I can do that – what’s the best resource for learning to write Sublime Text plugins? I know about the http://docs.sublimetext.info/en/latest/extensibility/plugins.html; is that what you would recommend?

Seeing your example made me want to dip my toes in the water and see how feasible it would be to make some other improvements I’ve been pining for. :slight_smile:

0 Likes

#8

The only resource that springs readily to mind is the tutorial written by @wbond (one of the Sublime developers), which is linked from the official documentation.

It’s for Sublime Text 2 so there may be some slight differences, but overall it’s a good overview of how things fit together. Off the top of my head I think the main changes would mostly be related to how ST2 uses Python 2.6.6 instead of 3.3.6 and the API is a little more extensive now than it used to be.

Apart from that, the official documentation contains an API Reference that you should at least skim to get a sense for what is possible with the API, and it may be handy to keep the Python 3.3 docs at the ready if you’re not familiar with Python in general.

A good source of information on how things work is plugins written by others. In this regard it can be handy to peek into the Default package. It ships with Sublime and as it’s name implies provides a fair amount of the default functionality of Sublime.

If you look inside of the folder that Sublime is installed in, there will be a folder named Packages, and inside of that folder is a file named Default.sublime-package. You can copy it out, rename it to Default.zip and extract it to view the contents.

It’s also possible to use PackageResourceViewer to look at individual files directly as well (if you’re one of the latest dev builds, the View Package File command that’s now built in).

1 Like