Page 1 of 1

Updated Code indentation

Posted: Tue Apr 15, 2014 2:28 am
by Zach
I don't know the extent to which how many internal PB commands are like this, but some PB commands are not setup for indentation, even though in example code they are written as such. I know sometimes I find it a pain in the ass to have to go back and manually indent stuff, and I think given they are internal commands, the IDE's indentation rules should be setup to include them by default..

Just to give an example, from the Help documentation (and I don't know if this is a special case because they are gadget calls, but) --

Code: Select all

If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    PanelGadget     (0, 8, 8, 306, 203)
      AddGadgetItem (0, -1, "Panel 1")
        PanelGadget (1, 5, 5, 290, 166)
          AddGadgetItem(1, -1, "Sub-Panel 1")
          AddGadgetItem(1, -1, "Sub-Panel 2")
          AddGadgetItem(1, -1, "Sub-Panel 3")
        CloseGadgetList()
      AddGadgetItem (0, -1,"Panel 2")
        ButtonGadget(2, 10, 15, 80, 24,"Button 1")
        ButtonGadget(3, 95, 15, 80, 24,"Button 2")
    CloseGadgetList()
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
This looks nice and is easy to read but if you were to manually type this code in, none of it would self-indent save for the If/EndIf block
I think that Gadgets (and other commands) where at least, you are specifically calling other commands that specifically apply to the previous command's generated results (ie AddGadgetItem implcitly adds the Gadget to the PanelGadget called above it) should definitely get indentation by default.

It's a pain to manually indent because when you hit Enter, the line immediately resets.. I'm aware we can enable a mode where it won't do this, but then IIRC you lose pretty much all auto-indentation as a result? meaning you'd have to take care to manually indent / reverse everything as you went. Which is also a pain

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 4:15 am
by TI-994A
Zach wrote:...PB commands are not setup for indentation...
Hi Zach. In the later versions of PureBasic, indentations for almost any keyword, including commands and gadget calls, can be custom preset here:

File > Preferences > Editor > Indentation

The set indentations will then flow relative to the code listing. :D

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 5:50 am
by Zach
Yes I'm aware of that, I just find it odd not all internal commands are setup for it.
I also am not sure what the values mean.. some are negative, some are positive, some are 0, etc. But no context is given for what different settings do (i.e what would a negative number accomplish)

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 8:27 am
by PB
Paired commands will indent anything between them, so If/EndIf and so on.

The positive numbers are how many spaces to indent, and negative are
how to many to move back.

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 9:15 am
by TI-994A
Zach wrote:...I also am not sure what the values mean.. some are negative, some are positive, some are 0, etc. But no context is given for what different settings do (i.e what would a negative number accomplish)
Hello again. The Before/After numbers in the indentation settings represent the number of tabs to apply. As the name implies, the Before tabs are applied immediately before the line containing the keyword (applied to the line itself), and the After tabs are applied to the line that follows it (applied to the next line).

For example, in my PureBasic IDE, I have set the default TAB value to be two spaces (File > Preferences > Editor > Tab Length). Then, in the indentation settings, I have added the StartDrawing()/StopDrawing() functions with the following settings:

Code: Select all

Keyword: StartDrawing
Before: 0
After: 1

Keyword: StopDrawing
Before: -1
After: 0

Code: Select all

StartDrawing(ImageOutput(#imgNo))
  ;whatever line follows the StartDrawing() function will be indented with 1 TAB
  ;the StopDrawing() line will automatically be indented by -1 TAB upon pressing ENTER
StopDrawing()
In fact, these settings are not restricted to just PureBasic commands, but can be applied to user keywords as well:

Code: Select all

Keyword: ZachOpen
Before: 0
After: 1

Keyword: ZachClose
Before: -1
After: 0

Code: Select all

ZachOpen
  ;this line will automatically be indented with 1 TAB
  ;ZachClose will automatically be indented with -1 TAB upon pressing ENTER
ZachClose
:D

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 12:05 pm
by bobobo
Startdrawing is not the best example, it has a returncode
and may fail

Code: Select all

If StartDrawing() ; startdrawing may fail
  drawsomething(...)
  StopDrawing()
EndIf

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 2:51 pm
by Zach
Thanks for the explanations

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 3:36 pm
by STARGÅTE
bobobo wrote:Startdrawing is not the best example, it has a returncode
and may fail

Code: Select all

If StartDrawing() ; startdrawing may fail
  drawsomething(...)
  StopDrawing()
EndIf
No, this is a good example, and also useful for all other examples.
The creation of a PanelGadget or some think else can also fail, so it is good to check the return value like:

Code: Select all

If PanelGadget (1, 5, 5, 290, 166)
  AddGadgetItem(1, -1, "Sub-Panel 1")
  AddGadgetItem(1, -1, "Sub-Panel 2")
  AddGadgetItem(1, -1, "Sub-Panel 3")
  CloseGadgetList()
EndIf

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 4:13 pm
by bobobo
That's what I meant :D

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 4:45 pm
by TI-994A
STARGÅTE wrote:No, this is a good example, and also useful for all other examples. The creation of a PanelGadget or some think else can also fail, so it is good to check the return value like:

Code: Select all

If PanelGadget (1, 5, 5, 290, 166)
  AddGadgetItem(1, -1, "Sub-Panel 1")
  AddGadgetItem(1, -1, "Sub-Panel 2")
  AddGadgetItem(1, -1, "Sub-Panel 3")
  CloseGadgetList()
EndIf
Hi STARGÅTE, bobobo. You're right; error checking is quite important. However, terminating an error-checking block with a simple EndIf makes no sense either. :)

I think that we all know that the examples posted in these forums are purely for demonstration purposes only, and not meant to be solid bullet-proof code. :lol:

Re: Updated Code indentation

Posted: Tue Apr 15, 2014 5:06 pm
by bobobo
Image