New feature - code snippets

LucaMs

Expert
Licensed User
Longtime User

Don't forget the useful...
Note that the special $end$ marking sets the cursor position after the developer set the placeholders.
:)
 

aeric

Expert
Licensed User
Longtime User
I know currently the Snippets feature only available for b4xlib or putting the text file inside the Snippets folder under additional folder,
but a quick wish is: support for jar library

 

aeric

Expert
Licensed User
Longtime User
[OK]...Now the bad news ... we must re-write all the threads with the snippets and change the "variables" with $variable$ !!!, and preferable at the first post attach a txt file snippet ! ?
There is one issue with the variable placeholder.
The IDE will confuse with the snippet contains Smart String Literals.
 

aeric

Expert
Licensed User
Longtime User
why ?... will not be a problem... because smart string literal... got following quote or "{"
B4X:
$"
${

I am sure that Erel... count them all..
For eg.
I have this code:
B4X:
' Return Web Page
Private Sub ShowPage
    Dim strMain As String = WebApiUtils.ReadTextFile("main.html")
    Dim strView As String = WebApiUtils.ReadTextFile("category.html")
    strMain = WebApiUtils.BuildDocView(strMain, strView)
    strMain = WebApiUtils.BuildHtml(strMain, Main.config)
    Dim strScripts As String = $"<script src="${Main.ROOT_URL}/assets/js/webapicategory.js"></script>"$
    strMain = WebApiUtils.BuildScript(strMain, strScripts)
    WebApiUtils.ReturnHTML(strMain, Response)
End Sub

The snippet highlighted: "<script src="

to solve it, I need to use the old way of concantenation.
B4X:
Dim strScripts As String = "<script src=""" & Main.ROOT_URL & "/assets/js/controller.js""></script>"
 

Magma

Expert
Licensed User
Longtime User
You mean... if you have into a snippet-text file the whole sub ?

I see... well maybe... Erel must have a check after $ for quote or { to be sure what will replace...
 

PaulMeuris

Well-Known Member
Licensed User
In code snippet:
dim $var$ as string
dim str as string = $$"this is a string litaral with variable $$${$$var$$}$ in it"$$
For the string literal you can use extra $ signs.
In the B4J IDE:

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...
 

Attachments

  • 1695383187954.png
    5.1 KB · Views: 146

Sandman

Expert
Licensed User
Longtime User
It does seem somewhat painful when you consider smart string literals. Considering this is a beta, perhaps Erel will change the format from $this$ to @this@ or #this#, or something else that makes more sense.
 

Daestrum

Expert
Licensed User
Longtime User
I'm really curious, what do you do instead?

A simple Log(..) and the stacktrace is all I need to correct errors. (Old school mainframe programmer - it's just how I learnt to debug)
 

Sandman

Expert
Licensed User
Longtime User
A simple Log(..) and the stacktrace is all I need to correct errors. (Old school mainframe programmer - it's just how I learnt to debug)
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.)
 

William Lancee

Well-Known Member
Licensed User
Longtime User

I like 2. since I don't have to go through the zip step. Easy to use Notepad++ to create, modify and test snippets.
But, the folder named 'Snippets' should be in the B4X subfolder of the additional libraries folder.

P.S. @Daestrum and @Sandman, I'm so old school that I don't have bugs anymore -> no need to debug, but I understand if some people do
 

William Lancee

Well-Known Member
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?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
@Erel

To help organize and maintain my snippet collection, is it possible to have sub folders in AdditionalLibs\B4X\Snippets ?
The names of these folders could be ignored by the IDE, but would be useful to group hundreds of snippets.