Skip to content
This repository
branch: BH2
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 1391 lines (1166 sloc) 48.653 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
from os.path import basename, exists, join, normpath
import sublime
import sublime_plugin
from time import time, sleep
import thread
import re
from bh_plugin import BracketPlugin, BracketRegion, ImportModule
from collections import namedtuple
import traceback

BH_MATCH_TYPE_NONE = 0
BH_MATCH_TYPE_SELECTION = 1
BH_MATCH_TYPE_EDIT = 2
DEFAULT_STYLES = {
    "default": {
        "icon": "dot",
        "color": "brackethighlighter.default",
        "style": "underline"
    },
    "unmatched": {
        "icon": "question",
        "color": "brackethighlighter.unmatched",
        "style": "outline"
    }
}
HV_RSVD_VALUES = ["__default__", "__bracket__"]

HIGH_VISIBILITY = False

GLOBAL_ENABLE = True


def bh_logging(msg):
    print(msg)


def bh_debug(msg):
    if sublime.load_settings("bh_core.sublime-settings").get('debug_enable', False):
        bh_logging(msg)


def underline(regions):
    """
Convert sublime regions into underline regions
"""

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


def load_modules(obj, loaded):
    """
Load bracket plugin modules
"""

    plib = obj.get("plugin_library")
    if plib is None:
        return

    try:
        module = ImportModule.import_module(plib, loaded)
        obj["compare"] = getattr(module, "compare", None)
        obj["post_match"] = getattr(module, "post_match", None)
        loaded.add(plib)
    except:
        bh_logging("BracketHighlighter: Could not load module %s" % plib)
        raise


def select_bracket_style(option):
    """
Configure style of region based on option
"""

    style = sublime.HIDE_ON_MINIMAP
    if option == "outline":
        style |= sublime.DRAW_OUTLINED
    elif option == "none":
        style |= sublime.HIDDEN
    elif option == "underline":
        style |= sublime.DRAW_EMPTY_AS_OVERWRITE
    return style


def select_bracket_icons(option, icon_path):
    """
Configure custom gutter icons if they can be located.
"""

    icon = ""
    small_icon = ""
    open_icon = ""
    small_open_icon = ""
    close_icon = ""
    small_close_icon = ""
    # Icon exist?
    if not option == "none" and not option == "":
        if exists(normpath(join(sublime.packages_path(), icon_path, option + ".png"))):
            icon = "../%s/%s" % (icon_path, option)
        if exists(normpath(join(sublime.packages_path(), icon_path, option + "_small.png"))):
            small_icon = "../%s/%s" % (icon_path, option + "_small")
        if exists(normpath(join(sublime.packages_path(), icon_path, option + "_open.png"))):
            open_icon = "../%s/%s" % (icon_path, option + "_open")
        else:
            open_icon = icon
        if exists(normpath(join(sublime.packages_path(), icon_path, option + "_open_small.png"))):
            small_open_icon = "../%s/%s" % (icon_path, option + "_open_small")
        else:
            small_open_icon = small_icon
        if exists(normpath(join(sublime.packages_path(), icon_path, option + "_close.png"))):
            close_icon = "../%s/%s" % (icon_path, option + "_close")
        else:
            close_icon = icon
        if exists(normpath(join(sublime.packages_path(), icon_path, option + "_close_small.png"))):
            small_close_icon = "../%s/%s" % (icon_path, option + "_close_small")
        else:
            small_close_icon = small_icon

    return icon, small_icon, open_icon, small_open_icon, close_icon, small_close_icon


def exclude_bracket(enabled, filter_type, language_list, language):
    """
Exclude or include brackets based on filter lists.
"""

    exclude = True
    if enabled:
        # Black list languages
        if filter_type == 'blacklist':
            exclude = False
            if language != None:
                for item in language_list:
                    if language == item.lower():
                        exclude = True
                        break
        #White list languages
        elif filter_type == 'whitelist':
            if language != None:
                for item in language_list:
                    if language == item.lower():
                        exclude = False
                        break
    return exclude


class BhEventMgr(object):
    """
Object to manage when bracket events should be launched.
"""

    @classmethod
    def load(cls):
        """
Initialize variables for determining
when to initiate a bracket matching event.
"""

        cls.wait_time = 0.12
        cls.time = time()
        cls.modified = False
        cls.type = BH_MATCH_TYPE_SELECTION
        cls.ignore_all = False

BhEventMgr.load()


class BhThreadMgr(object):
    """
Object to help track when a new thread needs to be started.
"""

    restart = False


class BhEntry(object):
    """
Generic object for bracket regions.
"""

    def move(self, begin, end):
        """
Create a new object with the points moved to the specified locations.
"""

        return self._replace(begin=begin, end=end)

    def size(self):
        """
Size of bracket selection.
"""

        return abs(self.begin - self.end)

    def toregion(self):
        """
Convert to sublime Region.
"""

        return sublime.Region(self.begin, self.end)


class BracketEntry(namedtuple('BracketEntry', ['begin', 'end', 'type'], verbose=False), BhEntry):
    """
Bracket object.
"""

    pass


