VSCODE snippet for creating new C# class with namespace declaration

This extension provides a context menu button to add a new class, which will auto populate the namespace.

Visual Studio Code has changed a bit since the last answer. It now provides the variable TM_DIRECTORY in snippets, but this is an absolute path. I've submitted an enhancement request to provide a relative path that could be transformed to a namespace. But honestly, I think the above extension satisfies my needs (and the context menu is a plus)


That's currently not possible. You have no possibility to retrieve the current filename, directory or other information in a snippet declaration file for Visual Studio Code.

You could create a snippet that lets you enter a namespace and a class name. But I guess this wouldn't help you so much. Nevertheless it'd look like this:

 "Namespace and class": {
    "prefix": "namespaceAndClass",
    "body": [
        "namespace $1",
        "{",
        "   class $2",
        "   {",
        "",
        "   }",
        "}"
    ],
    "description": "Create a namespace block with a class"
 }

In case you really want a snippet that fills in the correct namespace and class name based on the file path you could have a look at the OmniSharp project. This can give you an idea on how to improve the csharp-o extension in order to provide the correct data as a suggestion from within the plugin. But I think this is a much bigger task then typing namespace and class on your own.


A moderately dirty solution with the current variable and regex system of vscode is the following:

Assuming that your have all your projects in /your/projects/directory/

So project #1 is in /your/projects/directory/Project1/
And project #2 is in /your/projects/directory/Project2/
etc.

The following snippet will create a namespace implementation for all sub-directories:

Linux/ MacOS

"Namespace declaration":
{
    "prefix": "name",
    "description": "Creates a new namespace declaration.",
    "body":
    [
        "namespace ${TM_DIRECTORY/^\\/your\\/projects\\/directory(\\/([^\\/]+))(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?/$2${3:+.}$4${5:+.}$6${7:+.}$8${9:+.}$10${11:+.}$12${13:+.}$14${15:+.}$16${17:+.}$18${19:+.}$20/gi}",
        "{",
        "}"
    ]
}

Windows

"Namespace declaration":
{
    "prefix": "name",
    "description": "Creates a new namespace declaration.",
    "body":
    [
        "namespace ${TM_DIRECTORY/^c:\\\\your\\\\projects\\\\directory(\\\\([^\\\\]+))(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?/$2${3:+.}$4${5:+.}$6${7:+.}$8${9:+.}$10${11:+.}$12${13:+.}$14${15:+.}$16${17:+.}$18${19:+.}$20/gi}",
        "{",
        "}"
    ]
}

Explanation

  1. The snippet matches your base directory and up to ten sub-directories (the first directory is mandatory (\\/([^\\/]+)), while all additional nine ones are optional (\\/([^\\/]+))?)
  2. The namespace directive is then created with the first matched directory
  3. For every successful additional sub-directory match, a dot . is inserted (${3:+.}) with the sub-match of that group ($4); for unsuccessful groups, no dot inserted and the sub-match is empty

Enjoy :)