当前位置:Gxlcms > asp.net > VsCode插件开发之插件初步通信的方法步骤

VsCode插件开发之插件初步通信的方法步骤

时间:2021-07-01 10:21:17 帮助过:33人阅读

参考了Egret Wing,想像Egret Wing那样在上方titlebar最右边上面增加一个menu(这个menu相对于一个按钮,当点击这个按钮时会出现一个window弹框,这个window弹框里就包含相关的表单信息以供登录或者注册使用。我是以这个作为参考模板的。但是目前进展并不是很顺。于是我通过插件的方式暂时性解决了这个问题。但是觉得还不是想要的那样。

Egret Wing是这样的,如图所示:

不得不承认一点Egret Wing改造的挺不错的,不愧是对VsCode进行魔改。

今天先说一下通过插件通信。

我主要参考的是一个叫小茗同学的插件开发,并改造其插件来达到我的目的。

这个小茗同学我觉得他写的插件开发,我觉得不是特别详细全面,当然,参考意义还是有的。

他的插件开发目录如下:

他的插件github地址为:https://github.com/sxei/vscode-plugin-demo.git

他的插件可以在VsCode插件扩展中搜到(搜到后安装,然后直接在下载成功的插件的基础上改造),例如:

那么说说我是如何改造它的呢?

我主要改造它这么几个地方?

一个是图标,另外一个修改它的html界面(主要是修改custom-welcome.html),同时我要和还改了package.json文件。

插件开发可以用TypeScript,也可以用JavaScript。

如果是用TypeScript的话,通常扩展脚本文件是extension.ts形式存在,如果是JavaScript,则是以extension.js的形式存在。

在此我想强调的是改他人插件或者自己编写插件,以ts为例,主要把握也就两个文件,一个是extension.ts,另一个就是package.json。

如何从0开发以插件的相关视频,感兴趣的可以看看,感觉还是有一定的启发的:https://v.qq.com/x/page/k08220bdz3s.htmlb

我改造后的插件代码,放在我的个人github上,大家可以将其下载下来放入,如下两个文件中(任意一个都行):

注意:

.vscode文件夹:官方插件下载好默认放入的目录。

.vscode-oss-dev:下载源码,自己编译,下载插件放置的目录。

自己编译的不知道由于什么原因不能直接联网通信搜索一些应用市场下载的插件。

通常情况下(以.vscode-oss-dev为例),git clone下来我的插件地址,然后将其移植到这个目录就能看到对应的效果,效果图如下:

我的VsCode插件地址为:https://github.com/youcong1996/study_simple_demo/tree/vscode-plugin-communication

将其克隆下来放入.vscode或者.vscode-oss-dev中的extensions目录下即可起作用。

另外有一点要强调的是,如果是vscode非自己编译的,需要重启一下vscode,如果是自己编译的话,监听需要暂时中断重新输入(yarn watch)。

接下来说说我修改的三个地方。

1.修改package.json(包含图标一起说了及其点击登录的同时展示对应的左侧栏sidebar)

  1. {
  2. "name": "vscode-plugin-demo",
  3. "displayName": "vscode-plugin-demo",
  4. "description": "VSCode插件demo",
  5. "keywords": [
  6. "vscode",
  7. "plugin",
  8. "demo"
  9. ],
  10. "version": "1.0.3",
  11. "publisher": "sxei",
  12. "engines": {
  13. "vscode": "^1.27.0"
  14. },
  15. "categories": [
  16. "Other"
  17. ],
  18. "icon": "images/icon.png",
  19. "activationEvents": [
  20. "*"
  21. ],
  22. "main": "./src/extension",
  23. "contributes": {
  24. "configuration": {
  25. "type": "object",
  26. "title": "Code插件demo",
  27. "properties": {
  28. "vscodePluginDemo.yourName": {
  29. "type": "string",
  30. "default": "guest",
  31. "description": "你的名字"
  32. },
  33. "vscodePluginDemo.showTip": {
  34. "type": "boolean",
  35. "default": true,
  36. "description": "启动时显示自定义欢迎页"
  37. }
  38. }
  39. },
  40. "commands": [
  41. {
  42. "command": "extension.sayHello",
  43. "title": "Hello,小茗同学"
  44. },
  45. {
  46. "command": "extension.demo.getCurrentFilePath",
  47. "title": "获取当前文件(夹)路径"
  48. },
  49. {
  50. "command": "extension.demo.testMenuShow",
  51. "title": "这个菜单仅在JS文件中出现",
  52. "icon": {
  53. "light": "./images/tool-light.svg",
  54. "dark": "./images/tool-light.svg"
  55. }
  56. },
  57. {
  58. "command": "extension.demo.openWebview",
  59. "title": "打开WebView"
  60. },
  61. {
  62. "command": "extension.demo.showWelcome",
  63. "title": "显示自定义欢迎页"
  64. }
  65. ],
  66. "keybindings": [
  67. {
  68. "command": "extension.sayHello",
  69. "key": "ctrl+f10",
  70. "mac": "cmd+f10",
  71. "when": "editorTextFocus"
  72. },
  73. {
  74. "command": "extension.demo.openWebview",
  75. "key": "ctrl+f9",
  76. "mac": "cmd+f9",
  77. "when": "editorTextFocus"
  78. }
  79. ],
  80. "menus": {
  81. "editor/context": [
  82. {
  83. "when": "editorFocus",
  84. "command": "extension.sayHello",
  85. "group": "navigation@6"
  86. },
  87. {
  88. "when": "editorFocus",
  89. "command": "extension.demo.getCurrentFilePath",
  90. "group": "navigation@5"
  91. },
  92. {
  93. "when": "editorFocus && resourceLangId == javascript",
  94. "command": "extension.demo.testMenuShow",
  95. "group": "z_commands"
  96. },
  97. {
  98. "command": "extension.demo.openWebview",
  99. "group": "navigation"
  100. }
  101. ],
  102. "editor/title": [
  103. {
  104. "when": "editorFocus && resourceLangId == javascript",
  105. "command": "extension.demo.testMenuShow",
  106. "group": "navigation"
  107. }
  108. ],
  109. "editor/title/context": [
  110. {
  111. "when": "resourceLangId == javascript",
  112. "command": "extension.demo.testMenuShow",
  113. "group": "navigation"
  114. }
  115. ],
  116. "explorer/context": [
  117. {
  118. "command": "extension.demo.getCurrentFilePath",
  119. "group": "navigation"
  120. },
  121. {
  122. "command": "extension.demo.openWebview",
  123. "group": "navigation"
  124. }
  125. ]
  126. },
  127. "snippets": [
  128. {
  129. "language": "javascript",
  130. "path": "./snippets/javascript.json"
  131. },
  132. {
  133. "language": "html",
  134. "path": "./snippets/html.json"
  135. }
  136. ],
  137. "viewsContainers": {
  138. "activitybar": [
  139. {
  140. "id": "beautifulGirl",
  141. "title": "测试",
  142. "icon": "images/beautifulGirl.svg"
  143. }
  144. ]
  145. },
  146. "views": {
  147. "beautifulGirl": [
  148. {
  149. "id": "测试001",
  150. "name": "test"
  151. },
  152. {
  153. "id": "测试002",
  154. "name": "test"
  155. },
  156. {
  157. "id": "测试003",
  158. "name": "test"
  159. }
  160. ]
  161. },
  162. "iconThemes": [
  163. {
  164. "id": "testIconTheme",
  165. "label": "测试图标主题",
  166. "path": "./theme/icon-theme.json"
  167. }
  168. ]
  169. },
  170. "scripts": {
  171. "postinstall": "node ./node_modules/vscode/bin/install",
  172. "test": "node ./node_modules/vscode/bin/test"
  173. },
  174. "devDependencies": {
  175. "typescript": "^2.6.1",
  176. "vscode": "^1.1.6",
  177. "eslint": "^4.11.0",
  178. "@types/node": "^7.0.43",
  179. "@types/mocha": "^2.2.42"
  180. },
  181. "license": "SEE LICENSE IN LICENSE.txt",
  182. "bugs": {
  183. "url": "https://github.com/sxei/vscode-plugin-demo/issues"
  184. },
  185. "repository": {
  186. "type": "git",
  187. "url": "https://github.com/sxei/vscode-plugin-demo"
  188. },
  189. "homepage": "https://github.com/sxei/vscode-plugin-demo/blob/master/README.md",
  190. "__metadata": {
  191. "id": "ac2b7b16-d87f-4e51-87a8-37011e8aa713",
  192. "publisherId": "cdd0fc1d-3acf-4250-a09b-95545e29bdbc",
  193. "publisherDisplayName": "小茗同学"
  194. }
  195. }

在package.json我也就修改了这么几个地方,一个是views(这个view通常主要用于展示左侧的sidebar视图),一个是viewsContainers(我主要修改beautifulGirl.svg)。

修改后的效果分别如下所示:

2.通信(修改custom-welcome.html)

通信我目前采用最原始的javascript的ajax请求,其实jQuery及其vue.js的异步通信也是可以的。

这个custom-welcome.html你可以理解成它就是一个webview。

custom-welcome.html文件内容如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>自定义欢迎页</title>
  7. <link rel="stylesheet" href="../../lib/bootstrap-3.3.1/css/bootstrap.min.css" rel="external nofollow" >
  8. <link rel="stylesheet" href="../../lib/layui/css/layui.css" rel="external nofollow" >
  9. <style>
  10. html, body, #app {
  11. height: 100%;
  12. }
  13. ::-webkit-scrollbar {
  14. width: 10px;
  15. height: 10px
  16. }
  17. ::-webkit-scrollbar-track {
  18. border-radius: 10px;
  19. background-color: #d8dce5
  20. }
  21. ::-webkit-scrollbar-thumb {
  22. border-radius: 5px;
  23. background-color: #adadad
  24. }
  25. ::-webkit-scrollbar-thumb:hover {
  26. background-color: #929292
  27. }
  28. ::-webkit-scrollbar-thumb:active {
  29. background-color: #666363
  30. }
  31. ::-webkit-scrollbar-corner {
  32. background-color: #535353
  33. }
  34. ::-webkit-scrollbar-resizer {
  35. background-color: #ff6e00
  36. }
  37. .page-title {
  38. margin-bottom: 20px;
  39. }
  40. .control-label {
  41. font-weight: normal;
  42. }
  43. .btn-primary {
  44. background-color: #1890ff;
  45. border-color: #1890ff;
  46. outline: none;
  47. }
  48. .btn-primary:focus,
  49. .btn-primary:hover {
  50. background-color: #40a9ff;
  51. border-color: #40a9ff;
  52. outline: none;
  53. }
  54. .btn-primary.active,
  55. .btn-primary:active {
  56. background-color: #096dd9;
  57. border-color: #096dd9;
  58. color: #fff;
  59. outline: none;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div id="app" class="container-fluid">
  65. <h3 class="page-title">自定义欢迎页</h3>
  66. <p class="alert alert-success" style="width: 300px;">{{userName}},{{time}}好!
  67. <span id="info"></span>
  68. </p>
  69. <form class="form-horizontal">
  70. <div class="form-group">
  71. <div class="col-sm-6">
  72. <div id="form">
  73. <form>
  74. <p>用户名:<input type="text" id="userName" style="color:black;"/></p>
  75. <p>密  码  :<input type="password" id="password" style="color:black;"/></p>
  76. <p>            <input type="button" style="color:black;" value="提交" onclick="test()"/>
  77. <input type="button" value="打开" onclick="openLogin()"/>
  78. </form>
  79. </div>
  80. <div class="checkbox">
  81. <label>
  82. <input type="checkbox" v-model="show"> 启动时显示自定义欢迎页
  83.   
  84. <input type="button" onclick="register()" value="退出"/>
  85. </label>
  86. </div>
  87. </div>
  88. </div>
  89. </form>
  90. </div>
  91. <script src="../../lib/jquery/jquery.min.js"></script>
  92. <script src="../../lib/bootstrap-3.3.1/js/bootstrap.min.js"></script>
  93. <script src="../../lib/vue-2.5.17/vue.js"></script>
  94. <script src="../../src/view/custom-welcome.js"></script>
  95. <script src="../../lib/layui/layui.js"></script>
  96. <script src="../../lib/layer/layer-v3.1.1/layer/mobile/layer.js"></script>
  97. <script>
  98. function openLogin(){
  99. layui.use('layer', function(){
  100. var layer = layui.layer;
  101. layer.open({
  102. type: 2,
  103. title: 'Login',
  104. fix: false,
  105. maxmin: true,
  106. shadeClose: true,
  107. shade: 0.8,
  108. area: ['500px', '500px'],
  109. content: 'login.html',
  110. end: function () {
  111. location.reload();
  112. }
  113. });
  114. });
  115. }
  116. function test(){
  117. var userName = document.getElementById("userName").value;
  118. if(userName != null && userName != undefined){
  119. var xhr = new XMLHttpRequest();
  120. xhr.onreadystatechange = function () {
  121. if (xhr.readyState == 4) {
  122. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
  123. $("#info").html("登录成功");
  124. $("#form").hide();
  125. console.log('test:'+xhr.status);
  126. console.log(xhr.responseText);
  127. } else {
  128. console.log("请求成功:" + xhr.status);
  129. }
  130. }
  131. };
  132. xhr.open("POST", "http://www.test.com/test-web/sysUser/getUserCodeByInfo?userCode=2", true);
  133. xhr.send(null);
  134. }else{
  135. layui.use('layer', function(){
  136. var layer = layui.layer;
  137. layer.msg('userName为必填项');
  138. });
  139. }
  140. }
  141. function register(){
  142. $("#info").html("");
  143. $("#form").show();
  144. }
  145. </script>
  146. </body>
  147. </html>

这个html在浏览器上看到的效果如下所示:

目前这仅仅是一个很初级的(蹩脚通信),后续我将会继续补充对VsCode的源码解析及其插件开发相关的详细说明,由于目前掌握的比较分散不够系统,暂时延后讲解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行