Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Please discuss: a tough choice

52 views
Skip to first unread message

Edward K. Ream

unread,
Apr 2, 2025, 1:07:05 PMApr 2
to leo-editor

#4323 discusses two recently discovered bugs. Both bugs involve @language directives, but fixing them involves entirely different design questions. This post discusses only bug 2.


A subtle problem


Bug 2 arises when an @<file> node (call it node A) contains no @language directive. Imo, this is bad Leonine style because what Leo writes might depend on @language directives in the node's ancestors! And because of clones, there might even be several different @languages in effect for node A!


Let us say that node A is self-contained if Leo can read and write node A without examining any of node A's ancestors.


The workaround is clear. Good style dictates that all @<file> nodes should contain exactly one @language directive, thereby making them self-contained.


A complication


Leo's read code works without using (or knowing) what node A contains! I only recently realized this surprising fact. The read code gets the file's comment delimiters from the @+leo sentinel line.


In contrast, Leo's write code depends on the @language that is in effect for node A.


The tough choice


We have seen that good Leonine style dictates that all @<file> nodes should contain exactly one @language directive. The question is, what (if anything) should Leo do if that's not true?


Changing the rules for how Leo writes @<file> nodes would constitute a breaking change to Leo. Potentially, existing Leo outlines might write existing external files differently! Leo's code could change, provided that the effect of the code remains exactly the same.


Otoh, forgetting to add an @language directive to an @<file> node puts a time bomb into the outline. Moving node A might change the @language directive in effect for node A. As a result, Leo may mysteriously fail to write the external file correctly! In short, bug 2 may be rare, but it's far from harmless.


A possible solution


PR #4324 provides a fallback policy. The PR uses the language implied by the external file's extension to set the language (comment delimiters) to be used when writing the external file. This policy makes @<file> nodes self-contained.


Alas, this policy might cause existing Leo outlines to write existing external files differently. This could happen if the @language directive in effect for node A did not match the language computed by the file's extension. This actually happened in several of Leo's unit tests, but the PR easily corrected that problem.


Warnings and checks


On startup, Leo could warn if an @<file> node contained no (or more than one) @language directive. Such warnings are always annoying, but perhaps they would be wise.


Summary


PR #4324 changes both Leo's read and write code. However, Leo's read code does not depend on the questions raised here, so Leo's read code should work exactly as before.


Otoh, PR #4324 does implement a new policy for writing @<file> nodes. When writing an external file, the PR uses the file's extension to determine the language in effect, thereby making all @<file> nodes self-contained.


In rare cases, this new policy might change the effect of existing Leo outlines. What do you think? Is this policy reasonable? Is it worth the risk of changing the meaning of outlines?


All of your questions, comments, and suggestions are welcome.


Edward


P.S. The fix for the other bug (bug 1) will likely involve more complex code changes, but the design issues should be non-controversial.


EKR

Thomas Passin

unread,
Apr 2, 2025, 3:32:07 PMApr 2
to leo-editor
On Wednesday, April 2, 2025 at 1:07:05 PM UTC-4 Edward K. Ream wrote:

#4323 discusses two recently discovered bugs. Both bugs involve @language directives, but fixing them involves entirely different design questions. This post discusses only bug 2.


[snip]


The tough choice


We have seen that good Leonine style dictates that all @<file> nodes should contain exactly one @language directive. The question is, what (if anything) should Leo do if that's not true?


Changing the rules for how Leo writes @<file> nodes would constitute a breaking change to Leo. Potentially, existing Leo outlines might write existing external files differently! Leo's code could change, provided that the effect of the code remains exactly the same.


Otoh, forgetting to add an @language directive to an @<file> node puts a time bomb into the outline. Moving node A might change the @language directive in effect for node A. As a result, Leo may mysteriously fail to write the external file correctly! In short, bug 2 may be rare, but it's far from harmless.


A possible solution


PR #4324 provides a fallback policy. The PR uses the language implied by the external file's extension to set the language (comment delimiters) to be used when writing the external file. This policy makes @<file> nodes self-contained.


Alas, this policy might cause existing Leo outlines to write existing external files differently. This could happen if the @language directive in effect for node A did not match the language computed by the file's extension. This actually happened in several of Leo's unit tests, but the PR easily corrected that problem.


If a new policy is going to be applied, I think a new leo version sentinel should be applied.  The current one reads:

#@+leo-ver=5-thin

The read code might have to branch on the version, but that should be OK. One potential problem would be @files written by the new code but read by an older version of Leo.  This could only be a problem if a new @file is sent to someone who is running an older version of Leo.

Otherwise, I agree that without an @language directive the file extension should take precedence. We would have to decide how to handle the case where this extension is not known to Leo.

I also agree that having more than one @language directive makes for an impossible situation and would have to be handled.

Warning messages should be more informative than are the current ones about not writing a file because of orphaned nodes.  I'm never sure if it means that the outline hasn't been saved or that the external file hasn't been saved, and I don't know either what to to to fix things nor whether my work will be lost if I save (or don't save) the outline. I am inclined to think that if an outline is saved that contains an invalid @file, that file's headline should be changed to "@@file" so that all the nodes will be saved as is.  This will prevent corrupting an existing external file. Of course a warning message would have to be given to that effect.
 

Edward K. Ream

unread,
Apr 7, 2025, 5:55:25 AMApr 7
to leo-e...@googlegroups.com
On Wed, Apr 2, 2025 at 2:32 PM Thomas Passin wrote:

PR #4324 provides a fallback policy. The PR uses the language implied by the external file's extension to set the language (comment delimiters) to be used when writing the external file. This policy makes @<file> nodes self-contained.

...
If a new policy is going to be applied, I think a new leo version sentinel should be applied.

As a result of recent conversations, I now plan to make the PR be equivalent to existing code, except perhaps for strange code involving default settings in one or two minor plugins.

I thank you and Jake for highlighting the dangers involved in making any changes to Leo's read/write code. I'll review all the changes, line by line, to ensure there are no unintended consequences.

My recommendations for using Leo's directives remain, but nobody is under any obligation to follow them.

Edward

Edward K. Ream

unread,
Apr 7, 2025, 10:13:08 AMApr 7
to leo-editor
On Wednesday, April 2, 2025 at 12:07:05 PM UTC-5 Edward K. Ream wrote:

#4323 discusses two recently discovered bugs. Both bugs involve @language directives, but fixing them involves entirely different design questions. This post discusses only bug 2.

...

Bug 2 arises when an @<file> node (call it node A) contains no @language directive. Imo, this is bad Leonine style because what Leo writes might depend on @language directives in the node's ancestors! And because of clones, there might even be several different @languages in effect for node A!


I have decided not to attempt any fix for this bug. The consequences are too severe. Indeed, any change in this area might change the meaning of existing outlines.

Consequently, PR #4324 is intended to be exactly equivalent to Leo's legacy code. If it isn't, we'll make it so.

Edward
Reply all
Reply to author
Forward
0 new messages