roxma / nvim-completion-manager

:warning: PLEASE USE https://github.com/ncm2/ncm2 INSTEAD
MIT License
917 stars 49 forks source link

implement natural language source #90

Closed 257 closed 6 years ago

257 commented 7 years ago

probably brain-dead simple but just don't have the time to do it. this most useful for people who are learning a new language and need some help to write a simple email.

it would be nice to see a simple interface for adding system exec's also since look(1) and trans(1) seem like natural candidates here.

257 commented 7 years ago

a rough sketch for now:

# -*- coding: utf-8 -*-

# For debugging, use this command to start neovim:
#
# NVIM_PYTHON_LOG_FILE=nvim.log NVIM_PYTHON_LOG_LEVEL=INFO nvim
#
#
# Please register source before executing any other code, this allow cm_core to
# read basic information about the source without loading the whole module, and
# modules required by this module
from cm import register_source, getLogger, Base
register_source(name='look',
        priority=9,
        scoping=0,
        abbreviation='look',
        word_pattern=r'[\w/]+',
        cm_refresh_patterns=[r'\.$'],)

import json
import os
import subprocess
import glob

logger = getLogger(__name__)

class Source(Base):

    def __init__(self, nvim):
        super(Source, self).__init__(nvim)

        # dependency check
        try:
            from distutils.spawn import find_executable
            if not find_executable("look"):
                self.message('error', 'Can not find racer for completion, you need look(1)')
            if not find_executable("trans"):
                self.message('error', 'Can not find trans for completion, you need trans(1)')
        except Exception as ex:
            logger.exception(ex)

    def cm_refresh(self, info, ctx, *args):

        '''
        lnum = ctx['lnum']
        col = ctx['col']
        filepath = ctx['filepath']
        startcol = ctx['startcol']

        logger.debug("LOOK ctx['base']: [%s]", ctx['base'])
        logger.debug("LOOK base: [%s]", base.decode('utf-8'))
        logger.debug("LOOK len(base): [%d]", len(base))
        '''
        base = (ctx['base']).encode('utf-8')
        args = ['/usr/lib/plan9/bin/look', base]

        proc = subprocess.Popen(args=args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        result, errs = proc.communicate('', timeout=1)
        '''
        logger.debug("LOOK args: [%s]", args)
        logger.debug("LOOK errs: [%s]", errs.decode('utf-8'))
        logger.debug("LOOK result: [%s]", result.decode('utf-8'))
        logger.debug("LOOK lines: [%s]", matches)
        '''

        logger.debug("args: %s, result: [%s]", args, result.decode())

        matches = result.decode('utf-8').splitlines()

        self.complete(info, ctx, ctx['startcol'], matches)
257 commented 7 years ago

working on trans(1) version of it now. any suggestions?

roxma commented 7 years ago

Looks good to me. Should be ready after some cleanup.

balta2ar commented 7 years ago

@Shougo could the same be done in deoplete? Could you please provide an example on how to configure completion from /usr/share/dict/words file? I couldn't find it in the docs...

Shougo commented 7 years ago

@balta2ar No configuration. neco-look does not specify the file argument, so /usr/share/dict/words is used. It is neco-look issue. I don't understand the your reason though.

balta2ar commented 7 years ago

@Shougo I've installed neco-look and it's using /usr/share/dict/words just fine, completion is working for English words. But what if I have my own dictionary with my own words? Like these: https://github.com/Shougo/shougo-s-github/tree/master/vim/dict

How do I configure completions from a dictionary file? I thought file or dictionary sources are for that purpose. But I did dictionary+=/path/to/dictionary in vimrc and it didn't seem to help (at least deoplete did not provide any new completions). Now I don't quite understand how to use file and dictionary sources. Could you maybe extend documentation and give examples, please?

Shougo commented 7 years ago

Note: This is nvim-completion-manager issue page instead of deoplete.

I think you should use dictionary source instead. Dictionary source works well for me.

But I did dictionary+=/path/to/dictionary in vimrc and it didn't seem to help (at least deoplete did not provide any new completions).

Dictionary source uses buffer local 'dictionary' option. If you think you have configured deoplete properly, please create new issue in deoplete. I will test it.

ghost commented 7 years ago

Hey, I have tried to sort results from script above by length by adding matches.sort(key=len) but it seems like NCM does sorting by itself. Can that be changed? Thanks.

roxma commented 6 years ago

@katsika

Sorry for the late reply. I missed the notification.

Use a sort=0 option when you register the source. It will disable NCM's sorting.

roxma commented 6 years ago

I'm closing this issue due to inactivity.