Done! Thanks.
BracketHighlighter
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.
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 .
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.
@MKANET, please create an issue on Github, and I will try and get to it soon; this is a pretty straight forward task.
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!
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 .
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.
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)
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.
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
[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 .
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=â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.
I know, thatâs why i used quotes
Anyhow, for css(sass), php and js, this works every single time. Since the API sucks, we work with what we have
Man I completely missed those .
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.
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.
I canât seem to get the mouse thing to work at all. The log says itâs firing, but nothing happens. Any ideas?
And thanks to the dev for adding that new always_include_brackets option.
[quote]This is what you need:
{
ânameâ: âlatex_floatsâ,
âopenâ: â(\\begin\{(?:table|sidewaystable|figure|sidewaysfigure|algorithm)\})â,
âcloseâ: â(\\end\{(?:table|sidewaystable|figure|sidewaysfigure|algorithm)\})â,
// âopenâ: â(\begin\{sidewaystable\})â,
// âcloseâ: â(\end\{sidewaystable\})â,
âstyleâ: âdefaultâ,
âscope_excludeâ: âstringâ, âcommentâ],
âlanguage_filterâ: âwhitelistâ,
âlanguage_listâ: âLaTeXâ],
âenabledâ: true
},
[/quote]
Am I correct in assuming that this will not work with nested constructs? E.g.:
\begin{figure}[thb]
\begin{center}
\includegraphics]{Fig_Geometric_Nonlinear_Examples/Roll-up_Dyn/timegeo1.eps}
\end{center}
\end{figure}
I would think that it would be necessary to include an additional captured group in the open pattern, that was searched for in the close pattern. To try and get this to work Iâve been trying to hook up to the ht/xml tag matching. I tried adding the following (copying //HTML, and modifying the ends of the regex):
// LaTeX \begin{(...)} \end{(...)}
{
"name": "latex",
"open": "(\\\\begin\\{)(?=\\w\\:\\-]+(?:(?:\\s+\\w\\-:]+(?:\\s*=\\s*(?:\"^\"]*\"|'^']*'|^>\\s]+))?)*)\\s*\\/?>|\\/\\w\\:\\-]+^>]*\\})",
"close": "(?<=\\\\end\\{)(?:\\w\\:\\-]+(?:(?:\\s+\\w\\-:]+(?:\\s*=\\s*(?:\"^\"]*\"|'^']*'|^>\\s]+))?)*)\\s*\\/?|\\/\\w\\:\\-]+^>]*)(\\})",
"style": "tag",
"scope_exclude": "string", "comment"],
"language_filter": "whitelist",
"language_list": "LaTeX"],
"plugin_library": "bh_modules.tags",
"find_in_sub_search": "only",
"enabled": true
},
and then
"tag_mode": {
"xhtml": "XML", "LaTeX"], ...
But I donât get anything. I havenât been able to find info on how to hook up custom tags.