Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to extract Svelte HTML into a component #187

Open
milkbump opened this issue Jun 13, 2020 · 3 comments
Open

Ability to extract Svelte HTML into a component #187

milkbump opened this issue Jun 13, 2020 · 3 comments
Labels
feature request New feature or request

Comments

@milkbump
Copy link

It'd be nice to get the ability to extract sections of HTML into a separate component through a context-menu. For example, if I select a div and right-click, it should give me the option to extract it into a separate component.

Example extracting an expression to a variable VSCode:
ts-extract-local

☝️ I essentially would like to be able to do that with HTML, not just JS.

I've used @proverbial-ninja's vscode-component-extractor which is very handy for simple refactors.

But for more complex examples, understanding the Svelte's compiler's output is necessary. eg:

<!-- Number.svelte ->
<script>
	let one = 1;
</script>

<div class="number">
	<div class="one">{one}</div>
</div>

<style>
	.number { background-color: black };
	.one { color: white };
</style>

I'd like to select div.one > Extract to component and end up with:

<!-- Number.svelte ->
<script>
	let one = 1;
</script>

<div class="number">
	<One {one}/>
</div>

<style>
	.number { background-color: black };
</style>

<!-- One.svelte ->
<script>
	export let one;
</script>

<div class="one">{one}</div>

<style>
	.one { color: white };
</style>
@dummdidumm dummdidumm added the feature request New feature or request label Jun 14, 2020
dummdidumm pushed a commit to dummdidumm/language-tools that referenced this issue Jul 2, 2020
dummdidumm added a commit that referenced this issue Jul 6, 2020
* (feat) extract component refactoring

#187

* (feat) extract component now with prompt for path/name

- Is no longer part of the getCodeActions now, which might also improve performance a little

* tests

* docs

* lint

* (feat) update relative imports
@dummdidumm
Copy link
Member

This now exists (for a while) to some extend. Here's what you need to do:

  1. Select a range you want to export
  2. Fire up the command window
  3. "Svelte: Extract Component"
  4. Choose name/location relative to current component

What's missing:

  • no semantic analysis if selection range is valid
  • no analysis which script/style code should be transferred

@milkbump
Copy link
Author

milkbump commented Jan 9, 2021

Thanks for the heads-up!!

When I saw the initial PR, my immediate impulse was to try right-clicking. It didn't work, so I assumed it just hadn't shipped yet, and I forgot about it for a while. This was based on my expectations from extracting/refactoring support in JavaScript.

Did you consider implementing this via the context-menu?

@ziontee113
Copy link

Hi, I wonder if it's possible to have this functionality in the native LSP protocol code actions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants