Skip to content

Commit

Permalink
Merge pull request #698 from Shougo/typing
Browse files Browse the repository at this point in the history
[RDY] typing
  • Loading branch information
Shougo committed Aug 13, 2019
2 parents fb56c2c + 1bbdd74 commit 5d9798c
Show file tree
Hide file tree
Showing 67 changed files with 805 additions and 691 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lint:
flake8 --version
flake8 rplugin autoload/denite/_main.py
mypy --version
mypy --ignore-missing-imports --follow-imports=skip rplugin/python3/denite
mypy --ignore-missing-imports --follow-imports=skip --strict rplugin/python3/denite

test:
themis --version
Expand Down
37 changes: 21 additions & 16 deletions rplugin/python3/denite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# License: MIT license
# =============================================================================

import typing

from importlib.util import find_spec
from denite.rplugin import Rplugin
from denite.util import Nvim

if find_spec('yarp'):
import vim
Expand All @@ -16,49 +19,51 @@
import neovim
vim = neovim

Args = typing.List[typing.Any]

if hasattr(vim, 'plugin'):
# Neovim only

@vim.plugin
class DeniteHandlers(object):
def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
self._rplugin = Rplugin(vim)

@vim.function('_denite_init', sync=True)
def init_channel(self, args):
@vim.function('_denite_init', sync=True) # type: ignore
def init_channel(self, args: Args) -> None:
self._rplugin.init_channel(args)

@vim.rpc_export('_denite_start', sync=True)
def start(self, args):
@vim.rpc_export('_denite_start', sync=True) # type: ignore
def start(self, args: Args) -> None:
self._rplugin.start(args)

@vim.rpc_export('_denite_do_action', sync=True)
def do_action(self, args):
@vim.rpc_export('_denite_do_action', sync=True) # type: ignore
def do_action(self, args: Args) -> typing.Any:
return self._rplugin.do_action(args)

@vim.rpc_export('_denite_do_map', sync=True)
def do_map(self, args):
@vim.rpc_export('_denite_do_map', sync=True) # type: ignore
def do_map(self, args: Args) -> typing.Any:
return self._rplugin.do_map(args)

@vim.rpc_export('_denite_do_async_map', sync=False)
def do_async_map(self, args):
@vim.rpc_export('_denite_do_async_map', sync=False) # type: ignore
def do_async_map(self, args: Args) -> typing.Any:
return self._rplugin.do_map(args)

if find_spec('yarp'):

global_denite = Rplugin(vim)

def _denite_init():
def _denite_init() -> None:
pass

def _denite_start(args):
def _denite_start(args: Args) -> None:
global_denite.start(args)

def _denite_do_action(args):
def _denite_do_action(args: Args) -> typing.Any:
return global_denite.do_action(args)

def _denite_do_map(args):
def _denite_do_map(args: Args) -> typing.Any:
return global_denite.do_map(args)

def _denite_do_async_map(args):
def _denite_do_async_map(args: Args) -> typing.Any:
return global_denite.do_map(args)
9 changes: 5 additions & 4 deletions rplugin/python3/denite/aprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
# ============================================================================

import asyncio
import typing


class Process(asyncio.SubprocessProtocol):

def __init__(self, plugin):
def __init__(self, plugin: typing.Any) -> None:
self._plugin = plugin
self._vim = plugin._vim

def connection_made(self, transport):
def connection_made(self, transport: typing.Any) -> None:
self._unpacker = self._plugin._connect_stdin(
transport.get_pipe_transport(0))

def pipe_data_received(self, fd, data):
def pipe_data_received(self, fd: int, data: typing.Any) -> None:
unpacker = self._unpacker
unpacker.feed(data)
for child_out in unpacker:
self._plugin._queue_out.put(child_out)

def process_exited(self):
def process_exited(self) -> None:
pass
Empty file.
17 changes: 10 additions & 7 deletions rplugin/python3/denite/base/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
# License: MIT license
# ============================================================================

import typing
from abc import ABC, abstractmethod

import denite.util
from denite.util import Nvim, UserContext, Candidates


class Base(ABC):

def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
self.vim = vim
self.name = 'base'
self.description = ''
self.vars = {}
self.vars: typing.Dict[str, typing.Any] = {}

@abstractmethod
def filter(self, context):
def filter(self, context: UserContext) -> Candidates:
pass

def convert_pattern(self, input_str):
def convert_pattern(self, input_str: str) -> str:
return ''

def debug(self, expr):
def debug(self, expr: str) -> None:
denite.util.debug(self.vim, expr)

def print_message(self, context, expr):
def print_message(self, context: UserContext, expr: typing.Any) -> None:
context['messages'].append(self.name + ': ' + str(expr))

