2023-03-04 07:55:01 +00:00
|
|
|
// Copyright 2022-2023 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-03-05 03:01:30 +00:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use crate::formatter::{FormatRecorder, Formatter};
|
|
|
|
|
2023-03-04 07:55:01 +00:00
|
|
|
pub fn complete_newline(s: impl Into<String>) -> String {
|
|
|
|
let mut s = s.into();
|
|
|
|
if !s.is_empty() && !s.ends_with('\n') {
|
|
|
|
s.push('\n');
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2023-03-05 03:01:30 +00:00
|
|
|
|
|
|
|
/// Indents each line by the given prefix preserving labels.
|
|
|
|
pub fn write_indented(
|
|
|
|
formatter: &mut dyn Formatter,
|
|
|
|
recorded_content: &FormatRecorder,
|
|
|
|
mut write_prefix: impl FnMut(&mut dyn Formatter) -> io::Result<()>,
|
|
|
|
) -> io::Result<()> {
|
2023-03-05 03:33:40 +00:00
|
|
|
let data = recorded_content.data();
|
2023-03-05 03:01:30 +00:00
|
|
|
let mut new_line = true;
|
2023-03-05 03:33:40 +00:00
|
|
|
recorded_content.replay_with(formatter, |formatter, range| {
|
|
|
|
for line in data[range].split_inclusive(|&c| c == b'\n') {
|
2023-03-05 03:01:30 +00:00
|
|
|
if new_line && line != b"\n" {
|
|
|
|
// Prefix inherits the current labels. This is implementation detail
|
|
|
|
// and may be fixed later.
|
|
|
|
write_prefix(formatter)?;
|
|
|
|
}
|
|
|
|
formatter.write_all(line)?;
|
|
|
|
new_line = line.ends_with(b"\n");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|