C++ Vim宏生成C++;建造师
我想知道是否有可能在Vim中定义一个宏,让您执行以下操作。假设您有一个类定义C++ Vim宏生成C++;建造师,c++,vim,macros,C++,Vim,Macros,我想知道是否有可能在Vim中定义一个宏,让您执行以下操作。假设您有一个类定义 class CRectangle { int x; int y; _ }; 其中u指定当前光标位置 运行宏应自动生成 class CRectangle { int x; int y; public: CRectangle (int x, int y); ~CRectangle (); }; CRectangle::(int x, int y) { this->x = x;
class CRectangle {
int x;
int y;
_
};
其中u指定当前光标位置
运行宏应自动生成
class CRectangle {
int x;
int y;
public:
CRectangle (int x, int y);
~CRectangle ();
};
CRectangle::(int x, int y) {
this->x = x;
this->y = y;
}
我已经考虑了一段时间,但没有取得任何进展。
也许创建构造函数定义的要求有点过分。
至少获取构造函数声明是否可行
====
正如Rabbit所指出的,可能更希望生成如下内容
CRectangle::(int _x, int _y) : x(_x), y(_y) {}
好的。。。我很无聊
qm ; Gentlemen... start your macros (we'll call it 'm')
ma ; Mark your current location as 'a'
v ; switch to 'visual' mode
?{<cr> ; Search back to the opening brace (actually hit 'enter' for that <cr>)
l"by ; Go forward one character and yank the selection to buffer 'b'
b ; Go back one word
"cyw ; Copy the class name into buffer 'c'
'a ; Jump back to the starting location
opublic:<cr> ; add "public:"
()<esc>B"cP ; create an empty constructor
t)"bp ; Paste the list of arguments in
; Rather complex reformatting regex on the next line
:.,/)/s/\s*\w\+\s+\(\w+\);\n/_\1, /<cr>
kJ:s/,\s*)/)/<cr> ; Simple cleanup
A : {}<esc> ; Finish some of the basics
F:"bp ; Paste in the fields again for generating the initialization
; Below: Another fairly complicated formatting regex
:.,/{}/s/\s*\w\+\s\+\(\w\+\);\n/\1(_\1),/<cr>
:s/,\s*{/ {/<cr> ; Cleanup
kJ ; Finished with the constructor
q ; Finish macro (I'm going to omit the rather trivial destructor)
qm;先生们。。。启动宏(我们称之为“m”)
文科硕士将当前位置标记为“a”
v切换到“视觉”模式
?{;搜索回开始大括号(实际点击“回车”键)
l“by;前进一个字符并将所选内容拖动到缓冲区“b”
退一步说
“cyw;将类名复制到缓冲区'c'
'a;跳回起始位置
opublic:;添加“public:”
()B“cP;创建一个空构造函数
t) “bp;将参数列表粘贴到
;在下一行对regex进行相当复杂的重新格式化
:,/)/s/\s*\w\+\s+\(\w+\);\n/\u1/
kJ:s/,\s*)/;简单清理
答:{};完成一些基础知识
F:“bp;再次粘贴字段以生成初始化
;如下:另一个相当复杂的格式正则表达式
:,/{}/s/\s*\w\+\s\+\(\w\+\);\n/\1(\u1)/
:s/,\s*{/};清除
kJ;与构造函数一起完成
Finish宏(我将省略相当简单的析构函数)
我相信这是可以简化的……但作为对“可以做到吗?”的回答,是的……当然可以
注意,您还必须对其进行一些修改,以处理vim的格式化配置(自动缩进等)
如果您在类中组装变量时有点马虎,您可能必须将
/\s*\w\+\s\+\(\w\+\)\s*;\s*\n/
替换为/\s*\w\+\s\+\(\w\+\);\n/
这两个位置。(处理一些额外的空格)您不希望它使用成员初始化列表吗?无论如何,我毫不怀疑这是可能的-这就是我们正在谈论的vim。我只是不确定它是否真的有用。我恐怕以前从未听说过成员初始化列表。感谢您指出。对于该任务,最好使用代码段。例如,我高度推荐结束。一定要查看screencast,看看你能用它做什么。我一直在使用snipMate。我不知道UltiSnips在变量上支持正则表达式。非常感谢。包括语法错误?scnr