Auto-indenting generated scripts [SOLVED]

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Auto-indenting generated scripts [SOLVED]

Post by stam » Mon Jul 11, 2022 1:36 pm

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
Last edited by stam on Tue Jul 12, 2022 10:23 am, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Auto-indenting generated scripts

Post by dunbarx » Mon Jul 11, 2022 4:51 pm

Stam.

If they are already "generated from a script", they already have the spaces built into the text.

What am I missing?

Craig

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Auto-indenting generated scripts

Post by stam » Mon Jul 11, 2022 5:43 pm

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…

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Auto-indenting generated scripts

Post by LCMark » Mon Jul 11, 2022 6:02 pm

@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!
Last edited by LCMark on Tue Jul 12, 2022 6:43 am, edited 1 time in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Auto-indenting generated scripts

Post by stam » Tue Jul 12, 2022 1:29 am

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Auto-indenting generated scripts

Post by LCMark » 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.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Auto-indenting generated scripts

Post by stam » Tue Jul 12, 2022 10:00 am

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!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”