def error_message(self, context, expr):
def error_message(self, context: UserContext, expr: typing.Any) -> None:
prefix = self.name + ': '
if isinstance(expr, list):
for line in expr:
Expand Down
34 changes: 19 additions & 15 deletions rplugin/python3/denite/base/kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,71 @@
# License: MIT license
# ============================================================================

import typing

import denite.util
from denite.util import Nvim, UserContext


class Base(object):

def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
self.vim = vim
self.name = 'base'
self.default_action = 'echo'
self.persist_actions = ['echo', 'preview']
self.redraw_actions = []
self.persist_actions: typing.Union[str, typing.List[str]] = [
'echo', 'preview']
self.redraw_actions: typing.Union[str, typing.List[str]] = []

def debug(self, expr):
def debug(self, expr: str) -> None:
denite.util.debug(self.vim, expr)

def action_echo(self, context):
def action_echo(self, context: UserContext) -> None:
self.vim.command('redraw')
for target in context['targets']:
self.debug(target)
self.vim.call('denite#util#getchar')

def action_yank(self, context):
def action_yank(self, context: UserContext) -> None:
_yank(self.vim, "\n".join([
x['word'] for x in context['targets']
]))

def action_ex(self, context):
def action_ex(self, context: UserContext) -> None:
_ex(self.vim, "\n".join([
x['word'] for x in context['targets']
]))

def action_replace(self, context):
def action_replace(self, context: UserContext) -> None:
self.vim.command('normal! viw')
self.action_append(context)

def action_append(self, context):
def action_append(self, context: UserContext) -> None:
for target in context['targets']:
_paste(self.vim,
target.get('action__text', target['word']), 'p', 'v')

def get_action_names(self):
def get_action_names(self) -> typing.List[str]:
return ['default'] + [x.replace('action_', '') for x in dir(self)
if x.find('action_') == 0]

def action_preview(self, context):
def action_preview(self, context: UserContext) -> None:
pass


class Kind(Base):

def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
super().__init__(vim)


def _yank(vim, word):
def _yank(vim: Nvim, word: str) -> None:
vim.call('setreg', '"', word, 'v')
if vim.call('has', 'clipboard'):
vim.call('setreg', vim.eval('v:register'), word, 'v')


def _ex(vim, word):
def _ex(vim: Nvim, word: str) -> None:
# NOTE:
# <C-b> (\x02) in a command-line move the caret to the beginning.
# Somehow the key above works in 'input()' function as well.
Expand All @@ -73,7 +77,7 @@ def _ex(vim, word):
vim.command(expr)


def _paste(vim, word, command, regtype):
def _paste(vim: Nvim, word: str, command: str, regtype: str) -> None:
if regtype == '':
regtype = 'v'

Expand Down
30 changes: 16 additions & 14 deletions rplugin/python3/denite/base/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,43 @@
# License: MIT license
# ============================================================================

import typing
from abc import ABC, abstractmethod
import denite.util
from denite.util import Nvim, UserContext, Candidates

from denite.base.kind import Base as Kind


class Base(ABC):

def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
self.vim = vim
self.name = 'base'
self.syntax_name = ''
self.kind = Kind(self.vim)
self.kind: typing.Union[str, Kind] = Kind(self.vim)
self.default_action = 'default'
self.max_candidates = 1000
self.matchers = ['matcher/fuzzy']
self.sorters = ['sorter/rank']
self.converters = []
self.context = {}
self.vars = {}
self.matchers: typing.List[str] = ['matcher/fuzzy']
self.sorters: typing.List[str] = ['sorter/rank']
self.converters: typing.List[str] = []
self.context: UserContext = {}
self.vars: typing.Dict[str, typing.Any] = {}
self.is_public_context = False
self.is_volatile = False

def highlight(self):
def highlight(self) -> None:
pass

def define_syntax(self):
def define_syntax(self) -> None:
self.vim.command(
'syntax region ' + self.syntax_name + ' start=https:// end=/$/ '
'contains=deniteMatchedRange contained')

def print_message(self, context, expr):
def print_message(self, context: UserContext, expr: typing.Any) -> None:
context['messages'].append(self.name + ': ' + str(expr))

def error_message(self, context, expr):
def error_message(self, context: UserContext, expr: typing.Any) -> None:
prefix = self.name + ': '
if isinstance(expr, list):
for line in expr:
Expand All @@ -48,12 +50,12 @@ def error_message(self, context, expr):
context['error_messages'].append(prefix + str(expr))

@abstractmethod
def gather_candidates(self, context):
def gather_candidates(self, context: UserContext) -> Candidates:
pass

def debug(self, expr):
def debug(self, expr: str) -> None:
denite.util.debug(self.vim, expr)

def get_status(self, context):
def get_status(self, context: UserContext) -> str:
return ':'.join([self.name] +
([str(context['args'])] if context['args'] else []))
Loading

0 comments on commit 5d9798c

Please sign in to comment.