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

Check if file exists before loading editorconfig settings #3018

Merged
merged 1 commit into from
Sep 8, 2019

Conversation

mogenson
Copy link
Contributor

I noticed when using the recommended editorconfig hook from the Wiki, that the editorconfig.kak script failed for buffers that were not real files. For example *scratch*, *debug*, *make*, *grep*, etc.

This PR adds a check in the editorcofig-load command to see if a file exists before calling the external editorconfig utility.

It also moves the remove-hooks line to after the valid file check and directly prints the command to add a remove trailing whitespace hook from awk (if necessary). This prevents an invalid file from clearing a hook and removes the need to store an option and reference it from a spawned shell in the later hook.

@mogenson
Copy link
Contributor Author

Hello, does this PR seem reasonable?

print "autowrap-enable"
print "add-highlighter window/ column %sh{ echo $((" max_line_length "+1)) } default,bright-black"
file="${1:-$kak_buffile}"
if [ -f "$file" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [ -f "$file" ]; then
if [ -e "$file" ]; then

To handle symlinks.

@mawww
Copy link
Owner

mawww commented Aug 20, 2019

Hello,

I dont think the logic should be to disable editorconfig for non-existing files, what we want is to disable it for non-file buffers (scratch buffers in Kakoune lingo). One distinction between file and non-file buffers is that $kak_buffile will be an absolute path for any file buffer, but just match the buffer name for a non-file buffer, so I suspect ignoring all buffers where $kak_buffile does not start with a slash would do the trick.

EDIT: Also, sorry for the delay in taking a look at that.

Check if buffile is a full path by checking for the beginning
'/' character in editorconfig-load command. This avoids a parsing
error from feeding a *scratch*/*debug*/*grep*/etc. buffer name to the
editorcofig command. Don't clear editorconfig hooks until after checking
for a valid bufffile path. This way, opening the *debug* buffer will
not clear the hooks from a previously parsed .editorconfig file. If
trim_trailing_whitespace is true, print the hook directly from awk. This
removes the need to save a editorcofig_trim_trailing_whitespace option.

Note: Setting the max_line_length requires a window to be created.
Therefore, a global hook to load .editorconfig settings should be:

    hook global WinCreate .* %{editorconfig-load}
@mogenson
Copy link
Contributor Author

I've updated this PR to check if $kak_buffile begins with a / character.

As mawww suggested, this should work for all unix environments. On Cygwin, it appears you can specify a path that begins with a drive letter (ex: cd c:/some/path) but Cygwin will report back a path that begins with / (ex: /cygdrive/c/some/path).

Lastly, if you want to keep the editorconfig.kak script as-is. I've been using a WinCreate hook with regex to filter out buffer names that start with a *. This could be added to the wiki.

hook global WinCreate ^[^*]+$ %{ editorconfig-load } # no *buffers*

@mawww mawww merged commit 37ded5e into mawww:master Sep 8, 2019
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

Successfully merging this pull request may close these issues.

3 participants