-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cue/format: indent multiline string literals based on context.
Change-Id: Ic58d29dfa347a2f0b3642e56676c2c944dbb8b2d Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7283 Reviewed-by: CUE cueckoo <cueckoo@gmail.com> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
- Loading branch information
Showing
9 changed files
with
169 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2020 CUE 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 | ||
// | ||
// http://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. | ||
|
||
package literal | ||
|
||
import "strings" | ||
|
||
// IndentTabs takes a quoted string and reindents it for the given indentation. | ||
// If a string is not a multiline string it will return the string as is. | ||
func IndentTabs(s string, n int) string { | ||
indent := tabs(n) | ||
|
||
qi, _, _, err := ParseQuotes(s, s) | ||
if err != nil || !qi.multiline || qi.whitespace == indent { | ||
return s | ||
} | ||
|
||
search := "\n" + qi.whitespace | ||
replace := "\n" + indent | ||
|
||
return strings.ReplaceAll(s, search, replace) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2020 CUE 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 | ||
// | ||
// http://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. | ||
|
||
package literal | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIndentTabs(t *testing.T) { | ||
testCases := []struct { | ||
in string | ||
out string | ||
}{{ | ||
in: `""" | ||
foo | ||
bar | ||
"""`, | ||
out: `""" | ||
foo | ||
bar | ||
"""`, | ||
}, { | ||
in: `""" | ||
foo | ||
bar | ||
"""`, | ||
out: `""" | ||
foo | ||
bar | ||
"""`, | ||
}, { | ||
in: `""`, | ||
out: `""`, | ||
}} | ||
for _, tc := range testCases { | ||
t.Run("", func(t *testing.T) { | ||
got := IndentTabs(tc.in, 3) | ||
if got != tc.out { | ||
t.Errorf("got %s; want %s", got, tc.out) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters