Sublime Forum

BracketHighlighter

#139

Thanks for the reply. I really appreciate it! One more question…
Is there a way to specify minimum number of lines (or characters) threshold?

I dont want it to highlight brackets that are too close together. Its kind of distracting, and not necessary since it’s obvious where they are already. I just want it to highlight brackets that are further apart, if possible.

0 Likes

#140

Not currently. If I can get it in without mucking up anything else, I wouldn’t mind doing it. Please create a github issue at the repo.

0 Likes

#141

Done! Thanks.

0 Likes

#142

I noticed that Sublime Text locks up for a few seconds when I first open it with a a script file. I’m guessing it’s pretty resource intensive when bracket highlighter first initializes. Is there any way to somehow preemptively cache bracket highlighter so it doesn’t lockup ST3 every time when I close and restart ST3? Maybe, there are some performance tips/settings I could use to alleviate this symptom?

BTW: Add me to the list of people who think this plugin is a MUST HAVE for Sublime Text.

0 Likes

#143

This is what BracketHighlighter does:

  • On startup: start a thread to watch for view changes or selection changes and intialize the matcher object
  • On view focus: determine which rules etc. it needs to load up for the focused files
  • On selection: match and optionally run bracket plugin modules as requested

There is one thing that takes a second or two, and that is creating a unicode table for unicode property support in regex rules (like this r"\p{Ll}\p{Lu}]"). But it happens only once, and I now defer it until the first call to use it, then it shouldn’t happen again unless the plugin gets reloaded. Deferring this allows ST Gui to load up without any lag.

ST Startup will probably do all these things:

  • Startup: causing the BH plugin to load (pretty quick)
  • Load views and focus one causing BH to load up rules (also negligible; try switching tabs, this causes BH to this)
  • Places cursor in view which causes BH to do its first match causing the initial unicode table to load and then perform the match (this will cause the unicode property table to initialize)

Hardcoding the unicode table can probably help in the first view load time, or maybe caching that. I am sure this could probably just be loaded once, and then pickled for later use…

The plugin is surprisingly complicated to allow for all the different tweakable stuff and to be general enough to allow people to add their custom brackets via the settings files and/or specialized functions via bracket plugins. In general though, I don’t notice performance slow down because of the plugin. Probably the unicode table loading is the one thing that takes a couple of seconds.

If I get some time, I may look into caching the unicode property list. I also take pull requests :smile:.

0 Likes

#144

Thanks for taking a look at it. It certainly would be nice to be able to avoid the initial “lockup”. Im guessing it’s bit more of an issue for people like me who make small scripts (frequently exiting/starting Sublime Text). If I knew python, I would definitely attempt to fix it myself.

0 Likes

#145

@MKANET, please create an issue on Github, and I will try and get to it soon; this is a pretty straight forward task.

0 Likes

#146

Hi,

