Recent

Author Topic: IDE tabs and spaces, auto-indenting  (Read 2007 times)

chris_laz

  • New Member
  • *
  • Posts: 16
IDE tabs and spaces, auto-indenting
« on: March 20, 2025, 12:51:32 pm »
I'm not sure what I'm doing wrong, but I cannot get tabs/spacing/auto-indenting of code to work consistently, either in new units, or in old units converted from delphi (admittedly, some of these date back 20 or 30 years, and probably contain a mixture of tabs and spaces).

I am used to using Eclipse IDEs, where the auto-indenting just works, and creates consistent indenting, but in Lazarus the options and the results seem to be all over the place.

Is there one 'master' setting I can set to make this work consistently?
I'd like to use tabs in preference to spaces, with a tab width of 3, but the IDE seems to ignore everything I set.
It's quite frustrating!

Thanks

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #1 on: March 20, 2025, 01:18:39 pm »
There is one setting in the "Codetools" section. If that is on, then you get "smart", i.e. "best guess" indentation.

Turn it off, and you can use the settings under Editor > Indent and tabs.

---
However, if you want automatic extra indent added, when pressing enter after a "begin" then you need the "smart" codetools version. (and it ignores most of the editor settings).

Its either one or the other.

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #2 on: March 20, 2025, 01:39:52 pm »
Is that the Codetools-General Indentation for Pascal sources options?
I already have both of these options disabled -
  • On break line
  • On paste from clipboard

Even when creating a brand new form unit, my settings seem to be ignored, and the indentation appears to use 8 spaces, instead of a single tab equivalent to 3 spaces.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #3 on: March 20, 2025, 02:35:59 pm »
I already have both of these options disabled -
  • On break line
  • On paste from clipboard
Good, correct.

Quote
Even when creating a brand new form unit, my settings seem to be ignored, and the indentation appears to use 8 spaces, instead of a single tab equivalent to 3 spaces.

Indent, when
- you press return?
- you press tab
  - at the start of line
  - middle of line
- you press tab, while text is selected (or ctrl-I)
?

* Section Tabs

- You want to set the "Tab Width" in the combo box, first line, 2nd column to "3" (if that is what you want tabs to be).
  That affect all tabs, also those inserted by "Indent" below.

- Tabs to spaces: off (unless you want the tab key to insert spaces)
   Ignore the image, that are my settings, not for you
- Smart tabs: off
- Elastic tabs: off


* Section Indent
- Auto Indent: true (if you want to new lines to have the SAME indent as the line above)
- >>CHECK the dropdown<< https://wiki.lazarus.freepascal.org/IDE_Window:_Editor_Options_General#Auto_Indent_(mode_dropdown)

- Block indent
  - Spaces: 0
  - Tabs: 1

- Tab indent blocks: If a selection exists, then the "tab" key will act as Ctrl-I
« Last Edit: March 20, 2025, 02:39:30 pm by Martin_fr »

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #4 on: March 20, 2025, 02:49:53 pm »
There seem to be too many 'detail' options for very specific operations, and not enough 'master' options for setting the default behaviour.

My fundamental issue is that there doesn't seem to be a way to set the tabstop width globally. As I said, in Eclipse, this all 'just works'.

I'd like to revert to no auto-formatting at all, and then build up to a level I'm comfortable with. The auto-indenting seems to actively fight against me at present!

This is what a brand new empty form unit looks like for me - the indent is already too big - it's using 2 tabs, where I'd like it to be using 1 (screenshot 1).

Then if I add a method to the form and press Ctrl+Shift+C to create the implementation, it indents even more (screenshot 2)!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #5 on: March 20, 2025, 04:14:36 pm »
The settings I explained control what happens when
- you press the tab-key
- you press ctrl-i (or ctrl-u)
- you press enter, and have indent of the line above that you want to apply to the new line.

They also apply to any other #9 in your text, if any #9 already is in your text.
But they do not control how those #9 get into your text (except by the tab-key, ctrl-i or return).




