GithubHelp home page GithubHelp logo

anthrax3 / csharpmacros Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cezarypiatek/csharpmacros

0.0 1.0 0.0 480 KB

A simple template base system of macros for C# that can be executed in design time

License: MIT License

C# 93.05% PowerShell 6.95%

csharpmacros's Introduction

CsharpMacros

A simple template base system of macros for Visual Studio that can be executed in design time

examplle usage

Releases

Support for VisualStudio.

Install as a NuGet package or VSIX. Verify your Roslyn integration option in case you are using R#.

Support JetBrains Rider

Install as a nuget package

Support for VSCode

Install as a NuGet package and check Enable support for Roslyn analyzers, code fixes and rulesets in Settings.

LiveTemplates for CsharpMacros

To facilitate usage of CsharpMacros I created a couple of Resharper LiveTemplates. You can read more about that on the snippets sub-page.

Macro anatomy

macro anatomy

Every macro consists of the following parts

  1. Macro Header in the following format macros.macro_function(macro_params).
  2. Macro Template - a template of code that will be repeated for every element returned by the macro function. The template can contain placeholders in the following format ${attribute_name} that will be replaced with a given attribute value of the element returned by the macro function.
  3. macro_function represents one of the predefined functions that generate input data for the template.
  4. macro_params is the input for macro function.
  5. The placeholder that will be replaced with the value of the given attribute. Placeholders can also contain filters that transform the attribute value. Syntax for placeholder with filter ${attribute_Name | filter_name}. The filters can be combined together by chaining them with | operator. Currently available filters: lowercase, uppercase, pascalcase, camelcase, snakecase.

Macro functions

properties

Description: Returns a list of properties of given type accepted as the parameter.

Parameters: Type name. For types from other projects/assemblies include also the namespace.

Attributes:

  • name - name of the property
  • type - type of the property
  • typeLong - type of the property. For primitives from System namespace, holds the full name (example: int -> Int32)

Examples:

// macros.properties(SampleType)
// Console.WriteLine("Property '${name}' of type '${type}'")

methods

Description: Returns a list of methods of a given type accepted as the parameter.

Parameters: Type name. For types from other projects/assemblies include also the namespace.

Attributes:

  • name - name of the method
  • returnType - name of the return type
  • returnTypeLong - name of the return type. For primitives from System namespace, holds the full name. Example: int -> Int32)
  • signature - a complete method signature. Example: int Calculate(int a, int b)
  • returnOperator - empty string for void methods, otherwise return keyword
  • parameters - comma separated list of parameters wrapped in parenthesis. Example: (a, b, c)
  • genericParameters - comma separated list of generic type parameters wrapped in angle brackets. Example: <T1, T2, T3>
  • paramNameX - name of the parameter. X marks the number of the parameter.
  • paramTypeX - name of the parameter type. X marks the number of the parameter.
  • paramTypeLongX - name of the parameter type. X marks the number of the parameter. For primitives from System namespace, holds the full name (example: int -> Int32)

Examples:

// macros.methods(SampleType)
// Console.WriteLine("Method name: ${name}")
// Console.WriteLine("\t Return type: ${returnType}")
// Console.WriteLine("\t First parameter name: ${paramName1}")
// Console.WriteLine("\t First parameter type: ${paramType1}")

implement

Description: Returns a list of types that implement a given interface accepted as the parameter. Parameters: Interface name. For interfaces from other projects/assemblies include also the namespace.

Attributes:

  • name - the name of the class that implements the given interface
  • interface - for a non-generic interface, this is the same as the parameter of the macro function. For generic interface contains the name of the interface with generic parameter values.

Examples:

// macros.implement(ISampleInterface<>)
// Console.WriteLine("Interface ${interface} is implemented by ${name}")

derived

Description: Returns a list of types that inherit from a given class accepted as the parameter.

Parameters: Type name. For types from other projects/assemblies include also the namespace.

Attributes:

  • name - the name of the type that inherits from a given class
  • based - for generic non-generic base class this is the same as the parameter of the macro function. For generic base class contains the name of the class with generic parameter values.

Examples:

// macros.derived(BaseClass<>)
// Console.WriteLine("Class ${name} inherits from ${based}")

values

Description: Returns values from the predefined list provided as a macro parameter.

Parameters: List of comma-separated values or list of comma-separated tuples

Attributes:

  • value - for simple list
  • valueX - for list of tuples. X marks the number of the value inside the tuple

Examples:

Simple list:

// macros.values(banana, strawberry, apple)
// Console.WriteLine("Fruit ${value}")

List of tuples:

// macros.values((banana, yellow), (strawberry, red), (apple, green))
// Console.WriteLine("${value1} fruit has ${value2} color.")

Range

Description: Returns values from the predefined range.

Parameters: The begin and end of the range

Attributes:

  • index - current counter value
  • from - The begin of the range
  • to - The end of the range

Examples:

//[|macros.range(10, 15)|]
//Console.WriteLine("Value ${index} from range [${from} - ${to}");

Enum

Description: Returns a list of enum values

Parameters: Enum type. For types from other projects/assemblies include also the namespace.

Attributes:

  • name - the name of enum option

Examples:

//[|macros.enum(SampleEnumType)|]
//Console.WriteLine("Option: ${name}");

csharpmacros's People

Contributors

cezarypiatek avatar emostafaali avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.