1 //          Copyright Brian Schott 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 module dfmt.config;
7 
8 import dfmt.editorconfig;
9 
10 /// Brace styles
11 enum BraceStyle
12 {
13     unspecified,
14     /// $(LINK https://en.wikipedia.org/wiki/Indent_style#Allman_style)
15     allman,
16     /// $(LINK https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS)
17     otbs,
18     /// $(LINK https://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrup)
19     stroustrup
20 }
21 
22 enum TemplateConstraintStyle
23 {
24     unspecified,
25     conditional_newline_indent,
26     conditional_newline,
27     always_newline,
28     always_newline_indent
29 }
30 
31 /// Configuration options for formatting
32 struct Config
33 {
34     ///
35     OptionalBoolean dfmt_align_switch_statements;
36     ///
37     BraceStyle dfmt_brace_style;
38     ///
39     OptionalBoolean dfmt_outdent_attributes;
40     ///
41     int dfmt_soft_max_line_length = -1;
42     ///
43     OptionalBoolean dfmt_space_after_cast;
44     ///
45     OptionalBoolean dfmt_space_after_keywords;
46     ///
47     OptionalBoolean dfmt_split_operator_at_line_end;
48     ///
49     OptionalBoolean dfmt_selective_import_space;
50     ///
51     OptionalBoolean dfmt_compact_labeled_statements;
52     ///
53     TemplateConstraintStyle dfmt_template_constraint_style;
54 
55     mixin StandardEditorConfigFields;
56 
57     /**
58      * Initializes the standard EditorConfig properties with default values that
59      * make sense for D code.
60      */
61     void initializeWithDefaults()
62     {
63         pattern = "*.d";
64         end_of_line = EOL.lf;
65         indent_style = IndentStyle.space;
66         indent_size = 4;
67         tab_width = 4;
68         max_line_length = 120;
69         dfmt_align_switch_statements = OptionalBoolean.t;
70         dfmt_brace_style = BraceStyle.allman;
71         dfmt_outdent_attributes = OptionalBoolean.t;
72         dfmt_soft_max_line_length = 80;
73         dfmt_space_after_cast = OptionalBoolean.t;
74         dfmt_space_after_keywords = OptionalBoolean.t;
75         dfmt_split_operator_at_line_end = OptionalBoolean.f;
76         dfmt_selective_import_space = OptionalBoolean.t;
77         dfmt_compact_labeled_statements = OptionalBoolean.t;
78         dfmt_template_constraint_style = TemplateConstraintStyle.conditional_newline_indent;
79     }
80 
81     /**
82      * Returns:
83      *     true if the configuration is valid
84      */
85     bool isValid()
86     {
87         import std.stdio : stderr;
88 
89         if (dfmt_soft_max_line_length > max_line_length)
90         {
91             stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
92                     max_line_length, dfmt_soft_max_line_length);
93             return false;
94         }
95         return true;
96     }
97 }