Sublime Forum

BracketHighlighter

#72

[quote=“tito”]

[quote=“C0D312”]I still want to do some more playing around with the icons. For now, here’s an HTML ‘tag’ icon: cl.ly/2M1x0e2g141X1Y1I1B3z

Might want to change it later, but that’s it so far.[/quote]

Looks good, Maybe we can add yet another package :smiley: “GutterIcons” and collect there some nice similar polished icons for the different sizes and possible OS.
Providing an APi or something to use the icons, then other packages use these (if they whish) and the overall application will feel better.
Like 4 different linters using the same icons for displaying errors.[/quote]

I was just goofing around, but if someone else wanted to use gutter icons I coded a simple script called fancy_regions.py

[code]import sublime
from glob import glob
from os.path import exists, normpath, join, basename

REGION_STYLES = {
“solid”: 0,
“none”: sublime.HIDDEN,
“outline”: sublime.DRAW_OUTLINED,
“underline”: sublime.DRAW_EMPTY_AS_OVERWRITE
}

class FancyRegions(object):
def init(self, view):
self.view = view
self.__icon_path = “FancyRegions/icons”
self.__icons = ]
self.__icons_cached = False

def erase_regions(self, key):
    """
    Erase regions
    """

    self.view.erase_regions(key)

def add_outline_regions(self, key, regions, scope="text", icon="none", flags=0):
    """
    Add outline regions
    """

    self.add_regions(key=key, regions=regions, scope=scope, style="outline", flags=flags)

def add_underline_regions(self, key, regions, scope="text", icon="none", flags=0):
    """
    Add underline regions
    """

    self.add_regions(key=key, regions=regions, scope=scope, style="underline", flags=flags)

def add_hidden_regions(self, key, regions, scope="text", icon="none", flags=0):
    """
    Add hidden regions
    """

    self.add_regions(key=key, regions=regions, scope=scope, style="none", flags=flags)

def add_solid_regions(self, key, regions, scope="text", icon="none", flags=0):
    """
    Add solid regions
    """

    self.add_regions(key=key, regions=regions, scope=scope, style="solid", flags=flags)

def add_regions(self, key, regions, scope='text', style="solid", icon="none", flags=0):
    """
    Add regions with defined styling to a view
    """

    # Default flag settings
    options = 0

    # Check style type
    if style in REGION_STYLES:
        options |= REGION_STYLES[style]

    # Convert regions suitable for underlining if style underline
    if style == "underline":
        regions = self.__underline(regions)

    # Set additional flags if given
    if flags:
        options |= flags

    # If icon is defined and exists, set the icon path
    icon_path = ""
    if icon != "" and icon != "none":
        if self.view.line_height() < 16:
            icon += "_small"

        if exists(normpath(join(sublime.packages_path(), self.__icon_path, icon + ".png"))):
            icon_path = "../%s/%s" % (self.__icon_path, icon)

    # Apply region(s)
    self.view.add_regions(
        key,
        regions,
        scope,
        icon_path,
        options
    )

@property
def icons(self):
    """
    Get list of available icons in current icon path
    """

    if self.__icons_cached:
        return self.__icons:]
    else:
        if not self.index_icons():
            return self.__icons:]
        else:
            return ]

@property
def icon_path(self):
    """
    Return current path to icons (relative to Packages)
    """

    return self.__icon_path

@icon_path.setter
def icon_path(self, path):
    """
    Set current path to icons (relative to Packages)
    """

    file_path = path.replace('\\', '/').strip('/')
    full_path = normpath(join(sublime.packages_path(), file_path))
    if exists(full_path):
        self.__icon_path = file_path
        self.index_icons()

def index_icons(self):
    """
    Search for icons in current icon path and store results
    """

    errors = False
    self.__icons = ]
    file_path = normpath(join(sublime.packages_path(), self.__icon_path))
    if exists(file_path):
        self.__icons = [basename(png).rstrip(".png") for png in glob(file_path + "/*.png")]
        self.__icons_cached = True
    else:
        self.__icons = ]
        self.__icons_cached = False
        errors = True
    return errors

