Expand the vim plug-in by yourself-code_complete.vim

Source: Internet
Author: User
Document directory
  • Plugin Introduction
  • Plugin installation
  • Plug-in usage
  • Plugin limitations
  • Extension
  • Summary

Expand the vim plug-in by yourself-code_complete.vim

By Ma Dongliang (cream Loki)

One man's war (http://blog.csdn.net/MDL13412)

Plugin Introduction

Code_complete.vim is a common plug-in that provides functions such as completing function parameters and inserting code snippets. Currently, the plug-in author has migrated the project to GitHub for hosting.

Is the official demo (function parameter complementing function should be used with ctags ):

Install the plugin and copy code_complete.vim ~ /. Vim/plugin directory. Use ctags to generate the tags file (run in the Code directory and parse the current folder recursively). The Code is as follows:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ .

Change the generated tags file name to xxx_tags and copy it ~ /. Vim/tags directory, in ~ /. Add the following code to vimrc:

set tags+=~/.vim/tags/xxx_tags
Plug-in usage

This plug-in is easy to use. You only need to use the <tab> key to trigger the corresponding functions. The following describes the specific usage:

Note

The code_complete plug-in provides two types of Annotations:/***/Standard C language annotations, the other is the/*** doxygen-style document annotation that is used to annotate a line after a line (this plug-in does not provide the function to automatically generate function document annotation, this function allows you to use the more professional plug-in doxygentoolkit. ).

Preprocessing command

Header file inclusion

C Logic Structure

Main Function

Namespace

Function parameter completion

Plugin limitations

Through the above demonstration, we found some limitations of this plug-in:

  • The auto-completion code style is different from the one we use (the above example has changed to my own code style ).
  • If else structure and switch structure branches cannot be manually specified;
  • No function template is available for user-defined functions;
Plug-in extension custom code style

The following describes the if else structure. First, we can view the code before the change:

let g:template['c']['ife'] = "if( ".g:rs."...".g:re." )\<cr>{\<cr>".g:rs."...".g:re."\<cr>} else\<cr>{\<cr>".g:rs."...". g:re."\<cr>}"

Let's check the style in VIM:

In the brackets of if, there is a space between the condition expression and the brackets, which is different from our style. We can change it according to the following code:

let g:template['c']['ife'] = "if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} else\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"

Note: In vimscript, a string can be a string in the form of "or". The string is connected using.. \ <CR> is the carriage return on the keyboard. \ is used to escape the string. To view the key ing in Vim, use: Help keycode in vim. Let G: [XXX] In Template ['C'] ['xxx'] is the trigger sequence of code completion.

For other logical structures, spaces are mainly different from our style and can be customized according to the above principles.

If else structure and switch structure Customization

Below I will use the if else structure to demonstrate my extended plug-ins. The Code will be provided below:

The extended if else structure is used as follows:

IFE [x] <tab> in insert mode, enter Ife, followed by the number of else if () branches, and press <tab>.

I expanded 0 ~ The following section describes the branches of V9 ~ 3. Code definition:

let g:template['c']['ife0'] = "if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} else\<cr>{\<cr>".g:rs."...".            \g:re."\<cr>}"let g:template['c']['ife1'] = "if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else\<cr>{\<cr>".g:rs."...".            \g:re."\<cr>}"let g:template['c']['ife2'] = "if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else\<cr>{\<cr>".g:rs."...".            \g:re."\<cr>}"let g:template['c']['ife3'] = "if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else if (".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>} ".            \"else\<cr>{\<cr>".g:rs."...".            \g:re."\<cr>}"

I believe that the readers who have the foundation of programming have already seen the law. The number of else if branches is equal to the number of \ "else if (". g: Rs. "... ". g: re. ") \ <CR >{\ <CR> ". g: Rs. "... ". g: re. "\ <CR> }".

Let's take a look at the switch Demonstration:

The extended switch structure is used as follows:

Switch [x] <tab> in insert mode, enter the switch, followed by the number of branches of the case, and then press <tab>.

I expanded 1 ~ The following section describes the branches of V9 ~ 3. Code definition:

let g:template['c']['switch1'] = "switch (".g:rs."...".g:re.")\<cr>{\<cr>case ".            \g:rs."...".g:re.":\<cr>break;".            \"\<cr>default:\<cr>break;\<cr>}"let g:template['c']['switch2'] = "switch (".g:rs."...".g:re.")\<cr>{\<cr>case ".            \g:rs."...".g:re.":\<cr>break;".            \"\<cr>case ".g:rs."...".g:re.":\<cr>break;".            \"\<cr>default:\<cr>break;\<cr>}"let g:template['c']['switch3'] = "switch (".g:rs."...".g:re.")\<cr>{\<cr>case ".            \g:rs."...".g:re.":\<cr>break;".            \"\<cr>case ".g:rs."...".g:re.":\<cr>break;".            \"\<cr>case ".g:rs."...".g:re.":\<cr>break;".            \"\<cr>default:\<cr>break;\<cr>}"
Function Template

Next let's take a look at the extended results:

Through the example, we can see that the function template reduces the number of inputs and the number of cursor moves, which improves the programming efficiency. The following describes the usage:

F [x] <tab> enter F in insert mode, followed by the number of function parameters, and then press <tab>.

0 ~ Code for the three parameters:

let g:template['c']['f0']=g:rs."...".g:re."()\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"let g:template['c']['f1']=g:rs."...".g:re."(".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"let g:template['c']['f2']=g:rs."...".g:re."(".g:rs."...".g:re.", ".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"let g:template['c']['f3']=g:rs."...".g:re."(".g:rs."...".g:re.", ".g:rs."...".g:re.", ".g:rs."...".g:re.")\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"
Summary

This plug-in currently extends the C language, for C ++ extension has not been completed, after the extension plug-in: http://download.csdn.net/detail/mdl13412/4674025

The extension of this plug-in is not difficult. Readers can extend common code fragments to improve coding efficiency. In addition, the editor used by programmers should have good scalability :-)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.