Updated Code indentation

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Updated Code indentation

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Updated Code indentation

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Updated Code indentation

Post 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)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Updated Code indentation

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Updated Code indentation

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 206
Joined: Mon Jun 09, 2003 8:30 am

Re: Updated Code indentation

Post 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
사십 둘 .
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Updated Code indentation

Post by Zach »

Thanks for the explanations
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Updated Code indentation

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 206
Joined: Mon Jun 09, 2003 8:30 am

Re: Updated Code indentation

Post by bobobo »

That's what I meant :D
사십 둘 .
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Updated Code indentation

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 206
Joined: Mon Jun 09, 2003 8:30 am

Re: Updated Code indentation

Post by bobobo »

Image
사십 둘 .
Post Reply