So in your case, your template for a new unit (before you do any editing at all) is created with tabs (I assume, can't tell from the image).
I never changed any settings for this on my system, so I don't know too much about it.

But I did some test.
And it uses the settings from that page.

E.g "TForm2" is indented ONCE. And ONCE means, the amount of tabs and spaces specified in the "block indent" section (2 comboboxes on the right).

ONE INDENT = The amount of tabs in "block indent" (each going to the next tab-width) + the amount of spaces.
So "Tab width = 3", "2 Tabs", "1 Space" indents 3*2+1 = 7 columns

For me "Private" is also indented exactly ONCE, the same amount.
For some reason in your image it looks as if it was indented TWICE. I have no idea, I have not seen that before (though there is somewhere a template, that maybe can be edited. I never done that, I vaguely recall to have heard of it).

E.g. if I set block indent to "0 spaces" and "1 tab", then all of those lines are indented by ONE TAB.

Enable "Show special chars" to see what is tab, and what is space....

Well, two exception,
* If "tab to spaces" is checked
=> then all tabs are converted to spaces
* If "tab to spaces" is NOT checked
- If my tab width is 3
- and I set block indent to include "3 spaces"
=> then those 3 spaces are combined into one tab (i.e. "spaces to tabs")
(IMHO that could be seen as wrong, but don't know...)

But otherwise it follows those settings.

* I do not know why your "public" / "Private" lines are indented twice
* I do not know how you entered the "procedure test;" in the class's public section?
  And if you added indent there yourself?
  Nothing else should have added any indent, as far as I know (if you have the 2 codetool smart indent option off, as you stated).




When did Shift-Ctrl-C to get the implementation of procedure Test, the indent on the empty line also is according to the settings.

However, there may be a problem there. I don't know if there is a setting...

In my tests, that indent for the empty line below the "begin" is position only.
Meaning, neither tabs, nor spaces are inserted. The caret is only positioned (it is placed past the actual end of the line, at the position where the tabs would have ended).

But, that does not work if you want to have tabs. Because when you type (whenever you are past the end of line and type new text) the editor fills it with spaces. And there is no setting to change that. It will always be spaces.

There is a setting in "codetools > general" => "cursor behind eol". If you switch that off, then the caret is placed at the start of line. No indent is created for the empty line.

I don't know if there is a setting to actually create the indent for that empty line, and create it using the tabs...



Afaik there is a bug report asking for "smart indent" settings to be moved to the normal indent page.

If you can find it, probably also worth mentioning the "cursor behind eol" (though I never used that, and I don't know what else it does).

Also if indeed there is no setting to create indent with tabs for the empty line in the new method, then it may be worth a feature request...



Having gone testing through all that. Yes some of the settings are worse to find than I thought....

Unfortunately I only know half of those settings myself, as I do maintain some, but not all of the code that implements those features...

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #6 on: March 20, 2025, 04:23:03 pm »
Thanks for replying so quickly!
This was mainly just to get this frustration off my chest, as I've been fighting against the Lazarus IDE indentation and auto-formatting intermittently for a few months.
It seems that more and more indenting options get added to the IDE without full consideration of how they interact with each other - it's much more complicated than the last version of Delphi that I used, and much more complex than Eclipse, but doesn't work as well.
I'll keep experimenting, but my conclusion so far is that the editor is over-complicated - I think settings need to be taken away rather than added.

Having said all that though, the IDE indenting and auto-formatting is my only complaint about Lazarus - the rest of it is pretty impressive!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #7 on: March 20, 2025, 04:50:44 pm »
The amount of settings is both a blessing and a curse....
How they are presented in the options is indent a problem in some cases.

And with the editor, it is particular an issue, as codetools and editor are largely independent from an implementation view. But not from a user view. So the split of the options can be more than confusing.

The F1 is supposed to get you to the correct wiki. But it does not. Repointing the URL would be easy, but someone needs to take the old wiki pages and re-group (and update) the content for the current layout.

At also, even if you fully understand what each setting does, there may be cases that do not work. Either are not implemented, or broken.
That is a matter of, if no one reports it, then it never gets fixed.
(If it gets reported, it may still take time, unfortunately)

So, if there is no way to have tabs for the empty line in the new procedure, it is possible no one is aware of it (except for you, and currently me, but I will eventually forget).

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #8 on: March 20, 2025, 05:03:05 pm »
One additional point I've noticed.
When I highlight a block of text and press TAB, it indents the text by 2 tabstops (6 chars). SHIFT+TAB correctly unindents by a single tabstop.
It's infuriating.
I don't want the IDE guessing what my tabstops are, I just want to set them and stick to them.
Or should I just give up on tabs, and use spaces for indenting? This seems a bit  retrograde.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #9 on: March 20, 2025, 09:23:30 pm »
When I highlight a block of text and press TAB, it indents the text by 2 tabstops (6 chars). SHIFT+TAB correctly unindents by a single tabstop.

It works here.

What are your options?

You should have
"Block Indent (tabs)" = 1
"Block Indent (space)" = 0

That works for me.

At first I thought, maybe you set it to 2 tabs?
But then un-indent (shift tab) would also do 2 tabs (at least it does here).

Can you attach your Editoroptions.xml please? And Codetooloptions.xml too, just in case. Only those 2. If you prefer you can sent by pm/mail.
Depending on OS (and if no --pcp is set) in ~./lazarus or C:\Users\USERNAME\AppData\Local\lazarus )

And a small sample file in which it happens to you (so I can load it, select the same lines, and see what happens here).


Does it also happen when using  ctrl-i and ctrl-u ? (assuming you have default keymap)

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #10 on: March 21, 2025, 11:51:57 am »
Ah! That's one problem fixed, I can now indent and unindent correctly.
I had 1 and 3 in those options - there is nothing to indicate that those values are added together - I assumed they were exclusive.
I now have 0 for spaces, and 1 for tabs, and TAB block indent works as I want it to.
This doesn't fix all the other crazy auto-indenting going on, but I'll live with this slight improvement for now...!
Thanks!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #11 on: March 21, 2025, 03:29:53 pm »
I suspected some misunderstanding.

Now lets get some feedback, on how could the names (and grouping) of them be improved.
And also the help: https://wiki.lazarus.freepascal.org/IDE_Window:_Editor_Options_General#Indent_and_Tabs


One issue is the difference between
- tab-width (or tab-stop-every-n / at which columns tab-chars do stop)
- tab-char (the actual #9 in the text)
- tab key (that may or may not insert #9)
- space: the space char #32 or the amount of column in tab-width

So the combobox "Tab width" indicates at which columns there is a tab stop. Technically its "Tab stop distance" (but that is imho a bad name).

The 2 combo boxes that confused you "Block indent (tabs)" and "Block indent (spaces)" refer to the amount of #9 and #32 that are inserted. (So if you insert several #9 then that is several tab stops).

I guess you thought, it means: insert X tabs, each of a width of N spaces. But that can't be, because tabs inserted by this must have the same width as any other tab, after all, once they are inserted, they are a #9 with no knowledge what inserted them.



To complicate matters, SynEdit support something called "elastic tabs" where the tab width dynamically changes depending on the text in the surrounding lines. (a bit like a html table).

In that case "Tab width" becomes sort of a "minimum tab width" (one of 2 minimums)

"Tab width": minimum, counted from the previous tab stop
"Elastic min width": minimum, counted from the end of the text before the #9 (i.e. minimum amount of columns that are blank)

With a "tab width"=4 and "elastic min" =2, the text
Code: Text  [Select][+][-]
  1. FooBar#9Something

The first tab can not be at pos 4, because the text FooBar has 6 chars, and would not fit, so it would the first tab stop would be at 7 (making the tab 1 column).
But because of "elastic min"=2, it gets further pushed to 8, so the tabstop actually will be at 8.

If the text was
Code: Text  [Select][+][-]
  1. F#9Something
then the first tab stop can be at 4. Forced by "Tab width", and if "Tab Width was 1, then the first tab stop would be at 2 (assuming "elastic min"=1), or at 3 (for "elastic min"=2).


And there is "smart tabs" which will fill the line with #9 and #32 to align with the next column of the line above.

Code: Text  [Select][+][-]
  1. Foo SomeText Bar
  2. Stuff |
Pressing tab-key ( | = caret pos), will fill the line with #9 and #32 to match the position at the start of "Bar".

This will search multiple lines above, if the line directly above is too short.

But even in this mode, if #9 are used (if "Tabs to spaces" is off), the setting of "Tab Width" still matters. (and if changed, the alignment is lost).




So consider all that, how to best display?
- "Tab Width"  (better name???)
- "Tab to spaces"
affect every mode/setting (Not sure have to test block indent...)

So those 2 should be an the very top.

* "Elastic tab" and "Elastic min" could get their own section

* "Smart tabs", not sure...
- A section for the one setting only? Not really.
- Put it with "Tab width" as current? But it does not affect "Block indent"
- Maybe put it with "Elastic"? Name the section "Tab mode" ? (name suggestions welcome)
- ?

* Auto indent (on line break, have to check if there are other triggers)
Another section?
Should be different from "Block indent

* And at last, a section only "Block indent"
But that still does not explain that "Block indent (tabs)" is the count of tab chars, using the tab width from the very first section.




And with all that I still left out, moving the tab settings from codetools to this page. But that requires more work, than any of the above. The above is just moving controls in the form designer.

Moving the codetools stuff needs code in other units to be changed, so it saves the correct settings. Otherwise settings can get lost...

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #12 on: March 21, 2025, 03:40:14 pm »
Possibly the Block Indent (tabs) setting is redundant - if you want to indent by more than 1 tabstop, then you can press the key twice. This is how other IDEs handle it.
I've got used to opinionated IDEs in the last few years, Lazarus possibly has too many edit/format options!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11048
  • Debugger - SynEdit - and more
    • wiki
Re: IDE tabs and spaces, auto-indenting
« Reply #13 on: March 21, 2025, 03:56:22 pm »
I don't like removing options...

The experience I made more than once is that I added some feature, and was 110% sure I covered any possible setting and option. And someone came along with a really good use case that I had not even remotely though off.

Ideally, we could have an switch, to toggle between "simple" and "advanced" options. Then we could have a subset in the "simple" settings.
But doing that would require a lot of free time. And getting a good idea of what different people use, so that subset can be made at least somewhat reasonable.
Currently, at least I don't have that much time.

A wizard would also be an option, but again way outside the time that I can make available.

chris_laz

  • New Member
  • *
  • Posts: 16
Re: IDE tabs and spaces, auto-indenting
« Reply #14 on: March 21, 2025, 03:58:43 pm »
Yes, I agree it's difficult to remove options once they have been added.
I've done that myself, and then received complaints and had to reinstate options that I was certain nobody was using...

 

TinyPortal © 2005-2018