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 command that allows easy adding new snippets #23

Merged
merged 9 commits into from
Feb 21, 2019
Merged

add command that allows easy adding new snippets #23

merged 9 commits into from
Feb 21, 2019

Conversation

andreyorst
Copy link
Contributor

usage:

:snippets-add-snippet "hw" "Hello, World!" "c" # to add `hello world' snippet to `c' filetype
:snippets-add-snippet "hw" "Hello, World!" # to add `hello world' snippet to current active filetype
:snippets-add-snippet # to ask parameters via prompt

This command accepts 0 or 2 or 3 parameters. In case of 3 parameters, a new snippet will be created for desired filetype. In case of two parameters, active filetype will be used, and if there's no filetype, snippets will be created in global scope .*. If there's no parameters user will see prompt:

Trigger:

And If user inputs a trigger, new prompt appears:

Snippet Description:

In this case filetype isn't asked.

Copy link
Owner

@occivink occivink left a comment

Choose a reason for hiding this comment

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

I like the idea, can you move it to the snippets-directory.kak file? Also there seems to be some mismatch between snippets-add and snippets-create, I'm guessing you renamed those after testing :)

snippets.kak Outdated Show resolved Hide resolved
snippets.kak Outdated Show resolved Hide resolved
snippets.kak Outdated Show resolved Hide resolved
snippets.kak Outdated Show resolved Hide resolved
snippets.kak Outdated Show resolved Hide resolved
snippets.kak Outdated Show resolved Hide resolved
@andreyorst
Copy link
Contributor Author

andreyorst commented Feb 19, 2019

I'm guessing you renamed those after testing :)

Oi! I've thought that it's bit long to type. Also fuzzy matching allows to only type sadd instead of sncrea which is harder in terms of fingermoving :)

@andreyorst
Copy link
Contributor Author

andreyorst commented Feb 19, 2019

I want to add a hook to reload snippet dir, in a fashion that it will track only snippet buffer. I've used hook global BufWritePost snippet-path %{} for that but it doesn't seem to work. I don't see why.

Edit: nevermind, I've fixed the hook. However snippets don't reload for some reason. But hook executes.

@andreyorst
Copy link
Contributor Author

andreyorst commented Feb 19, 2019

Seems like snippet-directory-reload is executed in buffer scope of edited snippet. Should It be global or should I track last buffer wrom where the snippets-add-snippet were executed.

Also should I use tools-client for this command?

@occivink
Copy link
Owner

I've used hook global BufWritePost snippet-path %{} for that but it doesn't seem to work. I don't see why.

I think it would be preferable to setup a hook with buffer scope on the new buffer instead of relying on the filter.

Seems like snippet-directory-reload is executed in buffer scope of edited snippet. Should It be global?

I'm not sure what you mean, the snippets-directory-reload should only affect global scope by setting a filetype hook in it

@andreyorst
Copy link
Contributor Author

I think it would be preferable to setup a hook with buffer scope on the new buffer instead of relying on the filter.

Good idea indeed.

I'm not sure what you mean, the snippets-directory-reload should only affect global scope by setting a filetype hook in it

I can't use new snippet after hook executed.

@andreyorst
Copy link
Contributor Author

Ah, now I see it. I can't expand snippets after snippets-directory-reload, no matter how I execute it.

@occivink
Copy link
Owner

occivink commented Feb 19, 2019

Ah, now I see it. I can't expand snippets after snippets-directory-reload, no matter how I execute it.

Yeah it won't work (yet, see TODO) for existing buffers, but if you create a new buffer with the target filetype the new snippets should be taken into account (though I haven't really tested it...). Don't forget to use snippets-info to easily see which snippets are available.

@andreyorst
Copy link
Contributor Author

Current implementation allows only balanced strings as trigger or description fields. Should I fix it?

@occivink
Copy link
Owner

Yes please. To reduce the escaping needed, you should move the entire eval call inside menu to a separate command

