New feature - code snippets

LucaMs

Expert
Licensed User
Longtime User
So, my snippet looks like this:
B4X:
Private Sub Delete$Controller$ (id As Long)
    ' #Version = v2
    ' #Desc = Delete Item in MinimaList
    ' #Elements = [":id"]
    If id < 1 Then
        HRM.ResponseError = "Invalid id value"
        HRM.ResponseCode = 404
    Else
        Dim Index As Int = Main.$MinimaList$.IndexFromId(id)
        If Index < 0 Then
            HRM.ResponseError = "Item not found"
            HRM.ResponseCode = 404
        Else
            Main.$MinimaList$.Remove(Index)
            HRM.ResponseCode = 200
        End If
    End If
  
    If Main.SimpleResponse Then
        WebApiUtils.ReturnSimpleHttpResponse(HRM, "List", Response)
    Else
        WebApiUtils.ReturnHttpResponse(HRM, Response)
    End If
    If Main.KVS_ENABLED Then Main.WriteKVS("$MinimaList$", Main.$MinimaList$)
End Sub

The IDE will highlight the variables. Once I replace the first variable, all other variables (with the same placeholder) will be replaced.

before:
View attachment 146183

after:
View attachment 146184

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

1695373148238.png
 

aeric

Expert
Licensed User
Longtime User
[OK]...Now the bad news :rolleyes:... 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
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>"
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:
1695383236882.png

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
    1695383187954.png
    5.1 KB · Views: 119

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
@Erel
There are two ways to add new snippets:
  1. Inside a b4xlib library. Simply add a folder named Snippets and put the snippets files in that folder. Each snippet in its own text file.
  2. Under the Snippets folder in the additional libraries folder. Each snippet in a separate txt file.

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