Skip to content

Commit

Permalink
Merge pull request #998 from Shougo/typing
Browse files Browse the repository at this point in the history
Fix typing errors
  • Loading branch information
Shougo authored Aug 4, 2019
2 parents e8cab49 + 00d4d35 commit d58f8f2
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ doc/tags
vim-themis
.cache
.mypy_cache
.pytest_cache
tags
17 changes: 10 additions & 7 deletions rplugin/python3/deoplete/base/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
# License: MIT license
# ============================================================================

import typing

from abc import abstractmethod
from deoplete.logger import LoggingMixin
from deoplete.util import error_vim
from deoplete.util import error_vim, Nvim, UserContext, Candidates


class Base(LoggingMixin):

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

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

def get_var(self, var_name):
def get_var(self, var_name: str) -> typing.Optional[typing.Any]:
custom_vars = self.vim.call(
'deoplete#custom#_get_filter', self.name)
if var_name in custom_vars:
Expand All @@ -29,8 +32,8 @@ def get_var(self, var_name):
return None

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

def print_error(self, expr):
def print_error(self, expr: typing.Any) -> None:
error_vim(self.vim, expr)
40 changes: 22 additions & 18 deletions rplugin/python3/deoplete/base/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
# ============================================================================

import re
import typing
from abc import abstractmethod

from deoplete.logger import LoggingMixin
from deoplete.util import debug, error_vim
from deoplete.util import debug, error_vim, Nvim, UserContext, Candidates


class Base(LoggingMixin):

def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
self.vim = vim
self.description = ''
self.mark = ''
self.name = ''
self.max_pattern_length = 80
self.min_pattern_length = -1
self.input_pattern = ''
self.input_patterns = {}
self.input_patterns: typing.Dict[str, str] = {}
self.matchers = ['matcher_fuzzy']
self.sorters = ['sorter_rank']
self.converters = [
Expand All @@ -28,17 +31,17 @@ def __init__(self, vim):
'converter_truncate_kind',
'converter_truncate_info',
'converter_truncate_menu']
self.filetypes = []
self.keyword_patterns = []
self.filetypes: typing.List[str] = []
self.keyword_patterns: typing.List[str] = []
self.is_debug_enabled = False
self.is_bytepos = False
self.is_initialized = False
self.is_volatile = False
self.is_silent = False
self.rank = 100
self.disabled_syntaxes = []
self.events = None
self.vars = {}
self.disabled_syntaxes: typing.List[str] = []
self.events: typing.List[str] = []
self.vars: typing.Dict[str, typing.Any] = {}
self.max_abbr_width = 80
self.max_kind_width = 40
self.max_info_width = 200
Expand All @@ -47,27 +50,27 @@ def __init__(self, vim):
self.matcher_key = ''
self.dup = False

def get_complete_position(self, context):
def get_complete_position(self, context: UserContext) -> int:
m = re.search('(?:' + context['keyword_pattern'] + ')$|$',
context['input'])
return m.start() if m else -1

def print(self, expr):
def print(self, expr: typing.Any) -> None:
if not self.is_silent:
debug(self.vim, expr)

def print_error(self, expr):
def print_error(self, expr: typing.Any) -> None:
if not self.is_silent:
error_vim(self.vim, expr)

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

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

def get_var(self, var_name):
def get_var(self, var_name: str) -> typing.Optional[typing.Any]:
custom_vars = self.vim.call(
'deoplete#custom#_get_source_vars', self.name)
if var_name in custom_vars:
Expand All @@ -76,19 +79,20 @@ def get_var(self, var_name):
return self.vars[var_name]
return None

def get_filetype_var(self, filetype, var_name):
def get_filetype_var(self, filetype: str,
var_name: str) -> typing.Optional[typing.Any]:
var = self.get_var(var_name)
if var is None:
return None
ft = filetype if (filetype in var) else '_'
return var.get(ft, '')

def get_input_pattern(self, filetype):
def get_input_pattern(self, filetype: str) -> str:
if not self.input_patterns:
return self.input_pattern

ft = filetype if (filetype in self.input_patterns) else '_'
return self.input_patterns.get(ft, self.input_pattern)

def get_buf_option(self, option):
def get_buf_option(self, option: str) -> typing.Any:
return self.vim.call('getbufvar', '%', '&' + option)
3 changes: 2 additions & 1 deletion rplugin/python3/deoplete/filter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

# For backward compatibility
from deoplete.base.filter import Base as _Base
from deoplete.util import Nvim


class Base(_Base):
def __init__(self, vim):
def __init__(self, vim: Nvim) -> None:
super().__init__(vim)
15 changes: 10 additions & 5 deletions rplugin/python3/deoplete/filter/converter_auto_delimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
# License: MIT license
# ============================================================================

import typing

from deoplete.base.filter import Base
from deoplete.util import Nvim, UserContext, Candidates


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

self.name = 'converter_auto_delimiter'
Expand All @@ -17,19 +20,21 @@ def __init__(self, vim):
'delimiters': ['/'],
}

def filter(self, context):
delimiters = self.get_var('delimiters')
def filter(self, context: UserContext) -> Candidates:
delimiters: typing.List[str] = self.get_var( # type: ignore
'delimiters')
for candidate, delimiter in [
[x, last_find(x['abbr'], delimiters)]
for x in context['candidates']
if 'abbr' in x and x['abbr'] and
not last_find(x['word'], delimiters) and
last_find(x['abbr'], delimiters)]:
candidate['word'] += delimiter
return context['candidates']
return context['candidates'] # type: ignore


