如何创建一个简单的Visual Studio Code扩展
注:本文提到的代码示例下载地址>How to create a simple extension for VS Code
VS Code 是微软推出的一款轻量级的代码编辑器,免费,开源,支持多种语言,还能安装各种扩展。没有用过的同学可以下载下来感受一下,具体参见官方文档。
假设VS Code你已经安装好了,也已经大概玩过一遍了。接下来我们就开始讲讲怎么创建一个简单的VS Code扩展。
首先要装下node.js,然后通过命令行安装Yeoman,我们要通过这个工具来自动生成扩展代码:
>npm install -g yo generator-code
安装完了之后,再在命令行中敲>yo code

扩展支持JavaScript和TypeScript两种语言编写,我们这次选择JavaScript,使用上下键来选择,然后回车,下面输入你的扩展的各种信息

确认回车后,工具会帮我们生成一个Hello World扩展,我们这边取的名字是hellocode, 所以扩展所在的文件夹名就是hellocode,打开hellocode

运行>code .
hellocode文件夹将会通过VS Code打开,我们在这个VS Code窗口中按下F5来运行这个扩展,可以看到一个新的VS Code窗口会打开。我们在里面按下Ctrl+Shift+P,在弹出的命令栏中敲Hello World回车,你会看到弹出一个“Hello Worlld!”信息框。


关掉这个窗口,我们来看一下hellocode底下的文件,package.json 里有我们用工具生成代码的时候写的一些信息。

我们可以在这个里面修改配置,比如把title改成“Hi Code”,那我们调取这个扩展的时候就要敲“Hi Code”而不是“Hello World”了。
"contributes": {
"commands": [{
"command": "extension.sayHello",
"title": "Hi Code"
}]
},
那么弹出信息框的代码在哪里呢?
我们在extension.js里找到这段代码,activate 方法中,我们给“extension.sayHello”注册了方法,方法内容就是,弹出“Hello World!”信息框。
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "testcode" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('extension.sayHello', function () {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}
接下来我们稍微修改下这个方法,
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "hellocode" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('extension.sayHello', SelectItem);
context.subscriptions.push(disposable);
}
function SelectItem() {
vscode.window.showQuickPick(["a", "b"]).then(function (selected) {
if(selected){
vscode.window.showInformationMessage("Item '"+selected+"' has been selected!");}
});
}
我们在弹出信息框之前,调用了一个VC Code 的API,显示一个快速选择的列表,当用户选择之后,把选中的内容显示在信息框中。
按下F5来debug一下,


我们可以在extension.js中添加断点进行一步步调试,这边我就不一步步的放截图了,大家可以自己尝试着去做一下。
好了,这次的示范就到这里,希望能帮到大家~
想了解更多的VS Code API, 戳这边:https://code.visualstudio.com/docs/extensionAPI/vscode-api
这次的示例代码在这里可以下载:How to create a simple extension for VS Code
觉得这个例子太简单?这里有更多完整的示例:https://code.visualstudio.com/docs/tools/samples
如何创建一个简单的Visual Studio Code扩展的更多相关文章
- Visual Studio Code扩展
Visual Studio Code扩展 注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一 ...
- Visual Studio Code扩展:
Auto Close TagAuto Rename TagBeautifyChinese (Simplified) Language Pack for Visual Studio CodeClass ...
- Visual Studio Code 扩展工具集,记录
编码 提高效率及校验 Auto Close Tag 自动闭合标签 Auto Rename Tag 自动更改HTML/XML标签,不需要再进行二次修改,减少50%的工作量! Path Intellise ...
- 【实验手册】使用Visual Studio Code 开发.NET Core应用程序
.NET Core with Visual Studio Code 目录 概述... 2 先决条件... 2 练习1: 安装和配置.NET Core以及Visual Studio Code 扩展... ...
- 使用Visual Studio Code开发.NET Core看这篇就够了
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9926078.html 在本文中,我将带着大家一步一步的通过图文的形式来演示如何在Visual Studi ...
- Create A .NET Core Development Environment Using Visual Studio Code
https://www.c-sharpcorner.com/article/create-a-net-core-development-environment-using-visual-studio- ...
- 使用Visual Studio Code调试基于ActionScript的LayaAir HTML5游戏
使用Visual Studio Code(VS Code)调试的优势 使用VS Code我们可以极大地提高LayaAir Html5游戏项目的调试效率,VS Code的优势有以下几点: 在发生Java ...
- SharePoint Framework 在Visual Studio Code中调试你的本地解决方案
博客地址:http://blog.csdn.net/FoxDave Visual Studio Code不知道大家都有没有,界面清爽,编辑快速,是一个非常好的前端开发工具.本文介绍如何使用Goog ...
- 使用Visual Studio Code编写和激活ABAP代码 (上)
猪年春节后的第一篇,Jerry祝各位猪年大吉! 2019年的六分之一马上就快过完了,不知道大家在新的一年是否给自己定了新的小目标呢?这里Jerry先预祝大家到2019年年底的时候,在年初制定的小目标都 ...
随机推荐
- 我的EF功能
由来 话说这个功能想法由来与java的Hibernate功能,我需要一个类和数据库映射,很简单的写一个实体类简单配置一下就ok了, 很是方便, package com.game.po.log; imp ...
- Win10 连接L2TP VPN 失败解决方法
Win10 连接L2TP VPN 失败解决方法 iOS系统不知道在什么时候,已经不支持PPTP VPN.偶尔的机会刚好看到github上的一键式VPN服务器部署脚本setup-ipsec-vpn,就在 ...
- <精通JavaScript>---阅读笔记01
下面是阅读精通JavaScript书做的相关笔记. JS中的函数重载 函数重载必须依赖两件事情:判断传入参数数量的能力和判断传入参数类型的能力,在js中每个函数都带有一个仅在这个函数范围内作用的变量, ...
- sqlserver 游标的使用
declare @temp_temp uniqueidentifier--临时变量 DECLARE aaa CURSOR for select Id from A ------------------ ...
- ECharts的简单使用过程
网页中经常要使用图表,以前使用的是highcharts插件,现在发现echarts使用起来和highcharts差不多,但是个人感觉echarts更酷炫,以下是echarts的使用过程,其实highc ...
- 项目编码规范(Ali)
一.研发流程规范 二.SQL编码规范 数据库命名规范:数据库名一律小写,必须以字母开头.库名包含多个单词的,以下划线“_”分隔.如果采用分库方案,分库编号从“0”开始,用“0”左补齐为四位. 表名规范 ...
- instanceof 运算符
java中的instanceof=======>二元运算符 用法: result = object instanceof class 参数: result 是boolean类型 object 是 ...
- ERDAS 2014安装破解及汉化图文教程
关于ERDAS 2014 的安装破解及其汉化过程: 说明:汉化只是之针对英语能力不能正常满足使用软件的朋友们.如果英语过专八,完全没有必要汉化!在此之前查了好多关于ERDAS汉化的文档,可惜只是简单的 ...
- CSS3之盒子模型
display:box 使子元素成行排列如果父级宽度小于子级盒子 不会把超出部分挤出下面 而是直接超出 -box-orient:vertical 使盒子垂直显示 默认水平显示 -box-direct ...
- Git从码云Clone代码到本地
Git从码云或者Github 克隆代码到本地 1.下载安装Git,傻瓜式下一步下一步即可... 2.配置Git: 2.1.选择你要clone到本地的路径:右键--->$ Git Bash Her ...