城市直播房产教育博客汽车
投稿投诉
汽车报价
买车新车
博客专栏
专题精品
教育留学
高考读书
房产家居
彩票视频
直播黑猫
投资微博
城市上海
政务旅游

造轮子vscode插件支持json生成gostruct,cu

7月3日 碎骨族投稿
  最近学习了一下如何写vscode插件,不得不感叹大神写的vscode框架就是厉害,简单通过配置文件加上事件处理代码就可以扩展编辑器前端的能力。膜拜之余,造了一个轮子,交互过程如下,右键json文件选择json生成go结构体(JsonToGo)就可以生成json文件对应的选择生成golang代码或者结构体curl生成go代码(CurlToGo)就可以从curl命令(从浏览器的debugtool直接copy过来)生成对应的golang客户端代码,简单修改即可发起http请求。
  代码放在https:github。comxiazemingolangCodeHelper,由于没有自己的独立域名,暂时没有发布到vscode应用市场,感兴趣的小伙伴可以到github
  下载下来,然后通过导入vsix文件的方式来安装插件,编译好的插件位于:https:github。comxiazemingolangCodeHelperblobmaingolangCodeHelper0。0。1。vsix,通过这个插件我们可以在本地做常用的代码生成,提升我们的开发效率。比如:
  json文件:{name:golangCodeHelper,version:0。0。1,lockfileVersion:2,requires:true,packages:{:{name:golangCodeHelper,version:0。0。1,devDependencies:{typesglob:8。0。1,typesmocha:10。0。1,
  可以生成typeAutoGeneratedstruct{Namestringjson:nameDisplayNamestringjson:displayNameDescriptionstringjson:descriptionVersionstringjson:versionEnginesstruct{Vscodestringjson:vscode}json:enginesCategories〔〕stringjson:categoriesActivationEvents〔〕stringjson:activationEventsMainstringjson:mainContributesstruct{Commands〔〕struct{Commandstringjson:commandTitlestringjson:titleCategorystringjson:category,omitempty}json:commands
  在浏览器上复制下来的curlcurlhttps:ug。baidu。commcppcpcsearchHAccept:HAcceptLanguage:zhCN,q0。9HConnection:keepaliveHContentType:applicationjsonHCookie:BIDUPSID17AC3E8E8275F51ACFD6A4E37AC75966;PSTM1676794732;BAIDUID17AC3E8E8275F51AC7F6EA632EC051DD:FG1;HPSPSSID36555381123809238132381163815038175381733680237935380882635038119380973800837881;BDORZB490B5EBF6F3CD402E515D22BCDA1598;BAHECTOR0485ag8g0g2k8ha1250g0hjm1hv49q51k;delPer0;PSINO5;BAIDUIDBFESS17AC3E8E8275F51AC7F6EA632EC051DD:FG1;ZFYLzNgJDmgbcwFmDlSJY5RPCv4lgsx3Kzg2hlLesbqjdY:CHOrigin:https:www。baidu。comHReferer:https:www。baidu。coms?ieutf8f8rsvbp1rsvidx1tnbaiduwdcontenttype20application2Fjsonfenlei256oqcontenttype2520application252Fjsonrsvpqb58f78f900026b99rsvt1445vFHiJ7o4mPEOrw4MQTAUMrxUQJFcddCoRqhH8Q3CjHgKQK8m4sCm2NQrqlangcnrsventer0rsvdltbrsvbtypetinputT95112rsvsug3407rsvsug1450rsvsug7000rsvsug20rsvsug495250rsvsug1HSecFetchDest:emptyHSecFetchMode:corsHSecFetchSite:samesiteHUserAgent:Mozilla5。0(MIntelMacOSX10157)AppleWebKit537。36(KHTML,likeGecko)Chrome109。0。0。0Safari537。36Hsecchua:NotABv99,GoogleCv109,Cv109Hsecchuamobile:?0Hsecchuaplatform:macOSdataraw{invokeinfo:{pos1:〔{}〕,pos2:〔{}〕,pos3:〔{}〕}}compressed
  可以生成typePayloadstruct{InvokeInfoInvokeInfojson:invokeinfo}typePos1struct{}typePos2struct{}typePos3struct{}typeInvokeInfostruct{Pos1〔〕Pos1json:pos1Pos2〔〕Pos2json:pos2Pos3〔〕Pos3json:pos3}data:Payload{fillstruct}payloadBytes,err:json。Marshal(data)iferr!nil{handleerr}body:bytes。NewReader(payloadBytes)req,err:http。NewRequest(POST,https:ug。baidu。commcppcpcsearch,body)iferr!nil{handleerr}req。Header。Set(Accept,)req。Header。Set(AcceptLanguage,zhCN,q0。9)req。Header。Set(Connection,keepalive)req。Header。Set(ContentType,applicationjson)req。Header。Set(Cookie,BIDUPSID17AC3E8E8275F51ACFD6A4E37AC75966;PSTM1676794732;BAIDUID17AC3E8E8275F51AC7F6EA632EC051DD:FG1;HPSPSSID36555381123809238132381163815038175381733680237935380882635038119380973800837881;BDORZB490B5EBF6F3CD402E515D22BCDA1598;BAHECTOR0485ag8g0g2k8ha1250g0hjm1hv49q51k;delPer0;PSINO5;BAIDUIDBFESS17AC3E8E8275F51AC7F6EA632EC051DD:FG1;ZFYLzNgJDmgbcwFmDlSJY5RPCv4lgsx3Kzg2hlLesbqjdY:C)req。Header。Set(Origin,https:www。baidu。com)req。Header。Set(Referer,https:www。baidu。coms?ieutf8f8rsvbp1rsvidx1tnbaiduwdcontenttype20application2Fjsonfenlei256oqcontenttype2520application252Fjsonrsvpqb58f78f900026b99rsvt1445vFHiJ7o4mPEOrw4MQTAUMrxUQJFcddCoRqhH8Q3CjHgKQK8m4sCm2NQrqlangcnrsventer0rsvdltbrsvbtypetinputT95112rsvsug3407rsvsug1450rsvsug7000rsvsug20rsvsug495250rsvsug1)req。Header。Set(SecFetchDest,empty)req。Header。Set(SecFetchMode,cors)req。Header。Set(SecFetchSite,samesite)req。Header。Set(UserAgent,Mozilla5。0(MIntelMacOSX10157)AppleWebKit537。36(KHTML,likeGecko)Chrome109。0。0。0Safari537。36)req。Header。Set(SecChUa,NotABv99,GoogleCv109,Cv109)req。Header。Set(SecChUaMobile,?0)req。Header。Set(SecChUaPlatform,macOS)resp,err:http。DefaultClient。Do(req)iferr!nil{handleerr}deferresp。Body。Close()
  下面详细介绍下插件开发的详细流程。首先安装node环境nodevv18。0。0npmv8。6。0
  然后安装脚手架和插件打包工具npminstallgyogeneratorcodenpminstallgvscodevsce
  通过脚手架生成项目框架yocodeWelcometotheVisual(o)StudioCodeExtensiongenerator!(U)A。。Y?Whattypeofextensiondoyouwanttocreate?(Usearrowkeys)NewExtension(TypeScript)NewExtension(JavaScript)NewColorThemeNewLanguageSupportNewCodeSnippetsNewKeymapNewExtensionPackNewLanguagePack(Localization)NewWebExtension(TypeScript)NewNotebookRenderer(TypeScript)?Whattypeofextensiondoyouwanttocreate?NewExtension(TypeScript)?Whatsthenameofyourextension?hello?Whatstheidentifierofyourextension?hello?Whatsthedescriptionofyourextension?hello?Initializeagitrepository?No?Bundlethesourcecodewithwebpack?Yes?Whichpackagemanagertouse?npm
  它生成的代码很简单,是一个最简单的模板,能够在vscode中输出vscode,它的核心代码是下面几行,首先看下package。jsonactivationEvents:〔onCommand:hello。helloWorld〕,main:。distextension。js,contributes:{commands:〔{command:hello。helloWorld,title:HelloWorld}〕},
  其中activationEvents,就是我们的插件和对应的命令,。distextension。js,是插件的入口文件,commands,后面就是对应的我们的命令。然后我们看下入口文件。distextension。jsThemodulevscodecontainstheVSCodeextensibilityAPIIThismethodiscalledwhenyourextensionisactivatedYourextensionisactivatedtheveryfirsttimethecommandisexecutedexportfunctionactivate(context:vscode。ExtensionContext){Usetheconsoletooutputdiagnosticinformation(console。log)anderrors(console。error)Thislineofcodewillonlybeexecutedoncewhenyourextensionisactivatedconsole。log(Congratulations,yourextensionhelloisnowactive!);Thecommandhasbeendefinedinthepackage。jsonfileNowprovidetheimplementationofthecommandwithregisterCommandThecommandIdparametermustmatchthecommandfieldinpackage。jsonletdisposablevscode。commands。registerCommand(hello。helloWorld,(){ThecodeyouplaceherewillbeexecutedeverytimeyourcommandisexecutedDisplayamessageboxtotheuservscode。window。showInformationMessage(HelloWorldfromhello1!);});context。subscriptions。push(disposable);}Thismethodiscalledwhenyourextensionisdeactivatedexportfunctiondeactivate(){}
  它最核心的就是暴露了一个激活方法给vscode框架调用,在方法内部,通过vscode。commands。registerCommand,注册了需要执行的命令和对应的callback函数,并将命令发布到context,context。subscriptions。push(disposable);这样一个最简单的插件就制作完毕了。我们可以通过fnf5进行调试,没有问题后就可以通过vscepackage
  进行打包生成对应的插件文件xxx。vsix,我们发布到应有市场然后搜索,或者直接导入插件文件,就可以使用我们的插件了。
  熟悉完上述流程后,就可以开始制作自己的插件,首先在package。json里配置我们需要的命令和菜单。activationEvents:〔onCommand:golangCodeHelper。helloWorld,onCommand:golangCodeHelper。JsonToGo,onCommand:golangCodeHelper。CurlToGo,onCommand:golangCodeHelper。CurlToGoStructs〕,main:。distextension。js,contributes:{commands:〔{command:golangCodeHelper。helloWorld,title:golang编码辅助工具集},{command:golangCodeHelper。JsonToGo,title:json生成go结构体(JsonToGo),category:golangCodeHelper},{command:golangCodeHelper。CurlToGo,title:curl生成go代码(CurlToGo),category:生成golang代码或者结构体},{command:golangCodeHelper。CurlToGoStructs,title:curl生成go结构体(CurlToGoStructs),category:生成golang代码或者结构体}〕,menus:{explorercontext:〔{command:golangCodeHelper。JsonToGo,when:!explorerResourceIsFolder,group:2workspace1},{group:2workspace2,when:!explorerResourceIsFolder,submenu:golangCodeHelpersubmenugenerate}〕,golangCodeHelpersubmenugenerate:〔{command:golangCodeHelper。CurlToGo,group:1generate1},{command:golangCodeHelper。CurlToGoStructs,group:1generate2}〕},submenus:〔{id:golangCodeHelpersubmenugenerate,label:生成golang代码或者结构体}〕},
  然后注册我们的命令执行函数并且发布ThemodulevscodecontainstheVSCodeextensibilityAPIIconst{jsonToGo}require(。jsontogojsontogo。js);const{curlToGo}require(。curltogoresourcesjscurltogo。js);const{curlToGoStruct}require(。curltogostructcurltogostruct。js)parampath写入的文件路径paramcontent写入的文件内容paramfileName写入的文件名paramfileNameExtra当文件名存在于该文件夹下时的替代文件名constwriteFile(path:string,content:string,fileName?:stringundefined,fileNameExtra?:stringundefined){letnewfileNamefileNamejsontogo。constopt{flag:wx但是如果文件路径存在,则文件写入失败。覆盖写入:w};constexists:booleanfs。existsSync({path}{nodepath。sep}{newfileName});if(exists){newfileNamefileNameExtrafileNameExtra副本。}console。log(写入路径:{path}{nodepath。sep}{newfileName});fs。writeFile({path}{nodepath。sep}{newfileName},content,opt,(err){if(err){vscode。window。showErrorMessage(写入{newfileName}失败,可能原因是,改文件夹下已存在{newfileName});}vscode。window。showInformationMessage(已生成一个示例{newfileName});});};ThismethodiscalledwhenyourextensionisactivatedYourextensionisactivatedtheveryfirsttimethecommandisexecutedexportfunctionactivate(context:vscode。ExtensionContext){Usetheconsoletooutputdiagnosticinformation(console。log)anderrors(console。error)Thislineofcodewillonlybeexecutedoncewhenyourextensionisactivatedconsole。log(Congratulations,yourextensiongolangCodeHelperisnowactive!);Thecommandhasbeendefinedinthepackage。jsonfileNowprovidetheimplementationofthecommandwithregisterCommandThecommandIdparametermustmatchthecommandfieldinpackage。jsonletdisposablevscode。commands。registerCommand(golangCodeHelper。helloWorld,(){ThecodeyouplaceherewillbeexecutedeverytimeyourcommandisexecutedDisplayamessageboxtotheuservscode。window。showInformationMessage(HelloWorldfromgolangCodeHelper!);});letJsonToGovscode。commands。registerCommand(golangCodeHelper。JsonToGo,(e){vscode。window。showInformationMessage(JsonToGofromgolangCodeHelper!);constfilenamee?e?。fsPath?。split()。pop():vscode。window。activeTextEditor?。document。fileName。split()。pop();vscode。window。showInformationMessage(filenamee。fsPath);vardatafs。readFileSync(e。fsPath,utf8);vscode。window。showInformationMessage(data);console。log(data)console。log(jsonToGo)constgotjsonToGo(data,null,null,false);console。log(got)vscode。window。showInformationMessage(got);if(got。error){vscode。window。showInformationMessage(JsonToGofromgolangCodeHelperfailed!{got。error});}else{writeFile(nodepath。dirname(e。fsPath),got。go,jsontogo。go);}});letCurlToGovscode。commands。registerCommand(golangCodeHelper。CurlToGo,(e){vscode。window。showInformationMessage(curlToGofromgolangCodeHelper!);constfilenamee?e?。fsPath?。split()。pop():vscode。window。activeTextEditor?。document。fileName。split()。pop();vscode。window。showInformationMessage(filenamee。fsPath);vardatafs。readFileSync(e。fsPath,utf8);vscode。window。showInformationMessage(data);console。log(data)console。log(curlToGo)constgotcurlToGo(data,null,null,false);console。log(got)vscode。window。showInformationMessage(got);writeFile(nodepath。dirname(e。fsPath),got,curltogo。go);});letCurlToGoStructsvscode。commands。registerCommand(golangCodeHelper。CurlToGoStructs,(e){vscode。window。showInformationMessage(CurlToGoStructsfromgolangCodeHelper!);constfilenamee?e?。fsPath?。split()。pop():vscode。window。activeTextEditor?。document。fileName。split()。pop();vscode。window。showInformationMessage(filenamee。fsPath);vardatafs。readFileSync(e。fsPath,utf8);vscode。window。showInformationMessage(data);console。log(data)console。log(curlToGoStruct)constgotcurlToGoStruct(data,null,null,false);console。log(got)vscode。window。showInformationMessage(got);writeFile(nodepath。dirname(e。fsPath),got,curltogostruct。go);});context。subscriptions。push(disposable);context。subscriptions。push(JsonToGo);context。subscriptions。push(CurlToGo);context。subscriptions。push(CurlToGoStructs);}Thismethodiscalledwhenyourextensionisdeactivatedexportfunctiondeactivate(){}
  至此,插件制作完毕,当然vscode还支持更为复杂的插件,可以参考其官方文档进行学习。
投诉 评论 转载

历史上三个假太监有多奇葩?让太后生子,欺辱皇后,杀了皇帝提到太监这个身份,想必大家应该都不陌生。太监在现在的以古代为背景的电视剧中可谓不可或缺的角色,从《大明天下》中的魏忠贤到《甄嬛传》中的小允子、康禄海,不一而足。他们中有忠……去瑞士募资!2000亿券业龙头放大招,拟发行不超6。6亿股点蓝字关注,不迷路来源:中国基金报从沪伦通到中欧通,奔赴瑞士搞钱成了A股上市公司的新趋势。而作为资本市场的熟客,这一次证券行业的先行者是东方财富。10月31日……造轮子vscode插件支持json生成gostruct,cu最近学习了一下如何写vscode插件,不得不感叹大神写的vscode框架就是厉害,简单通过配置文件加上事件处理代码就可以扩展编辑器前端的能力。膜拜之余,造了一个轮子,交互过程如……他用缉毒的思路办森警案件!警号087633,我们等你虽已入冬,雨城雅安迎来阳光灿烂。四川省雅安市公安局森林警察支队驻扎的小楼里,同事轻轻推开综合大队办公室的门,仔细拂去大队长贾世忠座位上的灰尘。座位已经空了一个多月,一身没来得及……黄金白银价格连续第三个交易日下跌,收于一个多月以来的最低点周二黄金和白银价格连续第三次下跌,两种贵金属均跌至一个多月以来的最低水平。周二公布的数据显示,在美联储努力抑制通胀的情况下,8月份美国消费者信心调查出现四个月来首次上升,……国祚最久的封建王朝,大汉王朝400年,奠定中华文明大一统的基汉族是中华民族的主体,也是世界上人口最多的民族。但要是没有汉朝,汉就只是一条河流的名称,至多只会产生一些得名于这条河流的地名。汉元年(前206年)夏四月,被西楚霸王项羽封……为何大多数中国人认为炒股等同于赌博?1。法治不健全不规范,被坏人和腐败分子钻了空子。2。股市赚钱难,每一波牛市被收割,股民们心灰意冷。3。某些人赌性大,认知不到位,认为股市就是赌场。4。中国经济……民国十大军阀东北军阀东北军前身为奉系军阀张作霖所统率的军队,称为奉军。1929年,东北军被蒋介石改编为东北边防军,简称东北军,约30万人,由张作霖之子张学良统辖。九一八事变时,执行蒋……在国企上班,性格比较内向,也不会巴结领导,这样会影响职业前途吉成在平时不善于表达,年年是车间的先进,但是,还在车间工作了五六年,同事们都说他是由于自己的性格内向,影响了他的进步。吉成去给同学帮忙,却把医院的一台旧设备给修复了,惊呆了医院……2022年内地各省市存款和消费支出大比拼近期,2022年各省份GDP数据公布,毫无悬念广东、江苏、山东三省GDP总量依然占据前三名,这已经是多年的格局了。当然浙江省排名第4的位置也是多年未变了。但是浙江省却被认为辖区……倪妮刘诗诗拍摄双人杂志,黑白吊带上演性感尤物,你喜欢吗倪妮长着一张高级脸,在各大时尚大片的封面中,她的颜值绝对是碾压众人的存在。在大家看到她的封面杂志之后,就会被她的这种造型所吸引。回归到了波浪长发的造型,在雍容华贵的同时也展现出……超2000个服务队!黑龙江省巾帼志愿者达20余万人记者从黑龙江省妇联获悉,为弘扬志愿服务精神,增强对女性职工、青年职工的思想引领,2日,中建一局集团黑龙江分公司妇联成立巾帼志愿服务队,同步开展学雷锋巾帼志愿服务活动。据悉……
与革命文物面对面红色文物系列实力堪比华雄威望胜过夏侯惇的他,鏖战古城象鼻刀勇斗青龙偃月刀治重化积,信访积案有新解年龄大了,失业了几年了,找不到工作,一直在家里呆着,怎么办啊从为国争光到抹黑中国?如今他又被骂上热搜,连张艺谋都因此躺枪魏文侯只想靠强军在夹缝中生存,却靠魏武卒成为战国期第一位霸主如何做好居家健康监测?疾控专家这么说宁夏固原市历史上有哪些名人?王艺迪抗日成功,4重庆号巡洋舰起义归来同属闽南地区,为何泉州漳州海外侨胞数量相差如此巨大?乌害怕的事来了,美德库存将空,拜登中期选举要输?军援恐要断与书同行八年级作文阿富汗局势已成定局,如何评价美国20年阿富汗战略?婴幼儿洗衣液能去甲醛吗定价598元AfterShokz韶音OpenMove会是年轻红楼梦的著作权却仅归曹雪芹和高鹗无关六年级第二学期教学工作计划奶奶的粽子发一张你曾经旅游过的城市的夜景照片,大家猜猜是哪个城市?新冠疫苗接种前一天可以喝酒吗新冠疫苗接种前喝酒会怎么样治安安全事件类型打架斗殴如何处理?超市看到这5种洗面奶,一定多买几样屯起来,以后不一定能买到送陈用脩之辰州府通判

友情链接:中准网聚热点快百科快传网快生活快软网快好知文好找