QB64-Phoenix-Edition / QB64pe

The QB64 Phoenix Edition Repository
https://qb64phoenix.com
Other
131 stars 26 forks source link

Another Suggestion. #364

Closed lavender-programming closed 1 year ago

lavender-programming commented 1 year ago

I keep returning with ideas for QB64! When you Open a file. In-between the Open And Close the lines should be indented like with an If or any loop. It would make code read-ability a lot easier I think.

mkilgore commented 1 year ago

I think your mind is in the right place, but this approach doesn't really work because OPEN and CLOSE don't need to be in the same block (and commonly aren't). Ex, consider this code:

If fileNo = -1 Then
    fileNo = FREEFILE
    Open "foobar.txt" For Input As #fileNo
End If

' ...

Close #fileNo

There's no sane way to indent the OPEN/CLOSE combo because the OPEN is within its own indented If block. Things would get even messier if we had multiple OPEN and CLOSE statements to contend with (there's not always a 1:1 match).

What we could consider instead is a "blocked" open statement, where at the end of the block the file is automatically closed (so effectively, the OPEN/CLOSE are one statement). For example Python has the with statement which achieves this. Theoretically it could look something like this:

_With Open "foobar.txt" For Input As #fileNo
    ' do whatever with the open file
End _With
' When the code exits the `_With` block, the file is automatically closed

The inside of the _With block is indented because the Open file is scoped to only exist within that block. But additionally, you can never forget to CLOSE the file since the compiler will automatically call CLOSE for you whenever the code exits the block. It's not as flexible as the regular OPEN/CLOSE combo (so not a replacement), but for simple OPEN/CLOSE pairs it would be very effective.

SteveMcNeill commented 1 year ago

How about adding a simple indent command for manual spacing? (Github won't space these properly, but you get the concept.)

OPEN foo$ FOR INPUT AS #1 _INDENT 'also indents this line DO 'Stuff with file LOOP _INDENT OFF 'removes the indent after this line CLOSE

lavender-programming commented 1 year ago

That makes sense, The idea of the _With blocks are a good Idea. Thank you for the help.

RhoSigma-QB64 commented 1 year ago

Even it makes me a dick maybe, you get a hard objection from me...

OPEN/CLOSE is simply NOT a block statement, even if it appears as such in the code, Hence no indention please.

Same thing with the _WITH or _INDENT ideas. It's always freaking me out to see how the laziness to type brings up such stupid ideas, you don't want type too much, but expecting the language/IDE to make the code readable for you.

Every language, QB64 inclusive, supports comments. So you want better readable code? Then use comments to accomplish that.

lavender-programming commented 1 year ago

I understand your point. What might actually be a problem is having A LOT of code, like a multi-file codebase with 10s of 1,000s of lines. What would work is in .JSON, you can collapse things between Brackets, like a collapsible If statement. Instead of QB64 trying to figure out Indentation or Not Indenting, maybe collapsible blocks would be easier.

RhoSigma-QB64 commented 1 year ago

If your only concern is to get code out of the view you are not currently working on, or at least quickly see where such a block start/end, then you could give my Notepad++ setup a try (at least if you're on windows), It let's you create as much collapsable folds you want. https://qb64phoenix.com/forum/showthread.php?tid=86