Пример #1
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = context["sources"]
        results = []
        start_length = self.vim.eval("g:deoplete#auto_completion_start_length")
        for source_name, source in sorted(self.sources.items(), key=lambda x: x[1].rank, reverse=True):
            if (sources and not source_name in sources) or (
                source.filetypes and not context["filetype"] in source.filetypes
            ):
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if source.is_bytepos:
                charpos = bytepos2charpos(self.vim, cont["input"], charpos)
            cont["complete_str"] = cont["input"][charpos:]
            cont["complete_position"] = charpos2bytepos(self.vim, cont["input"], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 or (cont["event"] != "Manual" and len(cont["complete_str"]) < min_pattern_length):
                # Skip
                continue
            results.append({"name": source_name, "source": source, "context": cont})

        for result in results:
            context = result["context"]
            source = result["source"]
            context["candidates"] = source.gather_candidates(context)
            # self.debug(context['candidates'])

            # self.debug(context['complete_str'])
            # self.debug(context['candidates'])
            for filter_name in source.matchers + source.sorters + source.converters:
                if filter_name in self.filters:
                    context["candidates"] = self.filters[filter_name].filter(context)
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, "on_post_filter"):
                context["candidates"] = source.on_post_filter(context)

            # Set default menu
            for candidate in context["candidates"]:
                if not "menu" in candidate:
                    candidate["menu"] = source.mark
            # self.debug(context['candidates'])
        return results
Пример #2
0
    def gather_candidates(self, context):
        if not self._jedi:
            return []

        python_path = None
        if 'deoplete#sources#jedi#python_path' in context['vars']:
            python_path = context['vars'][
                'deoplete#sources#jedi#python_path']
        if python_path != self._python_path:
            self.set_env(python_path)

        line = context['position'][1]
        col = bytepos2charpos(
            context['encoding'], context['input'],
            context['complete_position'])
        buf = self.vim.current.buffer
        filename = str(buf.name)

        # Only use source if buffer is modified, to skip transferring, joining,
        # and splitting the buffer lines unnecessarily.
        modified = buf.options['modified']
        if not modified and os.path.exists(filename):
            source = None
        else:
            source = '\n'.join(getlines(self.vim))

        if (line != self.vim.call('line', '.')
                or context['complete_position'] >= self.vim.call('col', '$')):
            return []

        self.debug('Line: %r, Col: %r, Filename: %r, modified: %r',
                   line, col, filename, modified)

        script = self.get_script(source, line, col, filename,
                                 environment=self._env)

        try:
            completions = self.get_completions(script)
        except BaseException:
            if not self.ignore_errors:
                raise
            return []

        return self.finalize_completions(completions)
Пример #3
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        results = []
        for source_name, source in self.itersource(context):
            if source.disabled_syntaxes and 'syntax_name' not in context:
                context['syntax_name'] = get_syn_name(self.__vim)
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(self.__encoding, cont['input'],
                                          charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.__encoding, cont['input'], charpos)
            cont['max_abbr_width'] = min(source.max_abbr_width,
                                         cont['max_abbr_width'])
            cont['max_menu_width'] = min(source.max_menu_width,
                                         cont['max_menu_width'])
            if cont['max_abbr_width'] > 0:
                cont['max_abbr_width'] = max(20, cont['max_abbr_width'])
            if cont['max_menu_width'] > 0:
                cont['max_menu_width'] = max(10, cont['max_menu_width'])
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            if charpos < 0 or self.is_skip(
                    cont, source.disabled_syntaxes, source.min_pattern_length,
                    source.max_pattern_length, source.input_pattern):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # self.debug(source.name)
            self.profile_start(context, source.name)
            context['candidates'] = source.gather_candidates(context)
            self.profile_end(source.name)
            if context['candidates'] and isinstance(context['candidates'][0],
                                                    str):
                # Convert to dict
                context['candidates'] = [{
                    'word': x
                } for x in context['candidates']]

            ignorecase = context['ignorecase']
            smartcase = context['smartcase']
            camelcase = context['camelcase']
            try:
                # Set ignorecase
                if (smartcase or camelcase) and re.search(
                        r'[A-Z]', context['complete_str']):
                    context['ignorecase'] = 0

                for filter in [
                        self.__filters[x] for x in source.matchers +
                        source.sorters + source.converters
                        if x in self.__filters
                ]:
                    self.profile_start(context, filter.name)
                    context['candidates'] = filter.filter(context)
                    self.profile_end(filter.name)
            finally:
                context['ignorecase'] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            candidates = context['candidates']
            # Set default menu and icase
            mark = source.mark + ' '
            for candidate in candidates:
                candidate['icase'] = 1
                if source.mark != '' and candidate.get('menu',
                                                       '').find(mark) != 0:
                    candidate['menu'] = mark + candidate.get('menu', '')

            # self.debug(context['candidates'])
        return results