class ScopeEntry(namedtuple('ScopeEntry', ['begin', 'end', 'scope', 'type'], verbose=False), BhEntry):
    """
Scope bracket object.
"""

    pass


class BracketSearchSide(object):
    """
Userful structure to specify bracket matching direction.
"""

    left = 0
    right = 1


class BracektSearchType(object):
    """
Userful structure to specify bracket matching direction.
"""

    opening = 0
    closing = 1


class BracketSearch(object):
    """
Object that performs regex search on the view's buffer and finds brackets.
"""

    def __init__(self, bfr, window, center, pattern, scope_check, scope):
        """
Prepare the search object
"""

        self.center = center
        self.pattern = pattern
        self.bfr = bfr
        self.scope = scope
        self.scope_check = scope_check
        self.prev_match = [None, None]
        self.return_prev = [False, False]
        self.done = [False, False]
        self.start = [None, None]
        self.left = [[], []]
        self.right = [[], []]
        self.findall(window)

    def reset_end_state(self):
        """
Reset the the current search flags etc.
This is usually done before searching the other direction.
"""

        self.start = [None, None]
        self.done = [False, False]
        self.prev_match = [None, None]
        self.return_prev = [False, False]

    def remember(self, match_type):
        """
Remember the current match.
Don't get the next bracket on the next
request, but return the current one again.
"""

        self.return_prev[match_type] = True
        self.done[match_type] = False

    def findall(self, window):
        """
Find all of the brackets and sort them
to "left of the cursor" and "right of the cursor"
"""

        for m in self.pattern.finditer(self.bfr, window[0], window[1]):
            g = m.lastindex
            try:
                start = m.start(g)
                end = m.end(g)
            except:
                continue

            match_type = int(not bool(g % 2))
            bracket_id = (g / 2) - match_type

            if not self.scope_check(start, bracket_id, self.scope):
                if (end <= self.center if match_type else start < self.center):
                    self.left[match_type].append(BracketEntry(start, end, bracket_id))
                elif (end > self.center if match_type else start >= self.center):
                    self.right[match_type].append(BracketEntry(start, end, bracket_id))

    def get_open(self, bracket_code):
        """
Get opening bracket. Accepts a bracket code that
determines which side of the cursor the next match is returned from.
"""

        for b in self._get_bracket(bracket_code, BracektSearchType.opening):
            yield b

    def get_close(self, bracket_code):
        """
Get closing bracket. Accepts a bracket code that
determines which side of the cursor the next match is returned from.
"""

        for b in self._get_bracket(bracket_code, BracektSearchType.closing):
            yield b

    def is_done(self, match_type):
        """
Retrieve done flag.
"""

        return self.done[match_type]

    def _get_bracket(self, bracket_code, match_type):
        """
Get the next bracket. Accepts bracket code that determines
which side of the cursor the next match is returned from and
the match type which determines whether a opening or closing
bracket is desired.
"""

        if self.done[match_type]:
            return
        if self.return_prev[match_type]:
            self.return_prev[match_type] = False
            yield self.prev_match[match_type]
        if bracket_code == BracketSearchSide.left:
            if self.start[match_type] is None:
                self.start[match_type] = len(self.left[match_type])
            for x in reversed(range(0, self.start[match_type])):
                b = self.left[match_type][x]
                self.prev_match[match_type] = b
                self.start[match_type] -= 1
                yield b
        else:
            if self.start[match_type] is None:
                self.start[match_type] = 0
            for x in range(self.start[match_type], len(self.right[match_type])):
                b = self.right[match_type][x]
                self.prev_match[match_type] = b
                self.start[match_type] += 1
                yield b

        self.done[match_type] = True


class BracketDefinition(object):
    """
Normal bracket definition.
"""

    def __init__(self, bracket):
        """
Setup the bracket object by reading the passed in dictionary.
"""

        self.name = bracket["name"]
        self.style = bracket.get("style", "default")
        self.compare = bracket.get("compare")
        sub_search = bracket.get("find_in_sub_search", "false")
        self.find_in_sub_search_only = sub_search == "only"
        self.find_in_sub_search = sub_search == "true" or self.find_in_sub_search_only
        self.post_match = bracket.get("post_match")
        self.scope_exclude_exceptions = bracket.get("scope_exclude_exceptions", [])
        self.scope_exclude = bracket.get("scope_exclude", [])
        self.ignore_string_escape = bracket.get("ignore_string_escape", False)


class ScopeDefinition(object):
    """
Scope bracket definition.
"""

    def __init__(self, bracket):
        """
Setup the bracket object by reading the passed in dictionary.
"""

        self.style = bracket.get("style", "default")
        self.open = re.compile("\\A" + bracket.get("open", "."), re.MULTILINE | re.IGNORECASE)
        self.close = re.compile(bracket.get("close", ".") + "\\Z", re.MULTILINE | re.IGNORECASE)
        self.name = bracket["name"]
        sub_search = bracket.get("sub_bracket_search", "false")
        self.sub_search_only = sub_search == "only"
        self.sub_search = self.sub_search_only == True or sub_search == "true"
        self.compare = bracket.get("compare")
        self.post_match = bracket.get("post_match")
        self.scopes = bracket["scopes"]


