LoginSignup
1
2

More than 5 years have passed since last update.

goでのgodoc用コメント作成 正規表現

Last updated at Posted at 2016-10-12

エクスポートしているメソッドなどにドキュメントコメントをつける正規表現。
// 名称までは作成してくれるので、以降のコメントは自分で書く。

なお以下はIntelliJで動作確認しています。

type用

検索用正規表現

^type\s*([A-Z][\w]*)

置き換え

// $1 \n$0

実行例

置換前
type Sample struct {
    // do something
}
置換後
// Sample
type Sample struct {
    // do something
}

func(レシーバなし)用

検索用正規表現

^func\s*([A-Z][\w]*)

置き換え

// $1 \n$0

実行例

置換前
func Sample() bool {
    // do something
}
置換後
// Sample
func Sample() bool {
    // do something
}

func(レシーバあり)用

検索用正規表現

^func\s*\(\w*\s*\S*\s*([A-Z][\w]*)

置き換え

// $1 \n$0

実行例

置換前
func (s *Sample) TestMethod() bool{
  // do something
}
置換後
// TestMethod 
func (s *Sample) TestMethod() bool{
  // do something
}

struct内のフィールドなど

置き換えたい部分だけ選択してから置き換え実行する。
(余計なものまで引っかかるため)

検索用正規表現

^(\s*)([A-Z][\w]*)

置き換え

$1// $2 \n$0

実行例

置換前
type Example struct {
    Hoge string
    Fuga string
    *Embedded
}
置換後
type Example struct {
    // Hoge 
    Hoge string
    // Fuga 
    Fuga string
    *Embedded
}

追記
面倒になってきたのでperlで。
エラー処理など全く無いので自己責任で実行してください。

add_comment_go.sh
#!/bin/sh
perl -i -pe 's/^type\s*([A-Z][\w]*)/\/\/ $1 \n$&/g' $1
perl -i -pe 's/^func\s*([A-Z][\w]*)/\/\/ $1 \n$&/g' $1
perl -i -pe 's/^func\s*\(\w*\s*\S*\s*([A-Z][\w]*)/\/\/ $1 \n$&/g' $1
perl -i -pe 's/^(\s*)([A-Z][\w]*)/$1\/\/ $2 \n$&/g' $1
実行
add_comment_go.sh [対象ファイル]
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2