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
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#[cfg(feature = "py")]
use libcst_derive::TryIntoPy;

use super::{Codegen, CodegenState};

#[derive(Debug, Eq, PartialEq, Default, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct SimpleWhitespace<'a>(pub &'a str);

impl<'a> Codegen<'a> for SimpleWhitespace<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        state.add_token(self.0);
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct Comment<'a>(pub &'a str);

impl<'a> Default for Comment<'a> {
    fn default() -> Self {
        Self("#")
    }
}

impl<'a> Codegen<'a> for Comment<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        state.add_token(self.0);
    }
}

#[derive(Debug, Eq, PartialEq, Default, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct Newline<'a>(pub Option<&'a str>, pub Fakeness);

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Fakeness {
    Fake,
    Real,
}

impl Default for Fakeness {
    fn default() -> Self {
        Self::Real
    }
}

impl<'a> Codegen<'a> for Newline<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        if let Fakeness::Fake = self.1 {
            return;
        }
        if let Some(value) = self.0 {
            state.add_token(value);
        } else {
            state.add_token(state.default_newline);
        }
    }
}

#[derive(Debug, Eq, PartialEq, Default, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct TrailingWhitespace<'a> {
    pub whitespace: SimpleWhitespace<'a>,
    pub comment: Option<Comment<'a>>,
    pub newline: Newline<'a>,
}

impl<'a> Codegen<'a> for TrailingWhitespace<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        self.whitespace.codegen(state);
        if let Some(comment) = &self.comment {
            comment.codegen(state);
        }
        self.newline.codegen(state);
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct EmptyLine<'a> {
    pub indent: bool,
    pub whitespace: SimpleWhitespace<'a>,
    pub comment: Option<Comment<'a>>,
    pub newline: Newline<'a>,
}

impl<'a> Codegen<'a> for EmptyLine<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        if self.indent {
            state.add_indent()
        }
        self.whitespace.codegen(state);
        if let Some(comment) = &self.comment {
            comment.codegen(state);
        }
        self.newline.codegen(state);
    }
}

impl<'a> Default for EmptyLine<'a> {
    fn default() -> Self {
        Self {
            indent: true,
            whitespace: Default::default(),
            comment: Default::default(),
            newline: Default::default(),
        }
    }
}

impl<'a> EmptyLine<'a> {
    pub fn new(
        indent: bool,
        whitespace: SimpleWhitespace<'a>,
        comment: Option<Comment<'a>>,
        newline: Newline<'a>,
    ) -> Self {
        Self {
            indent,
            whitespace,
            comment,
            newline,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Default, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub struct ParenthesizedWhitespace<'a> {
    pub first_line: TrailingWhitespace<'a>,
    pub empty_lines: Vec<EmptyLine<'a>>,
    pub indent: bool,
    pub last_line: SimpleWhitespace<'a>,
}

impl<'a> Codegen<'a> for ParenthesizedWhitespace<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        self.first_line.codegen(state);
        for line in &self.empty_lines {
            line.codegen(state);
        }
        if self.indent {
            state.add_indent()
        }
        self.last_line.codegen(state);
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "py", derive(TryIntoPy))]
pub enum ParenthesizableWhitespace<'a> {
    SimpleWhitespace(SimpleWhitespace<'a>),
    ParenthesizedWhitespace(ParenthesizedWhitespace<'a>),
}

impl<'a> Codegen<'a> for ParenthesizableWhitespace<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>) {
        match self {
            Self::SimpleWhitespace(w) => w.codegen(state),
            Self::ParenthesizedWhitespace(w) => w.codegen(state),
        }
    }
}

impl<'a> Default for ParenthesizableWhitespace<'a> {
    fn default() -> Self {
        Self::SimpleWhitespace(SimpleWhitespace(""))
    }
}