is it possible to call a function from bracket highlighter from another ST plugin to find the position of the current opening (? So if this is the code

read.dta('filepath',|

and the curser us at |, I would like to get the position of the ( bracket. I know that I can program that with the ST API but I was wondering whether I can call a BracketHighlighter function to get that position?

Thanks for the great plugin!

0 Likes

#147

Yes you can. Or I should say you can send them from BH. I don’t currently have a command to call that just finds the brackets and returns the info without highlighting. If you plan on modifying the bracket, you might consider doing it all in a bh_plugin. But you at least need to call bh_plugin to send the info out:

So lets pretend I have a SublimePlugin called NotifyBrackets. It is going to select the actual brackets and notify the user the location of the brackets.

Directory structure (bh_plugins really shouldn’t be in a top level folder; they are going to dynamically be loaded when needed. So I put them in bh_modules with no init.py file)

- Packages
    - NotifyBrackets
        - bh_modules
            notifybracket.py
        Default.sublime-commands

Simple Code will format a message with the bracket locations and send the info to my SubNotify command to popup a notification bubble. It will also log it in the Sublime console. At the end, I modify the selections so BH will select the brackets when it is done processing all my brackets.

notifybracket.py
[pre=#232628]import BracketHighlighter.bh_plugin as bh_plugin
import sublime

class NotifyBracket(bh_plugin.BracketPluginCommand):
def run(self, edit, name):
text = “Bracket Type: %s\n” % name
text += “Left bracket range (%(begin)d, %(end)d)\n” % {“begin”: self.left.begin, “end”: self.left.end}
text += “Right bracket range (%(begin)d, %(end)d)” % {“begin”: self.right.begin, “end”: self.right.end}
sublime.run_command(“sub_notify”, {“title”: “NotifyBracket”, “msg”: text})
print(text)
self.selection = sublime.Region(self.left.begin, self.left.end), sublime.Region(self.right.begin, self.right.end)]

def plugin():
return NotifyBracket[/pre]

In my command file, I setup the calling of the command:
Default.sublime-commands
[pre=#232628]
// Highlight and notify about bracket
{
“caption”: “NotifyBracket: Highlight and Notify Brackets”,
“command”: “bh_key”,
“args”:
{
“plugin”:
{
“type”: “all”],
“command”: “NotifyBracket.bh_modules.notifybracket”
}
}
}
][/pre]

Output

Bracket Type: round Left bracket range (85, 86) Right bracket range (116, 117)

I believe most things for the bh_plugin API are documented…maybe not well, but they should be documented. If you find something missing, let me know. There are some complications you might run into if doing extremely complex modifications to a view while processing the brackets. I have a bug that causing bracket swapping not to work when you have multiple brackets targeted that are nested inside of each other. But most other things should work. That is just a limitation in the current iteration of bh_plugins that we have to live with right now.

Maybe in the future I will add a command to just return bracket positions. We shall see.

Hope that helps and didn’t overwhelm you :smile:.

0 Likes

#148

If you just really want a command that returns bracket positions without using a bh_plugin, feel free to create an issue. It might not be that hard to do…I would just need to find the time.

0 Likes

#149

Wow, thanks for the quick and extensive reply! That helps a lot for understanding BracketHighlighter plugins.

My goal basically is a ST auto-completion plugins that also shows auto-completions for function arguments. I don’t think that can be done in the BH plugin framework (see structure of ST auto complete extensions below). I basically need the word before the last (, which is the function name so that I can return the correct arguments. So for this code function_name(arg1=True,arg2=str(34), | (curser at |), I need “function_name” so that I can look up the arguments and return them for as auto-completions. I can come up with something but I thought BH would be much better in finding the position of (. Maybe I will open a ticket on github. Basically, the idea would be an ST API extension other ST packages can use.

class AutoCompletions(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        # code...
        return (
            ("arg1", "description"),
            ("arg2", "description")
        ], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
0 Likes

#150

It might be. BH is mostly optimized to find “matching” pairs. It is probably pretty reliable at finding left hanging brackets than it is at finding right hanging brackets. It is best if the brackets match though. This is mainly due to how complicated the system is for finding custom brackets and the fact that BH bails as soon has it is pretty sure it can’t find a match (less lag in searching keeps the plugin from annoying people). So it doesn’t always search both ways, but it usually starts out searching left first.

0 Likes

#151

First thank you for that great plugin! Can’t work without it.

Now i am looking at feature present in most editors, select the bracket scope content on double mouse click on bracket. This is very useful to select arrays and such.

Now i was wondering if that was possible with you plugin (maybe using command) without breaking the normal behaviour of double click (select word)
Finally it would need to select the brackets with it. i mention that because by using select scope content it doesnt actually select the brackets.

Thanks

0 Likes

#152

[quote=“farfromrefuge”]Now i am looking at feature present in most editors, select the bracket scope content on double mouse click on bracket. This is very useful to select arrays and such.

Now i was wondering if that was possible with you plugin (maybe using command) without breaking the normal behavior of double click (select word)[/quote]

Honestly, the current mouse API in Sublime sucks. I don’t think this can be done reliably.

I plan on adding a variant of the current command to select brackets as well github.com/facelessuser/Bracket … issues/132 …when I get some time and feel like working on it :smile:.

0 Likes

#153

I „fixed” the select the whole bracket scope by using this snippet (in .sublime-mousemap file, obviously)

{ "button": "button1", "count": 1, "modifiers": "button2"], "command": "expand_selection", "args": {"to": "brackets"}, "press_command": "drag_select" }

It doesn’t work on double click, but it works by holding click2 then press on click1 buttons.

0 Likes

#154

[quote=“iamntz”]I „fixed” the select the whole bracket scope by using this snippet (in .sublime-mousemap file, obviously)

{ "button": "button1", "count": 1, "modifiers": "button2"], "command": "expand_selection", "args": {"to": "brackets"}, "press_command": "drag_select" }

It doesn’t work on double click, but it works by holding click2 then press on click1 buttons.[/quote]

This doesn’t really “fix” it per se. BH selects more than common curly, round, and square brackets. BH has a number of custom brackets as well. Also the algorithm for built in ST bracket matching is different than BH, so this won’t always select what you see BH targeting (ST algorithm can be sometimes inconsistent with brackets next to each other, at least it use to be…haven’t checked recently).

The option of mouse modifier might be a nice touch though. As soon as I add the alternative bracket selection command (or command parameter), you should be able to use something like iamntz posted above for the BH variant that will target custom brackets as well.

0 Likes

#155

I know, that’s why i used quotes :wink:

Anyhow, for css(sass), php and js, this works every single time. Since the API sucks, we work with what we have :mrgreen:

0 Likes

#156

Man I completely missed those :smile:.

Anyways, thanks for sharing. I don’t use mouse maps at all, so it is nice to see things like that work…I should play around with the mouse maps a bit more.

0 Likes

#157

I added a flag for the bracket select command to always select the tags with content. So by just taking the current command and adding the always_include_brackets argument, you can select the content and tags:

[pre=#232628] // Select text including brackets
{
“keys”: “ctrl+alt+super+d”],
“command”: “bh_key”,
“args”:
{
“lines” : true,
“plugin”:
{
“type”: “all”],
“command”: “bh_modules.bracketselect”,
“args”: {“always_include_brackets”: true}
}
}
},[/pre]

Pair that with the mouse trick, and that should do it.

0 Likes

#158

no problem, thanks for answering !

0 Likes