1// Copyright 2020 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package literal
16
17import "strings"
18
19// IndentTabs takes a quoted string and reindents it for the given indentation.
20// If a string is not a multiline string it will return the string as is.
21func IndentTabs(s string, n int) string {
22	indent := tabs(n)
23
24	qi, _, _, err := ParseQuotes(s, s)
25	if err != nil || !qi.multiline || qi.whitespace == indent {
26		return s
27	}
28
29	search := "\n" + qi.whitespace
30	replace := "\n" + indent
31
32	return strings.ReplaceAll(s, search, replace)
33}
34