Last active
February 24, 2021 01:42
-
-
Save skt041959/b1dc2ac63a1b9bfb46041294392ce98c to your computer and use it in GitHub Desktop.
denite source of coc locations, such as references, symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pathlib import Path | |
from denite.base.source import Base | |
class Source(Base): | |
def __init__(self, vim): | |
super().__init__(vim) | |
self.name = 'coc-locations' | |
self.kind = 'file' | |
def on_init(self, context): | |
self.locations = self.vim.vars['coc_jump_locations'] | |
self.pwd = self.vim.call("getcwd") | |
self.word = self.vim.call("expand", "<cword>") | |
def define_syntax(self): | |
self.vim.command(f"syntax match {self.syntax_name}_Position /" | |
r" \[.\{-}\]" | |
f"/ contained containedin={self.syntax_name}") | |
self.vim.command(f"syntax match {self.syntax_name}_Text /" | |
r">\s.*$" | |
f"/ contained containedin={self.syntax_name}") | |
self.vim.command(f"syntax match {self.syntax_name}_Word /" | |
f"\\<{self.word}\\>" | |
f"/ contained containedin={self.syntax_name}") | |
def highlight(self): | |
self.vim.command(f"highlight default link {self.syntax_name}_Position Comment") | |
self.vim.command(f"highlight default link {self.syntax_name}_Text String") | |
self.vim.command(f"highlight default link {self.syntax_name}_Word Operator") | |
def gather_candidates(self, context): | |
return [self._convert(e) for e in self.locations] | |
def _convert(self, element): | |
try: | |
relpath = Path(element["filename"]).relative_to(self.pwd).as_posix() | |
except ValueError: | |
relpath = os.path.relpath(element["filename"], self.pwd) | |
relpath = min([relpath, element["filename"]], key=len) | |
position = f"{element['lnum']}:{element['col']:>3}" | |
candidate = { | |
'word': element['text'], | |
'abbr': f'{relpath:<35.35} [{position:>10}] {element["text"]}', | |
'action__path': element['filename'], | |
'action__line': element['lnum'] | |
} | |
return candidate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment