Skip to content

Commit

Permalink
Merge pull request #157 from Kuniwak/neovim-support
Browse files Browse the repository at this point in the history
Neovim support
  • Loading branch information
Kuniwak committed Feb 12, 2016
2 parents 41ede2a + 90fd350 commit 4fcd842
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 14 deletions.
16 changes: 13 additions & 3 deletions dev_tool/show_ast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import sys
from argparse import ArgumentParser
from pathlib import Path
from pprint import pprint

Expand All @@ -17,8 +18,17 @@ def prettify_node_type(node):


if __name__ == '__main__':
ast = Parser().parse_file(Path(sys.argv[1]))
arg_parser = ArgumentParser(prog='show_ast', description='Show AST')
arg_parser.add_argument('--enable-neovim', action='store_true', help='Enable Neovim syntax')
arg_parser.add_argument('files', nargs='*', help='File to parse')
namespace = vars(arg_parser.parse_args(sys.argv[1:]))

traverse(ast, on_enter=prettify_node_type)
filepaths = map(Path, namespace['files'])
enable_neovim = namespace['enable_neovim']

pprint(ast)
parser = Parser(enable_neovim=enable_neovim)

for filepath in filepaths:
ast = parser.parse_file(filepath)
traverse(ast, on_enter=prettify_node_type)
pprint(ast)
1 change: 1 addition & 0 deletions test/fixture/ast/fixture_to_parse_neovim.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tnoremap <C-h> <C-\><C-n><C-w>h
7 changes: 7 additions & 0 deletions test/unit/vint/ast/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
FIXTURE_FILE = get_fixture_path('fixture_to_parse.vim')
FIXTURE_FILE_EMPTY = get_fixture_path('fixture_to_parse_empty_file.vim')
FIXTURE_FILE_FF_DOS_FENC_CP932 = get_fixture_path('fixture_to_parse_windows.vim')
FIXTURE_FILE_NEOVIM = get_fixture_path('fixture_to_parse_neovim.vim')


class TestParser(unittest.TestCase):
Expand All @@ -21,6 +22,12 @@ def test_parse_file_on_ff_dos_and_fenc_cp932(self):
self.assertIs(ast['type'], 1)


def test_parse_file_when_neovim_enabled(self):
parser = Parser(enable_neovim=True)
ast = parser.parse_file(FIXTURE_FILE_NEOVIM)
self.assertIs(ast['type'], 1)


def test_parse_empty_file(self):
parser = Parser()
ast = parser.parse_file(FIXTURE_FILE_EMPTY)
Expand Down
51 changes: 51 additions & 0 deletions test/unit/vint/linting/config/test_config_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest

from vint.linting.config.config_util import get_config_value


class TestConfigUtil(unittest.TestCase):
def test_get_config_value_when_empty(self):
config_dict = {}

self.assertIsNone(get_config_value(config_dict, ['a']))


def test_get_config_value_when_nested_1_depth(self):
config_dict = {'a': 'A'}

self.assertIs(get_config_value(config_dict, ['a']), 'A')


def test_get_config_value_when_nested_2_depth(self):
config_dict = {'a': {'b': 'B'}}

self.assertIs(get_config_value(config_dict, ['a', 'b']), 'B')


def test_get_config_value_when_target_is_dict(self):
config_dict = {'a': {'b': 'B'}}

self.assertEqual(get_config_value(config_dict, ['a']), {'b': 'B'})


def test_get_config_value_when_target_is_depth_2_unexistent_dict(self):
config_dict = {'a': 'A'}

self.assertEqual(get_config_value(config_dict, ['c', 'b']), None)


def test_get_config_value_when_given_default(self):
config_dict = {'a': 'A'}

self.assertEqual(get_config_value(config_dict, ['c', 'b'], 'DEFAULT'), 'DEFAULT')


def test_get_config_value_when_given_default_but_not_used(self):
config_dict = {'a': 'A'}

