cue icon indicating copy to clipboard operation
cue copied to clipboard

cmd/fmt: edge cases for list indentations

Open verdverm opened this issue 4 years ago • 2 comments

What version of CUE are you using (cue version)?

0.4.0

What did you do?

cue fmt produces the following

foo: [{
	a: int
}, {
	b: int
}, {
	c: int
}]

bar: [{
	a: int
},
	{
		b: int
	},
	{
		c: int
	}]

abcdef: [...{}] & [
	{
		a: int
	},
	{
		a: int
	},
]
abcdefg: [...{}] & [
		{
		a: int
	},
	{
		a: int
	},
]

_abcdef: [...{}] & [
		{
		a: int
	},
	{
		a: int
	},
]

#abcdef: [...{}] & [
		{
		a: int
	},
	{
		a: int
	},
]

What did you expect to see?

Consistency for indentation level of list elements.

What did you see instead?

bar indents elements after the first

abc... the first line has extra indentation when the label reaches 7 chars

verdverm avatar Dec 14 '21 12:12 verdverm

I just ran into this yesterday. I found that by placing a comment on the fist line inside the list, separated by a blank line from the following elements restored the intended formatting.

In my case, it looked like this:

something: list.Concat([
	// Work around "cue fmt" defect.

	[
		for k, v in {
			// ...
		} {
			// ...
		},
	],
	[
		// ...
	],
])

seh avatar Dec 14 '21 14:12 seh

Confirmed with 28b42576 via

cp x.cue.golden x.cue
exec cue fmt x.cue
cmp x.cue x.cue.golden

-- x.cue.golden --
foo: [{
	a: int
}, {
	b: int
}, {
	c: int
}]

bar: [{
	a: int
},
{
	b: int
},
{
	c: int
}]

abcdef: [...{}] & [
	{
		a: int
	},
	{
		a: int
	},
]
abcdefg: [...{}] & [
	{
		a: int
	},
	{
		a: int
	},
]

_abcdef: [...{}] & [
	{
		a: int
	},
	{
		a: int
	},
]

#abcdef: [...{}] & [
	{
		a: int
	},
	{
		a: int
	},
]

Which gives the output:

> cp x.cue.golden x.cue
> exec cue fmt x.cue
> cmp x.cue x.cue.golden
--- x.cue
+++ x.cue.golden
@@ -9,12 +9,12 @@
 bar: [{
 	a: int
 },
-	{
-		b: int
-	},
-	{
-		c: int
-	}]
+{
+	b: int
+},
+{
+	c: int
+}]
 
 abcdef: [...{}] & [
 	{
@@ -25,7 +25,7 @@
 	},
 ]
 abcdefg: [...{}] & [
-		{
+	{
 		a: int
 	},
 	{
@@ -34,7 +34,7 @@
 ]
 
 _abcdef: [...{}] & [
-		{
+	{
 		a: int
 	},
 	{
@@ -43,7 +43,7 @@
 ]
 
 #abcdef: [...{}] & [
-		{
+	{
 		a: int
 	},
 	{

FAIL: /tmp/testscript789816755/repro.txt/script.txt:3: x.cue and x.cue.golden differ

myitcv avatar Dec 15 '21 06:12 myitcv