Home Download Buy Blog Forum Support

BracketHighlighter

Re: BracketHighlighter

Postby MKANET on Sat Nov 09, 2013 10:16 pm

Done! Thanks.
facelessuser wrote: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.
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: BracketHighlighter

Postby MKANET on Mon Nov 11, 2013 6:20 am

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.
MKANET
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: BracketHighlighter

Postby facelessuser on Mon Nov 11, 2013 7:53 am

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 :).
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: BracketHighlighter

Postby MKANET on Thu Nov 21, 2013 11:11 pm

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
 
Posts: 56
Joined: Fri Aug 30, 2013 5:06 pm

Re: BracketHighlighter

Postby facelessuser on Fri Nov 22, 2013 3:57 am

@MKANET, please create an issue on Github, and I will try and get to it soon; this is a pretty straight forward task.
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: BracketHighlighter

Postby gregor.hoch on Mon Jan 13, 2014 5:32 pm

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
Code: Select all
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!
gregor.hoch
 
Posts: 144
Joined: Sat Oct 01, 2011 7:54 pm

Re: BracketHighlighter

Postby facelessuser on Mon Jan 13, 2014 7:21 pm

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)
Code: Select all
- 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
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


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


Output
Code: Select all
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 :).
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: BracketHighlighter

Postby facelessuser on Mon Jan 13, 2014 7:34 pm

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.
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: BracketHighlighter

Postby gregor.hoch on Mon Jan 13, 2014 8:18 pm

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
Code: Select all
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.

Code: Select all
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)
gregor.hoch
 
Posts: 144
Joined: Sat Oct 01, 2011 7:54 pm

Re: BracketHighlighter

Postby facelessuser on Mon Jan 13, 2014 9:26 pm

gregor.hoch wrote: I can come up with something but I thought BH would be much better in finding the position of `(`.

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.
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

PreviousNext

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 5 guests