Пример #4
0
    def _gather_results(self, context):
        results = []

        for source in [x[1] for x in self._itersource(context)]:
            try:
                if source.disabled_syntaxes and 'syntax_names' not in context:
                    context['syntax_names'] = get_syn_names(self._vim)
                ctx = copy.deepcopy(context)

                charpos = source.get_complete_position(ctx)
                if charpos >= 0 and source.is_bytepos:
                    charpos = bytepos2charpos(
                        ctx['encoding'], ctx['input'], charpos)

                ctx['char_position'] = charpos
                ctx['complete_position'] = charpos2bytepos(
                    ctx['encoding'], ctx['input'], charpos)
                ctx['complete_str'] = ctx['input'][ctx['char_position']:]

                if charpos < 0 or self._is_skip(ctx, source):
                    if source.name in self._prev_results:
                        self._prev_results.pop(source.name)
                    # Skip
                    continue

                if (source.name in self._prev_results and
                        self._use_previous_result(
                            context, self._prev_results[source.name],
                            source.is_volatile)):
                    results.append(self._prev_results[source.name])
                    continue

                ctx['is_async'] = False
                ctx['is_refresh'] = True
                ctx['max_abbr_width'] = min(source.max_abbr_width,
                                            ctx['max_abbr_width'])
                ctx['max_kind_width'] = min(source.max_kind_width,
                                            ctx['max_kind_width'])
                ctx['max_menu_width'] = min(source.max_menu_width,
                                            ctx['max_menu_width'])
                if ctx['max_abbr_width'] > 0:
                    ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
                if ctx['max_kind_width'] > 0:
                    ctx['max_kind_width'] = max(10, ctx['max_kind_width'])
                if ctx['max_menu_width'] > 0:
                    ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

                # Gathering
                self._profile_start(ctx, source.name)
                ctx['candidates'] = source.gather_candidates(ctx)
                self._profile_end(source.name)

                if ctx['candidates'] is None:
                    continue

                ctx['candidates'] = convert2candidates(ctx['candidates'])

                result = {
                    'name': source.name,
                    'source': source,
                    'context': ctx,
                    'is_async': ctx['is_async'],
                    'prev_linenr': ctx['position'][1],
                    'prev_input': ctx['input'],
                    'input': ctx['input'],
                    'complete_position': ctx['complete_position'],
                    'candidates': ctx['candidates'],
                }
                self._prev_results[source.name] = result
                results.append(result)
            except Exception:
                self._source_errors[source.name] += 1
                if source.is_silent:
                    continue
                if self._source_errors[source.name] > 2:
                    error(self._vim, 'Too many errors from "%s". '
                          'This source is disabled until Neovim '
                          'is restarted.' % source.name)
                    self._ignore_sources.append(source.name)
                    continue
                error_tb(self._vim, 'Errors from: %s' % source.name)

        return results
Пример #5
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = sorted(self.sources.items(),
                         key=lambda x: get_custom(self.vim, x[1].name).get(
                             'rank', x[1].rank),
                         reverse=True)
        results = []
        start_length = self.vim.eval(
            'g:deoplete#auto_completion_start_length')
        ignore_sources = get_buffer_config(
            self.vim, context['filetype'],
            'b:deoplete_ignore_sources',
            'g:deoplete#ignore_sources',
            '{}')
        for source_name, source in sources:
            filetypes = get_custom(self.vim, source.name).get(
                'filetypes', source.filetypes)

            in_sources = not context['sources'] or (
                source_name in context['sources'])
            in_fts = not filetypes or (
                context['filetype'] in filetypes)
            in_ignore = source_name in ignore_sources
            if not in_sources or not in_fts or in_ignore:
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(
                    self.vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.vim, cont['input'], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = get_custom(self.vim, source.name).get(
                'min_pattern_length', source.min_pattern_length)
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length
            input_pattern = get_custom(self.vim, source.name).get(
                'input_pattern', source.input_pattern)

            if charpos < 0 or self.is_skip(cont,
                                           min_pattern_length,
                                           input_pattern):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # self.debug(source.name)
            context['candidates'] = source.gather_candidates(context)
            if context['candidates'] and isinstance(
                    context['candidates'][0], str):
                # Convert to dict
                context['candidates'] = [{'word': x}
                                         for x in context['candidates']]

            matchers = get_custom(self.vim, source.name).get(
                'matchers', source.matchers)
            sorters = get_custom(self.vim, source.name).get(
                'sorters', source.sorters)
            converters = get_custom(self.vim, source.name).get(
                'converters', source.converters)

            ignorecase = context['ignorecase']
            try:
                # Set ignorecase
                if context['smartcase'] and re.match(r'[A-Z]',
                                                     context['complete_str']):
                    context['ignorecase'] = 0

                for filter_name in matchers + sorters + converters:
                    if filter_name in self.filters:
                        context['candidates'] = self.filters[
                            filter_name].filter(context)
            finally:
                context['ignorecase'] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            if context['candidates'] and (
                    not re.match(r'\[.*\]',
                                 context['candidates'][0].get('menu', ''))):
                # Set default menu
                for candidate in context['candidates']:
                    candidate['menu'] = source.mark + ' ' + candidate.get(
                        'menu', '')

            # Set icase
            for candidate in context['candidates']:
                candidate['icase'] = 1
            # self.debug(context['candidates'])
        return results
Пример #6
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = context['sources']
        results = []
        start_length = self.vim.eval('g:deoplete#auto_completion_start_length')
        for source_name, source in sorted(self.sources.items(),
                                          key=lambda x: x[1].rank,
                                          reverse=True):
            if (sources and source_name not in sources) or (
                    source.filetypes
                    and not context['filetype'] in source.filetypes):
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(self.vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.vim, cont['input'], charpos)
            # debug(self.vim, source.rank)
            # debug(self.vim, source_name)
            # debug(self.vim, cont['input'])
            # debug(self.vim, charpos)
            # debug(self.vim, cont['complete_position'])
            # debug(self.vim, cont['complete_str'])

            min_pattern_length = get_custom(self.vim, source.name).get(
                'min_pattern_length', source.min_pattern_length)
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 or (cont['event'] != 'Manual' and
                               len(cont['complete_str']) < min_pattern_length):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # debug(self.vim, source.name)
            context['candidates'] = source.gather_candidates(context)
            if context['candidates'] and isinstance(context['candidates'][0],
                                                    str):
                # Convert to dict
                context['candidates'] = [{
                    'word': x
                } for x in context['candidates']]

            matchers = get_custom(self.vim,
                                  source.name).get('matchers', source.matchers)
            sorters = get_custom(self.vim,
                                 source.name).get('sorters', source.sorters)
            converters = get_custom(self.vim,
                                    source.name).get('converters',
                                                     source.converters)

            ignorecase = context['ignorecase']
            try:
                # Set ignorecase
                if context['smartcase'] and re.match(r'[A-Z]',
                                                     context['complete_str']):
                    context['ignorecase'] = 0

                for filter_name in matchers + sorters + converters:
                    if filter_name in self.filters:
                        context['candidates'] = self.filters[
                            filter_name].filter(context)
            finally:
                context['ignorecase'] = ignorecase
            # debug(self.vim, context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            if context['candidates'] and (not re.match(
                    r'\[.*\]', context['candidates'][0].get('menu', ''))):
                # Set default menu
                for candidate in context['candidates']:
                    candidate['menu'] = source.mark + ' ' + candidate.get(
                        'menu', '')
            # debug(self.vim, context['candidates'])
        return results
