VSCODE snippet to create a new C # class with namespace declaration

Now we can only create new file or folder . And it’s really annoying to write namespaces every time you create a class declaration.

But is it possible to create a new C # class file with automatic creation of corresponding namespaces inside? Or maybe some fragment?

+10
c # visual-studio-code
source share
3 answers

This is currently not possible. You cannot retrieve the current file name, directory, or other information in a Visual Studio code snippet declaration file.

You can create a snippet that allows you to enter a namespace and class name. But I think this will not help you much. However, it will look like this:

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

If you really need a fragment that fills in the correct namespace and class name based on the file path, you can take a look at the OmniSharp project . This may give you an idea of ​​how to improve the csharp-o extension to provide the correct data as a hint from the plugin. But I think this is a much more difficult task than entering the namespace and classes yourself.

+6
source share

This extension provides a context menu button for adding a new class that will automatically fill the namespace.

Visual Studio code has changed a bit since the last answer. Now it provides the TM_DIRECTORY variable in fragments, but this is the absolute path. I sent an improvement request to indicate a relative path that can be converted to a namespace. But honestly, I think the above extension satisfies my needs (and the context menu is a plus)

+5
source share

A moderately dirty solution with the current variable and the vscode regex system looks like this:

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

So, project number 1 is located in / your / projects / directory / Project1 /
And project number 2 is in / your / projects / directory / Project2 /
etc.

The following snippet will create a namespace implementation for all subdirectories:

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 subdirectories (the first directory is required (\\/([^\\/]+)) , while all the other nine are optional (\\/([^\\/]+))? )
  2. Then a namespace directive is created with the first matching directory
  3. For each successful match of the additional subdirectory, a period is set . inserted ( ${3:+.} ) with a sub-match of this group ( $4 ); for unsuccessful groups, a dot is not inserted, and the sub-item is empty

Enjoy :)

0
source share

All Articles