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_space_before_function_parameters;
48     ///
49     OptionalBoolean dfmt_split_operator_at_line_end;
50     ///
51     OptionalBoolean dfmt_selective_import_space;
52     ///
53     OptionalBoolean dfmt_compact_labeled_statements;
54     ///
55     TemplateConstraintStyle dfmt_template_constraint_style;
56     ///
57     OptionalBoolean dfmt_single_template_constraint_indent;
58     ///
59     OptionalBoolean dfmt_space_before_aa_colon;
60     ///
61     OptionalBoolean dfmt_keep_line_breaks;
62 
63     mixin StandardEditorConfigFields;
64 
65     /**
66      * Initializes the standard EditorConfig properties with default values that
67      * make sense for D code.
68      */
69     void initializeWithDefaults()
70     {
71         pattern = "*.d";
72         end_of_line = EOL.lf;
73         indent_style = IndentStyle.space;
74         indent_size = 4;
75         tab_width = 4;
76         max_line_length = 120;
77         dfmt_align_switch_statements = OptionalBoolean.t;
78         dfmt_brace_style = BraceStyle.allman;
79         dfmt_outdent_attributes = OptionalBoolean.t;
80         dfmt_soft_max_line_length = 80;
81         dfmt_space_after_cast = OptionalBoolean.t;
82         dfmt_space_after_keywords = OptionalBoolean.t;
83         dfmt_space_before_function_parameters = OptionalBoolean.f;
84         dfmt_split_operator_at_line_end = OptionalBoolean.f;
85         dfmt_selective_import_space = OptionalBoolean.t;
86         dfmt_compact_labeled_statements = OptionalBoolean.t;
87         dfmt_template_constraint_style = TemplateConstraintStyle.conditional_newline_indent;
88         dfmt_single_template_constraint_indent = OptionalBoolean.f;
89         dfmt_space_before_aa_colon = OptionalBoolean.f;
90         dfmt_keep_line_breaks = OptionalBoolean.f;
91     }
92 
93     /**
94      * Returns:
95      *     true if the configuration is valid
96      */
97     bool isValid()
98     {
99         import std.stdio : stderr;
100 
101         if (dfmt_soft_max_line_length > max_line_length)
102         {
103             stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
104                     max_line_length, dfmt_soft_max_line_length);
105             return false;
106         }
107         return true;
108     }
109 }