Skip to content

Commit

Permalink
Merge pull request #1 from liuchengxu/master
Browse files Browse the repository at this point in the history
Use new repo link and add README.md
  • Loading branch information
roxma authored Jul 24, 2018
2 parents 5bfefb0 + e3def26 commit 7837244
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ncm2-racer

[ncm2-racer](https://github.com/ncm2/ncm2-racer) is a Rust auto-completion plugin for [ncm2](https://github.com/ncm2/ncm2).

## Requirement

- [racer](https://github.com/racer-rust/racer)

ncm2-racer depends on [racer](https://github.com/racer-rust/racer).

Ensure you have installed racer, if not, please refer to https://github.com/racer-rust/racer#installation for more information.
38 changes: 25 additions & 13 deletions pythonx/ncm2_racer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@

import vim
from ncm2 import Ncm2Source, getLogger, Popen
import re
import os
import glob
import subprocess


logger = getLogger(__name__)

class Source(Ncm2Source):

class Source(Ncm2Source):
def check(self):
from distutils.spawn import find_executable
if not find_executable("racer"):
self.nvim.call('ncm2_racer#error', 'Can not find racer for completion, you need https://github.com/phildawes/racer')
self.nvim.call(
'ncm2_racer#error',
('Can not find racer for completion, '
'please check out https://github.com/racer-rust/racer'))
if not self._check_rust_src_path():
self.nvim.call('ncm2_racer#error', '$RUST_SRC_PATH not defined, please read https://github.com/phildawes/racer#configuration' )
self.nvim.call('ncm2_racer#error', (
'$RUST_SRC_PATH not defined, '
'please read https://github.com/racer-rust/racer#configuration'
))

def _check_rust_src_path(self):
if "RUST_SRC_PATH" in os.environ:
return os.environ["RUST_SRC_PATH"]
# auto detect, if user already run `rustup component add rust-src`
found = glob.glob(os.path.expanduser(
"~/.rustup/toolchains/*/lib/rustlib/src/rust/src"))
found = glob.glob(
os.path.expanduser(
"~/.rustup/toolchains/*/lib/rustlib/src/rust/src"))
if found:
logger.info("detect RUST_SRC_PATH as [%s]", found[0])
os.environ["RUST_SRC_PATH"] = found[0]
Expand All @@ -42,11 +47,16 @@ def on_complete(self, ctx, lines):
filepath = ctx['filepath']
startccol = ctx['startccol']

args = ['racer', 'complete-with-snippet', str(lnum), str(bcol - 1), filepath, '-']
proc = Popen(args=args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
args = [
'racer', 'complete-with-snippet',
str(lnum),
str(bcol - 1), filepath, '-'
]
proc = Popen(
args=args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)

result, errs = proc.communicate(src, timeout=30)

Expand Down Expand Up @@ -79,7 +89,8 @@ def on_complete(self, ctx, lines):
match = dict(word=word)

menu = fields[6]
if "RUST_SRC_PATH" in os.environ and menu.startswith(os.environ["RUST_SRC_PATH"]):
if "RUST_SRC_PATH" in os.environ and menu.startswith(
os.environ["RUST_SRC_PATH"]):
menu = menu[len(os.environ["RUST_SRC_PATH"]):]

match['menu'] = menu
Expand All @@ -97,6 +108,7 @@ def on_complete(self, ctx, lines):

self.complete(ctx, startccol, matches)


source = Source(vim)

on_complete = source.on_complete
Expand Down

0 comments on commit 7837244

Please sign in to comment.