Good News! Fuzzy Completion!

Mac OSX specific forum
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Good News! Fuzzy Completion!

Post by Piero »

I had to hack a bit because of IDE 6.10 "selection" problems… anyway here's version 0.0001a1!
See links for needed support stuff (e.g. fzf, if you cannot use brew…)
I plan to add completion of entire lines (procedures etc.) and more, must be easy now; just some regex here and there, and mostly on stuff already inside PureBasic.app… but I better wait until the IDE will be a bit more "stable"……
Apropos, did you ppl see what TextMate can do? Try "Edit Bundles"!

Anyway, I also made a 18-hours video explaining the basics of my creation… Enjoy!
Video (dedicated to Idle)

PB Text File
fzf silicon from brew install

New VIDEO dedicated to pf shadoko

READ POSTS BELOW FOR UPDATES!

AppleScript IDE tool:

Code: Select all

set pargr to "" -- Mac IDE Tool (no parameters needed) for Fuzzy Autocomplete!!!
try
	tell application "PureBasic" to activate
	tell application "System Events"
		set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 ¬
			of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic"
		
		repeat 16 times -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
			tell application process "PureBasic" to key code 123 using shift down
		end repeat
		delay 0.1
		tell scint to set sel to value of attribute "AXSelectedText"
		set nc to count last word of sel
		tell application process "PureBasic" to key code 124
		repeat nc times
			tell application process "PureBasic" to key code 123 using shift down
		end repeat -- END of HACK… 10 lines instead of 1… grrr…
		
		set src to words of ((value of scint) as string)
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to linefeed
		set src to src as text
		set AppleScript's text item delimiters to ASTID
		set src to quoted form of src
		tell scint to set sel to value of attribute "AXSelectedText" -- Paths to support file and app below…
		set pargr to paragraphs 2 thru -1 of (do shell script ¬
			"echo " & src & " | cat - ~/bin/pb.txt | sort -u | /opt/homebrew/bin/fzf -i -f " & sel)
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 1 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end tell
end try
Last edited by Piero on Sat Apr 13, 2024 3:12 pm, edited 2 times in total.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Huh?

Post by Piero »

No comments?

I think it's better this way: I don't really wanna sell my old Macs; I Love them
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Good News! Fuzzy Completion!

Post by Fred »

Good idea, I use this a lot in VS/Resharper IDE
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Good News! Fuzzy Completion!

Post by idle »

Piero wrote: Fri Apr 05, 2024 7:35 am Anyway, I also made a 18-hours video explaining the basics of my creation… Enjoy!
Video (dedicated to Idle)
Pineapple Pizza on the menu and I'll try one of those Scottish delicacies a Deep fried Mars bar. :lol:
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Good News! Fuzzy Completion!

Post by Piero »

"Regex PB words" version 0.0001a2

Note: you may need to launch it (as IDE tool) with its binary path:
Fuzzy.app/Contents/MacOS/applet
Anyway, you can also run it with a global launcher…

Code: Select all

-- Mac IDE Tool (no parameters needed) for Fuzzy Autocomplete!!! - Paths to support stuff on line below
property exp : " | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?' | cat - ~/bin/pb.txt  | sort -u | /opt/homebrew/bin/fzf -i -f "
try
	tell application "PureBasic" to activate
	tell application "System Events"
		set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 ¬
			of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic"
		set src to ((value of scint) as string)
		
		repeat 16 times -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
			tell application process "PureBasic" to key code 123 using shift down
		end repeat
		delay 0.1
		tell scint to set sel to value of attribute "AXSelectedText"
		set nc to count last word of sel
		tell application process "PureBasic" to key code 124
		repeat nc times
			tell application process "PureBasic" to key code 123 using shift down
		end repeat -- END of HACK… 10 lines instead of 1… grrr…
		
		tell scint to set sel to value of attribute "AXSelectedText"
		copy paragraphs 2 thru -1 of (do shell script "echo " & quoted form of src & exp & sel) to pargr
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 1 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end tell
end try
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Update!

Post by Piero »

Version 0.0001a3

