Page 1 of 1
Auto-indenting generated scripts [SOLVED]
Posted: Mon Jul 11, 2022 1:36 pm
by stam
Hi all,
is there an easy way to auto-indent scripts that are generated from a script? Or do i have to insert spaces/tabs?
I'm working on a helper app for my development that will produce moderately complex scripts i can use with my main app currently in development. The intention is that this will be a reusable tool (for me at least), so making an effort to make it more solid.
This works well so far - only the text produced is all left-aligned and doesn't look good/is less readable, unless i copy to the script editor and manually hit tab inside each generated handler.
The generated scripts are first copied to a field in the helper app for review prior to inserting into the app in development - is there a way to auto-indent the text inside a field (also not sure if colouring is easily achievable?), to simulate the view in the script editor? Just so it's easier to review...
Many thanks
Stam
Re: Auto-indenting generated scripts
Posted: Mon Jul 11, 2022 4:51 pm
by dunbarx
Stam.
If they are already "generated from a script", they already have the spaces built into the text.
What am I missing?
Craig
Re: Auto-indenting generated scripts
Posted: Mon Jul 11, 2022 5:43 pm
by stam
The script is generated on the fly based on data in fields and variables. This creates the text for the scripts in their entirety, either to be saved as script only stacks or where needed to be inserted into other scripts.
So typically something like “put xyz && field abc & return after tScript”.
So no, no “spaces” included.
I mean I could add spaces programmatically, but I was wondering if there is an IDE level handler I could hook into…
Re: Auto-indenting generated scripts
Posted: Mon Jul 11, 2022 6:02 pm
by LCMark
@stam: There isn't an 'official' IDE API for this - although if you post a request to the RQCC about it we can probably add one pretty easily as the code which does the indentation in the SE was exposed to work for the snippet manager we added in LiveCode 8 (?) [ and also so we could write unit tests for said indenter! ].
However, you could try doing this:
Code: Select all
dispatch "scriptFormatSnippet" to stack "com.livecode.scripteditor.behavior.editorcommon" with tTextToIndent
The indented text is then returned in `the result`.
Hope this helps!
Re: Auto-indenting generated scripts
Posted: Tue Jul 12, 2022 1:29 am
by stam
LCMark wrote: ↑Mon Jul 11, 2022 6:02 pm
However, you could try doing this:
Code: Select all
dispatch "scriptFormatSnippet" to "com.livecode.scripteditor.behavior.editorcommon" with tTextToIndent
The indented text is then returned in `the result`.
Dear Mark, thank you for the tip - that's very interesting but can't get it to work (i'm probably doing it wrong!).
I keep getting a chunk error (error in object expression). Perhaps i misunderstood - will this work in a normal text field?
As a test i created a button:
Code: Select all
on mouseUp
local tTextToIndent
put field "pathsScript" into tTextToIndent
dispatch "scriptFormatSnippet" to "com.livecode.scripteditor.behavior.editorcommon" with tTextToIndent
get the result -- to inspect it in 'it'
end mouseUp
The generated text in field "pathsScript" is for a scrip-only stack:
Code: Select all
script "lib.paths"
global gSettings
function projectLibPath
if the environment is "development" then
return libPath()
else
return specialFolderPath("documents") & "/MDM Assistant/" & gSettings["meetingShortName"] & "/Libraries"
end if
end projectLibPath
function canelaPath
if the environment is "development" then
return libPath() & "/canelaDB/Libraries/"
else
return specialFolderPath("documents") & "/MDM Assistant/" & gSettings["meetingShortName"] & "/canelaDB/Libraries/"
end if
end canelaPath
function libPath
if the environment is "development" then
return "/Users/stam/Documents/Developer/MDMA6/Libraries"
else
return specialFolderPath("resources") & "/Libraries"
end if
end libPath
Just a small, simple example and FWIW, if pasted into a script editor window it raises no errors and on tab all indents normally
Any tips? Do I need to assign a behaviour to the field?
It's not critical by any means (the code works!), but would be nice to produce correctly indented text...
Many thanks once again,
Stam
Re: Auto-indenting generated scripts
Posted: Tue Jul 12, 2022 6:45 am
by LCMark
@stam: Sorry - I made a mistake in my code example - it should be `stack "com.livecode....". The handler sits in one of the behavior scripts which make up the Script Editor - the actual code itself isn't specific to an SE instance or anything so it can be used by other things, its just that to call it you need to dispatch to that behavior stack, rather than call the handler directly. I've corrected my code example in my previous post.
Re: Auto-indenting generated scripts
Posted: Tue Jul 12, 2022 10:00 am
by stam
LCMark wrote: ↑Tue Jul 12, 2022 6:45 am
@stam: Sorry - I made a mistake in my code example - it should be `stack "com.livecode....". The handler sits in one of the behavior scripts which make up the Script Editor - the actual code itself isn't specific to an SE instance or anything so it can be used by other things, its just that to call it you need to dispatch to that behavior stack, rather than call the handler directly. I've corrected my code example in my previous post.
Amazing! Works perfectly

Thank you Mark!