self.assertEqual(get_config_value(config_dict, ['a'], 'DEFAULT'), 'A')



if __name__ == '__main__':
unittest.main()
54 changes: 47 additions & 7 deletions vint/_bundles/vimlparser.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions vint/asset/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmdargs:
verbose: no
severity: warning
error-limit: 50
env:
neovim: no

policies:
ProhibitImplicitScopeVariable:
Expand Down
5 changes: 3 additions & 2 deletions vint/ast/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ def __str__(self):


class Parser(object):
def __init__(self, plugins=None):
def __init__(self, plugins=None, enable_neovim=False):
""" Initialize Parser with the specified plugins.
The plugins can add attributes to the AST.
"""
self.plugins = plugins.values() if plugins else []
self._enable_neovim = enable_neovim


def parse(self, string):
""" Parse vim script string and return the AST. """
lines = string.split('\n')

reader = vimlparser.StringReader(lines)
parser = vimlparser.VimLParser()
parser = vimlparser.VimLParser(self._enable_neovim)
ast = parser.parse(reader)

# TOPLEVEL does not have a pos, but we need pos for all nodes
Expand Down
1 change: 1 addition & 0 deletions vint/linting/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _build_argparser(self):
parser.add_argument('-c', '--color', action='store_true', help='colorize output when possible')
parser.add_argument('-j', '--json', action='store_true', help='output json style')
parser.add_argument('-t', '--stat', action='store_true', help='output statistic info')
parser.add_argument('--enable-neovim', action='store_true', help='Enable Neovim syntax')
parser.add_argument('-f', '--format', help='set output format')
parser.add_argument('files', nargs='*', help='file or directory path to lint')

Expand Down
11 changes: 11 additions & 0 deletions vint/linting/config/config_cmdargs_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def _build_config_dict(self, env):
config_dict = self._normalize_severity(env, config_dict)
config_dict = self._normalize_max_violations(env, config_dict)
config_dict = self._normalize_format(env, config_dict)
config_dict = self._normalize_env(env, config_dict)

return config_dict

Expand Down Expand Up @@ -77,3 +78,13 @@ def _normalize_severity(self, env, config_dict):
config_dict_cmdargs['severity'] = Level.ERROR

return config_dict


def _normalize_env(self, env, config_dict):
env_cmdargs = env['cmdargs']
config_dict_cmdargs = config_dict['cmdargs']

if 'enable_neovim' in env_cmdargs:
config_dict_cmdargs['env'] = {'neovim': True}

return config_dict
10 changes: 10 additions & 0 deletions vint/linting/config/config_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def get_config_value(config_dict, keys, default=None):
inner_dict = config_dict

for key in keys[:-1]:
if key not in inner_dict:
return default

inner_dict = inner_dict[key]

return inner_dict.get(keys[-1], default)
9 changes: 7 additions & 2 deletions vint/linting/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from vint.linting.config.config_container import ConfigContainer
from vint.linting.config.config_dict_source import ConfigDictSource
from vint.linting.config.config_comment_source import ConfigCommentSource
from vint.linting.config.config_util import get_config_value
from vint.linting.level import Level


Expand All @@ -31,18 +32,22 @@ def __init__(self, policy_set, config_dict_global):
self._plugins = {
'scope': ScopePlugin(),
}
self._parser = self.build_parser()
self._policy_set = policy_set

self._config_comment_source = ConfigCommentSource()
self._config = self._decorate_config(config_dict_global,
self._config_comment_source)

self._parser = self.build_parser()

self._listeners_map = {}


def build_parser(self):
parser = Parser(self._plugins)
config_dict = self._config.get_config_dict()
enable_neovim = get_config_value(config_dict, ['cmdargs', 'env', 'neovim'], False)

parser = Parser(self._plugins, enable_neovim=enable_neovim)
return parser


Expand Down

0 comments on commit 4fcd842

Please sign in to comment.