How vscode creates custom code snippets

Here is an example of creating a php class code snippet:

  1. command+shift+pinput snippet;
  2. choose Preferences: Configure User Snippets;
  3. Then type phpEnter .

Note: Step 3 Select according to the file type of the code snippet you want to create. For example, if you want to create a java code snippet, you need to enter java. If you want to create a global code snippet that does not distinguish between file types, you need to type gloselect New Globle Snippets fileand then press Enter.

At this point, the editor opens a php.json file and modifies it to the following:

{
	// Place your snippets for php here. Each snippet is defined under a snippet name and has a prefix, body and
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
	// same ids are connected.
	// Example:
	"Class File Header": {
		"prefix": "myclass",
		"body": [
			"/**",
			" * $1",
			" * ",
			" * @author whoru.S.Q <[email protected]>",
			" * @created $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
			" */",
			"class ${2:ClassName} ${3:extends ${4:AnotherClass}} ${5:implements ${6:Interface}}",
			"{",
			"\n\t${7:public} function ${8:methodName}($9)",
			"\t{",
			"\t\t$0",
			"\t}\n",
			"}"
		],
		"description": "class with header"
	}
}

After saving, open a php file, enter the prefix, and press Enter after the shortcut prompt window myclassappears . At this time, our custom code fragment structure has been generated. Press the tabkey in turn to switch to each preset position to change, and you can quickly create our code needed.

vscode snippets

Basic syntax description

  • prefixString prefix that triggers the quick hint
  • bodycode snippet body
    • $numEach time you press the tabkey , the cursor moves to the right position, which $0means the last position of the cursor. If it is not set $0, the final position of the cursor is at the end of the file;
    • ${2:默认文本}Jump to the specified position and select the default text at the same time, which is convenient for modification;
    • $CURRENT_YEARis the quoted snippets built-in variable, others are:
      • TM_FILENAME current file name
      • TM_FILENAME_BASE current filename without extension
      • TM_DIRECTORY The absolute path of the directory to which the current file belongs
      • TM_FILEPATH absolute path of current file
      • CURRENT_YEAR Current year
      • CURRENT_YEAR_SHORT Current year, last two digits
      • CURRENT_MONTH Numerical form of the current month, two digits
      • CURRENT_MONTH_NAME The current month in English, such as July
      • CURRENT_MONTH_NAME_SHORT English abbreviation of the current month, such as Jul
      • CURRENT_DATE current day
      • CURRENT_DAY_NAME the current week, such as Monday
      • CURRENT_DAY_NAME_SHORT Abbreviated form of the current week, such as Mon
      • CURRENT_HOUR The current hour, in 24-hour format, with two digits
      • CURRENT_MINUTE The current minute, two digits
      • CURRENT_SECOND The current second, two digits
    • \nnewline
    • \tTabs
  • descriptionA description of the code snippet pair in the shortcut window

For more functions, please refer to

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256519&siteId=291194637