Sublime Forum

BracketHighlighter

#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

#159

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. :smiley:

0 Likes

#160

@facelessuser

[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.

0 Likes

#161

Probably will need to right a bh_plugin.

We have a proprietary language I have to use at work that is similar in this way:

for illustration

def somefunction var1 var2
        if (somecondition)
            some more code;
        eif
edef

So, I define my opening and closing brackets, and then sort out which opening matches to which closing by using a bh_plugin (do until is an exception):
[pre=#232628]def compare(name, first, second, bfr):
opening = bfrfirst.begin:first.end].lower()
closing = bfrsecond.begin:second.end].lower()
match = False
if opening == “do”:
if closing == “until”:
match = True
else:
match = “e” + opening == closing
return match[/pre]

Another example for handling case differences in PHP keywords:
github.com/facelessuser/Bracket … eywords.py

0 Likes

#162

Match when cursor is on outside edge of bracket:
Okay, everyone that has been wanting this, take some time to test the new experimental feature (ST3 only currently)

Add this setting to your “bh_core.sublime-settings”.

"bracket_outside_adjacent": true,

There is going to be some more overhead when this is turned on, but hopefully the impact won’t be too noticeable. I did this for you guys, so please do test and give feedback. If you are waiting for ST2, it may be a tomorrow or the day after.

0 Likes

#163

Few small things:

  1. It seems that this command doesn’t work anymore after the update (and boy, this command is damn useful!):
{ "keys": "ctrl+shift+space"], "command": "bh_key", "args": { "lines" : true, "plugin": { "type": "__all__"], "command": "bh_modules.bracketselect" } } }

The issue is present, with without bracket_outside_adjacent set to either true or false
2) add a different scope for adjacent, so it can have different color

0 Likes

#164
  1. This is now fixed. Funny, it had nothing to do with the new algorithm…just me with a stray mutli-cursor editing something I shouldn’t have.

  2. How come? Current algorithm doesn’t track such things. I would need to pass that info around, and it would require a bit of change to the code. Not sure I really want to spend time on that unless there was a really good reason.

FYI, I know tag outside adjacent just shows angles, I haven’t updated the tag bh_plugin with support yet.

0 Likes

#165

Tag support for experimental outside adjacent is in.

0 Likes