Пример #7
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        results = []
        for source_name, source in self.itersource(context):
            if source.disabled_syntaxes and 'syntax_name' not in context:
                context['syntax_name'] = get_syn_name(self.__vim)
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(
                    self.__encoding, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.__encoding, cont['input'], charpos)
            cont['max_abbr_width'] = min(source.max_abbr_width,
                                         cont['max_abbr_width'])
            cont['max_menu_width'] = min(source.max_menu_width,
                                         cont['max_menu_width'])
            if cont['max_abbr_width'] > 0:
                cont['max_abbr_width'] = max(20, cont['max_abbr_width'])
            if cont['max_menu_width'] > 0:
                cont['max_menu_width'] = max(10, cont['max_menu_width'])
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            if charpos < 0 or self.is_skip(cont, source.disabled_syntaxes,
                                           source.min_pattern_length,
                                           source.max_pattern_length,
                                           source.input_pattern):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # self.debug(source.name)
            self.profile_start(context, source.name)
            context['candidates'] = source.gather_candidates(context)
            self.profile_end(source.name)
            if context['candidates'] and isinstance(
                    context['candidates'][0], str):
                # Convert to dict
                context['candidates'] = [{'word': x}
                                         for x in context['candidates']]

            ignorecase = context['ignorecase']
            smartcase = context['smartcase']
            camelcase = context['camelcase']
            try:
                # Set ignorecase
                if (smartcase or camelcase) and re.search(
                        r'[A-Z]', context['complete_str']):
                    context['ignorecase'] = 0

                for filter in [self.__filters[x] for x
                               in source.matchers +
                               source.sorters +
                               source.converters
                               if x in self.__filters]:
                    self.profile_start(context, filter.name)
                    context['candidates'] = filter.filter(context)
                    self.profile_end(filter.name)
            finally:
                context['ignorecase'] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            candidates = context['candidates']
            # Set default menu and icase
            mark = source.mark + ' '
            for candidate in candidates:
                candidate['icase'] = 1
                if source.mark != '' and candidate.get(
                        'menu', '').find(mark) != 0:
                    candidate['menu'] = mark + candidate.get('menu', '')

            # self.debug(context['candidates'])
        return results
Пример #8
0
    def _get_result(self, context, source):
        if source.disabled_syntaxes and 'syntax_names' not in context:
            context['syntax_names'] = get_syn_names(self._vim)

        ctx = copy.deepcopy(context)

        charpos = source.get_complete_position(ctx)
        if charpos >= 0 and source.is_bytepos:
            charpos = bytepos2charpos(ctx['encoding'], ctx['input'], charpos)

        ctx['char_position'] = charpos
        ctx['complete_position'] = charpos2bytepos(ctx['encoding'],
                                                   ctx['input'], charpos)
        ctx['complete_str'] = ctx['input'][ctx['char_position']:]

        if charpos < 0 or self._is_skip(ctx, source):
            if source.name in self._prev_results:
                self._prev_results.pop(source.name)
            # Skip
            return {}

        if (source.name in self._prev_results and self._use_previous_result(
                context, self._prev_results[source.name], source.is_volatile)):
            return self._prev_results[source.name]

        ctx['is_async'] = False
        ctx['is_refresh'] = True
        ctx['max_abbr_width'] = min(source.max_abbr_width,
                                    ctx['max_abbr_width'])
        ctx['max_kind_width'] = min(source.max_kind_width,
                                    ctx['max_kind_width'])
        ctx['max_menu_width'] = min(source.max_menu_width,
                                    ctx['max_menu_width'])
        if ctx['max_abbr_width'] > 0:
            ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
        if ctx['max_kind_width'] > 0:
            ctx['max_kind_width'] = max(10, ctx['max_kind_width'])
        if ctx['max_menu_width'] > 0:
            ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

        # Gathering
        self._profile_start(ctx, source.name)
        ctx['candidates'] = source.gather_candidates(ctx)
        self._profile_end(source.name)

        if ctx['candidates'] is None:
            return {}

        ctx['candidates'] = convert2candidates(ctx['candidates'])

        return {
            'name': source.name,
            'source': source,
            'context': ctx,
            'is_async': ctx['is_async'],
            'prev_linenr': ctx['position'][1],
            'prev_input': ctx['input'],
            'input': ctx['input'],
            'complete_position': ctx['complete_position'],
            'candidates': ctx['candidates'],
        }
