def _highlight_tags(self, tags, curr_line): """To highlight search results.""" vk_colors = settings.get("visual_kinds_colors") vk_shape = settings.get("visual_kinds_shape") indicator = settings.get("current_line_indicator") for i, tag in enumerate(tags): if i == curr_line: offset = len(indicator.encode(v.encoding())) else: offset = len(indicator) if settings.get("visual_kinds", bool): offset += len(vk_shape.encode(v.encoding())) kind = tag["exts"].get("kind") if kind in vk_colors: patt = u"\c\%{}l{}".format(i+1, vk_shape.replace(u"u",u"%u")) v.highlight("SurferVisualKind_" + kind, patt) patt = u"\c\%{}l\%{}c.*".format(i+1, offset+len(tag["name"])+1) v.highlight("SurferShade", patt) for pos in misc.as_byte_indexes(tag["match_positions"], tag["name"]): patt = u"\c\%{}l\%{}c.".format(i+1, offset+pos+1) v.highlight("SurferMatches", patt)
def _setup_buffer(self): """To set sane options for the search results buffer.""" last_search = "" if v.eval("@/"): last_search = v.eval("@/").decode(v.encoding()).replace(u'"', u'\\"') self.exit_cmds.extend([ u"let @/=\"{}\"".format(last_search), u"set laststatus={}".format(v.opt("ls")), u"set guicursor={}".format(v.opt("gcr")), ]) commands = [ "let @/ = ''", "call clearmatches()" ] options = [ "buftype=nofile", "bufhidden=wipe", "nobuflisted", "noundofile", "nobackup", "noswapfile", "nowrap", "nonumber", "nolist", "textwidth=0", "colorcolumn=0", "laststatus=0", "norelativenumber", "nocursorcolumn", "nospell", "foldcolumn=0", "foldcolumn=0", "guicursor=a:hor5-Cursor-blinkwait100", ] if settings.get("cursorline", bool): options.append("cursorline") else: options.append("nocursorline") for opt in options: v.exe("try|setl {}|catch|endtry".format(opt)) for cmd in commands: v.exe(cmd)
def as_byte_indexes(indexes, s): """To transform character indexes into byte indexes.""" enc = v.encoding() idx = 0 byte_indexes = [] for i, c in enumerate(s): if i in indexes: byte_indexes.append(idx) idx += len(c.encode(enc)) return byte_indexes
def get(name, type=None): """To get the value of a vim variable.""" rawval = v.eval(prefix + name) if type is bool: return False if rawval == '0' else True elif type is int: return int(rawval) elif type is float: return float(rawval) elif isinstance(rawval, basestring): return rawval.decode(v.encoding()) else: return rawval
def _tag_count(self, tag): """To pick the best tag candidate for a given tag name. The number retruned is meant to be used in conjunction with the :tag vim command (see :h :tag) """ enc = v.encoding() candidates = v.call(u'taglist("{}")'.format(tag["name"])) if len(candidates) == 1: return 1, candidates[0]["filename"].decode(enc) # group tags by file name groups = [] for fname, g in groupby(candidates, key=itemgetter("filename")): groups.append((fname, list(g))) groups.sort(key=itemgetter(0)) # sort tags by the `line` field (XXX: or `cmd`?); tags from of the # current buffer are put first. This is ensures that the `:[count]tag # [name]` command will work as expected (see :h tag-priority) ordered_candidates = [] for fname, tags in groups: sorted_tags = sorted(tags, key=itemgetter("line")) if fname.decode(enc) == v.bufname(): ordered_candidates = sorted_tags + ordered_candidates else: ordered_candidates.extend(sorted_tags) files = [c["filename"].decode(enc) for c in ordered_candidates] scores = [0]*len(ordered_candidates) for i, candidate in enumerate(ordered_candidates): if candidate["cmd"].decode(enc) == tag["cmd"]: scores[i] += 1 if candidate["name"].decode(enc) == tag["name"]: scores[i] += 1 if candidate["filename"].decode(enc) == tag["file"]: scores[i] += 1 if candidate["line"].decode(enc) == tag["exts"].get("line"): scores[i] += 1 if candidate["kind"].decode(enc) == tag["exts"].get("kind"): scores[i] += 1 if candidate["language"].decode(enc) == tag["exts"].get("language"): scores[i] += 1 idx = scores.index(max(scores)) return idx + 1, files[idx]