2014-03-30 70 views
0

我倾向于依赖vim而不是完整的IDE来处理项目,而我发现自己定期做的其中一件事是创建一个新文件(s)与派生值。Vim使用模板创建文件

例如,创建一个新的C++类涉及创建.hpp文件和.cpp文件,添加文件的意见,许可,作者,构造函数/析构函数,复制,转让,搬迁等..

.HPP

class %Object% { 

public: 

    explicit %Object%() = default; 
    ~%Object%() = default; 

    %Object%(%Object%&& rhs) = default; 
    %Object%(const %Object%& rhs) = default; 
    %Object%& operator=(%Object%&& rhs) = default; 
    %Object%& operator=(const %Object%& rhs) = default; 

protected: 

private: 

} 

的.cpp
#include "%Object%.hpp" 

另一个例子是一个.h.c文件在C。

我对UltiSnipsmuTemplate有点熟悉,它们似乎都大大减少了样板。但是,我不清楚在文件范围之外是否有方法使用这些或其他内容。我写了一个非常快速和肮脏的bash scripts来做到这一点,我准备好在python中重新实现它,但我宁愿使用现有的插件。

有没有办法用UltiSnips,muTemplate或其他方法做到这一点?如果没有,是否有扩展现有插件的好方法?

回答

0

添加到您的启动文件时的一个:

" Function to substitute the class names in a file 
function! SubstituteClassName() 
    execute "1,$s/%Object%/" . expand("%:t:r") . "/g" 
endfunction 

" Function to create the skeleton of a header file 
function! CreateHeaderFile() 
    1 
    insert 
#pragma once 
#ifndef %Object%_H 
#define %Object%_H 

class %Object% { 

public: 

    explicit %Object%() = default; 
    ~%Object%() = default; 

    %Object%(%Object%&& rhs) = default; 
    %Object%(const %Object%& rhs) = default; 
    %Object%& operator=(%Object%&& rhs) = default; 
    %Object%& operator=(const %Object%& rhs) = default; 

protected: 

private: 

} 
. 
    call SubstituteClassName() 
endfunction 

" Function to create the skeleton of a source file 
function! CreateSourceFile() 
    1 
    insert 
#include "%Object%.hpp" 
. 
    call SubstituteClassName() 
endfunction 

function! CreateClassFiles(name) 

    " Open the header file. 
    execute "edit " . a:name . ".hpp" 
    " Create the skeleton of the header file 
    call CreateHeaderFile() 
    " Write the file 
    wa 

    " Open the source file. 
    execute "edit " . a:name . ".cpp" 
    " Create the skeleton of the header file 
    call CreateSourceFile() 
    " Write the file 
    wa 

endfunction 

现在你可以使用

call CreateClassFiles("myclassname") 
+0

我包含的代码仅仅是一个例子。我已经有bash [脚本](https://github.com/uxcn/new)这将为c/c + +,bash,haskell,java,python等做这个...我希望有更通用的东西存在。 – Jason

1

DISCL创建骨架.HPP和.cpp文件。我是mu-template和lh-cpp的维护者。不幸的是,我现在只是看到你的问题 - 我想说你不应该犹豫给我发电子邮件/一个问题/ ......我不确定这个问题仍然是打开的。我甚至不确定是否完全掌握了你正在寻找的东西。

由于您尝试过的版本,我已经在lh-cpp中添加了许多模板/片段/向导来生成classes according to their semantics。现在,您可以之一:

  • 扩大之类的东西value-classbase-class
  • 或调用一个函数来扩大同向导/片段/模板,并给它的参数。例如,用一个类似指针的参数来扩展一个值类将触发复制构造函数和赋值操作符的生成(否则,它们将被默认,显式或隐式地取决于选项和检测到的C++风格(C++ 98/03,C++ 11或更多 - rule of all or nothing仍然需要强制执行)。唉,这种方法目前不是很符合人体工程学原理,我必须找到一种简化这个任务的方法,你可以在test/spec directory of lh-cpp找到使用的例子。

注意,对于C++文件也是高度可定制的C++模板 - 。在每个项目的基础Usual licence texts准备包括一个新的C++文件知道如何将其相关的头文件(如果检测到)

+0

谢谢:)。我还没有见过lh-cpp。这更接近我最终想到的,但并不完全在那里。我真的没有时间思考这个问题。我肯定想重新访问我原本以某种形式写成的脚本(丑陋,丑陋的黑客)。我期望的最大的事情是尽可能简单和正交的解决方案,支持包含文件和/或目录的对象的概念。 – Jason

+0

@Jason,这可能不是讨论这个问题的最佳地方,我不确定“包含文件和/或目录”是什么意思。如果您的需要仍然存在,请不要犹豫,给我发一封邮件或使用追踪器来了解如何使用lh-cpp来满足您的需求。关于简单的使用,我仍然有最后一点https://github.com/LucHermitte/lh-cpp/blob/master/doc/TODO.md#other-snippets照顾。 –