Backspace key, delete indentation parts?

Working on new editor enhancements?
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Backspace key, delete indentation parts?

Post by highend »

Hi,

when the code is already indented and you press {Enter} to create a new line, this line has the same indention as the last one.
So far it's fine.

But if you now press {Backspace} to remove the indention step by step (I normally use 2 spaces as an indention) it only removes a single space.

Is there a configuration entry that allows me to use {Backspace} to work that way?

Some sample code with a visible cursor mark:
I'd like to go back one indention step to finish the With statement...

Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Backspace key, delete indentation parts?

Post by mk-soft »

No,
this is a normal behavior and cannot be changed.
After entering EndXYZ and Enter it will be corrected automatically.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Backspace key, delete indentation parts?

Post by BarryG »

@highend: There's a setting to use real tabs instead of spaces for indents ("Editor -> Use real tab"), which makes Backspace do what you want.
User avatar
yuki
Enthusiast
Enthusiast
Posts: 101
Joined: Sat Mar 31, 2018 9:09 pm

Re: Backspace key, delete indentation parts?

Post by yuki »

Like @BarryG says, using true tabs will solve this.

If you prefer spaces still, you can use an IDE tool to do this, provided you're on Windows.

Compile the following to EXE and store it somewhere handy:

Code: Select all

CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CompilerError "Sorry, this tool only works on Windows!"
CompilerEndIf