class StyleDefinition(object):
    """
Styling definition.
"""

    def __init__(self, name, style, default_highlight, icon_path):
        """
Setup the style object by reading the
passed in dictionary. And other parameters.
"""

        self.name = name
        self.selections = []
        self.open_selections = []
        self.close_selections = []
        self.center_selections = []
        self.color = style.get("color", default_highlight["color"])
        self.style = select_bracket_style(style.get("style", default_highlight["style"]))
        self.underline = self.style & sublime.DRAW_EMPTY_AS_OVERWRITE
        (
            self.icon, self.small_icon, self.open_icon,
            self.small_open_icon, self.close_icon, self.small_close_icon
        ) = select_bracket_icons(style.get("icon", default_highlight["icon"]), icon_path)
        self.no_icon = ""


class BhToggleStringEscapeModeCommand(sublime_plugin.TextCommand):
    """
Toggle between regex escape and
string escape for brackets in strings.
"""

    def run(self, edit):
        default_mode = sublime.load_settings("bh_core.sublime-settings").get('bracket_string_escape_mode', 'string')
        if self.view.settings().get('bracket_string_escape_mode', default_mode) == "regex":
            self.view.settings().set('bracket_string_escape_mode', "string")
            sublime.status_message("Bracket String Escape Mode: string")
        else:
            self.view.settings().set('bracket_string_escape_mode', "regex")
            sublime.status_message("Bracket String Escape Mode: regex")


class BhShowStringEscapeModeCommand(sublime_plugin.TextCommand):
    """
Shoe current string escape mode for sub brackets in strings.
"""

    def run(self, edit):
        default_mode = sublime.load_settings("BracketHighlighter.sublime-settings").get('bracket_string_escape_mode', 'string')
        sublime.status_message("Bracket String Escape Mode: %s" % self.view.settings().get('bracket_string_escape_mode', default_mode))


class BhToggleHighVisibilityCommand(sublime_plugin.ApplicationCommand):
    """
Toggle a high visibility mode that
highlights the entire bracket extent.
"""

    def run(self):
        global HIGH_VISIBILITY
        HIGH_VISIBILITY = not HIGH_VISIBILITY


class BhToggleEnableCommand(sublime_plugin.ApplicationCommand):
    """
Toggle global enable for BracketHighlighter.
"""

    def run(self):
        global GLOBAL_ENABLE
        GLOBAL_ENABLE = not GLOBAL_ENABLE


class BhKeyCommand(sublime_plugin.WindowCommand):
    """
Command to process shortcuts, menu calls, and command palette calls.
This is how BhCore is called with different options.
"""

    def run(self, threshold=True, lines=False, adjacent=False, ignore={}, plugin={}):
        # Override events
        BhEventMgr.ignore_all = True
        BhEventMgr.modified = False
        BhCore(
            threshold,
            lines,
            adjacent,
            ignore,
            plugin,
            True
        ).match(self.window.active_view())
        # Reset event settings
        BhEventMgr.ignore_all = False
        BhEventMgr.time = time()


