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 before and after colons (``:``).
19
20.. rubric:: Options
21
22* ``max-spaces-before`` defines the maximal number of spaces allowed before
23  colons (use ``-1`` to disable).
24* ``max-spaces-after`` defines the maximal number of spaces allowed after
25  colons (use ``-1`` to disable).
26
27.. rubric:: Default values (when enabled)
28
29.. code-block:: yaml
30
31 rules:
32   colons:
33     max-spaces-before: 0
34     max-spaces-after: 1
35
36.. rubric:: Examples
37
38#. With ``colons: {max-spaces-before: 0, max-spaces-after: 1}``
39
40   the following code snippet would **PASS**:
41   ::
42
43    object:
44      - a
45      - b
46    key: value
47
48#. With ``colons: {max-spaces-before: 1}``
49
50   the following code snippet would **PASS**:
51   ::
52
53    object :
54      - a
55      - b
56
57   the following code snippet would **FAIL**:
58   ::
59
60    object  :
61      - a
62      - b
63
64#. With ``colons: {max-spaces-after: 2}``
65
66   the following code snippet would **PASS**:
67   ::
68
69    first:  1
70    second: 2
71    third:  3
72
73   the following code snippet would **FAIL**:
74   ::
75
76    first: 1
77    2nd:   2
78    third: 3
79"""
80
81
82import yaml
83
84from yamllint.rules.common import is_explicit_key, spaces_after, spaces_before
85
86
87ID = 'colons'
88TYPE = 'token'
89CONF = {'max-spaces-before': int,
90        'max-spaces-after': int}
91DEFAULT = {'max-spaces-before': 0,
92           'max-spaces-after': 1}
93
94
95def check(conf, token, prev, next, nextnext, context):
96    if isinstance(token, yaml.ValueToken):
97        problem = spaces_before(token, prev, next,
98                                max=conf['max-spaces-before'],
99                                max_desc='too many spaces before colon')
100        if problem is not None:
101            yield problem
102
103        problem = spaces_after(token, prev, next,
104                               max=conf['max-spaces-after'],
105                               max_desc='too many spaces after colon')
106        if problem is not None:
107            yield problem
108
109    if isinstance(token, yaml.KeyToken) and is_explicit_key(token):
110        problem = spaces_after(token, prev, next,
111                               max=conf['max-spaces-after'],
112                               max_desc='too many spaces after question mark')
113        if problem is not None:
114            yield problem
115