VS Code 配置

通用的配置

control-shift-[ 可以折叠代码,这很好,但 vim 移动到 fold 上,会自动 unfold,github 上一圈吐槽的。

不过这也解决了,需要在 user settings 里加上:

    "vim.foldfix": true,

然后就可以快乐地 fold 代码,然后移动了。

VS Code 有一些必记的快捷键(你要用的话。。。逃不掉这些操作):

  • control-p 打开文件 (输入 ?可以查帮助)
  • control-shift-p 输命令
  • F12,control-F12,跳转到定义啥的(右键可以用 context menu 看这些操作)Ubuntu 上,Fn+Escape 可以切换 Fn 默认是否打开
  • alt-n 搜索 symbol
  • control-shift-f 搜索字符串

其他的一些配置:

    "files.trimTrailingWhitespace": true,

C++ 开发的配置

首先,用 clang-format 格式化代码,安装一下:

sudo apt-get install clang-format

在工程目录下建一个 Makefile:

ALL_SRCS = $(shell git ls-files    | grep -E '\.h$$|\.cpp$$|\.hpp$$|\\.c$$' | tr '\n' ' ')
MOD_SRCS = $(shell git ls-files -m | grep -E '\.h$$|\.cpp$$|\.hpp$$|\\.c$$' | tr '\n' ' ')
 
all: lint
lint:
    @clang-format -i $(MOD_SRCS)
lint-all:
    @clang-format -i $(ALL_SRCS)
install-hook:
    echo "#!/bin/sh\nmake lint-all" > .git/hooks/pre-commit
    chmod +x .git/hooks/pre-commit

再创建 .clang-format 文件:

Language:        Cpp
# BasedOnStyle:  LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterFunction:   true
  AfterClass:      true
  AfterControlStatement: false
  AfterEnum:       true
  AfterNamespace:  true
  AfterObjCDeclaration: false
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit:     80
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IndentCaseLabels: false
IndentWidth:     4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments:  true
SortIncludes:    true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        4
UseTab:          Never

然后就能在工程根目录用 make lint 格式化修改了的文件。用 make lint-all 格式化所有的代码。

还可以加一个 hook 让 git commit 的时候自动格式化。用 make install-hook

(写这几个小脚本用了我好几十分钟呢,不过以后就轻松了)

VS Code 用户,还可以安装 clang-format 插件,然后在个人设置里加上保存时自动格式化的配置。按 control-shift-p, 搜 'user settings' 打开配置,加上:

"editor.formatOnSave": true,
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容