@andreyorst
Copy link
Contributor Author

I don't see how it helps

@andreyorst
Copy link
Contributor Author

I can wrap this into awk script, but there shouldn't be any performance issues because of three sed calls, since menus will never be really big.

@occivink
Copy link
Owner

Yeah sed should be fine, especially if you wrap it with [ $var contains ' ] && var=$(echo $var | sed ...)

Regarding having another command you would still need to double up quotes, but at least you wouldn't have to octuple them (in a context of 'eval ''edit ''''$var'''' '' '. But feel free to it do your way. Or I can take care of this if you want.

@andreyorst
Copy link
Contributor Author

Yeah sed should be fine, especially if you wrap it with [ $var contains ' ] && var=$(echo $var | sed ...)

Well this means that there will be 3 check + 3 seds for each variable in case all true, and I'll need to break up single sed command into three, to be able to check those separately, because there's no way to use [ -z "${trigger##*&*\!*?*}" ] pattern, it won't work.

Regarding having another command you would still need to double up quotes

I don't double quotes in any way here. It handles those fine by itself.

But feel free to it do your way. Or I can take care of this if you want.

I've tested it with a bunch of weird strings passed as trigger and description, and it works fine. I suppose this command is already fine. The only thing that I suppose should be simplified, is it accepting 2 arguments instead of 3. This way it can be reused for snippets-edit-snippet command, that I'm planning to add. But I'm not sure if this is necessary to reuse this particular function.

@occivink
Copy link
Owner

I'm sorry but I don't think I'm being unreasonable when I say that the escaping logic in there is too complex, you have to keep track of which delimiters to double and where. I don't deny that it's possible to make it work like that, but I find it hard to understand (and consequently extend in the future).

Here is an alternative approach that only relies on single quotes for escaping and where the complexity won't increase as we add logic in the menu command. It may be that we have to add more arguments to pass to the new command, but it will simply be a matter of quadrupling quotes in them, like with filename.
I hope we can agree that this is a simpler approach, even if takes more lines to implement.

...
    printf 'menu -auto-single --'
    while [ $# -gt 0 ]; do
        dir="$1"
        # double-up single quotes if there's any
        [ -z "${dir##*'*}" ] && dir=$(printf %s "$dir" | sed -e "s/'/''/g")
        printf " '%s' " "$dir"

        filename="${1}/${filetype}/${trigger} - ${description}"
        # quadruple-up single quotes if any
        [ -z "${filename##*'*}" ] && filename=$(printf %s "$filename" | sed -e "s/'/''''/g")
        printf "'snippets-whatever ''%s'' '" "$filename"

        shift
    done
}}

define-command snippets-whatever -hidden -params 1 %{
    nop %sh{ mkdir -p $(dirname "$1") }
    edit %arg{1}
    hook -group snippets-add-watchers buffer BufWritePost .* "snippets-directory-reload"
    # maybe syntax highlighting and more
}

@andreyorst
Copy link
Contributor Author

andreyorst commented Feb 19, 2019

I hope we can agree that this is a simpler approach, even if takes more lines to implement.

of course! You're the boss here after all, hehe.

I'll redo it that way soon.

@andreyorst andreyorst mentioned this pull request Feb 19, 2019
@occivink occivink merged commit c8d72d7 into occivink:master Feb 21, 2019
@occivink
Copy link
Owner

Looks good, thanks

@andreyorst andreyorst deleted the add-snippet-command branch February 21, 2019 17:47
@andreyorst
Copy link
Contributor Author

If you don't mind, later I'll add a command that allows edit snippet via trigger only. So user could just type snippets-edit-snippet class and it will open a class snippet, or a menu with variants if there are several class snippets for current filetype. Right now user need to input description to edit snippet which isn't that convenient.

@occivink
Copy link
Owner

Ah right, that makes sense for editing existing snippets, I was thinking that the usecase was mainly creating new ones

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.

None yet

2 participants