z0mbix / vim-shfmt

Vim plugin for shfmt (https://github.com/mvdan/sh)
MIT License
135 stars 11 forks source link

Running Shfmt only works with saved copy of script #5

Closed andrewimeson closed 5 years ago

andrewimeson commented 5 years ago

Shfmt silently eats changes in the buffer that have not been written to the file.

Steps to Reproduce:

  1. Create a new file vim shfmt-test.bash
  2. Put initial contents into the file, and write it with :w
    #!/usr/bin/env bash
    # Reproducce vim-shfmt issue
    for bacon in "turkey" "pig"; do
    echo $bacon | grep -q "pig" &&
        echo "$bacon bacon is real bacon" ||
        echo "$bacon bacon is not real bacon"
    done
  3. Run :Shfmt, get the properly reformatted script
    #!/usr/bin/env bash
    # Reproducce vim-shfmt issue
    for bacon in "turkey" "pig"; do
    echo $bacon | grep -q "pig" &&
    echo "$bacon bacon is real bacon" ||
    echo "$bacon bacon is not real bacon"
    done
  4. Make some more edits, but do not write them to the file
    
    #!/usr/bin/env bash
    # Reproducce vim-shfmt issue
    for bacon in "turkey" "pig"; do
    echo $bacon | grep -q "pig" &&
    echo "$bacon bacon is real bacon" ||
    echo "$bacon bacon is not real bacon"
    done

echo "really important info that I forgot to add:" echo "bacon goes really well on pizza"

4. Run `:Shfmt` and watch the unwritten edits disappear
```bash
#!/usr/bin/env bash
# Reproducce vim-shfmt issue
for bacon in "turkey" "pig"; do
  echo $bacon | grep -q "pig" &&
    echo "$bacon bacon is real bacon" ||
    echo "$bacon bacon is not real bacon"
done

Environment: macOS 10.14.2 vim 8.1 (from Homebrew) vim-shfmt from commit 5f1fe6a with .vimrc options:

let g:shfmt_extra_args = '-i 2 -ci -s'
alpayne commented 5 years ago

vim-shfmt is indeed running against the last saved file instead of running as a filter. I've submitted a pull request with changes to have it run as a filter instead.