def __underline(self, regions):
    """
    Convert regions to individual empty regions for underlining
    """

    new_regions = ]
    for region in regions:
        start = region.begin()
        end = region.end()
        while start < end:
            new_regions.append(sublime.Region(start))
            start += 1
    return new_regions

[/code]

Allows you to do stuff like this: lets say you wanted to create an underlined region with the default scope and a custom icon found in the custom icon folder.

[code]
import sublime
from fancy_regions import FancyRegions

fancy = FancyRegions(sublime.active_window().active_view())
region_list = [sublime.Region(0, 6)]
fancy.add_underline_regions(‘test_region’, region_list, icon=“some_icon”)[/code]

It does common region types like solid, outline, underline, and it handles all of the custom icon stuff. You can even request a list of icons in the custom icon folder.

I could put this in a repository, but I am just not sure if anyone would actually use it or not. But I was just fooling around. :smile:

0 Likes

#73

hello, i use BracketHiglighter and i like it alot, can u maybe make this option:

default behavior in notepad++

can u add that red line, so its filling the gap between brackets .to see more clearly block where ur cursor is :smile: would be awsame <3

0 Likes

#74

Hmmm. Interesting. I was about to say that making that line would not be possible but then I realized, Yes it is. It might take some work, but with the custom gutter icons, a straight line isn’t out of the question. I personally don’t thing it’s that important of a feature but could be an interesting exercise for the gutter’s capabilities.

0 Likes

#75

I don’t think it is out of the realm of possibilities, but it does have some limitations.

[quote=“ManFromEarth”]hello, i use BracketHiglighter and i like it alot, can u maybe make this option:

default behavior in notepad++

can u add that red line, so its filling the gap between brackets .to see more clearly block where ur cursor is :smile: would be awsame <3[/quote]

To be honest, this would only really work if no_multi_select_icons was enabled. Highlighting multi-select brackets like that would just be a mess; it just wouldn’t work with how ST2 does things.

Tell you what, I will look into how good I could make it look with no_multi_select_icons enabled, and if I can get it to look decent, I might add it disabled by default.

0 Likes

#76

in this context, i was trying to recall if in previous Sublime versions the indent guides colored differently to indicate cursor location.
i was actully looking for this option a few months ago, guess it wasn’t even there, wasn’t it?

0 Likes

#77

[quote=“vitaLee”]in this context, i was trying to recall if in previous Sublime versions the indent guides colored differently to indicate cursor location.
i was actully looking for this option a few months ago, guess it wasn’t even there, wasn’t it?[/quote]

Oh yeah. @Facelessuser, settting indent guides to draw_active is essentially the same thing and a lot cleaner. So you might not want to waste your day fiddling with something that’s not really important/already implemented.

0 Likes

#78

[quote=“C0D312”]

[quote=“vitaLee”]in this context, i was trying to recall if in previous Sublime versions the indent guides colored differently to indicate cursor location.
i was actully looking for this option a few months ago, guess it wasn’t even there, wasn’t it?[/quote]

Oh yeah. @Facelessuser, settting indent guides to draw_active is essentially the same thing and a lot cleaner. So you might not want to waste your day fiddling with something that’s not really important/already implemented.[/quote]

Built in only does function blocks, not any bracket block. And trust me, it is supper easy to add (I’m not looking to spend a lot of time on anything today :smile:):

Cavet: when you space the lines further, they get further apart. But you can’t get better than this though, it is a limitation with ST2.


General Impression?

0 Likes

#79

thanks for the tip. :smile:
personally i think that indent guides in the gutter would be too much.

0 Likes

#80

That was fast. I, personally, don’t like it. Makes the gutter too busy; however, I’m impressed that it only took you a couple of minutes to implement.

0 Likes

#81

When you know the code well, stuff like this is trivial.

I agree though that it doesn’t look good. At least not to me.

0 Likes

#82

Though it was a fun exercise, I think I will hold off on adding the line between brackets. I can add it without affecting people that don’t like it, but it does seem unneeded with the current ST2 indentation highlight option. Notepad++ only does the line on foldable regions, and the indentation option in ST2 does the same. So I think what was being asked here is already accomplished in ST2.

I would have to get a lot of people asking for this to support such an option.

0 Likes

#83