class BhCore(object):
    """
Bracket matching class.
"""

    def __init__(self, override_thresh=False, count_lines=False, adj_only=None, ignore={}, plugin={}, keycommand=False):
        """
Load settings and setup reload events if settings changes.
"""
        self.settings = sublime.load_settings("bh_core.sublime-settings")
        if not keycommand:
            self.settings.clear_on_change('reload')
            self.settings.add_on_change('reload', self.setup)
        self.setup(override_thresh, count_lines, adj_only, ignore, plugin)

    def setup(self, override_thresh=False, count_lines=False, adj_only=None, ignore={}, plugin={}):
        """
Initialize class settings from settings file and inputs.
"""

        # Init view params
        self.last_id_view = None
        self.last_id_sel = None
        self.view_tracker = (None, None)
        self.ignore_threshold = override_thresh
        self.adj_only = adj_only if adj_only is not None else bool(self.settings.get("match_only_adjacent", False))
        self.auto_selection_threshold = int(self.settings.get("auto_selection_threshold", 10))
        self.no_multi_select_icons = bool(self.settings.get("no_multi_select_icons", False))
        self.count_lines = count_lines
        self.default_string_escape_mode = str(self.settings.get('bracket_string_escape_mode', "string"))
        self.show_unmatched = bool(self.settings.get("show_unmatched", True))

        # Init bracket objects
        self.bracket_types = self.settings.get("brackets", [])
        self.scope_types = self.settings.get("scope_brackets", [])

        # Init selection params
        self.use_selection_threshold = True
        self.selection_threshold = int(self.settings.get("search_threshold", 5000))
        self.new_select = False
        self.loaded_modules = set([])

        # High Visibility options
        self.hv_style = select_bracket_style(self.settings.get("high_visibility_style", "outline"))
        self.hv_underline = self.hv_style & sublime.DRAW_EMPTY_AS_OVERWRITE
        self.hv_color = self.settings.get("high_visibility_color", HV_RSVD_VALUES[1])

        # Init plugin
        self.plugin = None
        self.transform = set([])
        if 'command' in plugin:
            self.plugin = BracketPlugin(plugin, self.loaded_modules)
            self.new_select = True
            if 'type' in plugin:
                for t in plugin["type"]:
                    self.transform.add(t)

    def init_bracket_regions(self):
        """
Load up styled regions for brackets to use.
"""

        self.bracket_regions = {}
        styles = self.settings.get("bracket_styles", DEFAULT_STYLES)
        icon_path = self.settings.get("icon_path", "Theme - Default").replace('\\', '/').strip('/')
        # Make sure default and unmatched styles in styles
        for key, value in DEFAULT_STYLES.items():
            if key not in styles:
                styles[key] = value
                continue
            for k, v in value.items():
                if k not in styles[key]:
                    styles[key][k] = v
        # Initialize styles
        default_settings = styles["default"]
        for k, v in styles.items():
            self.bracket_regions[k] = StyleDefinition(k, v, default_settings, icon_path)

    def is_valid_definition(self, params, language):
        """
Ensure bracket definition should be and can be loaded.
"""

        return (
            not exclude_bracket(
                params.get("enabled", True),
                params.get("language_filter", "blacklist"),
                params.get("language_list", []),
                language
            ) and
            params["open"] is not None and params["close"] is not None
        )

    def init_brackets(self, language):
        """
Initialize bracket match definition objects from settings file.
"""

        self.find_regex = []
        self.sub_find_regex = []
        self.index_open = {}
        self.index_close = {}
        self.brackets = []
        self.scopes = []
        self.view_tracker = (language, self.view.id())
        self.enabled = False
        self.sels = []
        self.multi_select = False
        scopes = {}
        loaded_modules = self.loaded_modules.copy()

        for params in self.bracket_types:
            if self.is_valid_definition(params, language):
                try:
                    load_modules(params, loaded_modules)
                    entry = BracketDefinition(params)
                    self.brackets.append(entry)
                    if not entry.find_in_sub_search_only:
                        self.find_regex.append(params["open"])
                        self.find_regex.append(params["close"])
                    else:
                        self.find_regex.append(r"([^\s\S])")
                        self.find_regex.append(r"([^\s\S])")

                    if entry.find_in_sub_search:
                        self.sub_find_regex.append(params["open"])
                        self.sub_find_regex.append(params["close"])
                    else:
                        self.sub_find_regex.append(r"([^\s\S])")
                        self.sub_find_regex.append(r"([^\s\S])")
                except Exception, e:
                    bh_logging(e)

        scope_count = 0
        for params in self.scope_types:
            if self.is_valid_definition(params, language):
                try:
                    load_modules(params, loaded_modules)
                    entry = ScopeDefinition(params)
                    for x in entry.scopes:
                        if x not in scopes:
                            scopes[x] = scope_count
                            scope_count += 1
                            self.scopes.append({"name": x, "brackets": [entry]})
                        else:
                            self.scopes[scopes[x]]["brackets"].append(entry)
                except Exception, e:
                    bh_logging(e)

        if len(self.brackets):
            bh_debug(
                "BracketHighlighter: Search patterns:\n" +
                "(?:%s)\n" % '|'.join(self.find_regex) +
                "(?:%s)" % '|'.join(self.sub_find_regex)
            )
            self.sub_pattern = re.compile("(?:%s)" % '|'.join(self.sub_find_regex), re.MULTILINE | re.IGNORECASE)
            self.pattern = re.compile("(?:%s)" % '|'.join(self.find_regex), re.MULTILINE | re.IGNORECASE)
            self.enabled = True

    def init_match(self):
        """
Initialize matching for the current view's syntax.
"""

        self.chars = 0
        self.lines = 0
        syntax = self.view.settings().get('syntax')
        language = basename(syntax).replace('.tmLanguage', '').lower() if syntax != None else "plain text"

        if language != self.view_tracker[0] or self.view.id() != self.view_tracker[1]:
            self.init_bracket_regions()
            self.init_brackets(language)
        else:
            for r in self.bracket_regions.values():
                r.selections = []
                r.open_selections = []
                r.close_selections = []
                r.center_selections = []

    def unique(self):
        """
Check if the current selection(s) is different from the last.
"""

        id_view = self.view.id()
        id_sel = "".join([str(sel.a) for sel in self.view.sel()])
        is_unique = False
        if id_view != self.last_id_view or id_sel != self.last_id_sel:
            self.last_id_view = id_view
            self.last_id_sel = id_sel
            is_unique = True
        return is_unique

    def store_sel(self, regions):
        """
Store the current selection selection to be set at the end.
"""

        if self.new_select:
            for region in regions:
                self.sels.append(region)

    def change_sel(self):
        """
Change the view's selections.
"""

        if self.new_select and len(self.sels) > 0:
            if self.multi_select == False:
                self.view.show(self.sels[0])
            self.view.sel().clear()
            map(lambda x: self.view.sel().add(x), self.sels)

    def hv_highlight_color(self, b_value):
        """
High visibility highlight decesions.
"""

        color = self.hv_color
        if self.hv_color == HV_RSVD_VALUES[0]:
            color = self.bracket_regions["default"].color
        elif self.hv_color == HV_RSVD_VALUES[1]:
            color = b_value
        return color

    def highlight_regions(self, name, icon_type, selections, bracket, regions):
        """
Apply the highlightes for the highlight region.
"""

        if len(selections):
            self.view.add_regions(
                name,
                getattr(bracket, selections),
                self.hv_highlight_color(bracket.color) if HIGH_VISIBILITY else bracket.color,
                getattr(bracket, icon_type),
                self.hv_style if HIGH_VISIBILITY else bracket.style
            )
            regions.append(name)

    def highlight(self, view):
        """
Highlight all bracket regions.
"""

        for region_key in self.view.settings().get("bh_regions", []):
            self.view.erase_regions(region_key)

        regions = []
        icon_type = "no_icon"
        open_icon_type = "no_icon"
        close_icon_type = "no_icon"
        if not self.no_multi_select_icons or not self.multi_select:
            icon_type = "small_icon" if self.view.line_height() < 16 else "icon"
            open_icon_type = "small_open_icon" if self.view.line_height() < 16 else "open_icon"
            close_icon_type = "small_close_icon" if self.view.line_height() < 16 else "close_icon"
        for name, r in self.bracket_regions.items():
            self.highlight_regions("bh_" + name, icon_type, "selections", r, regions)
            self.highlight_regions("bh_" + name + "_center", "no_icon", "center_selections", r, regions)
            self.highlight_regions("bh_" + name + "_open", open_icon_type, "open_selections", r, regions)
            self.highlight_regions("bh_" + name + "_close", close_icon_type, "close_selections", r, regions)
        # Track which regions were set in the view so that they can be cleaned up later.
        self.view.settings().set("bh_regions", regions)

    def get_search_bfr(self, sel):
        """
Read in the view's buffer for scanning for brackets etc.
"""

        # Determine how much of the buffer to search
        view_min = 0
        view_max = self.view.size()
        if not self.ignore_threshold:
            left_delta = sel.a - view_min
            right_delta = view_max - sel.a
            limit = self.selection_threshold / 2
            rpad = limit - left_delta if left_delta < limit else 0
            lpad = limit - right_delta if right_delta < limit else 0
            llimit = limit + lpad
            rlimit = limit + rpad
            self.search_window = (
                sel.a - llimit if left_delta >= llimit else view_min,
                sel.a + rlimit if right_delta >= rlimit else view_max
            )
        else:
            self.search_window = (0, view_max)

        # Search Buffer
        return self.view.substr(sublime.Region(0, view_max))

    def match(self, view, force_match=True):
        """
Preform matching brackets surround the selection(s)
"""

        if view == None:
            return
        if not GLOBAL_ENABLE:
            for region_key in view.settings().get("bh_regions", []):
                view.erase_regions(region_key)
            return

        # Setup views
        self.view = view
        self.last_view = view
        num_sels = len(view.sel())
        self.multi_select = (num_sels > 1)

        if self.unique() or force_match:
            # Initialize
            self.init_match()

            # Nothing to search for
            if not self.enabled:
                return

            # Abort if selections are beyond the threshold
            if self.use_selection_threshold and num_sels >= self.selection_threshold:
                self.highlight(view)
                return

            multi_select_count = 0
            # Process selections.
            for sel in view.sel():
                bfr = self.get_search_bfr(sel)
                if not self.ignore_threshold and multi_select_count >= self.auto_selection_threshold:
                    self.store_sel([sel])
                    multi_select_count += 1
                    continue
                if not self.find_scopes(bfr, sel):
                    self.sub_search_mode = False
                    self.find_matches(bfr, sel)
                multi_select_count += 1

        # Highlight, focus, and display lines etc.
        self.change_sel()
        self.highlight(view)
        if self.count_lines:
            sublime.status_message('In Block: Lines ' + str(self.lines) + ', Chars ' + str(self.chars))

    def save_incomplete_regions(self, left, right, regions):
        """
Store single incomplete brackets for highlighting.
"""

        found = left if left is not None else right
        bracket = self.bracket_regions["unmatched"]
        if bracket.underline:
            bracket.selections += underline((found.toregion(),))
        else:
            bracket.selections += [found.toregion()]
        self.store_sel(regions)

    def save_regions(self, left, right, regions):
        """
Saved matched regions. Perform any special considerations for region formatting.
"""

        bracket = self.bracket_regions.get(self.bracket_style, self.bracket_regions["default"])
        lines = abs(self.view.rowcol(right.begin)[0] - self.view.rowcol(left.end)[0] + 1)
        if self.count_lines:
            self.chars += abs(right.begin - left.end)
            self.lines += lines
        if HIGH_VISIBILITY:
            if lines <= 1:
                if self.hv_underline:
                    bracket.selections += underline((sublime.Region(left.begin, right.end),))
                else:
                    bracket.selections += [sublime.Region(left.begin, right.end)]
            else:
                bracket.open_selections += [sublime.Region(left.begin)]
                if self.hv_underline:
                    bracket.center_selections += underline((sublime.Region(left.begin + 1, right.end - 1),))
                else:
                    bracket.center_selections += [sublime.Region(left.begin, right.end)]
                bracket.close_selections += [sublime.Region(right.begin)]
        elif bracket.underline:
            if lines <= 1:
                bracket.selections += underline((left.toregion(), right.toregion()))
            else:
                bracket.open_selections += [sublime.Region(left.begin)]
                bracket.close_selections += [sublime.Region(right.begin)]
                if left.size():
                    bracket.center_selections += underline((sublime.Region(left.begin + 1, left.end),))
                if right.size():
                    bracket.center_selections += underline((sublime.Region(right.begin + 1, right.end),))
        else:
            if lines <= 1:
                bracket.selections += [left.toregion(), right.toregion()]
            else:
                bracket.open_selections += [left.toregion()]
                bracket.close_selections += [right.toregion()]
        self.store_sel(regions)

    def sub_search(self, sel, search_window, bfr, scope=None):
        """
Search a scope bracket match for bracekts within.
"""

        bracket = None
        left, right = self.match_brackets(bfr, search_window, sel, scope)

        regions = [sublime.Region(sel.a, sel.b)]

        if left is not None and right is not None:
            bracket = self.brackets[left.type]
            left, right, regions = self.run_plugin(bracket.name, left, right, regions)

        # Matched brackets
        if left is not None and right is not None and bracket is not None:
            self.save_regions(left, right, regions)
            return True
        return False

    def find_scopes(self, bfr, sel):
        """
Find brackets by scope definition.
"""

        # Search buffer
        left, right, bracket, sub_matched = self.match_scope_brackets(bfr, sel)
        if sub_matched:
            return True
        regions = [sublime.Region(sel.a, sel.b)]

        if left is not None and right is not None:
            left, right, regions = self.run_plugin(bracket.name, left, right, regions)
            if left is None and right is None:
                self.store_sel(regions)
                return True

        if left is not None and right is not None:
            self.save_regions(left, right, regions)
            return True
        elif (left is not None or right is not None) and self.show_invalid:
            self.save_incomplete_regions(left, right, regions)
            return True
        return False

    def find_matches(self, bfr, sel):
        """
Find bracket matches
"""

        bracket = None
        left, right = self.match_brackets(bfr, self.search_window, sel)

        regions = [sublime.Region(sel.a, sel.b)]

        if left is not None and right is not None:
            bracket = self.brackets[left.type]
            left, right, regions = self.run_plugin(bracket.name, left, right, regions)

        # Matched brackets
        if left is not None and right is not None and bracket is not None:
            self.save_regions(left, right, regions)

        # Unmatched brackets
        elif (left is not None or right is not None) and self.show_unmatched:
            self.save_incomplete_regions(left, right, regions)

        else:
            self.store_sel(regions)

    def escaped(self, pt, ignore_string_escape, scope):
        """
Check if sub bracket in string scope is escaped.
"""

        if not ignore_string_escape:
            return False
        if scope and scope.startswith("string"):
            return self.string_escaped(pt)
        return False

    def string_escaped(self, pt):
        """
Check if bracket is follows escaping characters.
Account for if in string or regex string scope.
"""

        escaped = False
        start = pt - 1
        first = False
        if self.view.settings().get("bracket_string_escape_mode", self.default_string_escape_mode) == "string":
            first = True
        while self.view.substr(start) == "\\":
            if first:
                first = False
            else:
                escaped = False if escaped else True
            start -= 1
        return escaped

    def is_illegal_scope(self, pt, bracket_id, scope=None):
        """
Check if scope at pt X should be ignored.
"""

        bracket = self.brackets[bracket_id]
        if self.sub_search_mode and not bracket.find_in_sub_search:
            return True
        illegal_scope = False
        # Scope sent in, so we must be scanning whatever this scope is
        if scope != None:
            if self.escaped(pt, bracket.ignore_string_escape, scope):
                illegal_scope = True
            return illegal_scope
        # for exception in bracket.scope_exclude_exceptions:
        elif len(bracket.scope_exclude_exceptions) and self.view.match_selector(pt, ", ".join(bracket.scope_exclude_exceptions)):
            pass
        elif len(bracket.scope_exclude) and self.view.match_selector(pt, ", ".join(bracket.scope_exclude)):
            illegal_scope = True
        return illegal_scope

    def compare(self, first, second, bfr, scope_bracket=False):
        """
Compare brackets. This function allows bracket plugins to add aditional logic.
"""

        if scope_bracket:
            match = first is not None and second is not None
        else:
            match = first.type == second.type
        if match:
            bracket = self.scopes[first.scope]["brackets"][first.type] if scope_bracket else self.brackets[first.type]
            try:
                if bracket.compare is not None and match:
                    match = bracket.compare(
                        bracket.name,
                        BracketRegion(first.begin, first.end),
                        BracketRegion(second.begin, second.end),
                        bfr
                    )
            except:
                bh_logging("BracketHighlighter: Plugin Compare Error:\n%s" % str(traceback.format_exc()))
        return match

    def post_match(self, left, right, center, bfr, scope_bracket=False):
        """
Peform special logic after a match has been made.
This function allows bracket plugins to add aditional logic.
"""

        if left is not None:
            if scope_bracket:
                bracket = self.scopes[left.scope]["brackets"][left.type]
                bracket_scope = left.scope
            else:
                bracket = self.brackets[left.type]
            bracket_type = left.type
        elif right is not None:
            if scope_bracket:
                bracket = self.scopes[right.scope]["brackets"][right.type]
                bracket_scope = right.scope
            else:
                bracket = self.brackets[right.type]
            bracket_type = right.type
        else:
            return left, right

        self.bracket_style = bracket.style

        if bracket.post_match is not None:
            try:
                lbracket, rbracket, self.bracket_style = bracket.post_match(
                    self.view,
                    bracket.name,
                    bracket.style,
                    BracketRegion(left.begin, left.end) if left is not None else None,
                    BracketRegion(right.begin, right.end) if right is not None else None,
                    center,
                    bfr,
                    self.search_window
                )

                if scope_bracket:
                    left = ScopeEntry(lbracket.begin, lbracket.end, bracket_scope, bracket_type) if lbracket is not None else None
                    right = ScopeEntry(rbracket.begin, rbracket.end, bracket_scope, bracket_type) if rbracket is not None else None
                else:
                    left = BracketEntry(lbracket.begin, lbracket.end, bracket_type) if lbracket is not None else None
                    right = BracketEntry(rbracket.begin, rbracket.end, bracket_type) if rbracket is not None else None
            except:
                bh_logging("BracketHighlighter: Plugin Post Match Error:\n%s" % str(traceback.format_exc()))
        return left, right

    def run_plugin(self, name, left, right, regions):
        """
Run a bracket plugin.
"""

        lbracket = BracketRegion(left.begin, left.end)
        rbracket = BracketRegion(right.begin, right.end)

        if (
            ("__all__" in self.transform or name in self.transform) and
            self.plugin != None and
            self.plugin.is_enabled()
        ):
            lbracket, rbracket, regions = self.plugin.run_command(self.view, name, lbracket, rbracket, regions)
            left = left.move(lbracket.begin, lbracket.end) if lbracket is not None else None
            right = right.move(rbracket.begin, rbracket.end) if rbracket is not None else None
        return left, right, regions

    def match_scope_brackets(self, bfr, sel):
        """
See if scope should be searched, and then check
endcaps to determine if valid scope bracket.
"""

        center = sel.a
        left = None
        right = None
        scope_count = 0
        before_center = center - 1
        bracket_count = 0
        partial_find = None
        max_size = self.view.size() - 1
        selected_scope = None
        bracket = None

        # Cannot be inside a bracket pair if cursor is at zero
        if center == 0:
            return left, right, selected_scope, False

        # Identify if the cursor is in a scope with bracket definitions
        for s in self.scopes:
            scope = s["name"]
            extent = None
            exceed_limit = False
            if self.view.match_selector(center, scope) and self.view.match_selector(before_center, scope):
                extent = self.view.extract_scope(center)
                while not exceed_limit and extent.begin() != 0:
                    if self.view.match_selector(extent.begin() - 1, scope):
                        extent = extent.cover(self.view.extract_scope(extent.begin() - 1))
                        if extent.begin() < self.search_window[0] or extent.end() > self.search_window[1]:
                            extent = None
                            exceed_limit = True
                    else:
                        break
                while not exceed_limit and extent.end() != max_size:
                    if self.view.match_selector(extent.end(), scope):
                        extent = extent.cover(self.view.extract_scope(extent.end()))
                        if extent.begin() < self.search_window[0] or extent.end() > self.search_window[1]:
                            extent = None
                            exceed_limit = True
                    else:
                        break

            if extent is None:
                scope_count += 1
                continue

            # Search the bracket patterns of this scope
            # to determine if this scope matches the rules.
            bracket_count = 0
            scope_bfr = bfr[extent.begin():extent.end()]
            for b in s["brackets"]:
                m = b.open.search(scope_bfr)
                if m and m.group(1):
                    left = ScopeEntry(extent.begin() + m.start(1), extent.begin() + m.end(1), scope_count, bracket_count)
                m = b.close.search(scope_bfr)
                if m and m.group(1):
                    right = ScopeEntry(extent.begin() + m.start(1), extent.begin() + m.end(1), scope_count, bracket_count)
                if not self.compare(left, right, bfr, scope_bracket=True):
                    left, right = None, None
                # Track partial matches. If a full match isn't found,
                # return the first partial match at the end.
                if partial_find is None and bool(left) != bool(right):
                    partial_find = (left, right)
                    left = None
                    right = None
                if left and right:
                    break
                bracket_count += 1
            if left and right:
                break
            scope_count += 1

        # Full match not found. Return partial match (if any).
        if (left is None or right is None) and partial_find is not None:
            left, right = partial_find[0], partial_find[1]

        # Make sure cursor in highlighted sub group
        if (left and center <= left.begin) or (right and center >= right.end):
            left, right = None, None

        if left is not None:
            selected_scope = self.scopes[left.scope]["name"]
        elif right is not None:
            selected_scope = self.scopes[right.scope]["name"]

        if left is not None and right is not None:
            bracket = self.scopes[left.scope]["brackets"][left.type]
            if bracket.sub_search:
                self.sub_search_mode = True
                if self.sub_search(sel, (left.begin, right.end), bfr, scope):
                    return left, right, self.brackets[left.type], True
                elif bracket.sub_search_only:
                    left, right, bracket = None, None, None

        if self.adj_only:
            left, right = self.adjacent_check(left, right, center)

        left, right = self.post_match(left, right, center, bfr, scope_bracket=True)
        return left, right, bracket, False

    def match_brackets(self, bfr, window, sel, scope=None):
        """
Regex bracket matching.
"""

        center = sel.a
        left = None
        right = None
        stack = []
        pattern = self.pattern if not self.sub_search_mode else self.sub_pattern
        bsearch = BracketSearch(bfr, window, center, pattern, self.is_illegal_scope, scope)
        for o in bsearch.get_open(BracketSearchSide.left):
            if len(stack) and bsearch.is_done(BracektSearchType.closing):
                if self.compare(o, stack[-1], bfr):
                    stack.pop()
                    continue
            for c in bsearch.get_close(BracketSearchSide.left):
                if o.end <= c.begin:
                    stack.append(c)
                    continue
                elif len(stack):
                    bsearch.remember(BracektSearchType.closing)
                    break

            if len(stack):
                b = stack.pop()
                if self.compare(o, b, bfr):
                    continue
            else:
                left = o
            break

        bsearch.reset_end_state()
        stack = []

        # Grab each closest closing right side bracket and attempt to match it.
        # If the closing bracket cannot be matched, select it.
        for c in bsearch.get_close(BracketSearchSide.right):
            if len(stack) and bsearch.is_done(BracektSearchType.opening):
                if self.compare(stack[-1], c, bfr):
                    stack.pop()
                    continue
            for o in bsearch.get_open(BracketSearchSide.right):
                if o.end <= c.begin:
                    stack.append(o)
                    continue
                else:
                    bsearch.remember(BracektSearchType.opening)
                    break

            if len(stack):
                b = stack.pop()
                if self.compare(b, c, bfr):
                    continue
            else:
                if left is None or self.compare(left, c, bfr):
                    right = c
            break

        if self.adj_only:
            left, right = self.adjacent_check(left, right, center)

        return self.post_match(left, right, center, bfr)

    def adjacent_check(self, left, right, center):
        if left and right:
            if left.end < center < right.begin:
                left, right = None, None
        elif (left and left.end < center) or (right and center < right.begin):
            left, right = None, None
        return left, right

