1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use ahash::{AHashMap, AHashSet};
use itertools::Itertools;

use crate::core::config::{FluffConfig, Value};
use crate::utils::reflow::depth_map::DepthInfo;

type ConfigElementType = AHashMap<String, String>;
type ConfigDictType = AHashMap<String, ConfigElementType>;

/// Holds spacing config for a block and allows easy manipulation
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BlockConfig {
    pub spacing_before: String,
    pub spacing_after: String,
    pub spacing_within: Option<String>,
    pub line_position: Option<String>,
}

impl Default for BlockConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl BlockConfig {
    pub fn new() -> Self {
        BlockConfig {
            spacing_before: "single".to_string(),
            spacing_after: "single".to_string(),
            spacing_within: None,
            line_position: None,
        }
    }

    /// Mutate the config based on additional information
    pub fn incorporate(
        &mut self,
        before: Option<&str>,
        after: Option<&str>,
        within: Option<&str>,
        line_position: Option<&str>,
        config: Option<&ConfigElementType>,
    ) {
        let empty = AHashMap::new();
        let config = config.unwrap_or(&empty);

        self.spacing_before = before
            .map(ToOwned::to_owned)
            .or(config.get("spacing_before").cloned())
            .unwrap_or(self.spacing_before.clone())
            .to_string();

        self.spacing_after = after
            .map(ToOwned::to_owned)
            .or(config.get("spacing_after").cloned())
            .unwrap_or(self.spacing_after.clone())
            .to_string();

        self.spacing_within =
            within.map(ToOwned::to_owned).or(config.get("spacing_within").cloned());

        self.line_position =
            line_position.map(ToOwned::to_owned).or(config.get("line_position").cloned());
    }
}

/// An interface onto the configuration of how segments should reflow.
///
/// This acts as the primary translation engine between configuration
/// held either in dicts for testing, or in the FluffConfig in live
/// usage, and the configuration used during reflow operations.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ReflowConfig {
    configs: ConfigDictType,
    config_types: AHashSet<String>,
    /// In production, these values are almost _always_ set because we
    /// use `.from_fluff_config`, but the defaults are here to aid in
    /// testing.
    tab_space_size: usize,
    indent_unit: String,
    max_line_length: usize,
    hanging_indents: bool,
    skip_indentation_in: AHashSet<String>,
    allow_implicit_indents: bool,
    trailing_comments: String,
}

impl ReflowConfig {
    pub fn get_block_config(
        &self,
        block_class_types: &AHashSet<&str>,
        depth_info: Option<&DepthInfo>,
    ) -> BlockConfig {
        let block_class_types: AHashSet<String> =
            block_class_types.iter().map(|s| s.to_string()).collect();
        let configured_types = self.config_types.intersection(&block_class_types);

        let mut block_config = BlockConfig::new();

        if let Some(depth_info) = depth_info {
            let (mut parent_start, mut parent_end) = (true, true);

            for (idx, key) in depth_info.stack_hashes.iter().rev().enumerate() {
                let stack_position = &depth_info.stack_positions[key];

                if !["solo", "start"].contains(&stack_position.type_) {
                    parent_start = false;
                }

                if !["solo", "end"].contains(&stack_position.type_) {
                    parent_end = false;
                }

                if !parent_start && !parent_end {
                    break;
                }

                let parent_classes =
                    &depth_info.stack_class_types[depth_info.stack_class_types.len() - 1 - idx];
                let parent_classes: AHashSet<String> =
                    parent_classes.iter().map(|s| s.to_string()).collect();

                let configured_parent_types =
                    self.config_types.intersection(&parent_classes).collect_vec();

                if parent_start {
                    for seg_type in &configured_parent_types {
                        let seg_type = seg_type.to_string();
                        block_config.incorporate(
                            self.configs
                                .get(&seg_type)
                                .and_then(|conf| conf.get("spacing_before"))
                                .map(|it| it.as_str()),
                            None,
                            None,
                            None,
                            None,
                        );
                    }
                }

                if parent_end {
                    for seg_type in &configured_parent_types {
                        let seg_type = seg_type.to_string();
                        block_config.incorporate(
                            None,
                            self.configs
                                .get(&seg_type)
                                .and_then(|conf| conf.get("spacing_after"))
                                .map(|it| it.as_str()),
                            None,
                            None,
                            None,
                        );
                    }
                }
            }
        }

        for seg_type in configured_types {
            let seg_type = seg_type.to_string();
            block_config.incorporate(None, None, None, None, self.configs.get(&seg_type));
        }

        block_config
    }

    pub fn from_fluff_config(config: &FluffConfig) -> ReflowConfig {
        let configs = config.raw["layout"]["type"].as_map().unwrap().clone();
        let config_types = configs.keys().map(|x| x.to_string()).collect::<AHashSet<String>>();

        ReflowConfig {
            configs: convert_to_config_dict(configs),
            config_types,
            tab_space_size: config.raw["indentation"]["tab_space_size"].as_int().unwrap() as usize,
            indent_unit: config.raw["indentation"]["indent_unit"].as_string().unwrap().into(),
            max_line_length: config.raw["indentation"]["tab_space_size"].as_int().unwrap() as usize,
            hanging_indents: config.raw["indentation"]["hanging_indents"]
                .as_bool()
                .unwrap_or_default(),
            skip_indentation_in: config.raw["indentation"]["indent_unit"]
                .as_string()
                .unwrap()
                .split(',')
                .map(ToOwned::to_owned)
                .collect(),
            allow_implicit_indents: config.raw["indentation"]["allow_implicit_indents"]
                .as_bool()
                .unwrap(),
            trailing_comments: config.raw["indentation"]["trailing_comments"]
                .as_string()
                .unwrap()
                .into(),
        }
    }
}

fn convert_to_config_dict(input: AHashMap<String, Value>) -> ConfigDictType {
    let mut config_dict = ConfigDictType::new();

    for (key, value) in input {
        match value {
            Value::Map(map_value) => {
                let element = map_value
                    .into_iter()
                    .map(|(inner_key, inner_value)| {
                        if let Value::String(value_str) = inner_value {
                            (inner_key, value_str.into())
                        } else {
                            panic!("Expected a Value::String, found another variant.");
                        }
                    })
                    .collect::<ConfigElementType>();
                config_dict.insert(key, element);
            }
            _ => panic!("Expected a Value::Map, found another variant."),
        }
    }

    config_dict
}