Define scintillaHandle = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))
If Not scintillaHandle
  MessageRequester("Fatal Error", "Failed to retrieve PB_TOOL_Scintilla", #PB_MessageRequester_Error)
  End
EndIf

; Enable use of backspace to delete entire indent-levels at once, rather than only a single
; character, when deleting spaces at the start of a line.
SendMessage_(scintillaHandle, #SCI_SETBACKSPACEUNINDENTS, #True, #Null)

; Doubles caret thickness from 1px -> 2px to feel more cozy.
; A total matter of personal preference, but I recommend it for large font sizes or certain
; dark themes.
SendMessage_(scintillaHandle, #SCI_SETCARETWIDTH, 2, #Null)

; Enable DirectWrite for vastly improved text quality and ligature support.
; Requires:
;   - PB 6.03+
;   - Windows Vista+
; Does nothing if requirements not met.
SendMessage_(scintillaHandle, #SCI_SETTECHNOLOGY, #SC_TECHNOLOGY_DIRECTWRITE, #Null)

; This is an *absolute must* when using the IDE with an IME.
; Without this, the default mode is to show the IME windowed, which covers text and can be
; extremely difficult to read as it doesn't respect user theming nor font choice.
SendMessage_(scintillaHandle, #SCI_SETIMEINTERACTION, #SC_IME_INLINE, #Null)
Then, register two new IDE tools, both with the executable you've compiled, with these separate triggers:
  • Sourcecode loaded
  • New Sourcecode created
Suggested options:
  • Wait until tool quits
  • Run Hidden
  • Hide Tool from the Main menu
This tool does a few things, each of which you can comment out/delete at your leisure:
  • Enables use of backspace for deleting whole indent levels worth of spaces at the head of a line. (#SCI_SETBACKSPACEUNINDENTS)
  • Widens the caret from 1px to 2px, which for me feels cozier. Can be handy given dark themes/larger font size. (#SCI_SETCARETWIDTH)
  • Enables DirectWrite text rendering for vastly improved text quality and ligature support. (#SCI_SETTECHNOLOGY)
    • I highly recommend giving this a try; you might find text looks far nicer (and ligatures are great if your font supports it).
    • It will require PB 6.03 beta 4 or later (for newer Scintilla) to see results. For older PB versions, it is ignored.
  • Vastly improves IME behaviour, which is maybe not a concern for you, but is harmless otherwise. (#SCI_SETIMEINTERACTION)
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Re: Backspace key, delete indentation parts?

Post by highend »

After entering EndXYZ and Enter it will be corrected automatically.
Yeah but then I have a new (again: indented) line that I (eventually) need to remove.
Ofc not in the example code (it was just to demonstrate where the cursor is).
Because of that I like to enter an ending statement manually :)
There's a setting to use real tabs instead of spaces for indents ("Editor -> Use real tab"), which makes Backspace do what you want.
I don't use tabs (because I don't want to use them) and settled with spaces a long time ago (for all languages, not only for PB).

@Both
Thanks for the suggestions though!

@yuki
Awesome! It works as intended (it now removes 2 spaces at once).
I don't see a visual improvement currently (but I'm not using a font
that supports ligatures atm) but maybe that's just because
that I'm working on my Mac and have my PB IDE on a Windows VM
to which I connect via RDP...

There is one thing that would be the icing on the cake but if Scintilla
doesn't support that it's fine.
Is there a #SCI_... constant that does the following:
If a new indented line was created (by hitting {Enter} after an
already indented line) and you hit {Enter} again it would be
fantastic if the indentation of the former line is deleted
automatically.

Like:

Code: Select all

ForEach ReferenceFileContent()
  With ReferenceFileContent()
    ; Skip invalid entries
    If \Type = Functions::#LineInvalidKeyValue
      Continue
    EndIf
    <cursor position after {Enter} after EndIf>
    <new cursor position after {Enter} again>  

And because "<cursor position after {Enter} after EndIf>" just
has a leading indentation that one gets deleted (not the full
line, only the indentation).

If this is difficult to understand I'll upload a .gif animation
of how this works in Sublime Text (it deletes intermediate
indentations between multiple lines (that contain nothing)
but begins the next new line with the correct indentation...
User avatar
yuki
Enthusiast
Enthusiast
Posts: 101
Joined: Sat Mar 31, 2018 9:09 pm

Re: Backspace key, delete indentation parts?

Post by yuki »

highend wrote: Fri Oct 06, 2023 12:20 pm @yuki
Awesome! It works as intended (it now removes 2 spaces at once).
I don't see a visual improvement currently (but I'm not using a font
that supports ligatures atm) but maybe that's just because
that I'm working on my Mac and have my PB IDE on a Windows VM
to which I connect via RDP...
Glad that helped! And yeah, visual improvements from DirectWrite will vary depending on font face and scale.
highend wrote: Fri Oct 06, 2023 12:20 pm There is one thing that would be the icing on the cake but if Scintilla
doesn't support that it's fine.
Is there a #SCI_... constant that does the following:
If a new indented line was created (by hitting {Enter} after an
already indented line) and you hit {Enter} again it would be
fantastic if the indentation of the former line is deleted
automatically.
Hmm, I don't think that's trivially doable with a Scintilla config, though I could be wrong...

I'm pretty accustomed to that sort of behaviour from VSCode (as well as trimming all trailing whitespace on save), but I'm accustomed to switching when using the PB IDE as well.

You could use a separate tool to trim whitespace upon save, but it might be best not to fight the default behaviour here.
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Re: Backspace key, delete indentation parts?

Post by highend »

Thanks for the advice, yuki. I'll go the "clean everything on save" way then :mrgreen:
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Backspace key, delete indentation parts?

Post by AZJIO »

highend
You can use a regular expression, but I open the source in AkelPad and press Alt+Backspace, all trailing spaces are removed. You can also use the tool from HeX0R, but in addition to removing spaces, it also improves the indentation between operators (I've been using it for a long time, no problems).

regular expression:
\h+(?=\r|\n|\z)
replace with nothing
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Re: Backspace key, delete indentation parts?

Post by highend »

Thanks AZJIO.

I've done it in a similar way for too long (copying and pasting the code into Sublime Text, trimmed the trailing spaces via a plugin and copied everything back). Sick of doing it that way...

The tool from HeXOR only does it for files, not for the current code in the IDE and I really want to use as less steps to achieve this as possible (and reloading the code would be an unnecessary step as well). A regex would be a way but it requires opening the find & replace tool, choose the regex, click ok, etc. Too many steps as well.

So what I did is this:

I got the code from skywalk: viewtopic.php?p=518534#p518534
Thanks skywalk for kindly providing it!

I've added / modified a few functions (GetCurrentPos, GetLineFromPosition, GotoLine, Get & SetFirstVisibleLine, Get & SetAllText) and whenever I now presse CTRL+T that little tool is invoked and that trims all trailing whitespaces directly in the Gui and restores the view afterwards (first visible line and the caret is placed at the beginning of the line it was last in).

Happy now^^
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Backspace key, delete indentation parts?

Post by AZJIO »

highend wrote: Sat Oct 07, 2023 12:34 pm The tool from HeXOR only does it for files
No, it works everywhere, I use the hotkey Ctrl+Alt+Z to instantly fix it even if the file is not saved, look at the tool settings to understand how it works. The only drawback is that you lose the cancellation queue, since the file is automatically re-opened.
highend wrote: Sat Oct 07, 2023 12:34 pm I've done it in a similar way for too long (copying and pasting the code into Sublime Text,
You don't need to copy and paste to make a correction in another editor. Add any editor as a tool and make a button on the toolbar in the PureBasic IDE and pass the open file in the launch options. In fact, an editor will open, then you press the hotkey for correction and Ctrl + S (save), and Win + down arrow (collapse window), then press OK, agreeing to re-open the file in the PureBasic IDE.
I don't remove spaces too often because it doesn't actually make things visually easier for you, you don't even see them. I make these improvements when I'm about to put the source online or when I've completed some round of improvements. At the same time, I open it in another editor to correct the version of the program indicated at the end of the file, in hidden lines. It is convenient to write code in AkelPad due to the ability to insert code in entire fragments, so I always have two code editors open and I can open a file in both editors simply by pressing a hotkey.

Video with two editors
Video using regular expression
Video using the tool from HeXOR
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Re: Backspace key, delete indentation parts?

Post by highend »

No, it works everywhere
As I said: It operates on files, it doesn't access the Scintilla IDE gadget directly.
then you press the hotkey for correction and Ctrl + S (save), and Win + down arrow (collapse window), then press OK, agreeing to re-open the file in the PureBasic IDE
Again: Way to many steps involved.

I don't want to depend on having more than one IDE / editor open at the same time to do some (simple but necessary) stuff in Sublime Text (or any other editor).

The current solution requires a simple press of a hotkey (or I could even bind it to another trigger like "Sourcecode saved" if I want it to happen automatically) and it happens directly in the IDE itself. No additional file reloading, no other keys to press, mouse movement, etc.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Backspace key, delete indentation parts?

Post by AZJIO »

highend wrote: Sat Oct 07, 2023 2:59 pm requires a simple press of a hotkey
This is true if you are limited to one task, which is not always the case.
highend wrote: Sat Oct 07, 2023 2:59 pm As I said: It operates on files, it doesn't access the Scintilla IDE gadget directly.
Do you care about the result or the color of the tool icon?
highend
Enthusiast
Enthusiast
Posts: 169
Joined: Tue Jun 17, 2014 4:49 pm

Re: Backspace key, delete indentation parts?

Post by highend »

Do you care about the result or the color of the tool icon?
I care about not doing unnecessary stuff. Reloading a file in the IDE when changed by an external tool often leads to most part of the text being black until you scroll in the Gui again. The Scintilla way doesn't have this problem, at least I haven't seen it since I'm using it.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Backspace key, delete indentation parts?

Post by AZJIO »

highend wrote: Sat Oct 07, 2023 4:29 pm Reloading a file in the IDE when changed by an external tool often leads to most part of the text being black
I have never seen a problem after using the tool. A tool that processes files directly has the positive side that it can be used in any editor that is not Scintilla. There is nothing complicated about using Scintilla; there are several tools on the forum made using text capture directly from the Scintilla window, both all text and parts. FindAllReferences, Variable renaming
Post Reply