تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Automating with Snippets0:00

Do you ever find yourself typing the same code structures over and over throughout the day? Perhaps you're writing tests and constantly writing public, function, test, blah blah blah, and then the parenthesis and the squirrely braces? Well, let's look at how we can leverage the plugins we've already installed to automate this away. Let's jump back into our plugins.lua file, and we'll come down to where we installed lua-snp. So, lua-snp was a requirement of our completion plugin. As far as I understand, it needs a snippet engine to insert code onto the page.

Creating Snippet Files1:06

directory. So let's go ahead and we'll save this, and now we'll go ahead and create some snippets. So I'm going to open up the nvim tree on the side here, and let's come all the way up to our nvim directory. Inside here, I'm going to place a directory called snippets. And then inside here, I'm going to create a file called php.snippets. And the php at the start here is important. It needs to match the file type of the snippets we're writing so that lua-snp can load them in the right place. So now that we've got this file, I'm going to paste in a handful of snippets.

Understanding Snippet Syntax1:35

in the right place. So now that we've got this file, I'm going to paste in a handful of snippets. So this is the general structure of the snipmate snippets. They all start with the snippet keyword, and then we have our trigger text. And this is what we'll type to trigger our snippet. We can then give it an optional description. So in this case, this one is to create a public method. And then under here, we have the snippet itself, and it needs to be indented. So in this case, I'm creating a public function, and then we have these little placeholders. So the first one here is where my cursor will jump to first.

So in this case, I'm creating a public function, and then we have these little placeholders. So the first one here is where my cursor will jump to first. And then when I press tab, it'll jump me inside the parens here. And then when I press tab again, it'll jump me to the return type. And then finally, this zero one is special. So this is where it will go to at the end. But once it reaches this point, it knows that it's no longer inside snippet mode, and it'll return the tab key back to its normal behavior. I've set up one to do a protected method and one to do a private method as well. I also have these handy ones for creating tests.

I've set up one to do a protected method and one to do a private method as well. I also have these handy ones for creating tests. So this one is testT, and this one will create a test with the test prefix. And in this case, I don't bother putting any placeholders inside the function or the return type. I just have ones to fill out the test name and then to jump straight to the body. I also have one called testA, and this will do tests using the test annotation style instead. So in this case, it'll insert our little test annotation comment, and then it doesn't need to put the test prefix on here. And I work in different code bases that use the different standards, so it's nice to have.

Using Snippets in Laravel3:01

to put the test prefix on here. And I work in different code bases that use the different standards, so it's nice to have both snippets available. So let's go ahead and save this file, and then we'll jump over to our Laravel project. Let's open up NeoVim, and let's start by going to our User model. And let's come down to the bottom here, and let's try typing pub. And you'll see already that I'm getting some completion here. If I go all the way to pubf, you'll see the only option left is this snippet. So if I tab onto it and then press Enter, it will expand the snippet. So now I could type my method name, so promote, for example.

So if I tab onto it and then press Enter, it will expand the snippet. So now I could type my method name, so promote, for example. I can hit Tab, and then I could write level and title. I could push Tab again, and maybe I want to put void here, and then tab one more time into the method body where I could write the rest of my code. Now let's also have a look at that test one. So let's jump into the exampleTest that comes with Laravel. And let's come down the bottom here, and we'll say testT, and we'll tab onto this one. And then we could say testItDoesSomething. We'll come down and also do one for testA, and you'll see this time we get the test annotation.

Snippet Workflow Takeaways4:02

And then we could say testItDoesSomething. We'll come down and also do one for testA, and you'll see this time we get the test annotation without the test prefix, so it does something. So hopefully you see the power of snippets. As you're coding throughout the day, take note of any times when you're doing anything repetitive like this, and think about whether you could create a snippet to automate that for you. That's all for this one. In the next lesson, we'll talk about a refactoring tool called Factor. So long, everybody.

Writing SnippetsSnippet Completion

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