i have question regarding tag matching.
does it make sense to you to match tags when the cursor is between tags?
like:
<> |


| |
<> |

currently div tags are matched when cursor is within either one of the opening/closing tags
<>| <div | >
<>|
0 Likes

#84

[quote=“vitaLee”]i have question regarding tag matching.
does it make sense to you to match tags when the cursor is between tags?
like:
<> |


| |
<> |

currently div tags are matched when cursor is within either one of the opening/closing tags
<>| <div | >
<>| [/quote]

+1

0 Likes

#85

[quote=“vitaLee”]i have question regarding tag matching.
does it make sense to you to match tags when the cursor is between tags?
like:
<> |


| |
<> |

currently div tags are matched when cursor is within either one of the opening/closing tags
<>| <div | >
<>| [/quote]

It makes since, but it is not as easily done. I am kind of doing what I believe ST2 does, first I use the angle bracket matching to find a potential tag, then I see if it is a tag (opening or closing) and then I look for the pair if it is a tag. If you put a cursor in here:

(\w-]+)\s(:)\s**

Do you match the bracket or the tag?

If I added something like this I might take this approach if I find a bracket match before I find a tag, don’t search for tag, if I find a tag before I find a bracket match, then highlight the tags. That way in the above example, if I were inside the brackets, the tag does not get matched, but if I am outside the brackets, then the tags would get matched.

Let me play around with this.

0 Likes

#86

i was thinking the same.
match the first bracket you find in either direction, that’s why it’s called BracketHighlighter :smile:

0 Likes

#87

[quote=“vitaLee”]i was thinking the same.
match the first bracket you find in either direction, that’s why it’s called BracketHighlighter :smile:[/quote]

Its just fitting it into the algorithm is a little tricky. It was streamlined to do it one way, but to do it the way you are suggesting may take me a while to do well (if I get it working good at all). Also, it is not on the top of my list of things to do since it would also be big headache to make happen.

What I plan to do in the next week is to resolve my current issue with strings:

  • I don’t like that I am wasting time detecting regex conventions for different languages in strings, and I think it is silly I am highlighting “/” in regex strings. So I am thinking I want to only highlight quoted strings, but still allow the searching for and highlighting of internal string brackets in any non-quoted strings. If no brakcets are found in a non-quoted string, I want to ignore the string and find the next parent bracket. It would be a more generic support for brackets inside any string, and it would support more languages.
  • This could also apply to people who don’t want to highlight quotes, but still like to highlight brackets inside strings.
0 Likes

#88

Version 1.7

  • Hide parent quote highlighting when child quotes are highlighted
  • Allow the searching for brackets in non-quoted code scoped as strings (like regex)
  • Add setting “highlight_string_brackets_only” which allows never highlighting quotes but leaves internal string bracket highlighting on
  • deprecate “enable_forward_slash_regex_strings” in favor of “find_brackets_in any_strings”

Hopefully that all makes sense, basically I just made some tweaks to string highlighting and allow for string bracket highlighting now to occur in any string scope.

0 Likes

#89

Forgive me if this has been mentioned, but I’d find it epically cool and useful to have multiple bracket types highlighted at once (with only one insertion point) like this:

http://puu.sh/mBWQ

0 Likes

#90

[quote=“peppy”]Forgive me if this has been mentioned, but I’d find it epically cool and useful to have multiple bracket types highlighted at once (with only one insertion point) like this:

http://puu.sh/mBWQ[/quote]

The problem is, BracketHighlighter is designed to minimize how much time it is searching to give you pretty instant highlighting without bogging down your editor. By doing what you suggest, you would have to keep crawling back looking for even more brackets; this would take more time on every search.

Also, I think it would make things more cluttered. The point of BracketHighlighter is to quickly show you what block you are in, but if all the blocks get highlighted, it is not nearly as effective at quickly showing you were you are.

Though opinion may differ on this point, I do not foresee this becoming an addition to BracketHighlighter, but thanks for your interest.

0 Likes

#91

I figured that might be the case. If I get the time I might fork and give it a go to see how it handles performance-wise. I think it is be a matter of personal opinion whether such an addition makes things cluttered or more usable :smile:.

0 Likes