roxma / nvim-completion-manager

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

Ruby Rcodetools integration #54

Closed sphynx79 closed 7 years ago

sphynx79 commented 7 years ago

Please can you help me to use your plugin with rcodetools.

In this moment i use https://github.com/lifepillar/vim-mucomplete and work.

set completefunc give me this 21_RCT_completion

can i use this completefunc??

roxma commented 7 years ago

Doesn't support rcodetools's completefunc

  1. Don't use vim's builtin complete_add function. If you are using NCM, popup menu should always be managed by NCM. Simply return all the matches as array.
  2. Also avoid complete_check
  3. s:RCT_completion should be defined as global function. It's hard to get the real script function name outside the script.
  4. even avoid omnifunc if it's possible.
roxma commented 7 years ago

Just looked into rcodetools's completefunc, it's not hard to convert it into ncm source

Here's the demo code, simply place it into ncm's installation directory as pythonx/cm_sources/rcodetools.py.

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

from cm import register_source, getLogger, Base

register_source(name='rcodetools',
                priority=9,
                abbreviation='rb',
                scoping=True,
                scopes=['ruby'],
                sort=0,
                cm_refresh_patterns=[r'\.$'],)

import subprocess

logger = getLogger(__name__)

class Source(Base):

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

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

        src = self.get_src(ctx)

        # use stdin for rct-complete
        proc = subprocess.Popen(args=['rct-complete',
            '--completion-class-info',
            '--dev',
            '--fork',
            '--line=%s' % ctx['lnum'],
            '--column=%s' % (ctx['col']-1),
            '--filename=%s' % ctx['filepath']
            ],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.DEVNULL)

        result, errs = proc.communicate(src.encode('utf-8'),timeout=30)
        logger.debug("result %s", result)

        result = result.decode('utf-8')
        logger.info("result %s", result)

        results = result.split("\n")
        if not results:
            return

        matches = []
        for line in results:

            # sayHi\tHelloWorld#sayHi
            fields = line.split("\t")
            if not fields:
                continue

            word = fields[0]
            menu = ''
            if len(fields)>1:
                menu = fields[1]

            if not word.strip():
                continue

            item = dict(word=word,
                        icase=1,
                        dup=1,
                        menu=menu,
                        )

            matches.append(item)

        logger.info('matches %s', matches)
        self.complete(info, ctx, ctx['startcol'], matches)

You could use this as a start, and maintain a git repo for this purpose.

I didn't put it into NCM's repo or create another repo because I just learned how to write ruby's hello-world, I'm not capable of maintaining this.

Here's the screencast:

output

sphynx79 commented 7 years ago

I'm sorry but this not work for me, i put your script in nvim\plugged\nvim-completion-manager\pythonx\cm_sources\cm_rcodetools.py and restart nvim. But not work, maybe beacause i use windows, and a i saw in your script that you use fork in subprocess.Popen, but windows not support fork.

I resolve this problem with https://github.com/osyo-manga/vim-monster, i install this plugin and put in my init.vim

au User CmSetup call cm#register_source({'name' : 'monster', \ 'priority': 9, \ 'scoping': 1, \ 'scopes': ['ruby'], \ 'abbreviation': 'rb', \ 'cm_refreshpatterns':['[^. \t].\w', '[a-zA-Z]\w*::'], \ 'cm_refresh': {'omnifunc': 'monster#omnifunc'}, \ })

2017-03-15_11-24-18

roxma commented 7 years ago

Make sure rct-complete command is availabe on your system.

You need to install rcodetools then setup PATH environment variable.

Popen is available on windows, if you are on windows, you need to change

        proc = subprocess.Popen(args=['rct-complete',

into

        proc = subprocess.Popen(args=['ruby', 'path/to/rct-complete'
roxma commented 7 years ago

If it is still not working, follow the instructions from the README#trouble-shooting section for debugging.