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

Neoformat with svelte is causing unwritten changes to be undone #375

Open
itmecho opened this issue Sep 27, 2021 · 2 comments
Open

Neoformat with svelte is causing unwritten changes to be undone #375

itmecho opened this issue Sep 27, 2021 · 2 comments

Comments

@itmecho
Copy link

itmecho commented Sep 27, 2021

I have a basic svelte component:

<script>
	export let name = '';
	export let value = '';
	export let errorMessage = '';
	export let required = false;
</script>

<div class="pb-4">
	<input class="w-full p-2 bg-gray-200" type="input" {name} bind:value {required} />
	{#if value.length > 0 && errorMessage != ''}
		<p>{errorMessage}</p>
	{/if}
</div>

If I make a change and then run Neoformat, that change gets undone. For example, changing <p>{errorMessage}</p> to <p>Error: {errorMessage}</p> then running Neoformat reverts it back to <p>{errorMessage}</p>.

Here's the verbose output:

Neoformat: ['<script>', '^Iexport let name = '''';', '^Iexport let value = '''';', '^Iexport let errorMessage = '''';', '^Iexport let required = false;', '</script>', '', '<div class="pb-4">', '^I<input clas
s="w-full p-2 bg-gray-200" type="input" {name} bind:value {required} />', '^I{#if value.length > 0 && errorMessage != '''' && errorMessage != ''test''}', '^I^I<p>Error: {errorMessage}</p>', '^I{/if}', '</div
>']
Neoformat: node_modules/.bin/prettier --stdin-filepath --parser=svelte --plugin-search-dir=. "/home/iain/src/svelte-form/src/components/TextInput.svelte" 2> /tmp/neoformat/stderr.log
Neoformat: using stdin
Neoformat: ['<script>', '^Iexport let name = '''';', '^Iexport let value = '''';', '^Iexport let errorMessage = '''';', '^Iexport let required = false;', '</script>', '', '<div class="pb-4">', '^I<input clas
s="w-full p-2 bg-gray-200" type="input" {name} bind:value {required} />', '^I{#if value.length > 0 && errorMessage != '''' && errorMessage != ''test''}', '^I^I<p>{errorMessage}</p>', '^I{/if}', '</div>']
Neoformat: [0]
Neoformat: 0
Neoformat: stderr output redirected to file/tmp/neoformat/stderr.log
Neoformat: []
Neoformat: prettier formatted buffer

If however, I run noautocmd w first, then Neoformat it works perfectly

@sheodox
Copy link
Contributor

sheodox commented Oct 30, 2021

I ran into the same problem. Thanks for this issue, I couldn't for the life of me figure out what was causing this.

I'm using the autocmd specified in #134, but with your noautocmd w thing I was able to fix this for myself by using:

autocmd BufWritePre * try | undojoin | silent noautocmd w | Neoformat | catch /^Vim\%((\a\+)\)\=:E790/ | finally | silent Neoformat | endtry

Edit: actually this isn't ideal either, it gives you the same issue but in reverse. If you try and undo then it'll redo your changes on save.

Edit 2: I figured out the issue and submitted a PR (auto linked below)

kodiakhq bot pushed a commit that referenced this issue Oct 30, 2021
See #375. When making changes and then saving an unsaved buffer (like one would do with a BufWritePre autocmd to format on save) on a Svelte file the changes that haven't yet been saved get undone.

I noticed the Svelte formatter used a different order of prettier arguments than all of the other formatters that use prettier. The `--stdin-filepath` argument had a couple other arguments before the file path. I reordered them so the file path immediately follows `--stdin-filepath` and that seems to have fixed the issue for me.

I'm pretty sure what's happening is that Prettier ignores the buffer passed as stdin because it doesn't see the file path in the arguments as the value to `--stdin-filepath`, but instead as the file path to the file to format, so it just ignores the buffer text sent over stdin and loads the file from the filesystem which naturally won't have the buffer's changes as they haven't been saved yet. (You can see this by running the commands in the two different orders with `--loglevel debug` to see what it interpreted the cli arguments as.)

The current order is doing something like this:
![image](https://user-images.githubusercontent.com/3468630/139522331-e805670f-bf0e-488a-a3e5-6d0232d8696a.png)

The changed order is doing something like this:
![image](https://user-images.githubusercontent.com/3468630/139522352-74da6539-c4bd-4812-b9b0-e64fcb545b3d.png)
@sheodox
Copy link
Contributor

sheodox commented Oct 30, 2021

@itmecho This should work now.

nikvdp pushed a commit to nikvdp/neoformat that referenced this issue May 15, 2022
See sbdchd#375. When making changes and then saving an unsaved buffer (like one would do with a BufWritePre autocmd to format on save) on a Svelte file the changes that haven't yet been saved get undone.

I noticed the Svelte formatter used a different order of prettier arguments than all of the other formatters that use prettier. The `--stdin-filepath` argument had a couple other arguments before the file path. I reordered them so the file path immediately follows `--stdin-filepath` and that seems to have fixed the issue for me.

I'm pretty sure what's happening is that Prettier ignores the buffer passed as stdin because it doesn't see the file path in the arguments as the value to `--stdin-filepath`, but instead as the file path to the file to format, so it just ignores the buffer text sent over stdin and loads the file from the filesystem which naturally won't have the buffer's changes as they haven't been saved yet. (You can see this by running the commands in the two different orders with `--loglevel debug` to see what it interpreted the cli arguments as.)

The current order is doing something like this:
![image](https://user-images.githubusercontent.com/3468630/139522331-e805670f-bf0e-488a-a3e5-6d0232d8696a.png)

The changed order is doing something like this:
![image](https://user-images.githubusercontent.com/3468630/139522352-74da6539-c4bd-4812-b9b0-e64fcb545b3d.png)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants