1# -*- coding: utf-8 -*-
2# Copyright (C) 2016 Adrien Vergé
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17"""
18Use this rule to control the number of spaces after hyphens (``-``).
19
20.. rubric:: Options
21
22* ``max-spaces-after`` defines the maximal number of spaces allowed after
23  hyphens.
24
25.. rubric:: Default values (when enabled)
26
27.. code-block:: yaml
28
29 rules:
30   hyphens:
31     max-spaces-after: 1
32
33.. rubric:: Examples
34
35#. With ``hyphens: {max-spaces-after: 1}``
36
37   the following code snippet would **PASS**:
38   ::
39
40    - first list:
41        - a
42        - b
43    - - 1
44      - 2
45      - 3
46
47   the following code snippet would **FAIL**:
48   ::
49
50    -  first list:
51         - a
52         - b
53
54   the following code snippet would **FAIL**:
55   ::
56
57    - - 1
58      -  2
59      - 3
60
61#. With ``hyphens: {max-spaces-after: 3}``
62
63   the following code snippet would **PASS**:
64   ::
65
66    -   key
67    -  key2
68    - key42
69
70   the following code snippet would **FAIL**:
71   ::
72
73    -    key
74    -   key2
75    -  key42
76"""
77
78
79import yaml
80
81from yamllint.rules.common import spaces_after
82
83
84ID = 'hyphens'
85TYPE = 'token'
86CONF = {'max-spaces-after': int}
87DEFAULT = {'max-spaces-after': 1}
88
89
90def check(conf, token, prev, next, nextnext, context):
91    if isinstance(token, yaml.BlockEntryToken):
92        problem = spaces_after(token, prev, next,
93                               max=conf['max-spaces-after'],
94                               max_desc='too many spaces after hyphen')
95        if problem is not None:
96            yield problem
97