Autocomplete for bracket and quote

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Autocomplete for bracket and quote

Post by Wolfram »

Would be grate to have a autocomplete for bracket and quotes.
I mean, if I type ( I will get () and the cursor is between the bracket.
If I type () the cursor is behind the brackets.
macOS Catalina 10.15.7
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Autocomplete for bracket and quote

Post by Sicro »

+1
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Autocomplete for bracket and quote

Post by Dude »

Wolfram wrote:if I type ( I will get () and the cursor is between the bracket.
If I type () the cursor is behind the brackets.
You'll never be able to type () and have the cursor behind the brackets, because as soon as you type the first bracket then the second will be added and the cursor put between them (ie. your first request will be done). ;)
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Autocomplete for bracket and quote

Post by Sicro »

@Dude:
Think about it again. ;-)
1. You type "(" and ")" is appended. The cursor is now inside the brackets.
2. You type ")" and the automatically appended ")" will be overwritten. The cursor is now behind the ")".

P.S. Happy New Year 8)
Last edited by Sicro on Mon Jan 01, 2018 1:41 pm, edited 1 time in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Autocomplete for bracket and quote

Post by Dude »

Sorry, I don't get it. :(

After typing "(" the IDE puts "(|)" with the cursor in the middle. Correct? (Note the "|" means cursor).

But then the user then types ")" and the IDE shows "(|))" for a moment, but deletes it and makes it "()|" instead?

Is that right?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Autocomplete for bracket and quote

Post by Sicro »

Dude wrote:After typing "(" the IDE puts "(|)" with the cursor in the middle. Correct? (Note the "|" means cursor).
Yes.
Dude wrote:But then the user then types ")" and the IDE shows "(|))" for a moment, but deletes it and makes it "()|" instead?
Yes, but strictly speaking it looks like this:

Code: Select all

(|)  =>  Type(")")  =>  ()|)  =>  ()|
I thought about it again and now I have come to the conclusion that the auto-completion algorithm for brackets (and quotes) should work like this:

Code: Select all

Procedure OnKeyPress(Char$)
  Select Char$
    Case "("
      WriteChar(Char$)                 ; |   => (|
      WriteChar(")")                   ; (|  => ()|
      SetCursorPos(GetCursorPos() - 1) ; ()| => (|)
    Case ")"
      If GetCharBeforeCursor() = "(" And GetCharAfterCursor() = ")"
        SetCursorPos(GetCursorPos() + 1) ; (|) => ()|
      Else
        WriteChar(Char$) ; ?|? => ?)|?
      EndIf
  EndSelect
EndProcedure
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply