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

Update

Post by Piero »

To avoid problems with \c in code, I used the gnu echo (gecho)

Version 0.0001b2

Needs:
Support Text File
Homebrew's fzf
Homebrew's ggrep
Homebrew's gecho (coreutils)

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
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 "(/opt/homebrew/bin/gecho " & 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 "(/opt/homebrew/bin/gecho " & src & ggrep & "-Poi '^[^;]*procedure(?!return)[.]?\\w*\\s+\\K.*\\)' ) || echo Debug" without altering line endings) -- add source procedures
	set fsrc to fsrc & (do shell script "(/opt/homebrew/bin/gecho " & 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 "(/opt/homebrew/bin/gecho " & 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
PB version (parses all "open+saved" files):

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 \"(/opt/homebrew/bin/gecho \" & 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 \"(/opt/homebrew/bin/gecho \" & src & ggrep & \"-Poi '^[^;]*procedure(?!return)[.]?\\\\w*\\\\s+\\\\K.*\\\\)' ) || echo Debug\" without altering line endings) -- add source procedures\n"+
~"set fsrc to fsrc & (do shell script \"(/opt/homebrew/bin/gecho \" & 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 \"(/opt/homebrew/bin/gecho \" & 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)
Special Thanks: Richard Stallman, pf shadoko

Edit/Note:
In case of debugger windows "always on top" (PB preferences) it cannot type on code window (I tried to fix; found no way :cry:)
Is it a PB IDE bug?

Edit2:
Fixed Procedure/Macro regexps (to exclude the commented ones)
Note: Does NOT match the "spaced dot" case: Procedure a . b(c)
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Update

Post by Piero »

Just noticed that you can select PB words with ⌃⇧← (control–shift–left)… !?!
I'm not modifying previous version because it can be useful as example………

Version 0.0001b3

Needs:
Support Text File
Homebrew's fzf
Homebrew's ggrep
Homebrew's gecho (coreutils)

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
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 application process "PureBasic" to key code 123 using {shift down, control down} -- select word
	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 "(/opt/homebrew/bin/gecho " & 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 "(/opt/homebrew/bin/gecho " & src & ggrep & "-Poi '^[^;]*procedure(?!return)[.]?\\w*\\s+\\K.*\\)' ) || echo Debug" without altering line endings) -- add source procedures
	set fsrc to fsrc & (do shell script "(/opt/homebrew/bin/gecho " & 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 "(/opt/homebrew/bin/gecho " & 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
PB version (parses all "open+saved" files):

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 application process \"PureBasic\" to key code 123 using {shift down, control down} -- select 'word'\n"+
~"tell scint to set sel to value of attribute \"AXSelectedText\"\n"+
~"set fsrc to (do shell script \"(/opt/homebrew/bin/gecho \" & 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 \"(/opt/homebrew/bin/gecho \" & src & ggrep & \"-Poi '^[^;]*procedure(?!return)[.]?\\\\w*\\\\s+\\\\K.*\\\\)' ) || echo Debug\" without altering line endings) -- add source procedures\n"+
~"set fsrc to fsrc & (do shell script \"(/opt/homebrew/bin/gecho \" & 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 \"(/opt/homebrew/bin/gecho \" & 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)
Post Reply