diff --git a/src/cli_util.rs b/src/cli_util.rs index 3eb5aea7e..ed5bb9d0f 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -59,7 +59,6 @@ use thiserror::Error; use toml_edit; use tracing_subscriber::prelude::*; -use crate::commit_templater; use crate::config::{ config_path, AnnotatedValue, CommandNameAndArgs, ConfigSource, LayeredConfigs, }; @@ -68,6 +67,7 @@ use crate::merge_tools::{ConflictResolveError, DiffEditError}; use crate::template_parser::{TemplateAliasesMap, TemplateParseError}; use crate::templater::Template; use crate::ui::{ColorChoice, Ui}; +use crate::{commit_templater, text_util}; #[derive(Clone, Debug)] pub enum CommandError { @@ -1882,7 +1882,7 @@ impl DescriptionArg { impl From for DescriptionArg { fn from(s: String) -> Self { - DescriptionArg(complete_newline(s)) + DescriptionArg(text_util::complete_newline(s)) } } @@ -1898,14 +1898,6 @@ impl AsRef for DescriptionArg { } } -pub fn complete_newline(s: impl Into) -> String { - let mut s = s.into(); - if !s.is_empty() && !s.ends_with('\n') { - s.push('\n'); - } - s -} - #[derive(Clone, Debug)] pub struct RevisionArg(String); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 55112955f..0ad381e91 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -49,7 +49,7 @@ use jujutsu_lib::{conflicts, file_util, revset}; use maplit::{hashmap, hashset}; use crate::cli_util::{ - self, check_stale_working_copy, get_config_file_path, print_checkout_stats, + check_stale_working_copy, get_config_file_path, print_checkout_stats, resolve_multiple_nonempty_revsets, resolve_mutliple_nonempty_revsets_flag_guarded, run_ui_editor, serialize_config_value, short_commit_hash, user_error, user_error_with_hint, write_config_value_to_file, Args, CommandError, CommandHelper, DescriptionArg, RevisionArg, @@ -59,8 +59,8 @@ use crate::config::{AnnotatedValue, ConfigSource}; use crate::diff_util::{self, DiffFormat, DiffFormatArgs}; use crate::formatter::{Formatter, PlainTextFormatter}; use crate::graphlog::{get_graphlog, Edge}; -use crate::template_parser; use crate::ui::Ui; +use crate::{template_parser, text_util}; #[derive(clap::Parser, Clone, Debug)] enum Commands { @@ -1770,7 +1770,7 @@ fn edit_description( .filter(|line| !line.starts_with("JJ: ")) .join("\n"); description.truncate(description.trim_end_matches('\n').len()); - Ok(cli_util::complete_newline(description)) + Ok(text_util::complete_newline(description)) } fn cmd_describe( diff --git a/src/commit_templater.rs b/src/commit_templater.rs index 848896434..eb291edf2 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -23,7 +23,6 @@ use jujutsu_lib::op_store::WorkspaceId; use jujutsu_lib::repo::Repo; use jujutsu_lib::rewrite; -use crate::cli_util; use crate::formatter::Formatter; use crate::template_parser::{ self, CoreTemplatePropertyKind, FunctionCallNode, IntoTemplateProperty, TemplateAliasesMap, @@ -33,6 +32,7 @@ use crate::templater::{ self, IntoTemplate, PlainTextFormattedProperty, Template, TemplateFunction, TemplateProperty, TemplatePropertyFn, }; +use crate::text_util; struct CommitTemplateLanguage<'repo, 'b> { repo: &'repo dyn Repo, @@ -158,7 +158,7 @@ fn build_commit_keyword<'repo>( let repo = language.repo; let property = match name { "description" => language.wrap_string(wrap_fn(|commit| { - cli_util::complete_newline(commit.description()) + text_util::complete_newline(commit.description()) })), "change_id" => language.wrap_commit_or_change_id(wrap_fn(move |commit| { CommitOrChangeId::new(repo, IdKind::Change(commit.change_id().to_owned())) diff --git a/src/lib.rs b/src/lib.rs index e29de0e6c..4cc8612d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,5 +27,6 @@ pub mod operation_templater; mod progress; pub mod template_parser; pub mod templater; +pub mod text_util; pub mod time_util; pub mod ui; diff --git a/src/text_util.rs b/src/text_util.rs new file mode 100644 index 000000000..6d4e5ff2e --- /dev/null +++ b/src/text_util.rs @@ -0,0 +1,21 @@ +// 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. + +pub fn complete_newline(s: impl Into) -> String { + let mut s = s.into(); + if !s.is_empty() && !s.ends_with('\n') { + s.push('\n'); + } + s +}