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
use crate::string_ext::Indent;
use crate::StrExt;

pub fn strip_comment_markers(comment: &str) -> String {
	const MULTILINE_PREFIX: &str = "/*";
	const MULTILINE_CONT: &str = "*";
	const MULTILINE_SUFFIX: &str = "*/";
	const SINGLELINE: &str = "//";
	const DETAIL: &str = "!";
	const SINGLELINE_DETAIL: &str = "/";
	const SINGLELINE_SIDE: &str = "<";
	let mut singleline_delimited = true;
	let mut asterisk_indented = false;
	let mut lines = Vec::with_capacity(128);
	// first pass:
	// 1. checks whether the comment is single-line or multi-line delimited
	// 2. checks whether multiline comment is asterisk prefixed
	// 3. strips comment delimiters for multiline and single line comments
	// 4. collects resulting stripped lines into `lines` Vec
	for (i, mut line) in comment.lines_with_nl().enumerate() {
		let mut line_clean = line.trim_start();
		if i == 0 {
			if let Some(new_line) = line_clean.strip_prefix(MULTILINE_PREFIX) {
				line = new_line;
				singleline_delimited = false;
				if let Some(new_line) = line.strip_prefix(MULTILINE_CONT) {
					line = new_line;
					asterisk_indented = true;
				} else {
					asterisk_indented = false;
				}
				line_clean = line.trim_start();
			} else {
				singleline_delimited = true;
				asterisk_indented = false;
			}
		} else if let Some(line_clean) = line_clean.strip_prefix(MULTILINE_PREFIX) {
			line = line_clean
				.trim_start_matches(DETAIL)
				.trim_start_matches(MULTILINE_CONT)
				.trim_start();
		}
		if singleline_delimited && line_clean.starts_with(SINGLELINE) {
			line = &line_clean[SINGLELINE.len()..];
			if let Some(new_line) = line.strip_prefix(SINGLELINE_DETAIL) {
				line = new_line;
			} else if let Some(new_line) = line.strip_prefix(DETAIL) {
				line = new_line;
			}
			if let Some(new_line) = line.strip_prefix(SINGLELINE_SIDE) {
				line = new_line;
			}
		} else if asterisk_indented && i == 1 && !line_clean.starts_with(MULTILINE_CONT) {
			asterisk_indented = false;
		}
		lines.push(line);
	}
	let trim_last_empty_lines = |lines: &mut Vec<&str>| {
		while let Some(last_line) = lines.last() {
			if last_line.is_empty() {
				lines.pop();
			} else {
				break;
			}
		}
	};
	trim_last_empty_lines(&mut lines);
	// trim ending multiline delimiter
	if let Some(last_line) = lines.last_mut() {
		if !singleline_delimited {
			*last_line = last_line.trim_end();
			if last_line.ends_with(MULTILINE_SUFFIX) {
				*last_line = last_line[..last_line.len() - MULTILINE_SUFFIX.len()].trim_end();
			}
		}
	}
	trim_last_empty_lines(&mut lines);
	// second pass:
	// 1. calculates common indent
	// 2. for multiline asterisk prefixed comments, strips this prefix modifying `lines`
	let mut first_line_indent = None;
	let mut common_indent: Option<Indent> = None;
	for line in &mut lines {
		if !singleline_delimited && asterisk_indented {
			let line_trimmed = line.trim_start();
			if let Some(line_trimmed) = line_trimmed.strip_prefix(MULTILINE_CONT) {
				*line = line_trimmed;
			} else {
				let trim_start = line.trim_start_idx().min(2);
				*line = &line[trim_start..];
			}
		}
		if first_line_indent.is_none() {
			first_line_indent = Some(line.detect_indent());
		} else {
			let detected_indent = line.detect_indent();
			if !line[detected_indent.len..].trim_start().is_empty() {
				if let Some(common_indent) = common_indent.as_mut() {
					*common_indent = (*common_indent).min(detected_indent);
				} else {
					common_indent = Some(line.detect_indent());
				}
			}
		}
	}
	let mut out = String::with_capacity(comment.len());
	for (i, mut line) in lines.into_iter().enumerate() {
		if i == 0 {
			line = &line[first_line_indent.unwrap_or_default().len..];
			if line.trim().is_empty() {
				continue;
			}
		} else {
			let indent_len = common_indent.unwrap_or_default().len;
			if line.len() > indent_len {
				line = &line[indent_len..];
			} else {
				line = "\n";
			}
		}
		let line_clean_end = line.trim_end();
		if let Some(suffix) = line_clean_end.strip_suffix(MULTILINE_SUFFIX) {
			out += suffix.trim_end();
			out.push('\n');
		} else {
			out += line;
		}
	}

	let out_trim_end = out.trim_end_idx();
	out.drain(out_trim_end..);
	out
}