Added all the "function prototypes" of PB help to the Support File
Added procedures completion (the regex I used needs Homebrew's ggrep: it can be very useful!)

Enjoy!

Code: Select all

-- AppleScript PB IDE Tool (no parameters needed) for Fuzzy Autocomplete!
property exp : " | cat - ~/bin/pb.txt | sort -u | /opt/homebrew/bin/fzf -i -f " -- paths to support stuff here
property ggrep : " | /opt/homebrew/bin/ggrep " -- gnu grep path ; it will be called ggrep
tell application "PureBasic" to activate -- just in case (needed only for app launchers/debug)
tell application "System Events"
	set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic" -- in this case, only for better code readability…
	
	repeat 16 times -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
		tell application process "PureBasic" to key code 123 using shift down
	end repeat
	delay 0.1
	tell scint to set sel to ((value of attribute "AXSelectedText") as string)
	set nc to count last word of sel
	tell application process "PureBasic" to key code 124
	repeat nc times
		tell application process "PureBasic" to key code 123 using shift down
	end repeat -- END of HACK… 10 lines instead of 1… grrr…
	
	tell scint to set sel to value of attribute "AXSelectedText"
	delay 0.1
	tell application process "PureBasic" to key code 51 -- delete (exclude from source)
	set src to quoted form of ((value of scint) as string) -- source
	set fsrc to (do shell script "echo " & src & " | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?'") & "\n" -- extract PB "words"
	set fsrc to fsrc & (do shell script "echo " & src & ggrep & "-Poi '[^;]*procedure\\w*\\s+\\K.*\\)'") & "\n" -- add source procedures
	copy {sel} & paragraphs of (do shell script "echo " & quoted form of fsrc & exp & quoted form of sel) to pargr
	try
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 2 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end try
end tell
Edit/PS:

Code: Select all

tell application process "PureBasic" to key code 53 -- press esc
;)

It took less than 2 minutes to extract "function prototypes" from PB help, with TextMate ;) ;) ;)
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Good News! Fuzzy Completion!

Post by Piero »

Version 0.0001a5

Trapped possible errors, also bugfixing the "no procedures" error (remember: it needs homebrew ggrep)
Added some cocoa to detect option key up (my shortcut is ⌥Z)
Added esc in case of PB autocomplete open
Fixed sort

Code: Select all