bh_match = BhCore().match
bh_debug("BracketHighlighter: Match object loaded.")


class BhListenerCommand(sublime_plugin.EventListener):
    """
Manage when to kick off bracket matching.
Try and reduce redundant requests by letting the
background thread ensure certain needed match occurs
"""

    def on_load(self, view):
        """
Search brackets on view load.
"""

        if self.ignore_event(view):
            return
        BhEventMgr.type = BH_MATCH_TYPE_SELECTION
        sublime.set_timeout(bh_run, 0)

    def on_modified(self, view):
        """
Update highlighted brackets when the text changes.
"""

        if self.ignore_event(view):
            return
        BhEventMgr.type = BH_MATCH_TYPE_EDIT
        BhEventMgr.modified = True
        BhEventMgr.time = time()

    def on_activated(self, view):
        """
Highlight brackets when the view gains focus again.
"""

        if self.ignore_event(view):
            return
        BhEventMgr.type = BH_MATCH_TYPE_SELECTION
        sublime.set_timeout(bh_run, 0)

    def on_selection_modified(self, view):
        """
Highlight brackets when the selections change.
"""

        if self.ignore_event(view):
            return
        if BhEventMgr.type != BH_MATCH_TYPE_EDIT:
            BhEventMgr.type = BH_MATCH_TYPE_SELECTION
        now = time()
        if now - BhEventMgr.time > BhEventMgr.wait_time:
            sublime.set_timeout(bh_run, 0)
        else:
            BhEventMgr.modified = True
            BhEventMgr.time = now

    def ignore_event(self, view):
        """
Ignore request to highlight if the view is a widget,
or if it is too soon to accept an event.
"""

        return (view.settings().get('is_widget') or BhEventMgr.ignore_all)


