Article
Eduard Lebedyuk · Dec 3, 2019 1m read

Adding your own snippets to VS Code

One of the most useful features in Studio is code snippets.

Here's how to add snippets to VSCode.

Here's a generalized instructions.

1. Go to File - Preferences - User Snippets and choose objectscript.

2. Add your snippet, here's an example:

"SQL Statement": {
    "prefix": ["sql"],
    "body": ["#dim rs As %SQL.ISelectResult",
            "set rs = ##class(%SQL.Statement).%ExecDirect(,\"SELECT * FROM\")",
            "while rs.%Next() {",
            "\twrite rs.ID, !",
            "}"]
}

In here:

  • prefix - what you need to type for the snippet to appear
  • body - snippet body

Even better, snippets can include placeholders, for example:

"Method": {
    "prefix": ["method"],
    "body": ["set sc = ##class(${1:class}).${2:method}()"]
}

After you insert this snippet you're automatically moved to the start of the first placeholder, use <TAB> no iterate through the placeholders.

Happy coding!

61
0 3 1 297

Replies

What a great idea :)

Here is my contribution :

{
    "Request Class": {
        "prefix": "ClassRequest",
        "body": [
            "Class ${0:ClassName} Extends Ens.Request",
            "{",
            "\t$1",
            "}"
        ],
        "description": "Message helper"
    },

    "Response Class": {
        "prefix": "ClassResponse",
        "body": [
            "Class ${0:ClassName} Extends Ens.Response",
            "{",
            "\t$1",
            "}"
        ],
        "description": "Message helper"
    },

    "Operation Class": {
        "prefix": "ClassOperation",
        "body": [
            "Class ${0:ClassName}  Extends Ens.BusinessOperation",
            "{",
            "\tParameter ADAPTER = \"${1:Adapter}\";",
            "\tProperty Adapter As ${1:Adapter};",
            "\tParameter INVOCATION = \"Queue\";",
            "\n",
            "Method ${2:Methode}(pRequest As ${3:Request}, Output pResponse As ${4:Response}) As %Status",
            "{",
            "\tset tStatus = $$$$OK",
            "\tset pResponse = ##class(${4:Response}).%New()",
            "",
            "\ttry{",
            "\t\t\n",
            "\t}",
            "\tcatch exp",
            "\t{",
            "\t\tset tStatus = exp.AsStatus()",
            "\t}",
            "\tQuit tStatus",
            "}",
            "XData MessageMap",
            "{",
            "<MapItems>",
            "\t<MapItem MessageType=\"${3:Request}\">",
            "\t\t<Method>${2:Methode}</Method>",
            "\t</MapItem>",
            "</MapItems>",
            "}",
            "}",
        ],
        "description": "Operation helper"
    },

    "Service Class": {
        "prefix": "ClassService",
        "body": [
            "Class ${0:ClassName} Extends Ens.BusinessService",
            "{",
            "Parameter ADAPTER = \"${1:Adapter}\";",
            "Property TargetConfigNames As %String(MAXLEN = 1000) [ InitialExpression = \"${2:BusinessProcess}\" ];",
            "Parameter SETTINGS = \"TargetConfigNames:Basic:selector?multiSelect=1&context={Ens.ContextSearch/ProductionItems?targets=1&productionName=@productionId}\";",
            "Method OnProcessInput(pDocIn As %RegisteredObject, Output pDocOut As %RegisteredObject) As %Status",
            "{",
            "\tset status = $$$$OK",
            "",
            "\ttry {",
            "",
            "\t\tfor iTarget=1:1:$L(..TargetConfigNames, \",\") {",
            "\t\t\tset tOneTarget=$ZStrip($P(..TargetConfigNames,\",\",iTarget),\"<>W\")  Continue:\"\"=tOneTarget",
            "\t\t\t$$$$ThrowOnError(..SendRequestSync(tOneTarget,pDocIn,.pDocOut))",
            "\t\t}",
            "\t} catch ex {",
            "\t\tset status = ex.AsStatus()",
            "\t}",
            "",
            "\tQuit status",
            "}",
            "",
            "}",
        ],
        "description": "Operation helper"
    },
}