Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Command DIYNew to Visual Studio Code? Get it now.

Command DIY

talltotal

|
4 installs
| (0) | Free
custom command through configuration; suport js script, js module.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Command DIY

配置自定义指令,支持js脚本、js模块。

Custom command through configuration; suport js script, js module.

使用:

  1. 增加配置
  2. 执行 command-diy 或 快捷键 ctrl+6(max·cmd+6)(默认)

use:

  1. Add configuration
  2. Execute command-diy or shortcut key ctrl+6 (max·cmd+6) (default)

配置·Configuration

Extension Settings

插件全局可配项:

  • CommandDIY.commands: 对象数组,对象属性有:
    • label: string: 字符串,指令名
    • jsScript: string | string[]: 字符串数组,指令执行的JS脚本
      • 上下文中的常量
        • $pasteText?: string 剪贴板文本
        • $pasteHtml?: string 剪贴板富文本(暂只支持macos)
        • $selections: string[] 选中的字符串数组
        • $filename: false | string 当前文件
      • 支持返回(ReturnData)
    • filename?: string: 字符串,用于确认是否执行指令的文件名正则;未定义即所有文件或无active文件

This extension contributes the following settings:

  • CommandDIY.commands: Object Array, with properties:
    • label: string: command name.
    • jsScript: string | string[]: js script executed by command.
      • Constant in context
        • $pasteText?: string
        • $pasteHtml?: string (Only macos is supported temporarily)
        • $selections: string[]
        • $filename: false | string
      • Support return(ReturnData).
    • filename?: string: regexp file name used to confirm whether to execute command; undefined means all files or no active files.
interface ReturnDataItem {
  // message
  msg?: string | {
    type?: 'info' | 'error' | 'warning', // default 'info'
    content: string
  },
  // replace selection or insert beginning
  replace?: string | string[]
  copy?: string
}
interface ReturnDataListItem {
  msg?: string | {
    type?: 'info' | 'error' | 'warning', // default 'info'
    content: string
  },
  replace?: string
}

// 数组时,和 `selections` 一一对应
// Arrays, one-to-one correspondence with `selections`
type ReturnData = ReturnDataItem | ReturnDataListItem[]

如: For example:

{
  "CommandDIY.commands": [
    {
      "label": "插入时间.Insert time",
      "jsScript": [
        "var time = (new Date()).toLocaleString()",
        "return ($selections||[]).map(() => ({ replace: time }))"
      ],
      "filename": ".*"
    }
  ]
}

package.json

在工作区文件夹下package.json文件中配置command-diy字段。

Configure command-diy in the package.json file under the workspace folder.

如: For example:

{
  "command-diy": [
    {
      "label": "D",
      "jsScript": [
        "return { msg: 'My command D!' }"
      ]
    }
  ]
}

.command-diy.json

在工作区文件夹下.command-diy.json文件中配置,支持JSONC。

Configure in the .command-diy.json file under the workspace folder, and support JSONC.

如: For example:

// json with comments
[
  {
    // show info msg
    "label": "D",
    "jsScript": [
      "return { msg: 'My command D!' }"
    ]
  },
  {
    // show info msg only in `src/services` folder and end with `.ts`
    "label": "I",
    "jsScript": [
      "return { msg: 'My command I!' }"
    ],
    "filename": "^src/services/.+?\\.ts$"
  },
  {
    // run module
    // `test.js`: 
    // module.exports = ({$selections, $filename}) => { 
    //   return $selections.map(() => ({ replace: Date.now() })) 
    // }
    "label": "Y",
    "jsModule": "./test.js",
  }
]

Enjoy!

  • Contact us
  • Jobs
  • Privacy
  • Terms of use
  • Trademarks
© 2023 Microsoft