'Determines if the nearest bracket that occurs before the given
selection is closed. If the bracket should be closed, returns the
closing bracket to use.
Note that this will not work if the arguments to the command span
multiple lines, but we generally don\'t support that anyway.
:param view:
the current view
:param sel:
a sublime.Region indicating the selected area'

def get_closing_bracket(self, view, sel):

    candidates = None
    if ((not hasattr(self, 'last_view')) or (self.last_view != view.id())):
        self.last_view = view.id()
        self.use_full_scan = get_setting('smart_bracket_scan_full_document', False)
        candidates = self.candidates = {}
    if (not self.use_full_scan):
        candidates = {}
        try:
            look_around = int(get_setting('smart_bracket_look_around', 5))
        except ValueError:
            look_around = 5
    if (candidates is None):
        try:
            candidates = self.candidates
        except:
            candidates = self.candidates = {}
    if (type(sel) is sublime.Region):
        (start, end) = (sel.begin(), sel.end())
    else:
        start = end = sel
    if self.use_full_scan:
        prefix = view.substr(getRegion(0, start))
        prefix_start = 0
        suffix_end = view.size()
    else:
        prefix_lines = view.lines(getRegion(0, start))
        if (len(prefix_lines) >= look_around):
            prefix_start = prefix_lines[(- look_around)].begin()
        else:
            prefix_start = prefix_lines[0].begin()
        suffix_lines = view.lines(getRegion(end, view.size()))
        if (len(suffix_lines) >= look_around):
            suffix_end = suffix_lines[(look_around - 1)].end()
        else:
            suffix_end = suffix_lines[(-1)].end()
        prefix = view.substr(getRegion(prefix_start, start))
    (open_bracket, last_index) = (None, (-1))
    for char in self.MATCH_CHARS:
        index = prefix.rfind(char)
        if (index > last_index):
            (open_bracket, last_index) = (char, index)
    if (last_index == (-1)):
        return None
    close_bracket = self.MATCH_CHARS[open_bracket]
    open_brackets = []
    closed_brackets = []
    if (open_bracket not in candidates):
        candidates[open_bracket] = results = []
        start = prefix_start
        re_str = ((re.escape(open_bracket) + '|') + re.escape(close_bracket))
        while True:
            if (start >= suffix_end):
                break
            c = view.find(re_str, start)
            if ((c is None) or (c.begin() == (-1))):
                break
            if (c.end() > suffix_end):
                break
            if (view.score_selector(c.begin(), 'comment') != 0):
                start = c.end()
                continue
            results.append(c)
            start = c.end()
    for candidate in candidates[open_bracket]:
        if (view.substr(candidate) == open_bracket):
            if ((len(open_brackets) == 0) and (candidate.begin() > end)):
                break
            open_brackets.append(candidate)
        else:
            try:
                removed = open_brackets.pop()
                if (candidate.end() < start):
                    closed_brackets.append(removed)
                    closed_brackets.append(candidate)
            except IndexError:
                if (candidate.end() > end):
                    break
    if (len(closed_brackets) > 0):
        candidates[open_bracket] = [c for c in candidates[open_bracket] if (c not in closed_brackets)]
    return (close_bracket if (len(open_brackets) > 0) else None)
