New feature - code snippets

William Lancee

Well-Known Member
Licensed User
Longtime User
@PaulMeuris , @Erel , @aeric

Doubling $ doesn't work well on smart strings with ${ } constructs
And smart strings don't work without doubling $

Edit: @aeric pointed out that @PaulMeuris already tested that tripling $ for ${ } constructs works: $$${value}

B4X:
'Iterate over keys and values
For Each key As Object In $map$.Keys
    Dim value As Object = $map$.Get(key)

    Dim s as string = $$"The Quick $${value}Brown Fox"$$
    $end$
    Dim s as string = $$"The Quick $${value}Brown Fox"$$
Next

'Yields
    For Each key As Object In map.Keys
        Dim value As Object = map.Get(key)
   
        Dim s As String = $"The Quick {value}Brown Fox"$
       
        Dim s As String = $"The Quick {value}Brown Fox"$
    Next
 
Last edited:

Magma

Expert
Licensed User
Longtime User

aeric

Expert
Licensed User
Longtime User
In code snippet:

For the string literal you can use extra $ signs.
In the B4J IDE:
View attachment 146196
after substitution:
Dim myvar As String
Dim str As String = $"this is a string litaral with variable ${myvar} in it"$
This not so elegant workaround works...
Ya and but then the string in between the $ signs is highlighted even though it is not a placeholder.

edit: The highlighting is gone when lose focus.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Take note that the description (comment on first line) is not included when applying the snippet
 

LucaMs

Expert
Licensed User
Longtime User
I started on converting my collection of snippets.
But I realize as @LucaMs pointed out, naming of the text files is super critical.
Especially if Forum members want to share snippets.

I wonder if @Erel has any recommendations?
Or perhaps @LucaMs?

Prefix with object type? Suffix with object type?
Short without articles and prepositions?
Keywords to be avoided, since they conflict?
I was thinking of a prefix of 3 or 4 letters (str_ for snippets concerning strings, for example) but then, having seen an animated gif published by Erel, I realized that the editor's intellisense lists commands that contain a certain number of letters, not just those that begin with those letters, so it is not essential, just remember a minimum of the content of the name, that the name is logical.
 

AnandGupta

Expert
Licensed User
Longtime User
So basically you're playing the game in hard mode. ? I hear the level endboss is coding without Log() and disabling stacktraces. ?

(I'm clearly still on the tutorial level.)
I use both, debug and log.
And same way in all my development programming languages Clipper, Xbase++,C#,B4X etc.

There are times when log gives faster result than debugging.
One should not think pencil as inferior to pen, IMHO.
 

cheveguerra

Member
Licensed User
Longtime User
Hi everybody,

I created the Snippets folder, created and copied some code in 3 files (the first one is the code from Erel), zipped the directory, renamed to "CodeSnippets.b4xLib", copied the file to "AddLibs\B4A" (That is my Additional Libraries directory) and restarted the IDE ... but nothing happens when I write "code" or "media" or whatever ... what am I doing wrong? ?

I used default compression when zipping.

I copied also the b4xLib file to "AddLibs", but without luck.

Tried checking the checkbox of CodeSnippets in the "Libraries Manager" tab.

I have version 12.5 64 Bit installed.

Best regards

1695659578836.png
 

OliverA

Expert
Licensed User
Longtime User
I have version 12.5 64 Bit installed.
This is currently only available in the latest B4J beta version. It will most likely be included in future B4A versions.
 

mangojack

Expert
Licensed User
Longtime User
Hi everybody,

I created the Snippets folder, created and copied some code in 3 files (the first one is the code from Erel),...
noticed in your post the naming of your snippet text files ...


I believe that the snippet text file name does Not need the "code_" prefix.

Unzipping jOKHttpUtils.b4xlib file that was packaged with the recent B4J beta , Erel has included 2 snippets.

Capture2.png




The IDE adds the prefix "Code_" to all file names located in the Snippets folder and displays the list when you start typing ... code


This is a test snippet I created.

Captur3.png




Capture.png



Cheers
 

ilan

Expert
Licensed User
Longtime User
this looks really good Erel. Just 2 questions.

1, will it fill now the suggestions dialog when start typing? What will happen if there would be 200 snippets? will it slow down the typing in the IDE because of the huge suggestion dialog?

2, you wrote in post #1 that it is in a b4xlib, how do you update it? you need always to open the lib get the class update and compile again to b4xlib? wouldn't be a code module more efficient in this case?
 

LucaMs

Expert
Licensed User
Longtime User
2, you wrote in post #1 that it is in a b4xlib, how do you update it? you need always to open the lib get the class update and compile again to b4xlib? wouldn't be a code module more efficient in this case?
It will be a good opportunity, for me or others, to create a tool that allows you to update the b4xlib ?

1, will it fill now the suggestions dialog when start typing? What will happen if there would be 200 snippets? will it slow down the typing in the IDE because of the huge suggestion dialog?
Good doubt. I don't think there will be speed problems and typing even just 3 characters will be enough to greatly limit what will be displayed. My opinion, obviously.
 

ilan

Expert
Licensed User
Longtime User
B4xlib doesn't need to be compiled.
Just use WinRAR and drop inside the Snippets folder.
but adding removing code needs extra work. i would like to open it like a code module, update and save again in additional library folder to use it in other apps.
dont want to unzip, zip,...
 

PaulMeuris

Well-Known Member
Licensed User
You can open the b4xlib archive with the 7-zip application.
Select the text file you want to edit and press Enter or F4.
Save the changes when asked.
1695717799341.png
1695717873269.png

Of course you have made a backup copy of the b4xlib file first...
And if you are using the b4xlib in a project you should refresh the list of the Library Manager.
 

Attachments

  • 1695717363348.png
    1695717363348.png
    36.4 KB · Views: 120
  • 1695717643414.png
    1695717643414.png
    6.7 KB · Views: 122

aeric

Expert
Licensed User
Longtime User
but adding removing code needs extra work. i would like to open it like a code module, update and save again in additional library folder to use it in other apps.
dont want to unzip, zip,...
You can put a Snippets folder inside additional library folder.
 
Top