def last_find(s, needles):
def last_find(s: str, needles: typing.List[str]) -> typing.Optional[str]:
for needle in needles:
if len(s) >= len(needle) and s[-len(needle):] == needle:
return needle
return None
8 changes: 5 additions & 3 deletions rplugin/python3/deoplete/filter/converter_auto_paren.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
# ============================================================================

import re

from deoplete.base.filter import Base
from deoplete.util import Nvim, UserContext, Candidates


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

self.name = 'converter_auto_paren'
self.description = 'auto add parentheses converter'

def filter(self, context):
def filter(self, context: UserContext) -> Candidates:
p1 = re.compile(r'\(\)?$')
p2 = re.compile(r'\(.*\)')
for candidate in [
Expand All @@ -24,4 +26,4 @@ def filter(self, context):
(('abbr' in x and p2.search(x['abbr'])) or
('info' in x and p2.search(x['info'])))]:
candidate['word'] += '('
return context['candidates']
return context['candidates'] # type: ignore
14 changes: 8 additions & 6 deletions rplugin/python3/deoplete/filter/converter_remove_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
# ============================================================================

import re

from deoplete.base.filter import Base
from deoplete.util import Nvim, UserContext, Candidates


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

self.name = 'converter_remove_overlap'
self.description = 'remove overlap converter'

def filter(self, context):
def filter(self, context: UserContext) -> Candidates:
if not context['next_input']:
return context['candidates']
return context['candidates'] # type: ignore
m = re.match(r'\S+', context['next_input'])
if not m:
return context['candidates']
return context['candidates'] # type: ignore
next_input = m.group(0)
for [overlap, candidate] in [
[x, y] for x, y
Expand All @@ -29,10 +31,10 @@ def filter(self, context):
if 'abbr' not in candidate:
candidate['abbr'] = candidate['word']
candidate['word'] = candidate['word'][: -overlap]
return context['candidates']
return context['candidates'] # type: ignore


def overlap_length(left, right):
def overlap_length(left: str, right: str) -> int:
pos = len(right)
while pos > 0 and not left.endswith(right[: pos]):
pos -= 1
Expand Down
8 changes: 5 additions & 3 deletions rplugin/python3/deoplete/filter/converter_remove_paren.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
# ============================================================================

import re

from deoplete.base.filter import Base
from deoplete.util import Nvim, UserContext, Candidates


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

self.name = 'converter_remove_paren'
self.description = 'remove parentheses converter'

def filter(self, context):
def filter(self, context: UserContext) -> Candidates:
for candidate in [x for x in context['candidates']
if '(' in x['word']]:
candidate['word'] = re.sub(r'\(.*\)(\$\d+)?', '',
candidate['word'])
return context['candidates']
return context['candidates'] # type: ignore
19 changes: 12 additions & 7 deletions rplugin/python3/deoplete/filter/converter_reorder_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
# License: MIT license
# ============================================================================

from deoplete.base.filter import Base
import re
import typing

from deoplete.base.filter import Base
from deoplete.util import Nvim, UserContext, Candidates


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

self.name = 'converter_reorder_attr'
Expand All @@ -19,7 +22,9 @@ def __init__(self, vim):
}

@staticmethod
def filter_attrs(candidates, preferred_order_attrs, max_list_size=500):
def filter_attrs(candidates: Candidates,
preferred_order_attrs: typing.Dict[str, typing.Any],
max_list_size: int = 500) -> Candidates:
context_candidates = candidates[:]
new_candidates = []
new_candidates_len = 0
Expand Down Expand Up @@ -57,11 +62,11 @@ def filter_attrs(candidates, preferred_order_attrs, max_list_size=500):

return new_candidates

def filter(self, context):
preferred_order_attrs = self.get_var('attrs_order').get(
context['filetype'], [])
def filter(self, context: UserContext) -> Candidates:
preferred_order_attrs = self.get_var( # type: ignore
'attrs_order').get(context['filetype'], [])
if not context['candidates'] or not preferred_order_attrs:
return context['candidates']
return context['candidates'] # type: ignore

max_list_size = self.vim.call(
'deoplete#custom#_get_option', 'max_list'
Expand Down
10 changes: 5 additions & 5 deletions rplugin/python3/deoplete/filter/converter_truncate_abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
# ============================================================================

from deoplete.base.filter import Base
from deoplete.util import truncate_skipping
from deoplete.util import truncate_skipping, Nvim, UserContext, Candidates


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

self.name = 'converter_truncate_abbr'
self.description = 'truncate abbr converter'

def filter(self, context):
def filter(self, context: UserContext) -> Candidates:
max_width = context['max_abbr_width']
if max_width <= 0:
return context['candidates']
return context['candidates'] # type: ignore

footer_width = max_width / 3
for candidate in context['candidates']:
candidate['abbr'] = truncate_skipping(
candidate.get('abbr', candidate['word']),
max_width, '..', footer_width)
return context['candidates']
return context['candidates'] # type: ignore
Loading

0 comments on commit d58f8f2

Please sign in to comment.