mikaelbr / vscodemod

VSCode extension for doing codemod on selected text
30 stars 3 forks source link

Passing cursor position/selection range to custom codemod that operates on entire file #7

Open jedwards1211 opened 4 years ago

jedwards1211 commented 4 years ago

I'm in the process of switching from Atom to VSCode. I had a bunch of custom codemods at my disposal in Atom that I'm planning to get working in VSCode, for instance: https://github.com/jedwards1211/material-ui-codemorphs

For convenience, my addStyles codemod in that package doesn't require the user to select the component to be modified; instead it can just determine which component contains the cursor position by using my https://github.com/jedwards1211/jscodeshift-paths-in-range package.

Furthermore, it wouldn't be sufficient for addStyles to transform only the selected code, because it also has to add import statements to the top of the file, and potentially rename some identifiers. But it has to know the cursor position/selection to determine which component to modify.

My own codemods for adding block statements to arrow functions also worked this way; it's a lot less hassle to just put the cursor inside the function than selecting the whole thing.

I was planning on making an extension for running codemods, then I found your extension. This wouldn't currently satisfy my needs, but I figured I would talk to you about this before making my own extension. Basically it would have to pass the selection range (or cursor position) to the transform via the options argument. This will make it possible to do more complex transforms in the future than just operating on selected code.

jedwards1211 commented 4 years ago

Also btw, I was thinking it would be more convenient to be able to map codemods to top-level VSCode commands (and even keyboard shortcuts), rather than having to select the codemod after running the "Run codemod on selection" command.