· Sep 2, 2020

Is it possible to export User-Defined Code Fragments from Studio?

It's well-known among Studio users that besides few predefined code fragments (for ObjectScript, Basic, MV Basic) it's possible to add user-defined code fragments. I found it rather convenient to use them as patterns that help to follow some conventions (internal standards) of writing, say, methods descriptions.

But I didn't find a way how to share these patterns, except dumb copy-pasting. Did somebody succeed with this task? Any help would be appreciated.

Discussion (10)1
Log in or sign up to continue

As with everything in VS code you can link the code snippets at a USER level or at a WORKSPACE level

Another trick is to write snippets of code as #defines in an INCLUDE file and then reference the snippet using the syntax $$${snippet_nanme}

Then Include the include file in the classes you write.

If you work your way through the InterSystems include files you will find many examples where the ISC developers have done exactly this.

Here is an example of a #define for a block of code

#define JSONError400(%ErrorResponseMessage) ##Continue
   Set %response.Status ..#HTTP400BADREQUEST  ##Continue
   Set:'$D(tErrorResp) tErrorResp=##class(PJH.HST.JSON.Proxy.ErrorStatus).%New() ##Continue
   Set tErrorResp.error %ErrorResponseMessage_" Contact system administrator" ##Continue
   ;Do ##class(%ZEN.Auxiliary.jsonProvider).%ObjectToJSON(tErrorResp,.tVisit,,"ltw")

The #continue at the end of each line tells the .inc processor to proceed to the next line

then in your code you just write your line of code as

     do $$$JSONError400(%ErrorResponseMessage)



Thank you, Nigel.

Multi-line macros don't meet my needs. What I really need are fillable patterns (templates), to prompt developers on writing methods (functions) descriptions in a standardized manner, something like this: 

/// --------------------------------------------------------------------------------------------
/// Method purpose
///  **Arguments**
/// #. *pArg1*:
/// #. *pArg2*:
///  **Returns**
///  **Notes**
///  **Call sample**
/// ::
///   ; code line 1
///   ; code line 2

Ah, ok

I'm afraid I don't have an easy answer for this one. I just tried creating an abstract class with all of the comments indicating the contents and structure and then inherited it into another new persistent class but all of the /// comments did not load into the new class. I tried compiling the class and then used the View-Class Documentation in the hope that the comments would appear in the documentation but they did not :-( 

Back to the drawing board