def store_buffer(self, buffer, base, cur_nr, cur_line): nr = buffer.number encoding = get_encoding(nr) if nr == cur_nr: start = cur_line - 1000 end = cur_line + 1000 if start < 0: start = 0 data = ' '.join( itertools.chain(buffer[start:cur_line], buffer[cur_line + 1:end])) self.current = set(self.pat.findall(to_unicode(data, encoding))) self.current.difference_update([base]) elif buffer.valid and len(buffer) <= 10000: ftime = getftime(nr) if ftime < 0: return if nr not in self.cache or ftime > self.cache[nr]['t']: self.cache[nr] = {'t': ftime} data = to_unicode(' '.join(buffer[:]), encoding) words = set(self.store) words.update(set(self.pat.findall(data))) self.store.clear() self.store.extend(words)
def test_unicode(vim_mod): buffer = Buffer(1) with open('./tests/test.txt') as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get('buffer') buf.input_data = to_unicode('pielę pielęgn', 'utf-8') assert buf.start_column() == 7 assert buf.get_completions(b'piel\xc4\x99gn') == [ {'menu': '[ID]', 'word': to_unicode('pielęgniarką', 'utf-8')}, {'menu': '[ID]', 'word': to_unicode('pielęgniarkach', 'utf-8')} ]
def test_on_data(vim_mod): omnifunc = mock.Mock() vim_mod.current.buffer.options['omnifunc'] = b'csscomplete#CompleteCSS' vim_mod.funcs[b'csscomplete#CompleteCSS'] = omnifunc vim_mod.current.window.cursor = (1, 2) vim_mod.buffers = [] omni = completor.get('omni') omni.trigger_cache = {} omni.ft = 'css' assert omni.on_data(b'complete', b'text') == [] omni.trigger_cache = { 'css': re.compile('([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$', re.X | re.U)} omnifunc.side_effect = [1, [b'text-transform']] assert omni.on_data(b'complete', b'#') == [] omnifunc.side_effect = [0, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = 'text' assert omni.on_data(b'complete', b'text') == [b'text-transform'] omnifunc.assert_called_with(0, b'text') omnifunc.side_effect = [17, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = to_unicode('które się nią opiekują', 'utf-8') omni.on_data(b'complete', omni.input_data) omnifunc.assert_called_with(0, b'opiekuj\xc4\x85')
def test_parse(vim_mod): items = [ b'COMPLETION: pclose : [#int#]pclose(<#FILE *#>)', b'COMPLETION: hello : [#int#]hello', b'COMPLETION: b : [#int#]b', b'COMPLETION: Pattern: fake hello', ] cpp = completor.get('cpp') cpp.input_data = to_unicode('b->', 'utf-8') assert cpp.on_data(b'complete', items) == [{ 'dup': 1, 'menu': b'int pclose(FILE *)', 'word': b'pclose' }, { 'dup': 1, 'menu': b'int hello', 'word': b'hello' }, { 'dup': 1, 'menu': b'int b', 'word': b'b' }, { 'dup': 1, 'menu': b'hello', 'word': b'fake' }]
def get_encoding(nr): try: getbufvar = vim.Function('getbufvar') encoding = getbufvar(nr, '&encoding') except vim.error: encoding = '' return to_unicode(encoding or 'utf-8', 'utf-8')
def parse(self, base): nr = vim.current.buffer.number encoding = get_encoding(nr) tags = vim.eval('taglist("{}")'.format(to_unicode(base, encoding))) names = list(set([tag['name'] for tag in tags])) return [{'word': name, 'menu': '[Tag]'} for name in sorted(names)]
def test_on_data(vim_mod): omnifunc = mock.Mock() vim_mod.current.buffer.options['omnifunc'] = b'csscomplete#CompleteCSS' vim_mod.funcs[b'csscomplete#CompleteCSS'] = omnifunc vim_mod.current.window.cursor = (1, 2) vim_mod.buffers = [] omni = completor.get('omni') omni.trigger_cache = {} omni.ft = 'css' assert omni.on_data(b'complete', b'text') == [] omni.trigger_cache = { 'css': re.compile('([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$', re.X | re.U) } omnifunc.side_effect = [1, [b'text-transform']] assert omni.on_data(b'complete', b'#') == [] omnifunc.side_effect = [0, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = 'text' assert omni.on_data(b'complete', b'text') == [b'text-transform'] omnifunc.assert_called_with(0, b'text') omnifunc.side_effect = [17, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = to_unicode('które się nią opiekują', 'utf-8') omni.on_data(b'complete', omni.input_data) omnifunc.assert_called_with(0, b'opiekuj\xc4\x85')
def parse(self, base): if not self.ft or not base or base.endswith((' ', '\t')): return [] if self.ft not in _cache: try: _cache[self.ft] = self._get_candidates() except Exception: _cache[self.ft] = [] # token = base.split()[-1] token = to_bytes(base, get_encoding())[self.start_column():] token = to_unicode(token, 'utf-8') if len(token) < self.get_option('min_chars'): return [] candidates = [ dict(item) for item in _cache[self.ft] if item['abbr'].startswith(token.encode('utf-8')) ] offset = len(to_bytes(base[:-len(token)], get_encoding())) for c in candidates: c['word'] = c['abbr'] c['offset'] = offset return candidates
def parse(self, data): try: data = to_unicode(data[0], 'utf-8') return [i for i in json.loads(data) if not self.input_data.endswith(i['word'])] except Exception: return []
def parse_data(self): remain = self.buf.getvalue() while True: try: parts = remain.split(b'\r\n\r\n', 1) if len(parts) != 2: break header = parts[0] body = parts[1] length = content_length(header) if length is None: logger.warning('no content-length') break if len(body) < length: break data = body[:length] remain = body[length:] logger.info("parsing %r", data) yield json.loads(to_unicode(data, 'utf-8')) except Exception as e: logger.exception(e) raise self.buf = io.BytesIO(remain) # Seek to end. self.buf.seek(0, 2)
def test_request(vim_mod): vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.buffer.name = '/home/vagrant/bench.vim' vim_mod.current.window.cursor = (1, 5) racer = completor.get('rust') racer.input_data = to_unicode('self.', 'utf-8') assert racer.prepare_request(b'complete') == \ 'complete 1 5 /home/vagrant/bench.vim /tmp/vJBio2A/2.vim'
def test_format_cmd(vim_mod): vim_mod.funcs['line2byte'] = mock.Mock(return_value=20) vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.window.cursor = (1, 5) go = completor.get('go') go.input_data = to_unicode('self.', 'utf-8') assert go.format_cmd() == [ 'gocode', '-f=csv', '--in=/tmp/vJBio2A/2.vim', 'autocomplete', 24]
def on_definition(self, items): data = to_unicode(b''.join(items), 'utf-8') spec = json.loads(data) if self.use_guru_for_def: pos = spec['objpos'] name = spec['desc'] else: pos = spec['pos'] name = spec['name'] path, lnum, col = pos.rsplit(':', 2) return [{'filename': path, 'lnum': lnum, 'col': col, 'name': name}]
def test_format_cmd(vim_mod): vim_mod.funcs['line2byte'] = mock.Mock(return_value=20) vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.window.cursor = (1, 5) go = completor.get('go') go.input_data = to_unicode('self.', 'utf-8') assert go.format_cmd() == [ 'gocode', '-f=csv', '--in=/tmp/vJBio2A/2.vim', 'autocomplete', 24 ]
def gen_entry(self, base): gather_candidates = vim.Function('necovim#gather_candidates') binput_data = to_bytes(self.input_data, get_encoding()) bbase = to_bytes(base, get_encoding()) candidates = gather_candidates(binput_data, bbase) for entry in candidates: score = test_subseq(base, to_unicode(entry[b'word'], 'utf-8')) if score is None: continue yield entry, score
def on_complete(self, data): try: data = to_unicode(data[0], 'utf-8') \ .replace('\\u', '\\\\\\u') \ .replace('\\U', '\\\\\\U') return [ i for i in json.loads(data) if not self.input_data.endswith(i['word']) ] except Exception as e: logger.exception(e) return []
def on_complete(self, data): try: data = to_unicode(data[0], 'utf-8') \ .replace('\\u', '\\\\\\u') \ .replace('\\U', '\\\\\\U') items = json.loads(data) logger.info('%r => %r', self.input_data, items) return [i for i in json.loads(data) if not self.input_data.endswith(i['word'])] except Exception as e: logger.exception(e) return []
def test_format_cmd(vim_mod): vim_mod.funcs['expand'] = mock.Mock(return_value=b'/tmp/vJBio2A') vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.cpp') vim_mod.current.window.cursor = (1, 5) cpp = completor.get('cpp') cpp.input_data = to_unicode('self.', 'utf-8') assert cpp.format_cmd() == [ 'clang', '-fsyntax-only', '-I', '/tmp/vJBio2A', '-Xclang', '-code-completion-macros', '-Xclang', '-code-completion-at=/tmp/vJBio2A/2.cpp:1:6', '/tmp/vJBio2A/2.cpp']
def test_get_cmd_info(vim_mod): vim_mod.funcs['expand'] = mock.Mock(return_value=b'/tmp/vJBio2A') vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.cpp') vim_mod.current.window.cursor = (1, 5) cpp = completor.get('cpp') cpp.input_data = to_unicode('self.', 'utf-8') assert cpp.get_cmd_info(b'complete')['cmd'] == [ 'clang', '-fsyntax-only', '-I', '/tmp/vJBio2A', '-Xclang', '-code-completion-macros', '-Xclang', '-code-completion-at=/tmp/vJBio2A/2.cpp:1:6', '/tmp/vJBio2A/2.cpp']
def test_parse(): items = [ b'MATCH FrameHandler,155,7,./src/event.rs,Struct,struct FrameHandler', b'MATCH tcp_port,1075,4,./src/event.rs,StructField,Option<u16>', b'MATCH run,1092,11,./src/event.rs,Function,pub fn run(&mut self)' ] racer = completor.get('rust') racer.input_data = to_unicode('self.', 'utf-8') assert racer.on_data(b'complete', items) == [ {'offset': 5, 'dup': 0, 'menu': b'struct FrameHandler', 'word': b'FrameHandler'}, # noqa {'offset': 5, 'dup': 0, 'menu': b'Option<u16>', 'word': b'tcp_port'}, {'offset': 5, 'dup': 0, 'menu': b'fn run(&mut self)', 'word': b'run'} ]
def test_unicode(vim_mod): buffer = Buffer(1) with open('./tests/test.txt') as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get('buffer') buf.input_data = to_unicode('pielę pielęgn', 'utf-8') assert buf.start_column() == 7 assert buf.on_data(b'complete', b'piel\xc4\x99gn') == [{ 'menu': '[ID]', 'word': to_unicode('pielęgniarką', 'utf-8') }, { 'menu': '[ID]', 'word': to_unicode('pielęgniarkach', 'utf-8') }]
def test_unicode(vim_mod, create_buffer): buffer = create_buffer(1) with open("./tests/test.txt") as f: buffer[:] = f.read().split("\n") vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get("buffer") buf.input_data = to_unicode("pielę pielęgn", "utf-8") assert buf.start_column() == 7 assert buf.on_data(b"complete", b"piel\xc4\x99gn") == [ { "offset": 0, "menu": "[ID]", "word": to_unicode("pielęgniarką", "utf-8"), }, { "offset": 0, "menu": "[ID]", "word": to_unicode("pielęgniarkach", "utf-8"), }, ]
def parse(self, items): res = [] prefix = '' match = pat.search(self.input_data) if match: prefix = match.group() if prefix.startswith(b'.'): prefix = prefix.strip(b'.') try: data = to_unicode(items[0], 'utf-8') for item in json.loads(data): if not item[b'abbr'].startswith(prefix): continue item[b'word'] = placeholder.sub( replace, to_unicode(item[b'word'], 'utf-8')) item[b'menu'] = item[b'menu'].replace( 'source.lang.swift.decl.', '') res.append(item) return res except Exception: return []
def store_buffer(self, buffer, base, cur_nr, cur_line): nr = buffer.number encoding = get_encoding(nr) if nr == cur_nr: start = cur_line - 1000 end = cur_line + 1000 if start < 0: start = 0 data = ' '.join(itertools.chain(buffer[start:cur_line], buffer[cur_line + 1:end])) self.current = set(self.pat.findall(to_unicode(data, encoding))) self.current.difference_update([base]) elif buffer.valid and len(buffer) <= 10000: ftime = getftime(nr) if ftime < 0: return if nr not in self.cache or ftime > self.cache[nr]['t']: self.cache[nr] = {'t': ftime} data = to_unicode(' '.join(buffer[:]), encoding) words = set(self.store) words.update(set(self.pat.findall(data))) self.store.clear() self.store.extend(words)
def test_parse(vim_mod): items = [ b'COMPLETION: pclose : [#int#]pclose(<#FILE *#>)', b'COMPLETION: hello : [#int#]hello', b'COMPLETION: b : [#int#]b', b'COMPLETION: Pattern: fake hello', ] cpp = completor.get('cpp') cpp.input_data = to_unicode('b->', 'utf-8') assert cpp.on_data(b'complete', items) == [ {'dup': 1, 'menu': b'int pclose(FILE *)', 'info': b'int pclose(FILE *)', 'word': b'pclose'}, {'dup': 1, 'menu': b'int hello', 'info': b'int hello', 'word': b'hello'}, {'dup': 1, 'menu': b'int b', 'info': b'int b', 'word': b'b'}, {'dup': 1, 'menu': b'hello', 'info': b'hello', 'word': b'fake'} ]
def has_omnifunc(self, ft): if ft not in self.trigger_cache: name = '{}_omni_trigger'.format(ft) option = self.get_option(name) if not option: return False try: self.trigger_cache[ft] = re.compile( to_unicode(option, 'utf-8'), re.X | re.U) except Exception: return False try: return bool(vim.current.buffer.options['omnifunc']) except vim.error: return False
def parse(self, data): if len(data) == 0: return [] res = [] data = to_unicode(data[0], 'utf-8') try: data = json.loads(data) except json.decoder.JSONDecodeError: return [] if not 'suggestions' in data: return [] for item in data['suggestions']: res.append({ 'word': item['name'], 'menu': item['info'] }) return res
def on_definition(self, data): return json.loads(to_unicode(data[0], 'utf-8'))
def on_doc(self, items): data = to_unicode(items[0], 'utf-8') spec = json.loads(data) _import = 'import "{}"'.format(spec['import']) doc = '\n\n'.join([_import, spec['decl'], spec['doc']]) return [doc]
def on_complete(self, data): try: data = to_unicode(data[0], 'utf-8').replace('\\u', '\\\\u') return json.loads(data) except Exception: return []
def parse(self, data): try: data = to_unicode(data[0], 'utf-8') return json.loads(data) except Exception: return []
def rename_request(self, name): c = self.gen_position_request(Rename) c.set_name(to_unicode(name, 'utf-8')) req_id, req = c.to_request() self.current_id = req_id return req