Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Mar 20, 2016
1 parent 524b031 commit 1fedd12
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
License: MIT license
AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions autoload/denite/custom.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"=============================================================================
" FILE: custom.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================

function! denite#custom#get(source_name) abort "{{{
let source = copy(denite#custom#get_source_var(a:source_name))
return extend(source, s:custom._, 'keep')
endfunction"}}}

function! denite#custom#get_source_var(source_name) abort "{{{
if !exists('s:custom')
let s:custom = {}
let s:custom._ = {}
endif

if !has_key(s:custom, a:source_name)
let s:custom[a:source_name] = {}
endif

return s:custom[a:source_name]
endfunction"}}}

function! denite#custom#set(source_name, option_name, value) abort "{{{
for key in split(a:source_name, '\s*,\s*')
let custom_source = denite#custom#get_source_var(key)
let custom_source[a:option_name] = a:value
endfor
endfunction"}}}

" vim: foldmethod=marker
36 changes: 36 additions & 0 deletions autoload/denite/util.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"=============================================================================
" FILE: util.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================

function! denite#util#set_default(var, val, ...) abort "{{{
if !exists(a:var) || type({a:var}) != type(a:val)
let alternate_var = get(a:000, 0, '')

let {a:var} = exists(alternate_var) ?
\ {alternate_var} : a:val
endif
endfunction"}}}
function! denite#util#print_error(string) abort "{{{
echohl Error | echomsg '[denite] ' . a:string | echohl None
endfunction"}}}
function! denite#util#print_warning(string) abort "{{{
echohl WarningMsg | echomsg '[denite] ' . a:string | echohl None
endfunction"}}}

function! denite#util#convert2list(expr) abort "{{{
return type(a:expr) ==# type([]) ? a:expr : [a:expr]
endfunction"}}}

function! denite#util#redir(cmd) abort "{{{
let [save_verbose, save_verbosefile] = [&verbose, &verbosefile]
set verbose=0 verbosefile=
redir => res
silent! execute a:cmd
redir END
let [&verbose, &verbosefile] = [save_verbose, save_verbosefile]
return res
endfunction"}}}

" vim: foldmethod=marker
Empty file added rplugin/python3/denite.py
Empty file.
27 changes: 27 additions & 0 deletions rplugin/python3/denite/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# =============================================================================
# FILE: denite.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# =============================================================================

import neovim
from denite.denite import Denite


@neovim.plugin
class DeniteHandlers(object):
def __init__(self, vim):
self.__vim = vim

@neovim.function('_denite', sync=True)
def init_python(self, args):
self.__denite = Denite(self.__vim)
self.__vim.vars['denite#_channel_id'] = self.__vim.channel_id
pass

@neovim.command('Denite', sync=True, nargs='*')
def start(self, args):
self.__denite = Denite(self.__vim)
self.__vim.vars['denite#_channel_id'] = self.__vim.channel_id
self.__vim.vars['denite#args'] = args
self.__denite.start({})
104 changes: 104 additions & 0 deletions rplugin/python3/denite/denite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# ============================================================================
# FILE: denite.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

from denite.util import error, globruntime, get_custom

import denite.sources
import denite.filters

import importlib.machinery
import os.path
import copy
import traceback

denite.sources # silence pyflakes
denite.filters # silence pyflakes


class Denite(object):

def __init__(self, vim):
self.__vim = vim
self.__filters = {}
self.__sources = {}
self.__runtimepath = ''

def start(self, context):
if self.__vim.options['runtimepath'] != self.__runtimepath:
# Recache
self.load_sources()
self.load_filters()
self.__runtimepath = self.__vim.options['runtimepath']

try:
# start = time.time()
candidates = self.gather_candidates(context)
self.__vim.current.buffer.append([x['word'] for x in candidates])
# self.error(str(time.time() - start))
except Exception:
for line in traceback.format_exc().splitlines():
error(self.__vim, line)
error(self.__vim,
'An error has occurred. Please execute :messages command.')
candidates = []

self.__vim.vars['denite#_context'] = {
'candidates': candidates,
}

def gather_candidates(self, context):
sources = self.__sources.items()
results = []
for source_name, source in sources:
cont = copy.deepcopy(context)

cont['candidates'] = source.gather_candidates(context)
results += cont['candidates']
return results

def debug(self, expr):
denite.util.debug(self.__vim, expr)

def error(self, msg):
self.__vim.call('denite#util#print_error', msg)

def load_sources(self):
# Load sources from runtimepath
for path in globruntime(self.__vim,
'rplugin/python3/denite/sources/base.py'
) + globruntime(
self.__vim,
'rplugin/python3/denite/sources/*.py'):
name = os.path.basename(path)[: -3]
module = importlib.machinery.SourceFileLoader(
'denite.sources.' + name, path).load_module()
if not hasattr(module, 'Source') or name in self.__sources:
continue

source = module.Source(self.__vim)

# Set the source attributes.
source.matchers = get_custom(
self.__vim, source.name).get('matchers', source.matchers)
source.sorters = get_custom(self.__vim, source.name).get(
'sorters', source.sorters)
source.converters = get_custom(self.__vim, source.name).get(
'converters', source.converters)

self.__sources[name] = source

def load_filters(self):
# Load filters from runtimepath
for path in globruntime(self.__vim,
'rplugin/python3/denite/filters/base.py'
) + globruntime(
self.__vim,
'rplugin/python3/denite/filters/*.py'):
name = os.path.basename(path)[: -3]
filter = importlib.machinery.SourceFileLoader(
'denite.filters.' + name, path).load_module()
if hasattr(filter, 'Filter') and name not in self.__filters:
self.__filters[name] = filter.Filter(self.__vim)
23 changes: 23 additions & 0 deletions rplugin/python3/denite/filters/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ============================================================================
# FILE: base.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

from abc import abstractmethod
import denite.util


class Base(object):

def __init__(self, vim):
self.vim = vim
self.name = 'base'
self.description = ''

@abstractmethod
def filter(self, context):
pass

def debug(self, expr):
denite.util.debug(self.vim, expr)
26 changes: 26 additions & 0 deletions rplugin/python3/denite/sources/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ============================================================================
# FILE: base.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

from abc import abstractmethod
import denite.util


class Base(object):

def __init__(self, vim):
self.vim = vim
self.name = 'base'
self.matchers = []
self.sorters = []
self.converters = []
self.rank = 100

@abstractmethod
def gather_candidate(self, context):
pass

def debug(self, expr):
denite.util.debug(self.vim, expr)
22 changes: 22 additions & 0 deletions rplugin/python3/denite/sources/rec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ============================================================================
# FILE: rec.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

from .base import Base
import subprocess


class Source(Base):

def __init__(self, vim):
Base.__init__(self, vim)

self.name = 'rec'

def gather_candidates(self, context):
args = ['find', '-L']
return [{'word': x, 'action__path': x}
for x in subprocess.check_output(args).decode(
'utf-8').split('\n')]
48 changes: 48 additions & 0 deletions rplugin/python3/denite/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ============================================================================
# FILE: util.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================

import json
import os
import sys


def set_default(vim, var, val):
return vim.call('denite#util#set_default', var, val)


def convert2list(expr):
return (expr if isinstance(expr, list) else [expr])


def globruntime(vim, path):
return vim.funcs.globpath(vim.options['runtimepath'], path, 1, 1)


def debug(vim, expr):
try:
json_data = json.dumps(str(expr).strip())
except Exception:
vim.command('echomsg string(\'' + str(expr).strip() + '\')')
else:
vim.command('echomsg string(\'' + escape(json_data) + '\')')


def error(vim, msg):
vim.call('denite#util#print_error', msg)


def escape(expr):
return expr.replace("'", "''")


def get_custom(vim, source_name):
return vim.call('denite#custom#get', source_name)


def load_external_module(file, module):
current = os.path.dirname(os.path.abspath(file))
module_dir = os.path.join(os.path.dirname(current), module)
sys.path.insert(0, module_dir)
6 changes: 6 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -e

nosetests -v rplugin/python3
flake8 rplugin/

0 comments on commit 1fedd12

Please sign in to comment.