Пример #9
0
    def gather_results(self, context):
        results = []

        for source in [x[1] for x in self.itersource(context)]:
            try:
                if source.disabled_syntaxes and 'syntax_names' not in context:
                    context['syntax_names'] = get_syn_names(self._vim)
                ctx = copy.deepcopy(context)
                ctx['is_async'] = False

                charpos = source.get_complete_position(ctx)
                if charpos >= 0 and source.is_bytepos:
                    charpos = bytepos2charpos(
                        ctx['encoding'], ctx['input'], charpos)

                ctx['char_position'] = charpos
                ctx['complete_position'] = charpos2bytepos(
                    ctx['encoding'], ctx['input'], charpos)
                ctx['complete_str'] = ctx['input'][ctx['char_position']:]

                if charpos < 0 or self.is_skip(ctx, source):
                    if source.name in self._prev_results:
                        self._prev_results.pop(source.name)
                    # Skip
                    continue

                if (not source.is_volatile and
                        source.name in self._prev_results and
                        self.use_previous_result(
                            context, self._prev_results[source.name])):
                    results.append(self._prev_results[source.name])
                    continue

                ctx['max_abbr_width'] = min(source.max_abbr_width,
                                            ctx['max_abbr_width'])
                ctx['max_menu_width'] = min(source.max_menu_width,
                                            ctx['max_menu_width'])
                if ctx['max_abbr_width'] > 0:
                    ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
                if ctx['max_menu_width'] > 0:
                    ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

                # Gathering
                self.profile_start(ctx, source.name)
                ctx['candidates'] = source.gather_candidates(ctx)
                self.profile_end(source.name)

                if ctx['candidates'] is None:
                    continue

                ctx['candidates'] = convert2candidates(ctx['candidates'])

                result = {
                    'name': source.name,
                    'source': source,
                    'context': ctx,
                    'is_async': ctx['is_async'],
                    'prev_linenr': ctx['position'][1],
                    'prev_input': ctx['input'],
                }
                self._prev_results[source.name] = result
                results.append(result)
            except Exception:
                self._source_errors[source.name] += 1
                if self._source_errors[source.name] > 2:
                    error(self._vim, 'Too many errors from "%s". '
                          'This source is disabled until Neovim '
                          'is restarted.' % source.name)
                    self._ignored_sources.add(source.path)
                    self._sources.pop(source.name)
                    continue
                error_tb(self._vim,
                         'Could not get completions from: %s' % source.name)

        return results
Пример #10
0
    def gather_results(self, context):
        results = []

        for source_name, source in list(self.itersource(context)):
            try:
                if source.disabled_syntaxes and 'syntax_names' not in context:
                    context['syntax_names'] = get_syn_names(self.__vim)
                ctx = copy.deepcopy(context)
                charpos = source.get_complete_position(ctx)
                if charpos >= 0 and source.is_bytepos:
                    charpos = bytepos2charpos(ctx['encoding'], ctx['input'],
                                              charpos)
                ctx['complete_str'] = ctx['input'][charpos:]
                ctx['complete_position'] = charpos2bytepos(
                    ctx['encoding'], ctx['input'], charpos)
                ctx['max_abbr_width'] = min(source.max_abbr_width,
                                            ctx['max_abbr_width'])
                ctx['max_menu_width'] = min(source.max_menu_width,
                                            ctx['max_menu_width'])
                if ctx['max_abbr_width'] > 0:
                    ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
                if ctx['max_menu_width'] > 0:
                    ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

                if charpos < 0 or self.is_skip(ctx, source.disabled_syntaxes,
                                               source.min_pattern_length,
                                               source.max_pattern_length,
                                               source.input_pattern):
                    # Skip
                    continue

                # Gathering
                self.profile_start(ctx, source.name)
                ctx['candidates'] = source.gather_candidates(ctx)
                self.profile_end(source.name)

                if 'candidates' not in ctx or not ctx['candidates']:
                    continue

                if ctx['candidates'] and isinstance(ctx['candidates'][0], str):
                    # Convert to dict
                    ctx['candidates'] = [{
                        'word': x
                    } for x in ctx['candidates']]

                # Filtering
                ignorecase = ctx['ignorecase']
                smartcase = ctx['smartcase']
                camelcase = ctx['camelcase']

                # Set ignorecase
                if (smartcase or camelcase) and re.search(
                        r'[A-Z]', ctx['complete_str']):
                    ctx['ignorecase'] = 0

                for filter in [
                        self.__filters[x] for x in source.matchers +
                        source.sorters + source.converters
                        if x in self.__filters
                ]:
                    try:
                        self.profile_start(ctx, filter.name)
                        ctx['candidates'] = filter.filter(ctx)
                        self.profile_end(filter.name)
                    except Exception:
                        self.__filter_errors[filter.name] += 1
                        if self.__source_errors[filter.name] > 2:
                            error(
                                self.__vim, 'Too many errors from "%s". '
                                'This filter is disabled until Neovim '
                                'is restarted.' % filter.name)
                            self.__ignored_filters.add(filter.path)
                            self.__filters.pop(filter.name)
                            continue
                        error_tb(self.__vim,
                                 'Could not filter using: %s' % filter)

                ctx['ignorecase'] = ignorecase

                # On post filter
                if hasattr(source, 'on_post_filter'):
                    ctx['candidates'] = source.on_post_filter(ctx)

                # Set default menu and icase
                mark = source.mark + ' '
                for candidate in ctx['candidates']:
                    candidate['icase'] = 1
                    if source.mark != '' \
                            and candidate.get('menu', '').find(mark) != 0:
                        candidate['menu'] = mark + candidate.get('menu', '')

                results.append({
                    'name': source_name,
                    'source': source,
                    'context': ctx,
                })
            except Exception:
                self.__source_errors[source_name] += 1
                if self.__source_errors[source_name] > 2:
                    error(
                        self.__vim, 'Too many errors from "%s". '
                        'This source is disabled until Neovim '
                        'is restarted.' % source_name)
                    self.__ignored_sources.add(source.path)
                    self.__sources.pop(source_name)
                    continue
                error_tb(self.__vim,
                         'Could not get completions from: %s' % source_name)
        return results
