Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

wszgrcy
wszgrcy

Posted on

Implementing programmable code snippets in VSCode - `Code Recycle`

  • How to customize code snippets in the editor? Provide the completion prefix, provide the completion content, and then you can use it directly
  • But specific content can only be inserted in the current file and input location, sometimes it may not meet the requirements
  • If we want to introduce a service that automatically creates when it does not exist, we can only rely on ourselves because the code snippet isstatic and cannot beprogrammed
  • If you have such a need and want to implement it, why not give it a try withCode Recycle

Import a service

  • Create code snippet configuration
{"prefix":["import"],"body":" ","regexp":{"pattern":"\\.([\\p{L}\\p{N}]+)","flag":"u"},"pattern":"**/*.ts","dynamicMode":true,"script":"./import-class.ts"}
Enter fullscreen modeExit fullscreen mode
  • Implement call function
letfn:ScriptFunction=async(util,rule,host,injector)=>{letimportName=util.documentContext.snippetParameters![1];let_=util.lodash;letclassName=_.upperFirst(_.camelCase(importName));letrootCtx=util.initContext();awaitutil.changeList([// Check if the class exists{path:`*.ts`,name:'classPath',glob:true,list:[{query:`export class${className}`,mode:'like',optional:true,callback(context,index){letclassPathCtx=context.getContext('root.classPath');classPathCtx.data=context.node!.path!;},},],},],rootCtx);letfilePath=rootCtx.getContext('root.classPath').data;if(!filePath){// Create from templateawaitutil.changeList([{type:'copy',from:join(__dirname,'./template/__name@dasherize__.ts'),to:'./',pathTemplate:'@angular-devkit',contentTemplate:'@angular-devkit',templateContext:{name:importName},},]);letchangedRecord=host.records();filePath=util.path.normalize((changedRecord[0]asany).path);}letpathRelative=require('../shared/path-relative');// insert referenceletchanged=awaitutil.changeList([{path:util.filePathGroup.currentPath,list:[{range:[0,0],replace:`import {${className}} from '${pathRelative(util.filePathGroup.currentPath,filePath,util)}'\n`,},{range:util.documentContext.insertRange![0].range,replace:`let${importName} = new${className}()`,},],},]);awaitutil.updateChangeList(changed);};
Enter fullscreen modeExit fullscreen mode
  • When enteringimport.hello in thets file in the editor, and completing the completion, theHello class will be automatically introduced and initialized

More?

  • examples
  • The tool currently supports the execution ofCLI andVSCode Extension. Script Supportyaml/js/ts
  • You can viewdocument learn more
  • If you want to see more instances, you canvisit the repository view and Run
  • If you are already interested, you canstart quickly

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromwszgrcy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp