Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ It can currently be found at:
- [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=bard83.csharpextension)
- Open VSX (not yet published)

## Table of Contents

- [Features](#features)
- [Default Templates](#default-templates)
- [Custom Templates](#custom-templates)
- [Code Actions](#code-actions)
- [Configuration](#configuration)
- [License](#license)
- [Legacy Repositories](#legacy-repositories)

## Features

C# Extensions provides a set of templates to create C# components like classes, interfaces, enums and so on. It also provides some code actions to generate constructors from properties and body expression constructors from properties.

CSharp items can be created from the VSCode command palette (i.e. Class, Interface, Struct and so on.). The extension determinates the destination path based on the current opened file in the editor.

In case no files are currently opened in the editor, it will be shown an input box where the destination path must be typed. The destination path must be valid and within the workspace folder. In case the input path is left empty the final destination path will be the current workspace folder.

This extension traverses up the folder tree to find the project.json or *.csproj and uses that as the parent folder to determine namespaces.
This extension traverses up the folder tree to find the `project.json` or `*.csproj` and uses that as the parent folder to determine namespaces.

![List of features](./featureimages/extension_list.gif)

### Default Templates

C# Extensions provides the following templates:
The C# Extensions tool provides multiple default templates for creating C# components, such as classes, interfaces, and enumerations. Some examples of these templates are:

- **Add C# Class**: Creates a new C# class file with the specified name and the current namespace based on the folder structure. The class will be created with a default using section.

Expand All @@ -31,21 +42,11 @@ C# Extensions provides the following templates:

![Add C# Interface](./featureimages/create_interface_template.gif)

- **Add C# Struct**: Creates a new C# struct file with the specified name and the current namespace based on the folder structure.

![Add C# Struct](./featureimages/create_struct_template.gif)

- **Add C# Record**: Creates a new C# record file with the specified name and the current namespace based on the folder structure. This template is available **only for Frameworks that support C# 9.0 or higher**.

![Add C# Record](./featureimages/create_record_template.gif)

- **Add C# Enum**: Creates a new C# enum file with the specified name and the current namespace based on the folder structure.

![Add C# Enum](./featureimages/create_enum_template.gif)
For more details about the default templates, please refer to the [Templates documentation](./TEMPLATES.md).

### Custom Templates

The custom template must be defined in the vscode `settings.json` file. Access to File->Preference->Settings, Explode the Extensions section and select C# Extension then click on `edit in settings.json` .In the new section `csharpextensions.templates` must define the list of `items` which contain the custom templates. An item template is defined like below:
C# extensions allows users to define custom templates to suit their specific needs. The custom template must be defined in the vscode `settings.json` file. Access to File->Preference->Settings, Explore the Extensions section and select C# Extension, then click on `edit in settings.json`. In the new section `csharpextensions.templates` must define the list of `items` which contain the custom templates. An item template is defined like below:

```json
{
Expand All @@ -69,27 +70,21 @@ The custom template must be defined in the vscode `settings.json` file. Access t
}
```

`visibility` C# component visibility (public, private and etc...);
Please note that the code defined inside **the custom template should be valid C# code**. This extension does not perform any validation on it.

`construct` actually supported `class`, `interface` and `struct`;
For more details about the custom templates, please refer to the [Templates documentation](./TEMPLATES.md).

`header` is used to group all the necessary usings module. Each using must be separated by a `;`. The keyword `using` or the new line `\n` can be omitted. "using System;\nusing System.Runtime.Serialization;\nusing System.Text.Json;" and "System;System.Runtime.Serialization;System.Text.Json" produce the same output. Implicit usings rules will be applied.
- **Modify settings.json to add new custom template**

`genericsDefinition` used to specify the generics for the construct automatically enclosed between `<>`;
![Modify settings.json to add new custom template](./featureimages/search_custom_template.gif)

`declaration` used to append all the necessary extended or implemented class or interface. The colon before the declaration will be automatically added. It could be used to add also generic clauses.

`attributes` used to specify the attributes for the construct. The attributes must be specified in a list of string. Using the placeholder `${classname}` the construct name will be replaced instead.

`genericsWhereClauses` used to define the generics where clauses inside the custom template.
- **Add new custom template**

`body` body of template. It might be whatever C# code. The placeholder `${classname}` gets replaced with the file name if it's defined.
![Add new custom template](./featureimages/create_custom_template.gif)

Please note that the code defined inside the custom template should be valid C# code. This extension does not perform any validation on it.
- **Add a file using custom template**

- **Add new custom template**

![Add new custom template](./featureimages/customTemplate.gif)
![Add sample custom template](./featureimages/create_sample_custom_template.gif)

### Code Actions

Expand All @@ -103,9 +98,13 @@ To activate the code actions, place the cursor on a class declaration and open t

![Add body expression constructor from properties](./featureimages/code_action_body_expression_ctor_from_properties.gif)

-----------------------------------------------------------------------------------------------------------
## Configuration

C# Extensions can be configured to customize its behavior and features. The configuration options allow you to enable or disable specific features, set default values, and tailor the extension to your preferences.

To see all the available configuration options, please refer to the [Configuration documentation](./CONFIG.md).

## Licence
## License

MIT

Expand Down
81 changes: 81 additions & 0 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# CsharpExtensions Configuration

C# Extensions can be configured to customize its behavior and features. The configuration options allow you to enable or disable specific features, set default values, and tailor the extension to your preferences.

## Private Members Prefix

Prefix for generated private member declarations.

- **Name**: csharpextensions.privateMemberPrefix
- **Default**: ""
- **Example**: "_" would generate private members like "_myField" instead of "myField".

## Reformat After Change

The document will be reformatted after code actions are used.

- **Name**: csharpextensions.reFormatAfterChange
- **Default**: true

## Templates

Defines custom templates, based on the custom sections `header`,`visibility`,`attributes`, `construct`, `declaration` and `body`.

More details about the custom templates can be found in the [Templates documentation](./TEMPLATES.md).

- **Name**: csharpextensions.templates
- **Default**: {}

## Use File Scoped Namespaces

Implements file scoped namespaces for generated code. This setting is only available for C# 10 and later.

**Note**: This setting will not available if your framework target is lower than .NET 6, as file scoped namespaces are not supported in earlier versions.

- **Name**: csharpextensions.useFileScopedNamespace
- **Default**: false
- **Example**:

```csharp
//Earlier versions of C#
namespace MyNamespace
{
public class MyClass
{
// Class members
}
}
```

```csharp
//C# 10 and later with file scoped namespaces
namespace MyNamespace;

public class MyClass
{
// Class members
}
```

## Use This For Constructor Assignments

Whether or not a constructor assignment of a property or variable should be prefixed with `this`.

- **Name**: csharpextensions.useThisForCtorAssignments
- **Default**: true

## Usings Implicit

If created classes should exclude implicit usings.

**Note**: Only applies when a project's `ImplicitUsings` has been set to enabled, and when `csharpextensions.usings.include` is set to true.

- **Name**: csharpextensions.usings.implicit
- **Default**: false

## Usings Include

When a class is created, include the default usings.

- **Name**: csharpextensions.usings.include
- **Default**: true
83 changes: 83 additions & 0 deletions docs/TEMPLATES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# CsharpExtensions Templates

C# Extensions provides a set of default templates for creating common C# constructs such as classes, interfaces, structs, records, and enums. Additionally, it allows users to define custom templates to suit their specific needs.

## Default Templates

This extension includes the following default templates:

- **Add C# Class**: Creates a new C# class file with the specified name and the current namespace based on the folder structure. The class will be created with a default using section.

![Add C# Class](../featureimages/create_class_template.gif)

- **Add C# Interface**: Creates a new C# interface file with the specified name and the current namespace based on the folder structure.

![Add C# Interface](../featureimages/create_interface_template.gif)

- **Add C# Struct**: Creates a new C# struct file with the specified name and the current namespace based on the folder structure.

![Add C# Struct](../featureimages/create_struct_template.gif)

- **Add C# Record**: Creates a new C# record file with the specified name and the current namespace based on the folder structure. This template is available **only for Frameworks that support C# 9.0 or higher**.

![Add C# Record](../featureimages/create_record_template.gif)

- **Add C# Enum**: Creates a new C# enum file with the specified name and the current namespace based on the folder structure.

![Add C# Enum](../featureimages/create_enum_template.gif)

## Custom Templates

The C# extension allows users to define custom templates that suit their specific needs. These templates must be defined in the VSCode `settings.json` file. To access this file, go to `File > Preferences > Settings`. Then, explore the Extensions section and select C# Extension. Finally, click on Edit in `settings.json`. In the new section, `csharpextensions.templates` define a list of `items` containing the custom templates. An item template is defined as follows:

```json
{
"name": "MyCustomTemplate",
"visibility": "public",
"construct": "class",
"description": "My awesome c# template",
"header": "using System;\nusing System.Runtime.Serialization;\nusing System.Text.Json;",
"attributes": [
"DbContext(typeof(AppDbContext))",
"Migration(\"${classname}\")"
],
"genericsDefinition": "I,J,K",
"declaration": "ISerializable, IEquatable",
"genericsWhereClauses": [
"where I : class",
"where J : struct",
"where K : IMyInterface",
],
"body": "public void MyFancyMethod(string variable)\n{\n System.Console.WriteLine(\"Hello World\");\n}"
}
```

- `visibility` C# component visibility (public, private and etc...);

- `construct` actually supported `class`, `interface` and `struct`;

- `header` is used to group all the necessary usings module. Each using must be separated by a `;`. The keyword `using` or the new line `\n` can be omitted. "using System;\nusing System.Runtime.Serialization;\nusing System.Text.Json;" and "System;System.Runtime.Serialization;System.Text.Json" produce the same output. Implicit usings rules will be applied.

- `genericsDefinition` used to specify the generics for the construct automatically enclosed between `<>`;

- `declaration` used to append all the necessary extended or implemented class or interface. The colon before the declaration will be automatically added. It could be used to add also generic clauses.

- `attributes` used to specify the attributes for the construct. The attributes must be specified in a list of string. Using the placeholder `${classname}` the construct name will be replaced instead.

- `genericsWhereClauses` used to define the generics where clauses inside the custom template.

- `body` body of template. It might be whatever C# code. The placeholder `${classname}` gets replaced with the file name if it's defined.

**IMPORTANT** Please note that the code defined inside **the custom template should be valid C# code**. This extension does not perform any validation on it.

- **Modify settings.json to add new custom template**

![Modify settings.json to add new custom template](../featureimages/search_custom_template.gif)

- **Add new custom template**

![Add new custom template](../featureimages/create_custom_template.gif)

- **Add a file using custom template**

![Add sample custom template](../featureimages/create_sample_custom_template.gif)
Binary file added featureimages/create_custom_template.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added featureimages/create_sample_custom_template.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added featureimages/search_custom_template.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.