5

次のことができるマクロをVimで定義できるかどうか疑問に思っています。クラス定義があるとします

class CRectangle {
  int x;
  int y;
  _
};

ここで、_は現在のカーソル位置を指定します。

マクロを実行すると、自動的に生成されます

class CRectangle {
  int x;
  int y;

public:
  CRectangle (int x, int y);
  ~CRectangle ();
};

CRectangle::(int x, int y) {
  this->x = x;
  this->y = y;
}

私はこれについてしばらく考えていましたが、どこにも行きませんでした。おそらく、コンストラクター定義を作成するのは少し多すぎて質問できません。少なくともコンストラクター宣言を取得することは可能ですか?

====

sftrabbitが指摘しているように、次のようなものを生成する方がおそらくより望ましいでしょう。

CRectangle::(int _x, int _y) : x(_x), y(_y) {}
4

1 に答える 1

10

わかりました...私は退屈しました...

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)

これは単純化できると確信しています...しかし、「それは可能ですか?」への答えとして。はい...確かにできます。

また、vimがフォーマット(自動インデントなど)用に構成されている場合でも、処理するために多少変更する必要があることに注意してください。

/\s*\w\+\s\+\(\w\+\)\s*;\s*\n/クラスで変数を組み立てることについて少しずさんな場合は、/\s*\w\+\s\+\(\w\+\);\n/両方の場所に交換する必要があるかもしれません。(物の周りにいくつかの余分なスペースを処理する)

于 2013-03-23T02:16:25.363 に答える