0

I wrote this snippet and I'm looking for a way to edit the import line at the head of the file and add the correct additions to Component class.

"Angular Lifecycle Hooks": {
 "prefix": "nglifecycle",
 "body": [
    "ngOnChanges() {",
    "\t// called before any bindings are made and before ngOnInit()",
    "\t// Here you can access the change detection results, and make any updates you want.",
    "\tconsole.log('ngOnChanges');",
    "}",
    "",
    "ngOnInit() {",
    "\t// called once after the first ngOnChanges()",
    "\tconsole.log('ngOnInit');",
    "}",
    ""
  ],

 "description": "Angular Lifecycle Hooks Snippet"
}

Is there a way to do so?

Example for what I'm trying to achieve:

The snippet generate the text in the body tag, let's say use it in line number 15. And I have this line, near the top of the file

import {Component} from '@angular/core'

I'm looking for a way that the snippet will update that line to

import {Component, OnInit, OnChange} from '@angular/core'

You see it is adding OnInit and OnChange which were added in the snippet body as ngOnChnages() and ngOnInit().

2
  • I'm not really following. It sounds like you want the snippet to edit part of the file before the snippet, but how are you expecting a snippet to do that? You can edit to clarify. For tips about clarifying, see How to ask a good question. It might help to illustrate what you want with some example code before the changes and what you want the code to be after the changes. Also, if you could minimize the example, that'd help. Commented Feb 12, 2023 at 20:18
  • Done, Thanks . Love to hear your feedback again and if you able to help it will be lovely Commented Feb 14, 2023 at 9:17

2 Answers 2

1

You cannot do that solely with built-in functionality, but it is pretty easy with an extension. For example, Find and Transform (disclaimer: I wrote it), allows you to insert the snippet AND do a find and replace elsewhere in the document.

Using this keybinding (in your keybindings.json):

{
  "key": "alt+d",                  // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "preCommands": {
      "command": "editor.action.insertSnippet",
      "args": {
        "name": "Angular Lifecycle Hooks"
      }
    },
    "find": "(?<=^import\\s*\\{)(\\s*[^}]*?)(?=\\s*\\}\\s*from\\s*'@angular\/core'$)",
    "replace": "$1, OnInit, OnChange",
    "isRegex": true,
    "postCommands": "cancelSelection"
  },
  "when": "editorTextFocus && !editorReadonly && editorLangId == javascript"
},

inseert snippet and change imports at top of file

Your Angular Lifecycle Hooks snippet is still located in some snippets file.

The keybinding first inserts the snippet at the cursor and then does a find/replace for your import {Component} from '@angular/core' text.

Note: if your langID is not javascript swap that part with your file's language identifier - click on the language in the lower right-hand corner of the window and see the language ID, in parentheses like (javascript) - just use the part in the parentheses.

The regex find works if there are leading or trailing spaces in the {}, and Component does not need to be there already. The additions onInit and onChange will simply be added to whatever is already there. If need be, the regex could be modified to handle other cases.

  • Also, this uses a keybinding to trigger essentially a macro extension. I doubt there is any way to accomplish what you want solely with a snippet prefix (unless you write a custom extension) and have that snippet prefix trigger both a snippet insertion and an edit elsewhere in the document.
Sign up to request clarification or add additional context in comments.

Comments

0

Snippet is an elementary functionality to add some text with modification of selected part of document and some other smple things. It doesn't know anything about rest part of your document. But since you added vscode-extensions tag, maybe you can write your own simple extension doing the things you want :) With its help you can modify document after snippet was inserted

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.