neoformat icon indicating copy to clipboard operation
neoformat copied to clipboard

Neoformat with svelte is causing unwritten changes to be undone

Open itmecho opened this issue 4 years ago • 2 comments

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

itmecho avatar Sep 27 '21 10:09 itmecho

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)

sheodox avatar Oct 30 '21 04:10 sheodox

@itmecho This should work now.

sheodox avatar Oct 30 '21 16:10 sheodox