BEIKE blog

備忘録です

vscodeでclang-formatを使用する

概要

最近、formaterをgithub actionsで使うようになりました。先にそっち?って感じですよね。 手元でも動かせるようにしたのでやり方を備ぼる。

環境

ubuntu 18.04

vscode 1.58.2

やり方

1.clang-formatをインストール

sudo apt install clang-format-6.0

2.vscodeの拡張のClang-Formatを入れる

https://i.gyazo.com/6316b34f1a1fcf65dc61f013e53b5cae.png

3.vscodeのsetting.jsonに保存時にフォーマットしてくれるように設定を追記する

---中略---- 
 "clang-format.executable": "/usr/bin/clang-format-6.0",
  "editor.formatOnSave": true,
  "editor.formatOnType": false,
  "[cpp]": {
    "editor.defaultFormatter": "xaver.clang-format"
  },
}

4.ワークスペース下に.clang-formatのファイルを置く .clang-formatの配置場所の例

https://i.gyazo.com/65bfb06181fd78f180ad1352c140eb2e.png

.clang-formatの例

---
Language:        Cpp
# BasedOnStyle:  Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  AfterExternBlock: true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false        # Unlike GNU style, do not indent braces
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom # See BraceWrapping section
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     100
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks:   Preserve
IncludeCategories:
  - Regex:           '^<ext/.*\.(h|hh|hpp|cuh)>'
    Priority:        2
  - Regex:           '^<.*\.(h|hh|hpp|cuh)>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth:     2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
# clang-format6 uses 'Delimiter' and clang-format7 uses 'Delimiters' as field.
# RawStringFormats:
#   - Delimiter:       pb
#     Language:        TextProto
#     BasedOnStyle:    google
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Auto
TabWidth:        8
UseTab:          Never

ctl+cとctl+\でプロセスを速攻落とせるらしい

概要

プロセスを速攻落とす方法を知ったので備ぼる

やりかた

ctl+cをターミナルで打った後にctl+\を打つ。

ctl+\だけでもできる。(オススメしない)

完了

例えば、今までctl+cで10秒ぐらい掛かってたgazeboのプロセスkillが 1秒で終わるようになる。

これは良いやり方なのか知らべてないが。。。。(たまにgazeboが上手く落ちないときがある)

gazeboのwindowが残ったままになる場合

killall gzclient 

特定のtopicとかをクリップボードにコピーする方法

概要

トピックを毎回rostopic listで調べてカーソール使ってコピーしてペーストする作業って なんだかんだ面倒くさいです。クリップボードにコピー出来たらストレスフリーで良いのになと 思ってました。そんなことはやれるようになってるだろうと思って調べたら確かにできそうなコマンドが ありました。ということで備ぼります。

インストールするもの

sudo apt-get install xsel

実行コマンド

perl -pe 's/\n/ /' | xsel --clipboard --input

実例

roscore
rostopic list | grep ros | perl -pe 's/\n/ /' | xsel --clipboard --input

コマンドが長いのでエイリアス

alias x="perl -pe 's/\n/ /' | xsel --clipboard --input"

roscore
rostopic list | grep ros | x

優勝👑

同一リポジトリでの作業方法

概要

誰かのpcにあるリポジトリで開発をする時にどうやって複数人で上手くGitHubでの作業を行っていくのかっていう疑問がありますよね。ある程度の流れを抑えたので、備ぼる。

やり方

まず、誰かのPCに作業用のリポジトリがあるとします。

hoge@hoge:~/test/

そのリポジトリGitHub上でフォークします。フォークしたら、リモートリポジトリとして 誰かのPCの作業用のリポジトリにURLを追加します。 addの後ろのhogeは、git remote -vで確認して同じ名前が無いことを確認しましょう。

hoge@hoge:~/test/$ git remote add hoge 追加したいリポジトリ

次に、自分のフォークしたリポジトリのブランチを誰かのPCの作業用のリポジトリに追加したいと考えます。そのために以下のコマンドを実行します。

fetchの後ろのhogeは、先程のaddした名前と同じものです。

hoge@hoge:~/test/$ git fetch hoge <ブランチ名>
hoge@hoge:~/test/$ git checkout <ブランチ名>

上手くブランチが切り替わらなかった場合は(変更が残ってる場合) error: Your local changes to the following files would be overwritten by checkout:みたいなことを言われたらgit stash(作業途中の場合)git commitgit checkout .(変更全消し)をしましょう。


これで自分がフォークしたブランチを持ってくることが出来たので、 git addしてgit commitをするのですが他のユーザーが使っていることを考えて一時的に 自分のユーザー名でcommitをした方が良いと考えます。方法は以下のとおりです。

hoge@hoge:~/test/$ git add hoge
hoge@hoge:~/test/$ git -c user.name='hoge' -c user.email='hofe@hoge.com' commit

後は、commitしたものを自分のGitHubのフォークしたリポジトリに反映させます。

hoge@hoge:~/test/$ git push hoge

反映させたら、GitHubでPR(フォーク元を修正したい場合)を投げて完了です。