func format_source_snippet(source: &String) -> StringNormalizes a declaration extracted from a nested source range.
Extraction removes the declaration's first-line indentation but retains its owner's indentation on later lines. This removes that common closing-line indent and trailing horizontal whitespace while preserving the source's token spacing and line breaks.
View Source
pub func format_source_snippet(source: &String) -> String {
let lines: [String] = []
for line in source.split(character: '\n').into_iter() {
lines.push(trim_trailing_space(line: line.to_string()))
}
let indent = 0
let index = lines.count - 1
loop index > 0 {
if lines[index].byte_count > 0 {
indent = leading_indent(line: lines[index])
break
}
index = index - 1
}
let output = StringBuilder()
let line_index = 0
loop line_index < lines.count {
let line: &String = lines[line_index]
let remove = if line_index > 0 && leading_indent(line: line) >= indent { indent } else { 0 }
output.append(string: byte_slice(text: line, start: remove, end: line.byte_count))
if line_index + 1 < lines.count { output.append(character: '\n') }
line_index = line_index + 1
}
output.finish()
}