diff --git a/README.md b/README.md index eb2dc8c2f3702d33bce5479ad8b561b95003967e..59ff8ef8547e82a1216783e389c4cecd2006c2ff 100644 --- a/README.md +++ b/README.md @@ -61,41 +61,60 @@ ![文件查找](https://gitee.com/HGtz2222/VimForCpp/raw/master/screenshot/%E5%BF%AB%E9%80%9F%E6%9F%A5%E6%89%BE%E6%96%87%E4%BB%B6.gif) +### 变量重命名 +TODO + +### 查找函数调用位置 +TODO + +### 快速查看函数声明 +TODO + +## 包含的插件列表 +TODO + ## 快速使用 +目前IDE相关快捷键分为五个类别: -### 普通模式下快捷键 +前缀键为 "空格" +第二键指定分类 +第三键指定功能 -``` - 快速运行(仅限单个文件) - 打开函数列表 - 打开文件管理器 - 文档注释 - 检查文件编译错误(仅限 c++) - 尝试匹配代码片段 - 跳转到函数声明/定义 - 返回到之前的代码 - <;+m> 高亮/取消高亮光标下的文本 - 快速折叠/取消折叠代码 -``` +分类键和功能键按照 "asdfg qwert" 的方式排序. +后续会支持快捷键提示 -### 插入模式下快捷键 +例如: +1. 跳转到函数定义: ``` - jk 回到普通模式并保存 - H J K L 快速移动窗口 - T 快速切换.c文件和.h文件 - e 以标签页打开文件 +fa ``` -### 可视模式下快捷键 - +2. 变量重命名 ``` - < 向左缩进 - > 向右缩进 - S+( 给选中文本外面加一层括号(带空格) - S+) 给选中文本外面加一层括号(不带空格) +fd ``` +快捷键具体定义参考 vim/init.vim 中按键映射部分. + +**注意**: 当前的快捷键设定处在测试阶段, 可能会有大规模变动 + + +### 窗口(a) +TODO + +### 查找(s) +TODO + +### 调试(d) +TODO + +### 语义(f) +TODO + +### 格式(g) +TODO + ## 关于图标显示 基于 vim-devicons 展示图标. vim-devicons依赖 Nerd Font @@ -109,7 +128,7 @@ vim-devicons依赖 Nerd Font 其他字体可以参考文件 **xshell可用字体** -注意: 为了避免字体兼容性的问题, 显示图标功能默认已经关闭. 如需打开, 修改 ~/.vimrc 中的 +注意: 如果出现图标不能正确显示的问题, 可以注释掉 vim-devicons " Plug 'ryanoasis/vim-devicons' diff --git a/cquery/config/cquery.config b/cquery/config/cquery.config index 560ad7fcc07dcaffc2ec298ccc7871f7569bac6e..19e6f4618d53559e5c354d0697a2831bc88e9d0d 100644 --- a/cquery/config/cquery.config +++ b/cquery/config/cquery.config @@ -5,5 +5,5 @@ # Includes -I/usr/include --I/usr/include/c++/4.8.5 +-I/usr/include/c++/4.8.2 -I. diff --git a/cquery/config/settings.json b/cquery/config/settings.json index c6098a2a572d57e2f0c42b62b008d4fa5fba792b..58fe08f9fc8485523ed298207279bc071e818fad 100644 --- a/cquery/config/settings.json +++ b/cquery/config/settings.json @@ -1,6 +1,6 @@ { "initializationOptions": { - "cacheDirectory": "/tmp/cquery" + "cacheDirectory": "/tmp/cquery/cache" } } diff --git a/fonts/CONSOLAS_NERD_FONT_MONO.TTF b/fonts/CONSOLAS_NERD_FONT_MONO.TTF new file mode 100644 index 0000000000000000000000000000000000000000..1d37bf6ce48bc924997182d6765b0ded7a5d061b Binary files /dev/null and b/fonts/CONSOLAS_NERD_FONT_MONO.TTF differ diff --git a/nvim b/nvim new file mode 100755 index 0000000000000000000000000000000000000000..fbfce4124dbeeb82b81fabbec5324398fd8df646 Binary files /dev/null and b/nvim differ diff --git a/start.sh b/start.sh index 41a9029bda71c822f791ff5a01f3f2a2844152a2..34ad8ca102545cb59f27e95efc276bb57492b324 100644 --- a/start.sh +++ b/start.sh @@ -1,6 +1,9 @@ #!/usr/bin/sh +# set -x install_user_home=$1 vimforcpp_home=$install_user_home/.VimForCpp +# 是否安装 cquery 标记. +install_cquery_flag=0 function InstallEnv() { # 检查操作系统版本是否 ok @@ -23,20 +26,23 @@ function InstallEnv() { # 安装 neovim echo "尝试安装 neovim" yum -y install epel-release - yum install -y neovim.x86_64 python2-neovim.noarch + yum install -y neovim.x86_64 python36-neovim.noarch nvim --version if [ $? -ne 0 ]; then echo "neovim 安装失败!" exit 1 fi + # 由于 centos7 yum 源上默认的 neovim 升级到了 neovim 0.3.0, 但是这个版本 + # 目前还有问题. 所以仍然需要使用 neovim 0.2.2. 上面的操作只是为了安装 Python 扩展 + yum install -y fuse-libs.x86_64 fuse.x86_64 # 敲下 vim 命令实际启动了 nvim touch $install_user_home/.bashrc - grep -q "nvim" $install_user_home/.bashrc - if [ $? -ne 0 ]; then - echo 'alias vim="nvim"' >> $install_user_home/.bashrc - fi + echo "alias vim='$vimforcpp_home/nvim'" >> $install_user_home/.bashrc + # 安装 ctags yum -y install ctags + # 安装 unzip + yum -y install unzip echo "环境检测完毕!" } @@ -60,8 +66,14 @@ function GetWhiteList() { if grep -q "YouCompleteMe" $whitelist; then # 发现白名单中包含了 YCM, 则把对应的动态库的白名单也放进去. # TODO 后续考虑根据操作系统版本来决定下载哪个库 + echo "YCM.zip" >> $whitelist echo "YCM.so" >> $whitelist fi + + # 发现白名单中包含 cquery, 则需要准备后续的环境变量和安装额外的库 + if grep -q "LanguageClient" $whitelist; then + install_cquery_flag=1 + fi } function DownloadPlugin() { @@ -73,6 +85,8 @@ function DownloadPlugin() { git config core.sparsecheckout true GetWhiteList $vimforcpp_home $bundle_dir git pull origin master + # 增加对 YCM 的解压缩 + unzip YCM.zip cp $bundle_dir/YCM.so/el7.x86_64/* $bundle_dir/YouCompleteMe/third_party/ycmd/ if [ $? -ne 0 ]; then echo "插件下载失败!" @@ -81,6 +95,29 @@ function DownloadPlugin() { echo "插件下载完毕!" } +function InstallCQuery() { + # 0. 检查如果不需要安装 cquery 就直接返回 + if [ $install_cquery_flag -eq 0 ]; then + return 0; + fi + # 1. 安装依赖的库 + if [ ! -f /usr/lib64/libatomic.so.1 ]; then + echo "未找到 libstdatomic, 尝试安装..." + yum install -y libatomic.x86_64 + fi + # 2. 添加环境变量 + if ! grep -q ".VimForCpp/vim/bundle/YCM.so/el7.x86_64" $install_user_home/.bashrc; then + echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/.VimForCpp/vim/bundle/YCM.so/el7.x86_64' >> $install_user_home/.bashrc + fi + # 3. 准备临时目录 + if [ -d /tmp/cquery -o -f /tmp/cquery ]; then + rm -rf /tmp/cquery + fi + mkdir /tmp/cquery + mkdir /tmp/cquery/cache + cp $vimforcpp_home/cquery/config/settings.json /tmp/cquery/ +} + function LinkDir() { # 先备份原有的 vim 配置文件 today=`date +%m%d` @@ -103,13 +140,23 @@ function LinkDir() { chown -R $install_user:$install_user $install_user_home/.vim chown -R $install_user:$install_user $install_user_home/.vimrc chown -R $install_user:$install_user $install_user_home/.ycm_extra_conf.py + chown -R $install_user:$install_user $vimforcpp_home/nvim + + if [ $install_cquery_flag -eq 1 ]; then + chown -R $install_user:$install_user /tmp/cquery + ln -s $vimforcpp_home/cquery/config/cquery.config $install_user_home/.cquery + chown -R $install_user:$install_user $install_user_home/.cquery + fi } + # 1. 检查并安装依赖的软件 InstallEnv # 2. 从码云上下载 vim 配置 DownloadVimConfig # 3. 从码云上下载依赖的插件 DownloadPlugin -# 4. 备份对应用户的 .vim 目录, 并且建立好连接, 并修改文件权限 +# 4. 决定是否安装 cquery +InstallCQuery +# 5. 备份对应用户的 .vim 目录, 并且建立好连接, 并修改文件权限 LinkDir echo '安装成功! 请手动执行 "source ~/.bashrc" 或者重启终端, 使 vim 配置生效!' diff --git a/uninstall.sh b/uninstall.sh index a8a805b919e4a55448b4bb35705ae95da26f02fe..372c3cda9bbf5b64226b974aa7981ef646ae583e 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -2,4 +2,6 @@ rm -rf ~/.VimForCpp rm -f ~/.vim rm -f ~/.vimrc rm -f ~/.ycm_extra_conf.py +rm -f ~/.cquery +[ -d /tmp/cquery ] && rm -rf /tmp/cquery echo "卸载完毕!" diff --git a/vim/init.vim b/vim/init.vim index fd37405b81f5eaf1b2fb66d656758258f6fbf278..96c2b5157d8de8f214b01f49ffc5e342007c4254 100644 --- a/vim/init.vim +++ b/vim/init.vim @@ -15,12 +15,12 @@ Plug 'python-mode/python-mode' Plug 'scrooloose/nerdcommenter' Plug 'Yggdroot/LeaderF' Plug 'cpiger/NeoDebug' -Plug 'cpiger/NeoDebug' Plug 'ryanoasis/vim-devicons' Plug 'autozimu/LanguageClient-neovim', { \ 'branch': 'next', \ 'do': 'bash install.sh', \ } +Plug 'fatih/vim-go' call plug#end() @@ -73,24 +73,6 @@ if has("autocmd") endif -"""""""""""""""""""""""""" 按键映射 -" 调整窗口移动 -nnoremap H h -nnoremap J j -nnoremap K k -nnoremap L l -" 快速保存 -nnoremap w :w -inoremap jk :w -inoremap :w -" 快速缩进 -vnoremap < >gv -" 快速打开标签页 -nnoremap t :AT -nnoremap e :tabe - - """""""""""""""""""""""""" airline 彩色状态栏/标签页 " 这几个是挑选的还不错的几个 airline 主题 " let g:airline_theme="ravenpower" @@ -156,7 +138,7 @@ let g:airline_symbols.readonly = '⭤' let g:airline_symbols.linenr = '' -"""""""""""""""""""""""""" 代码折叠(按下 zz 快速折叠/展开函数) +"""""""""""""""""""""""""" 代码折叠 set foldenable " 折叠方法 " manual 手工折叠 @@ -169,7 +151,6 @@ set foldmethod=indent set foldlevel=99 " 代码折叠自定义快捷键 zz let g:FoldMethod = 0 -nnoremap zz :call ToggleFold() fun! ToggleFold() if g:FoldMethod == 0 exe "normal! zM" @@ -181,19 +162,15 @@ fun! ToggleFold() endfun -"""""""""""""""""""""""""" 快速运行(按下 F5 快速运行) -nnoremap :call Compile() +"""""""""""""""""""""""""" 快速运行 func! Compile() exec "w" if &filetype == 'c' - exec "!gcc -g % -o %<" - exec "!./%<" + exec "!gcc -g % -o %< && ./%<" elseif &filetype == 'cpp' - exec "!g++ -g % -o %<" - exec "!./%<" + exec "!g++ -g % -o %< && ./%<" elseif &filetype == 'cc' - exec "!g++ -g % -o %<" - exec "!./%<" + exec "!g++ -g % -o %< && ./%<" elseif &filetype == 'python' exec "!python %" elseif &filetype == 'sh' @@ -203,10 +180,6 @@ endfunc """""""""""""""""""""""""" YouCompleteMe -nnoremap -nnoremap :YcmCompleter GoToDefinitionElseDeclaration -nnoremap :YcmDiags - set completeopt=longest,menu autocmd InsertLeave * if pumvisible() == 0|pclose|endif inoremap pumvisible() ? "\" : "\" @@ -233,25 +206,19 @@ let g:ycm_semantic_triggers = { """""""""""""""""""""""""" Tagbar(函数列表) -nnoremap :TagbarToggle let g:tagbar_ctags_bin='/usr/bin/ctags' let g:tagbar_width=20 let g:tagbar_left=1 let g:tagbar_sort = 0 -"""""""""""""""""""""""""" Dox(注释文档) -nnoremap :Dox - - """""""""""""""""""""""""" NERDTree(文件列表) -nnoremap :NERDTreeToggle let g:NERDTreeWinPos="right" let g:NERDTreeWinSize=20 """""""""""""""""""""""""" CtrlP(快速查找文件) -let g:ctrlp_working_path_mode='a' +" let g:ctrlp_working_path_mode='a' """""""""""""""""""""""""" cpp-enhanced-highlight(c++ 语法高亮) @@ -273,27 +240,187 @@ let g:UltiSnipsJumpBackwardTrgger="" """""""""""""""""""""""""" LeaderF let g:Lf_WindowHeight = 0.30 -let g:Lf_ShortcutF = '' -nnoremap fm :LeaderfMru -nnoremap ff :LeaderfFunction -nnoremap fb :LeaderfBuffer -"""""""""""""""""""""""""" LeaderF +"""""""""""""""""""""""""" cquery let g:LanguageClient_serverCommands = { - \ 'cpp': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], - \ 'cxx': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], - \ 'cc': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], - \ 'c': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], - \ 'h': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], - \ 'hpp': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cq.log'], + \ 'cpp': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], + \ 'cxx': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], + \ 'hpp': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], + \ 'cc': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], + \ 'c': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], + \ 'h': ['~/.VimForCpp/cquery/bin/cquery', '--log-file=/tmp/cquery/cq.log'], \ } -let g:LanguageClient_loadSettings = 1 -let g:LanguageClient_settingsPath = '~/.VimForCpp/cquery/config/settings.json' - -nnoremap gh :call LanguageClient_textDocument_hover() -nnoremap gd :call LanguageClient_textDocument_definition() -nnoremap gr :call LanguageClient_textDocument_references() -nnoremap gs :call LanguageClient_textDocument_documentSymbol() -nnoremap gn :call LanguageClient_textDocument_rename() +let g:LanguageClient_loadSettings = 1 +let g:LanguageClient_settingsPath = '/tmp/cquery/settings.json' + +" 关闭 cquery 的诊断信息, 使用 Ycm 的足矣 +let g:LanguageClient_diagnosticsEnable=0 + +" 指定项目的根目录标记. +let g:LanguageClient_rootMarkers = ['.root', '.svn', '.git'] + + +""""""""""""""""""""""" PyMode +let g:pymode_lint_cwindow = 0 +let g:pymode_lint_ignore = ["E305", "E501", "E302", "E391", "W391", "W605",] + + +""""""""""""""""""""""" 按键映射和快捷键提示 +" 跳转到定义 +nnoremap :YcmCompleter GoToDefinitionElseDeclaration +" 返回到跳转前的位置 +nnoremap + +" 切换鼠标状态 +function! MouseToggle() + if &mouse ==# "" + set mouse=a + else + set mouse= + endif +endfunction + +" 切换绝对行号和相对行号 +function! NumToggle() + if &relativenumber ==# "norelativenumber" + set relativenumber + else + set norelativenumber + endif +endfunction + +function GuideEsc() + unmap a + unmap s + unmap d + unmap f + unmap g + unmap w + unmap q + unmap + echo "" +endfunction +function Terminal() + sp + terminal +endfunction + +function MenuA() + echo "[a] 跳转定义 [s] 查找引用 [d] 重命名 [f] 修正错误 [g] 函数签名 [q] 取消" + nnoremap a :call GuideEsc():call LanguageClient_textDocument_definition() + nnoremap s :call GuideEsc():call LanguageClient_textDocument_references():lopen:lopen + nnoremap d :call GuideEsc():call LanguageClient_textDocument_rename() + nnoremap f :call GuideEsc():YcmCompleter FixIt + nnoremap g :call GuideEsc():call LanguageClient_textDocument_hover() + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +function MenuS() + echo "[a] 查找函数 [s] 打开文件 [d] buffer查找 [f] 最近文件 [q] 取消" + nnoremap a :call GuideEsc():LeaderfFunction + nnoremap s :call GuideEsc():LeaderfFile + nnoremap d :call GuideEsc():LeaderfBuffer + nnoremap f :call GuideEsc():LeaderfMru + nnoremap g + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +function MenuD() + echo "[a] 编译运行 [s] 编译检查 [q] 取消" + nnoremap a :call GuideEsc():call Compile() + nnoremap s :call GuideEsc():YcmDiags + nnoremap d + nnoremap f + nnoremap g + nnoremap w +endfunction + +function MenuF() + echo "[a] 函数列表 [s] 文件列表 [d] .h/.c [f] 标签页 [q] 取消" + nnoremap a :call GuideEsc():TagbarToggle + nnoremap s :call GuideEsc():NERDTreeToggle + nnoremap d :call GuideEsc():AT + nnoremap f :call GuideEsc():tabe + nnoremap g + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +function MenuWA() + echo "[a] 切换鼠标 [s] 切换粘贴 [d] 切换行号 [f] 不可见字符 [q] 取消" + nnoremap a :call GuideEsc():call MouseToggle() + nnoremap s :call GuideEsc():set paste! + nnoremap d :call GuideEsc():call NumToggle() + nnoremap f :call GuideEsc():set list! + nnoremap g + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +function MenuWS() + echo "[a] 文档注释 [s] 折叠/展开 [q] 取消" + nnoremap a :call GuideEsc():Dox + nnoremap s :call GuideEsc():call ToggleFold() + nnoremap d + nnoremap f + nnoremap g + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() + " TODO 快速注释和格式整理 +endfunction + +function MenuW() + echo "[a] 切换选项 [s] 代码格式 [q] 取消" + nnoremap a :call GuideEsc():call MenuWA() + nnoremap s :call GuideEsc():call MenuWS() + nnoremap d + nnoremap f + nnoremap g + nnoremap w + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +function GuideMapTopMenu() + nnoremap a :call MenuA() + nnoremap s :call MenuS() + nnoremap d :call MenuD() + nnoremap f :call MenuF() + nnoremap t :call Terminal() + nnoremap g + nnoremap w :call MenuW() + nnoremap q :call GuideEsc() + nnoremap :call GuideEsc() +endfunction + +nnoremap :call GuideEntry() +function GuideEntry() + " 1. 重新映射相关快捷键到 space + call GuideMapTopMenu() + " 2. 打印菜单 + echo "[a] 语义 [s] 查找 [d] 调试 [f] 窗口 [w] 其他 [q] 取消 [t] 终端" +endfunction + + +""" 其他 +" 调整窗口移动 +nnoremap H h +nnoremap J j +nnoremap K k +nnoremap L l +" 快速保存 +inoremap jk :w +nnoremap :w +inoremap :w +" 快速缩进 +vnoremap < >gv