def bh_run():
    """
Kick off matching of brackets
"""

    BhEventMgr.modified = False
    window = sublime.active_window()
    view = window.active_view() if window != None else None
    BhEventMgr.ignore_all = True
    bh_match(view, True if BhEventMgr.type == BH_MATCH_TYPE_EDIT else False)
    BhEventMgr.ignore_all = False
    BhEventMgr.time = time()


def bh_loop():
    """
Start thread that will ensure highlighting happens after a barage of events
Initial highlight is instant, but subsequent events in close succession will
be ignored and then accounted for with one match by this thread
"""

    while not BhThreadMgr.restart:
        if BhEventMgr.modified == True and time() - BhEventMgr.time > BhEventMgr.wait_time:
            sublime.set_timeout(bh_run, 0)
        sleep(0.5)

    if BhThreadMgr.restart:
        BhThreadMgr.restart = False
        sublime.set_timeout(lambda: thread.start_new_thread(bh_loop, ()), 0)

if not 'running_bh_loop' in globals():
    running_bh_loop = True
    thread.start_new_thread(bh_loop, ())
    bh_debug("BracketHighlighter: Starting Thread")
else:
    bh_debug("BracketHighlighter: Restarting Thread")
    BhThreadMgr.restart = True
Something went wrong with that request. Please try again.