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

Add trim_file command and trim_file_on_save configuration option #1481

Open
poliorcetics opened this issue Jan 11, 2022 · 13 comments · May be fixed by #8366
Open

Add trim_file command and trim_file_on_save configuration option #1481

poliorcetics opened this issue Jan 11, 2022 · 13 comments · May be fixed by #8366
Assignees
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements

Comments

@poliorcetics
Copy link
Contributor

Describe your feature request

I know there is an trim_selection command but using it breaks the current selection, which is a not very helpful. It's also not very practical to do it for each opened file when ending a somewhat long session.

@poliorcetics poliorcetics added the C-enhancement Category: Improvements label Jan 11, 2022
@poliorcetics
Copy link
Contributor Author

This would maybe need an additional config option to not do it for heavy files, since trimming could be quite long on files of 100k lines ? Even if there is no trimming to be done, each line must be checked individually (though that should be parallel once the initial reading is done).

@poliorcetics
Copy link
Contributor Author

poliorcetics commented Jan 11, 2022

Add-on: maybe a default option + a per language one since markdown uses "two spaces at end of line" as code for "insert visible line break here" but I don't know of any other commonly used language that does something like this

@EpocSquadron
Copy link
Contributor

It's worth noting that trim_selection is intended to literally change the selections to exclude whitespace from their ends. It does not actually remove the whitespace from the document.

Currently, we rely on the LSP implementation to format on document save, which will often include trimming whitespace from the end of lines. If the LSP is not present or doesn't implement formatting, then you are currently stuck doing it manually. There is discussion of what to do about this elsewhere but I'm on mobile, may find it later and comment.

In the meantime, you can select the entire document (%), then pipe to a formatter on the command line (|prettier<enter> for example).

@EpocSquadron
Copy link
Contributor

#1396 would somewhat alleviate this issue.

@kirawi kirawi added the A-helix-term Area: Helix term improvements label Jan 11, 2022
@Omnikar
Copy link
Contributor

Omnikar commented Jan 11, 2022

Describe your feature request

I know there is an trim_selection command but using it breaks the current selection, which is a not very helpful. It's also not very practical to do it for each opened file when ending a somewhat long session.

As for applying some operation to all the files in a long session, macros can probably help automate that.

@Omnikar
Copy link
Contributor

Omnikar commented Jan 11, 2022

Here's a macro that should trim trailing whitespace from the end of all lines in the current buffer, save it, then close it. If you yank the macro into the @ register, you should be able to just hold q (or type a large enough number followed by q) to apply the macro to every open buffer.

%<A-s>Xs\s+\n<ret>vhvd:w<ret>:bc<ret>

@poliorcetics
Copy link
Contributor Author

#1396 would somewhat alleviate this issue.

I don't like this solution much; it forces users to have a default language server on every new install of helix and to wait for it to spin up to quickly trim a file that failed an editor-config check.

@paveloom
Copy link

paveloom commented Apr 20, 2022

Using the current version from the master branch, you can do the following:

# ~/.config/helix/config.toml
# <...>
[keys.normal]
C-s = [
    "save_selection",
    "select_all",
    # Delete trailing whitespace from end of each line
    ":pipe sed 's/[ \t]*$//'",
    # Delete trailing blank lines (including whitespace) from end of the buffer
    ":pipe awk '/^\\s*$/ {b=b $0 \"\\n\"; next;} {printf \"%s\",b; b=\"\"; print;}'",
    "collapse_selection",
    "jump_backward",
    "commit_undo_checkpoint",
    ":write",
]
# <...>

Current issues:

References:

@vasylnakvasiuk
Copy link

vasylnakvasiuk commented Nov 22, 2022

Would be nice to have similar to VS Code flags:

image

@rcorre
Copy link
Contributor

rcorre commented Feb 7, 2023

Note that if you have git installed, you can use git stripspace as a language formatter:

[[language]]
name = "gdscript"
language-server = { command = "nc", args = ["localhost", "6005"], language-id = "gdscript" }
formatter = { command = "git", args = ["stripspace"] }

As noted by @DannyJJK, this has some other consequences. Use the sed suggestions elsewhere in this thread.

@kirawi kirawi linked a pull request Sep 24, 2023 that will close this issue
@DannyJJK
Copy link

DannyJJK commented Nov 3, 2023

Note that if you have git installed, you can use git stripspace as a language formatter:

[[language]]
name = "gdscript"
language-server = { command = "nc", args = ["localhost", "6005"], language-id = "gdscript" }
formatter = { command = "git", args = ["stripspace"] }

I'm not sure if that's a good idea, the documentation for this says:

NOTE: This is intended for cleaning metadata, prefer the --whitespace=fix mode of git-apply[1] for correcting whitespace of patches or files in the repository.

https://git-scm.com/docs/git-stripspace#_description

@Kurt-Shiwz
Copy link

Kurt-Shiwz commented Nov 17, 2023

Using the current version from the master branch, you can do the following:

# ~/.config/helix/config.toml
# <...>
[keys.normal]
C-s = [
    "save_selection",
    "select_all",
    # Delete trailing whitespace from end of each line
    ":pipe sed 's/[ \t]*$//'",
    # Delete trailing blank lines (including whitespace) from end of the buffer
    ":pipe awk '/^\\s*$/ {b=b $0 \"\\n\"; next;} {printf \"%s\",b; b=\"\"; print;}'",
    "collapse_selection",
    "jump_backward",
    "commit_undo_checkpoint",
    ":write",
]
# <...>

Current issues:

References:

thx, i try to keep the file has new line on end of file, it work.

C-s = [
    "save_selection",
    "select_all",
    # Delete trailing whitespace from end of each line
    ":pipe sed 's/[ \t]*$//'",
    # Delete trailing blank lines (including whitespace) from end of the buffer
    ":pipe awk '/^\\s*$/ {b=b $0 \"\\n\"; next;} {printf \"%s\",b; b=\"\"; print;}'",
    "collapse_selection",
    "goto_last_line",
    "add_newline_below",
    "commit_undo_checkpoint",
    ":write",
]

I want know is there some other way to do that?

@rcorre
Copy link
Contributor

rcorre commented Mar 14, 2024

FWIW, if you use sed as a formatter in languages.toml, you don't need all the selection/checkpoint stuff:

formatter = { command = "sed", args = ["s/[ \t]*$//"] }

lukehsiao added a commit to lukehsiao/dotfiles that referenced this issue Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements
Projects
None yet
9 participants