Use the Macros plug-in in Visual Studio to add comments, time, and automatic scripts to your code

Title: use the Macros plug-in in visual studio to add comments, time, and automatic scripts to the code
date: 2020-09-11
sidebarDepth: 2
tags:

  • code
  • Visual studio
  • time
  • notes
  • Macros
    categories:
  • development
  • Microsoft Technology
  • windows

Macros for Visual Studio plug-in is a very easy-to-use automatic script plug-in. It supports various versions of visual studio and is used to create todo, time notes and comments at the beginning and end of files.

Macros for Visual Studio plug-in is a very easy-to-use automatic script plug-in. It supports various versions of visual studio and is used to create todo, time notes and comments at the beginning and end of files.

brief introduction

What is creating information notes?

Creating information notes is similar to the creation information of files. Generally speaking, there must be at least the creator and creation time. If someone changes the method, you should also add an update information comment.

Why add a creation information note?

When the code is modified by many people, the problem of who should modify the code is not very important. However, when the code is modified by the same person, the problem of who should modify the code will appear directly.

Although using svn and git management tools, you can have submission logs and manage them conveniently, if you annotate them in the code, it will better improve the efficiency.

If it is in vs code, you can use plug-ins such as Insert Date String. After deployment, you can insert the current time by using ctrl+Shift+I. obviously, this is not enough for large-scale visual stuido programming.

Create information notes

  • 1. The original idea was to rewrite the built-in document comments of VS, but no such solution was found. The basic solutions found are to modify the template file when creating a new class or interface to add comments. There is no solution to add comments on methods.

After this scheme is denied, try to use custom code snippets to add creation information comments on the basis of generated comments.

  • 2. Custom code snippet

Custom snippet reference example:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>cm</Title>
            <Shortcut>cm</Shortcut>
            <Description>Code snippet for new code comment</Description>
            <Author>liweihan</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>Description</ID>
                    <ToolTip>Method description</ToolTip>
                    <Default>Method description</Default>
                </Literal>
                <Literal>
                    <ID>Author</ID>
                    <ToolTip>author</ToolTip>
                    <Default>liweihan</Default>
                </Literal>
                <Literal>
                    <ID>CreateDate</ID>
                    <ToolTip>date</ToolTip>
                    <Default>@@Today</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
            <![CDATA[/// <summary>
            /// $Description$
            /// @Author:$Author$
            /// @CreateDate:$CreateDate$$end$
            /// </summary>]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

There are still many deficiencies in this code segment. Now the author is dead, and the creation time needs to be manually modified after inserting the code segment. I hope to automatically generate the author and the current date. There is no problem for the author to be dead, because this code segment is only used by me, and it is more convenient to write it directly. Here, a variable is used for modification. The key is that the current date cannot be obtained, After consulting a lot of materials, the code segment does not support inserting the current date directly. Someone has discussed this problem on stackoverflow. The solution is to use macros to automatically generate the current date, but the solution mentioned in this article is not available, but it gives me the idea to solve the problem - using macros to insert the current date.

  • 3. Current date of macro insertion I downloaded a plug-in for managing macros - Macros for Visual studio in the extension and update of VS

You can also download it directly from the market of visual studio at:
https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.MacrosforVisualStudio

Here is an official example:

This is the deployment environment in visual studio:

After downloading and installing, you need to restart VS. after restarting, you can see the following under tools in the menu bar:

The macro manager comes with some Samples, including a macro that can insert dates

Select Assign Shortcut to set the shortcut key, which can be used in VS. the default format is "mm/dd/yyyy"

If you don't like the default date format like me, you can right-click the corresponding item and select "open" for editing. There are js codes in it. It will be more convenient to modify if there are some js bases. I modified the code and changed the date format to "yyyy MM DD" format.

  • 4. Complete operation process

First, enter "/ /" in the method body, and VS will automatically generate default document comments. Write the comments first
Copy the comments in the < summary > section, select the < summary > section, keep the parameters and return information, enter the shortcut key of the defined code segment, and double-click the Tab key to insert the code segment
Paste the copied comments to the method description and add comments
Press Tab to switch to the current date. At @ Today, press the macro shortcut key set to insert the current date, and VS will automatically insert the current date

Update information notes

Update information code snippet example:

<?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>up</Title>
                <Shortcut>up</Shortcut>
                <Description>Code snippets that update code comments</Description>
                <Author>liweihan</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>Description</ID>
                        <ToolTip>Update description</ToolTip>
                        <Default>Update description</Default>
                    </Literal>
                    <Literal>
                        <ID>UpdatedBy</ID>
                        <ToolTip>Update person</ToolTip>
                        <Default>liweihan</Default>
                    </Literal>
                    <Literal>
                        <ID>UpdatedDate</ID>
                        <ToolTip>date</ToolTip>
                        <Default>@@Today</Default>
                    </Literal>
                </Declarations>
                <Code Language="csharp">
                <![CDATA[/// $Description$ @UpdatedBy:$UpdatedBy$ @UpdatedDate:$UpdatedDate$$end$ ]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

Add custom snippets to VS

Open the Snippet Manager in VS

Select "CSharp" as the language, select "My Code Snippets", and select "Visual C#". Here, in order to distinguish my own code segment from the system code segment, I select "My Code Snippets", copy the path in the location, and open the path in Explorer

Copy the customized code segment (*. snippet) to the directory opened in the previous step, and restart VS

Posted by [UW] Jake on Tue, 17 May 2022 16:48:48 +0300