Пример #11
0
    def gather_results(self, context):
        results = []

        for source_name, source in self.itersource(context):
            try:
                if source.disabled_syntaxes and 'syntax_names' not in context:
                    context['syntax_names'] = get_syn_names(self.__vim)
                ctx = copy.deepcopy(context)
                charpos = source.get_complete_position(ctx)
                if charpos >= 0 and source.is_bytepos:
                    charpos = bytepos2charpos(
                        ctx['encoding'], ctx['input'], charpos)
                ctx['complete_str'] = ctx['input'][charpos:]
                ctx['complete_position'] = charpos2bytepos(
                    ctx['encoding'], ctx['input'], charpos)
                ctx['max_abbr_width'] = min(source.max_abbr_width,
                                            ctx['max_abbr_width'])
                ctx['max_menu_width'] = min(source.max_menu_width,
                                            ctx['max_menu_width'])
                if ctx['max_abbr_width'] > 0:
                    ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
                if ctx['max_menu_width'] > 0:
                    ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

                if charpos < 0 or self.is_skip(ctx, source.disabled_syntaxes,
                                               source.min_pattern_length,
                                               source.max_pattern_length,
                                               source.input_pattern):
                    # Skip
                    continue

                # Gathering
                self.profile_start(ctx, source.name)
                ctx['candidates'] = source.gather_candidates(ctx)
                self.profile_end(source.name)

                if 'candidates' not in ctx or not ctx['candidates']:
                    continue

                if ctx['candidates'] and isinstance(ctx['candidates'][0], str):
                    # Convert to dict
                    ctx['candidates'] = [{'word': x}
                                         for x in ctx['candidates']]

                # Filtering
                ignorecase = ctx['ignorecase']
                smartcase = ctx['smartcase']
                camelcase = ctx['camelcase']

                # Set ignorecase
                if (smartcase or camelcase) and re.search(r'[A-Z]',
                                                          ctx['complete_str']):
                    ctx['ignorecase'] = 0

                for filter in [self.__filters[x] for x
                               in source.matchers +
                               source.sorters +
                               source.converters
                               if x in self.__filters]:
                    try:
                        self.profile_start(ctx, filter.name)
                        ctx['candidates'] = filter.filter(ctx)
                        self.profile_end(filter.name)
                    except:
                        self.__filter_errors[filter.name] += 1
                        if self.__source_errors[filter.name] > 2:
                            error(self.__vim, 'Too many errors from "%s". '
                                  'This filter is disabled until Neovim '
                                  'is restarted.' % filter.name)
                            self.__filters.pop(filter.name)
                            continue
                        error_tb(self.__vim,
                                 'Could not filter using: %s' % filter)

                ctx['ignorecase'] = ignorecase

                # On post filter
                if hasattr(source, 'on_post_filter'):
                    ctx['candidates'] = source.on_post_filter(ctx)

                # Set default menu and icase
                mark = source.mark + ' '
                for candidate in ctx['candidates']:
                    candidate['icase'] = 1
                    if source.mark != '' \
                            and candidate.get('menu', '').find(mark) != 0:
                        candidate['menu'] = mark + candidate.get('menu', '')

                results.append({
                    'name': source_name,
                    'source': source,
                    'context': ctx,
                })
            except:
                self.__source_errors[source_name] += 1
                if self.__source_errors[source_name] > 2:
                    error(self.__vim, 'Too many errors from "%s". '
                          'This source is disabled until Neovim '
                          'is restarted.' % source_name)
                    self.__sources.pop(source_name)
                    continue
                error_tb(self.__vim,
                         'Could not get completions from: %s' % source_name)

        return results
