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
use lazy_static::lazy_static;
use regex::{Regex, RegexBuilder};

lazy_static! {
    // Regex that matches the /* */ style comment in
    // the beginning of the file.
    static ref DOCBLOCK_RE: Regex = {
        RegexBuilder::new("^\\s*/\\*\\*?(?P<block>(?:.|\\n)*\\*/)")
            .multi_line(true)
            .build()
            .unwrap()
    };
    // Regex that can match individual lines and capture anything that looks like:
    //      @some_key some value
    static ref DIRECTIVE_RE: Regex = Regex::new("^@(?P<key>\\w+)\\s+(?P<value>.*)$").unwrap();
}

#[derive(Debug, Clone)]
enum Line {
    Directive { key: String, value: Option<String> },
    Text(String),
}

// Struct that represents a source file, which contains an optional
// docblock and the rest of the file.
// Docblock values can be mutated and file can be reprinted back with
// values updated.
#[derive(Debug)]
pub struct SourceFile {
    doc_block: Vec<Line>,
    pub rest: String,
}

impl SourceFile {
    pub fn from_source(source: &str) -> Self {
        if let Some(captures) = DOCBLOCK_RE.captures(source) {
            if let Some(block) = captures.name("block") {
                // split the file into two pieces
                //      - docblock
                //      - rest of the code without docblock
                let doc_block_str = &source[block.start()..block.end()]
                    // can probably do it with the regex, but i can't figure out how
                    .trim_end_matches("*/");
                let rest = &source[block.end()..].trim_start();

                let lines = doc_block_str.split('\n').collect::<Vec<&str>>();
                let lines = lines
                    .iter()
                    // trim all the witespace around as well as the leading `*` in the beginning of each comment line
                    .map(|l| l.trim().trim_start_matches('*').trim_start())
                    .filter(|l| !l.is_empty())
                    .map(|l| {
                        if let Some(captures) = DIRECTIVE_RE.captures(l) {
                            let key = captures
                                .name("key")
                                .expect("`key` capture must be there")
                                .as_str()
                                .to_string();

                            let value = captures.name("value").map(|v| v.as_str().to_string());
                            Line::Directive { key, value }
                        } else {
                            Line::Text(l.to_string())
                        }
                    })
                    .collect::<Vec<Line>>();

                return Self {
                    doc_block: lines,
                    rest: rest.to_string(),
                };
            }
        }

        Self {
            doc_block: vec![],
            rest: source.to_string(),
        }
    }

    // Set a docblock directive. e.g. `set_directive("cat", Some("dog"));
    // will add:
    //      @cat dog
    // line to the docblock of the file
    pub fn set_directive(&mut self, key: &str, value: Option<&str>) {
        let mut to_add = Some(Line::Directive {
            key: key.to_string(),
            value: value.map(|v| v.to_string()),
        });

        let existing = self.doc_block.iter_mut().find(|l| {
            if let Line::Directive { key: k, .. } = l {
                if key == k {
                    return true;
                }
            }
            false
        });

        // If there's already a directive with the same key, replace it with a new one
        if let Some(directive @ Line::Directive { .. }) = existing {
            *directive = to_add.take().expect("must be there");
        } else {
            // otherwise insert a new one
            let mut lines = vec![];
            std::mem::swap(&mut self.doc_block, &mut lines);

            // We'll insert it after the latest directive line in the docblock to keep
            // directive grouped.
            for line in lines.into_iter().rev() {
                // If to_add is still there, and the next line (in rev order) is a directive
                // we'll take value and push it onto the vec.
                if let (Some(_), Line::Directive { .. }) = (&mut to_add, &line) {
                    self.doc_block.push(to_add.take().expect("must be there"));
                }
                // add lines in reverse order one by one
                self.doc_block.push(line);
            }

            // If it still there (e.g. docblock was empty). Add it
            if let Some(line) = to_add {
                self.doc_block.push(line);
            }

            // reverse order again
            self.doc_block.reverse();
        }
    }

    // Add text to docblock.
    pub fn add_text(&mut self, text: &str) {
        for line in text.lines() {
            self.doc_block.push(Line::Text(line.to_string()))
        }
    }

    pub fn to_source(&self) -> String {
        let mut result = String::new();

        if !self.doc_block.is_empty() {
            result.push_str("/*\n");

            for line in &self.doc_block {
                result.push_str(" * ");
                match line {
                    Line::Text(t) => result.push_str(&t),
                    Line::Directive { key, value } => result.push_str(
                        format!(
                            "@{} {}",
                            key,
                            value.as_ref().map(|s| s.as_str()).unwrap_or_default()
                        )
                        .trim(),
                    ),
                }
                result.push('\n');
            }

            result.push_str(" */\n\n");
        }

        result.push_str(&self.rest);
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use k9::*;

    #[test]
    fn from_source() {
        let source = "
/*
 * @hello world
 * yo
 */

use a::b::c;
1 + 1";

        let mut source_file = SourceFile::from_source(source);
        source_file.set_directive("dog", Some("cat"));
        source_file.set_directive("hi", Some("hello"));
        source_file.set_directive("hello", Some("bro"));
        source_file.set_directive("ohio", None);
        source_file.add_text(
            "
Empty line followed by some text. That
spans across multiple lines.

And also hase some empty lines in between text
blocks.",
        );

        snapshot!(
            source_file.to_source(),
            "
/*
 * @hello bro
 * @dog cat
 * @hi hello
 * @ohio
 * yo
 * 
 * Empty line followed by some text. That
 * spans across multiple lines.
 * 
 * And also hase some empty lines in between text
 * blocks.
 */

use a::b::c;
1 + 1
"
        );
    }
}