-- AppleScript PB IDE Tool (no parameters needed) for Fuzzy Autocomplete!
property exp : " | cat - ~/bin/pb.txt | /opt/homebrew/bin/fzf -i -f " -- paths to support stuff here
property ggrep : " | /opt/homebrew/bin/ggrep " -- gnu grep path; it will be called ggrep
use AppleScript version "2.3" -- Mavericks (10.9) or later
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSEvent
tell application "PureBasic" to activate -- just in case (needed only for app launchers/debug)
tell application "System Events"
	repeat -- wait for option key up (PB shortcut)
		if not (((current application's NSEvent's modifierFlags()) div (current application's NSAlternateKeyMask as integer)) mod 2) > 0 then exit repeat
		delay 0.3
	end repeat
	tell application process "PureBasic" to key code 53 -- press esc
	set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic" -- in this case, only for better code readability…
	
	repeat 16 times -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
		tell application process "PureBasic" to key code 123 using shift down
	end repeat
	delay 0.1
	tell scint to set sel to ((value of attribute "AXSelectedText") as string)
	set nc to count last word of ("Error " & sel)
	tell application process "PureBasic" to key code 124
	repeat nc times
		tell application process "PureBasic" to key code 123 using shift down
	end repeat -- END of HACK… 10 lines instead of 1… grrr…
	
	tell scint to set sel to value of attribute "AXSelectedText"
	delay 0.1
	tell application process "PureBasic" to key code 51 -- delete (exclude from source)
	set src to quoted form of ((value of scint) as string) -- source
	set fsrc to (do shell script "(echo " & src & " | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?' | sort -u ) || echo Debug") & "\n" -- extract PB "words"
	set fsrc to fsrc & (do shell script "(echo " & src & ggrep & "-Poi '[^;]*procedure\\w*\\s+\\K.*\\)' | sort -u ) || echo Debug") & "\n" -- add source procedures
	if sel is not "" then copy {sel} & paragraphs of (do shell script "(echo " & quoted form of fsrc & exp & quoted form of sel & ") || echo Debug") to pargr
	try
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 2 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end try
end tell
Last edited by Piero on Fri Apr 12, 2024 5:22 pm, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Good News! Fuzzy Completion!

Post by Piero »

I imagine the Gurus of this Forum seeing "delay 0.3"… it must feel like I would feel eating pineapple pizza with ketchup
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Update

Post by Piero »

Small update to script above because sort -u pipe wasn't working as expected
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Finally! Fuzzy Autocomplete!

Post by Piero »

I found what the problem was! Hint: \r!!!
Now works very well! (at least on Monterey M1)

Version 0.0001b1
Optimized the delays (for my Mac…)
Put a minimum of 3 characters needed (max is 16, see the hack…)
Updated procedure regex

See VIDEO!

Needs:
Support Text File (updated Apr 09, 2024)
Homebrew's fzf
Homebrew's ggrep

Enjoy!

Code: Select all

-- AppleScript PB IDE Tool (no parameters needed) for Fuzzy Autocomplete!
property exp : " | cat - ~/bin/pb.txt | tr '\r' '\n' | sort -u | /opt/homebrew/bin/fzf -i -f " -- paths to support stuff here
property ggrep : " | /opt/homebrew/bin/ggrep " -- homebrew gnu grep path; it will be called ggrep
use AppleScript version "2.3" -- Mavericks (10.9) or later
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSEvent
tell application "PureBasic" to activate -- just in case (needed only for app launchers/debug)
tell application "System Events"
	repeat -- wait for option key up (PB shortcut)
		if not (((current application's NSEvent's modifierFlags()) div (current application's NSAlternateKeyMask as integer)) mod 2) > 0 then exit repeat
		delay 0.1
	end repeat
	tell application process "PureBasic" to key code 53 -- press esc
	set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic" -- in this case, only for better code readability…
	
	repeat 16 times -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
		tell application process "PureBasic" to key code 123 using shift down
	end repeat
	delay 0.1
	tell scint to set sel to ((value of attribute "AXSelectedText") as string)
	set nc to count last word of ("Error " & sel)
	tell application process "PureBasic" to key code 124
	repeat nc times
		tell application process "PureBasic" to key code 123 using shift down
	end repeat -- END of HACK… 10 lines instead of 1… grrr…
	
	tell scint to set sel to value of attribute "AXSelectedText"
	set src to quoted form of ((value of scint) as string) -- source
	set fsrc to (do shell script "(echo " & src & " | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?' ) || echo Debug") & "\n" -- extract PB "words"
	set fsrc to fsrc & (do shell script "(echo " & src & ggrep & "-Poi '[^;]*procedure[.]?\\w*\\s+\\K.*\\)' ) || echo Debug") & "\n" -- add source procedures
	if (count words of sel) > 0 and (count sel) > 2 then copy paragraphs of (do shell script "(echo " & quoted form of fsrc & exp & quoted form of sel & ") || echo Debug") to pargr
	try
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 2 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end try
end tell
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Video

Post by Piero »

User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Have Fun!

Post by Piero »

Version 0.0001b2

Hacked my own hack :mrgreen: (found I could reliably read cursor position, so I used ⌥←)
Removed "option key wait" (better use a simple shortcut like ⌥Z)
Added Macros (definition) so now you can "clearly" see how to add patterns

Needs:
Support Text File (updated Apr 09, 2024)
Homebrew's fzf
Homebrew's ggrep

Have Fun!
Piero

Code: Select all

-- AppleScript PB IDE Tool (no parameters needed) for Fuzzy Autocomplete!
property exp : " | cat - ~/bin/pb.txt | tr '\r' '\n' | sort -u | /opt/homebrew/bin/fzf -i -f " -- paths to support stuff here
property ggrep : " | /opt/homebrew/bin/ggrep " -- homebrew gnu grep path; it will be called ggrep
tell application "PureBasic" to activate -- just in case (needed only for app launchers/debug)
tell application "System Events"
	tell application process "PureBasic" to key code 53 -- press esc
	set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of item 1 of (windows of application process "PureBasic" whose name begins with "PureBasic") -- in this case, only for better code readability…
	tell scint to set c1 to item 2 of (value of attribute "AXSelectedTextRange" as list) -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…
	tell application process "PureBasic" to key code 123 using option down
	tell scint to set c2 to item 2 of (value of attribute "AXSelectedTextRange" as list)
	repeat c1 - c2 times
		tell application process "PureBasic" to key code 124 using shift down
	end repeat -- END HACK
	delay 0.1
	tell scint to set sel to value of attribute "AXSelectedText"
	set src to quoted form of ((value of scint) as string) -- source
	set fsrc to (do shell script "(echo " & src & " | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?' ) || echo Debug" without altering line endings) -- extract PB "words"
	set fsrc to fsrc & (do shell script "(echo " & src & ggrep & "-Poi '[^;]*procedure[.]?\\w*\\s+\\K.*\\)' ) || echo Debug" without altering line endings) -- add source procedures
	set fsrc to fsrc & (do shell script "(echo " & src & ggrep & "-Poi '[^;]*macro\\s+\\K\\w+\\s*(\\(.*\\))' ) || echo Debug" without altering line endings) -- add source macros
	if (count words of sel) > 0 and (count sel) > 2 then copy paragraphs of (do shell script "(echo " & quoted form of fsrc & exp & quoted form of sel & ") || echo Debug") to pargr
	try
		tell application "PureBasic" to set pargr to choose from list pargr default items {item 2 of pargr}
		tell application process "PureBasic" to keystroke {item 1 of pargr}
	end try
end tell
Small edit: used "without altering line endings", but left "| tr '\r' '\n' |" (in case of "bad editing" of the support file…)
Edit: now also works with debugger open
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

PB Version!

Post by Piero »

Here's the PB version
It will parse all your (saved + open) source files

Enjoy!
Piero

Code: Select all

; PB IDE Tool (no parameters needed) for Fuzzy Autocomplete!

Procedure simpleShell(ShellCommand$)
   Protected shell
   shell = RunProgram("/bin/sh","","", #PB_Program_Open|#PB_Program_Write)
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      CloseProgram(shell)
   EndIf
EndProcedure

ExamineEnvironmentVariables() 
PBSOURCES.s = GetEnvironmentVariable("PB_TOOL_FileList")

AScr.s = ~"osascript\n"+
~"set src to \"\"\n"+
~"repeat with i in paragraphs of \"" + PBSOURCES + ~"\"\n"+
~"set src to src & (do shell script \"cat \" & quoted form of i without altering line endings) & \"\\n\"\n"+
~"end repeat\n"+
~"set src to quoted form of src -- sources\n"+
~"property exp : \" | cat - ~/bin/pb.txt | tr '\\r' '\\n' | sort -u | /opt/homebrew/bin/fzf -i -f \" -- paths to support stuff here\n"+
~"property ggrep : \" | /opt/homebrew/bin/ggrep \" -- homebrew gnu grep path; it will be called ggrep\n"+
~"tell application \"PureBasic\" to activate -- just in case (needed only for app launchers/debug)\n"+
~"tell application \"System Events\"\n"+
~"tell application process \"PureBasic\" to key code 53 -- press esc\n"+
~"set scint to a reference to text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of item 1 of (windows of application process \"PureBasic\" whose name begins with \"PureBasic\") -- in this case, only for better code readability…\n"+
~"tell scint to set c1 to item 2 of (value of attribute \"AXSelectedTextRange\" as list) -- HACK to bypass PB 6.10 IDE bug; ⌥← works, but ⌥⇧← does not…\n"+
~"tell application process \"PureBasic\" to key code 123 using option down\n"+
~"tell scint to set c2 to item 2 of (value of attribute \"AXSelectedTextRange\" as list)\n"+
~"repeat c1 - c2 times\n"+
~"tell application process \"PureBasic\" to key code 124 using shift down\n"+
~"end repeat -- END HACK\n"+
~"delay 0.1\n"+
~"tell scint to set sel to value of attribute \"AXSelectedText\"\n"+
~"set fsrc to (do shell script \"(echo \" & src & \" | grep -o -E '[*@#!]?[_a-zA-Z]+[0-9_a-zA-Z]*[$]?' ) || echo Debug\" without altering line endings) -- extract PB \"words\"\n"+
~"set fsrc to fsrc & (do shell script \"(echo \" & src & ggrep & \"-Poi '[^;]*procedure[.]?\\\\w*\\\\s+\\\\K.*\\\\)' ) || echo Debug\" without altering line endings) -- add source procedures\n"+
~"set fsrc to fsrc & (do shell script \"(echo \" & src & ggrep & \"-Poi '[^;]*macro\\\\s+\\\\K\\\\w+\\\\s*(\\\\(.*\\\\))' ) || echo Debug\" without altering line endings) -- add source macros\n"+
~"if (count words of sel) > 0 and (count sel) > 2 then copy paragraphs of (do shell script \"(echo \" & quoted form of fsrc & exp & quoted form of sel & \") || echo Debug\") to pargr\n"+
~"try\n"+
~"tell application \"PureBasic\" to set pargr to choose from list {sel} & pargr default items {item 1 of pargr}\n"+
~"tell application process \"PureBasic\" to keystroke {item 1 of pargr}\n"+
~"end try\n"+
~"end tell"

simpleShell(AScr)
Edit (18 Apr 2024): small tweak to default selection (only for this PB version).
Also Note: fzf was updated 4 days ago; now it's even faster!
Last edited by Piero on Thu Apr 18, 2024 7:28 pm, edited 4 times in total.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: Good News! Fuzzy Completion!

Post by pf shadoko »

a video for me, great!
(after viewing, I'm a bit disappointed, I was thinking of a documentary on my life and work :cry: )

since v6, the list of authorized constants is displayed for function Arguments, too bad it requires typing "#pb_" (especially for beginners)
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Good News! Fuzzy Completion!

Post by Piero »

pf shadoko wrote: Wed Apr 17, 2024 9:11 amsince v6, the list of authorized constants is displayed for function Arguments, too bad it requires typing "#pb_" (especially for beginners)
Yep, that's why I think fuzzy completion can be useful; it just takes some tries to learn how to type good "abbreviations"…
Dedicated to you for your amazing paysage work (that I also used to test this thing)
Thanks!
Post Reply