Пример #12
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = context['sources']
        results = []
        start_length = self.vim.eval(
            'g:deoplete#auto_completion_start_length')
        for source_name, source in sorted(self.sources.items(),
                key=lambda x: x[1].rank, reverse=True):
            if (sources and not source_name in sources) \
                    or (source.filetypes and
                        not context['filetype'] in source.filetypes):
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(
                    self.vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos :]
            cont['complete_position'] = charpos2bytepos(
                self.vim, cont['input'], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 \
                    or (cont['event'] != 'Manual' \
                        and len(cont['complete_str']) < min_pattern_length):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            context['candidates'] = source.gather_candidates(context)
            if context['candidates'] \
                    and type(context['candidates'][0]) == type(''):
                # Convert to dict
                context['candidates'] = \
                    [{ 'word': x } for x in context['candidates'] ]

            # self.debug(context['candidates'])

            # self.debug(context['complete_str'])
            # self.debug(context['candidates'])
            for filter_name in \
                    source.matchers + source.sorters + source.converters:
                if filter_name in self.filters:
                    context['candidates'] = \
                        self.filters[filter_name].filter(context)
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            # Set default menu
            for candidate in context['candidates']:
                candidate['menu'] = \
                    source.mark + ' ' + candidate.get('menu', '')
            # self.debug(context['candidates'])
        return results
Пример #13
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = sorted(self.__sources.items(),
                         key=lambda x: get_custom(self.__vim, x[1].name).get(
                             'rank', x[1].rank),
                         reverse=True)
        results = []
        start_length = self.__vim.vars['deoplete#auto_complete_start_length']
        for source_name, source in self.itersource(context, sources):
            if source.disabled_syntaxes and 'syntax_name' not in context:
                context['syntax_name'] = get_syn_name(self.__vim)
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(
                    self.__vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.__vim, cont['input'], charpos)
            cont['max_abbr_width'] = min(source.max_abbr_width,
                                         cont['max_abbr_width'])
            cont['max_menu_width'] = min(source.max_menu_width,
                                         cont['max_menu_width'])
            if cont['max_abbr_width'] > 0:
                cont['max_abbr_width'] = max(20, cont['max_abbr_width'])
            if cont['max_menu_width'] > 0:
                cont['max_menu_width'] = max(10, cont['max_menu_width'])
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 or self.is_skip(cont, source.disabled_syntaxes,
                                           min_pattern_length,
                                           source.max_pattern_length,
                                           source.input_pattern):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # self.debug(source.name)
            self.profile_start(source.name)
            context['candidates'] = source.gather_candidates(context)
            self.profile_end(source.name)
            if context['candidates'] and isinstance(
                    context['candidates'][0], str):
                # Convert to dict
                context['candidates'] = [{'word': x}
                                         for x in context['candidates']]

            ignorecase = context['ignorecase']
            smartcase = context['smartcase']
            camelcase = context['camelcase']
            try:
                # Set ignorecase
                if (smartcase or camelcase) and re.search(
                        r'[A-Z]', context['complete_str']):
                    context['ignorecase'] = 0

                for filter in [self.__filters[x] for x
                               in source.matchers +
                               source.sorters +
                               source.converters
                               if x in self.__filters]:
                    self.profile_start(filter.name)
                    context['candidates'] = filter.filter(context)
                    self.profile_end(filter.name)
            finally:
                context['ignorecase'] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            if context['candidates'] and not (
                    re.match(r'\[.*\]',
                             context['candidates'][0].get('menu', ''))):
                # Set default menu
                for candidate in context['candidates']:
                    candidate['menu'] = source.mark + ' ' + candidate.get(
                        'menu', '')

            # self.debug(context['candidates'])
        return results
Пример #14
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = context['sources']
        results = []
        start_length = self.vim.eval('g:deoplete#auto_completion_start_length')
        for source_name, source in sorted(self.sources.items(),
                                          key=lambda x: x[1].rank,
                                          reverse=True):
            if (sources and not source_name in sources) \
                    or (source.filetypes and
                        not context['filetype'] in source.filetypes):
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if source.is_bytepos:
                charpos = bytepos2charpos(self.vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.vim, cont['input'], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 \
                    or (cont['event'] != 'Manual' \
                        and len(cont['complete_str']) < min_pattern_length):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']
            context['candidates'] = source.gather_candidates(context)
            # self.debug(context['candidates'])

            # self.debug(context['complete_str'])
            # self.debug(context['candidates'])
            for filter_name in \
                    source.matchers + source.sorters + source.converters:
                if filter_name in self.filters:
                    context['candidates'] = \
                        self.filters[filter_name].filter(context)
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            # Set default menu
            for candidate in context['candidates']:
                if not 'menu' in candidate:
                    candidate['menu'] = source.mark
            # self.debug(context['candidates'])
        return results
Пример #15
0
def test_pos():
    assert util.bytepos2charpos('utf-8', 'foo bar', 3) == 3
    assert util.bytepos2charpos('utf-8', 'あああ', 3) == 1
    assert util.charpos2bytepos('utf-8', 'foo bar', 3) == 3
    assert util.charpos2bytepos('utf-8', 'あああ', 3) == 9
Пример #16
0
    def _get_result(self, context, source):
        if source.disabled_syntaxes and 'syntax_names' not in context:
            context['syntax_names'] = get_syn_names(self._vim)

        ctx = copy.deepcopy(context)

        charpos = source.get_complete_position(ctx)
        if charpos >= 0 and source.is_bytepos:
            charpos = bytepos2charpos(
                ctx['encoding'], ctx['input'], charpos)

        ctx['char_position'] = charpos
        ctx['complete_position'] = charpos2bytepos(
            ctx['encoding'], ctx['input'], charpos)
        ctx['complete_str'] = ctx['input'][ctx['char_position']:]

        if charpos < 0 or self._is_skip(ctx, source):
            if source.name in self._prev_results:
                self._prev_results.pop(source.name)
            # Skip
            return {}

        if (source.name in self._prev_results and
                self._use_previous_result(
                    context, self._prev_results[source.name],
                    source.is_volatile)):
            return self._prev_results[source.name]

        ctx['is_async'] = False
        ctx['is_refresh'] = True
        ctx['max_abbr_width'] = min(source.max_abbr_width,
                                    ctx['max_abbr_width'])
        ctx['max_kind_width'] = min(source.max_kind_width,
                                    ctx['max_kind_width'])
        ctx['max_info_width'] = min(source.max_info_width,
                                    ctx['max_info_width'])
        ctx['max_menu_width'] = min(source.max_menu_width,
                                    ctx['max_menu_width'])
        if ctx['max_abbr_width'] > 0:
            ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
        if ctx['max_kind_width'] > 0:
            ctx['max_kind_width'] = max(10, ctx['max_kind_width'])
        if ctx['max_info_width'] > 0:
            ctx['max_info_width'] = max(10, ctx['max_info_width'])
        if ctx['max_menu_width'] > 0:
            ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

        # Gathering
        self._profile_start(ctx, source.name)
        ctx['candidates'] = source.gather_candidates(ctx)
        self._profile_end(source.name)

        if ctx['candidates'] is None:
            return {}

        ctx['candidates'] = convert2candidates(ctx['candidates'])

        return {
            'name': source.name,
            'source': source,
            'context': ctx,
            'is_async': ctx['is_async'],
            'prev_linenr': ctx['position'][1],
            'prev_input': ctx['input'],
            'input': ctx['input'],
            'complete_position': ctx['complete_position'],
            'candidates': ctx['candidates'],
        }
Пример #17
0
def test_pos():
    assert util.bytepos2charpos('utf-8', 'foo bar', 3) == 3
    assert util.bytepos2charpos('utf-8', 'あああ', 3) == 1
    assert util.charpos2bytepos('utf-8', 'foo bar', 3) == 3
    assert util.charpos2bytepos('utf-8', 'あああ', 3) == 9
Пример #18
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = context["sources"]
        results = []
        start_length = self.vim.eval("g:deoplete#auto_completion_start_length")
        for source_name, source in sorted(self.sources.items(), key=lambda x: x[1].rank, reverse=True):
            if (sources and not source_name in sources) or (
                source.filetypes and not context["filetype"] in source.filetypes
            ):
                continue
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(self.vim, cont["input"], charpos)
            cont["complete_str"] = cont["input"][charpos:]
            cont["complete_position"] = charpos2bytepos(self.vim, cont["input"], charpos)
            # debug(self.vim, source.rank)
            # debug(self.vim, source_name)
            # debug(self.vim, cont['input'])
            # debug(self.vim, charpos)
            # debug(self.vim, cont['complete_position'])
            # debug(self.vim, cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 or (cont["event"] != "Manual" and len(cont["complete_str"]) < min_pattern_length):
                # Skip
                continue
            results.append({"name": source_name, "source": source, "context": cont})

        for result in results:
            context = result["context"]
            source = result["source"]

            # debug(self.vim, source.name)
            context["candidates"] = source.gather_candidates(context)
            if context["candidates"] and type(context["candidates"][0]) == type(""):
                # Convert to dict
                context["candidates"] = [{"word": x} for x in context["candidates"]]

            matchers = get_custom(self.vim, source.name).get("matchers", source.matchers)
            sorters = get_custom(self.vim, source.name).get("sorters", source.sorters)
            converters = get_custom(self.vim, source.name).get("converters", source.converters)

            ignorecase = context["ignorecase"]
            try:
                # Set ignorecase
                if context["smartcase"] and re.match(r"[A-Z]", context["complete_str"]):
                    context["ignorecase"] = 0

                for filter_name in matchers + sorters + converters:
                    if filter_name in self.filters:
                        context["candidates"] = self.filters[filter_name].filter(context)
            finally:
                context["ignorecase"] = ignorecase
            # debug(self.vim, context['candidates'])

            # On post filter
            if hasattr(source, "on_post_filter"):
                context["candidates"] = source.on_post_filter(context)

            if context["candidates"] and (not re.match(r"\[.*\]", context["candidates"][0].get("menu", ""))):
                # Set default menu
                for candidate in context["candidates"]:
                    candidate["menu"] = source.mark + " " + candidate.get("menu", "")
            # debug(self.vim, context['candidates'])
        return results
Пример #19
0
 def test_pos(self):
     eq_(bytepos2charpos('utf-8', 'foo bar', 3), 3)
     eq_(bytepos2charpos('utf-8', 'あああ', 3), 1)
     eq_(charpos2bytepos('utf-8', 'foo bar', 3), 3)
     eq_(charpos2bytepos('utf-8', 'あああ', 3), 9)
Пример #20
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = sorted(
            self.__sources.items(), key=lambda x: get_custom(self.__vim, x[1].name).get("rank", x[1].rank), reverse=True
        )
        results = []
        start_length = self.__vim.vars["deoplete#auto_complete_start_length"]
        ignore_sources = get_buffer_config(
            self.__vim, context["filetype"], "b:deoplete_ignore_sources", "g:deoplete#ignore_sources", "{}"
        )
        for source_name, source in sources:
            in_sources = not context["sources"] or (source_name in context["sources"])
            in_fts = not source.filetypes or (context["filetype"] in source.filetypes)
            in_ignore = source_name in ignore_sources
            if not in_sources or not in_fts or in_ignore:
                continue
            if source.disabled_syntaxes and "syntax_name" not in context:
                context["syntax_name"] = get_syn_name(self.__vim)
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(self.__vim, cont["input"], charpos)
            cont["complete_str"] = cont["input"][charpos:]
            cont["complete_position"] = charpos2bytepos(self.__vim, cont["input"], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = source.min_pattern_length
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length

            if charpos < 0 or self.is_skip(
                cont, source.disabled_syntaxes, min_pattern_length, source.max_pattern_length, source.input_pattern
            ):
                # Skip
                continue
            results.append({"name": source_name, "source": source, "context": cont})

        for result in results:
            context = result["context"]
            source = result["source"]

            # self.debug(source.name)
            self.profile_start(source.name)
            context["candidates"] = source.gather_candidates(context)
            self.profile_end(source.name)
            if context["candidates"] and isinstance(context["candidates"][0], str):
                # Convert to dict
                context["candidates"] = [{"word": x} for x in context["candidates"]]

            ignorecase = context["ignorecase"]
            smartcase = context["smartcase"]
            camelcase = context["camelcase"]
            try:
                # Set ignorecase
                if (smartcase or camelcase) and re.search(r"[A-Z]", context["complete_str"]):
                    context["ignorecase"] = 0

                for filter in [
                    self.__filters[x]
                    for x in source.matchers + source.sorters + source.converters
                    if x in self.__filters
                ]:
                    self.profile_start(filter.name)
                    context["candidates"] = filter.filter(context)
                    self.profile_end(filter.name)
            finally:
                context["ignorecase"] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, "on_post_filter"):
                context["candidates"] = source.on_post_filter(context)

            if context["candidates"] and not (re.match(r"\[.*\]", context["candidates"][0].get("menu", ""))):
                # Set default menu
                for candidate in context["candidates"]:
                    candidate["menu"] = source.mark + " " + candidate.get("menu", "")

            # self.debug(context['candidates'])
        return results
Пример #21
0
    def gather_results(self, context):
        # sources = ['buffer', 'neosnippet']
        # sources = ['buffer']
        sources = sorted(self.__sources.items(),
                         key=lambda x: get_custom(self.__vim, x[1].name).get(
                             'rank', x[1].rank),
                         reverse=True)
        results = []
        start_length = self.__vim.vars['deoplete#auto_complete_start_length']
        ignore_sources = get_buffer_config(self.__vim, context['filetype'],
                                           'b:deoplete_ignore_sources',
                                           'g:deoplete#ignore_sources', '{}')
        for source_name, source in sources:
            filetypes = get_custom(self.__vim,
                                   source.name).get('filetypes',
                                                    source.filetypes)

            in_sources = not context['sources'] or (source_name
                                                    in context['sources'])
            in_fts = not filetypes or (context['filetype'] in filetypes)
            in_ignore = source_name in ignore_sources
            if not in_sources or not in_fts or in_ignore:
                continue
            disabled_syntaxes = get_custom(self.__vim, source.name).get(
                'disabled_syntaxes', source.disabled_syntaxes)
            if disabled_syntaxes and 'syntax_name' not in context:
                context['syntax_name'] = get_syn_name(self.__vim)
            cont = copy.deepcopy(context)
            charpos = source.get_complete_position(cont)
            if charpos >= 0 and source.is_bytepos:
                charpos = bytepos2charpos(self.__vim, cont['input'], charpos)
            cont['complete_str'] = cont['input'][charpos:]
            cont['complete_position'] = charpos2bytepos(
                self.__vim, cont['input'], charpos)
            # self.debug(source.rank)
            # self.debug(source_name)
            # self.debug(cont['input'])
            # self.debug(charpos)
            # self.debug(cont['complete_position'])
            # self.debug(cont['complete_str'])

            min_pattern_length = get_custom(self.__vim, source.name).get(
                'min_pattern_length', source.min_pattern_length)
            if min_pattern_length < 0:
                # Use default value
                min_pattern_length = start_length
            max_pattern_length = get_custom(self.__vim, source.name).get(
                'max_pattern_length', source.max_pattern_length)
            input_pattern = get_custom(self.__vim, source.name).get(
                'input_pattern', source.input_pattern)

            if charpos < 0 or self.is_skip(cont, disabled_syntaxes,
                                           min_pattern_length,
                                           max_pattern_length, input_pattern):
                # Skip
                continue
            results.append({
                'name': source_name,
                'source': source,
                'context': cont,
            })

        for result in results:
            context = result['context']
            source = result['source']

            # self.debug(source.name)
            self.profile_start(source.name)
            context['candidates'] = source.gather_candidates(context)
            self.profile_end(source.name)
            if context['candidates'] and isinstance(context['candidates'][0],
                                                    str):
                # Convert to dict
                context['candidates'] = [{
                    'word': x
                } for x in context['candidates']]

            matchers = get_custom(self.__vim,
                                  source.name).get('matchers', source.matchers)
            sorters = get_custom(self.__vim,
                                 source.name).get('sorters', source.sorters)
            converters = get_custom(self.__vim,
                                    source.name).get('converters',
                                                     source.converters)

            ignorecase = context['ignorecase']
            smartcase = context['smartcase']
            camelcase = context['camelcase']
            try:
                # Set ignorecase
                if (smartcase or camelcase) and re.search(
                        r'[A-Z]', context['complete_str']):
                    context['ignorecase'] = 0

                for filter in [
                        self.__filters[x]
                        for x in matchers + sorters + converters
                        if x in self.__filters
                ]:
                    self.profile_start(filter.name)
                    context['candidates'] = filter.filter(context)
                    self.profile_end(filter.name)
            finally:
                context['ignorecase'] = ignorecase
            # self.debug(context['candidates'])

            # On post filter
            if hasattr(source, 'on_post_filter'):
                context['candidates'] = source.on_post_filter(context)

            if context['candidates'] and not (re.match(
                    r'\[.*\]', context['candidates'][0].get('menu', ''))):
                # Set default menu
                for candidate in context['candidates']:
                    candidate['menu'] = source.mark + ' ' + candidate.get(
                        'menu', '')

            # self.debug(context['candidates'])
        return results
Пример #22
0
    def gather_results(self, context):
        if not self.use_previous_result(context):
            self._prev_linenr = context['position'][1]
            self._prev_input = context['input']
            self._results = {}

        results = self._results

        for source in [
                x[1] for x in self.itersource(context)
                if x[1].name not in results
        ]:
            try:
                if source.disabled_syntaxes and 'syntax_names' not in context:
                    context['syntax_names'] = get_syn_names(self._vim)
                ctx = copy.deepcopy(context)
                ctx['is_async'] = False

                charpos = source.get_complete_position(ctx)
                if charpos >= 0 and source.is_bytepos:
                    charpos = bytepos2charpos(ctx['encoding'], ctx['input'],
                                              charpos)

                ctx['char_position'] = charpos
                ctx['complete_position'] = charpos2bytepos(
                    ctx['encoding'], ctx['input'], charpos)
                ctx['complete_str'] = ctx['input'][ctx['char_position']:]

                if charpos < 0 or self.is_skip(ctx, source.disabled_syntaxes,
                                               source.min_pattern_length,
                                               source.max_pattern_length,
                                               source.input_pattern):
                    # Skip
                    continue

                ctx['max_abbr_width'] = min(source.max_abbr_width,
                                            ctx['max_abbr_width'])
                ctx['max_menu_width'] = min(source.max_menu_width,
                                            ctx['max_menu_width'])
                if ctx['max_abbr_width'] > 0:
                    ctx['max_abbr_width'] = max(20, ctx['max_abbr_width'])
                if ctx['max_menu_width'] > 0:
                    ctx['max_menu_width'] = max(10, ctx['max_menu_width'])

                # Gathering
                self.profile_start(ctx, source.name)
                ctx['candidates'] = source.gather_candidates(ctx)
                self.profile_end(source.name)

                if not ctx['is_async'] and ('candidates' not in ctx
                                            or not ctx['candidates']):
                    continue

                ctx['candidates'] = convert2candidates(ctx['candidates'])

                results[source.name] = {
                    'name': source.name,
                    'source': source,
                    'context': ctx,
                    'is_async': ctx['is_async'],
                }
            except Exception:
                self._source_errors[source.name] += 1
                if self._source_errors[source.name] > 2:
                    error(
                        self._vim, 'Too many errors from "%s". '
                        'This source is disabled until Neovim '
                        'is restarted.' % source.name)
                    self._ignored_sources.add(source.path)
                    self._sources.pop(source.name)
                    continue
                error_tb(self._vim,
                         'Could not get completions from: %s' % source.name)

        return results.values()