tig icon indicating copy to clipboard operation
tig copied to clipboard

Refactor: break up nested ternaries into multiple lines

Open krobelus opened this issue 3 years ago • 4 comments

We have some places where multiple ternary operators are nested on a single line. Break them up into multiple lines. I think this is much easier to read because the indentation shows the nesting structure. No functional change here.

Reading through format_append_arg(), I noticed that "echo %%(commit" with the missing closing parenthesis is not supported. Probably not worth spending too much time on that.

krobelus avatar Oct 26 '21 19:10 krobelus

Not much time needed, I think this should do the job:

-               if (var && !closing)
+               if (var && !closing && !esc)

When doing 6134336, I took it as a challenge to do it in one line only, so I'll have hard time to let it go :-), but, agreed, as we have no plans to enter IOCCC with Tig, we should prioritize the readability.

koutcher avatar Nov 04 '21 20:11 koutcher

if (var && !closing && !esc)

Problem is that %%(foo will become %%(foo, so there is no way to express %(foo. So the rule should probably be: %%( becomes %(, and elsewhere % is not special. That would mean %%%( becomes %%( which feels weird. Also it might be legit to want %%(branch) to output %some-branch, which we don't support. Anyway it's really not important, these are mostly theoretical issues. But if we make more changes to the expansion code, we should probably reduce the number of special cases.

I took it as a challenge to do it in one line only

Haha yeah, I was mildly surprised when I came across this while trying to understand the formatting logic

krobelus avatar Nov 11 '21 22:11 krobelus

You're right, I missed that point. This is better but as you said, we cannot get %some-branch. As in Tig context there is very little reason to want %( without closing ) we may leave it as it is today.

-               if (var && !closing)
-                       return false;
+               if (var && !closing) {
+                       if (!esc)
+                               return false;
+                       else
+                               ++arg;
+               }

koutcher avatar Nov 11 '21 22:11 koutcher

Ideally we can interpret each %% as % (like printf). This has a fairly low risk of breaking scripts.

krobelus avatar Jan 08 '22 16:01 krobelus