如何配置?Sublime?Text4為?C++?編輯器
概述
涉及以下插件的安裝和配置Package Control
Terminus
LSP
LSP-clangd
clang-format
LSP-pyright
LSP-json
配置sublime
安裝Package Control
以進行包管理。
Terminus
安裝Terminus
以實現(xiàn)sublime text4內(nèi)的terminal。
綁定快捷鍵:
[ { "keys": [ "ctrl+shift+t" ], "command": "terminus_open", "args": { // 打開時要執(zhí)行的命令 // "cmd": "fish", "cwd": "${file_path:${folder}}" } } ]
自定義在Terminus
的終端中編譯運行cpp文件:
在Tools->Build System->New Build System
中新建編譯文件,保存為CppTerminus.sublime-build
,替換內(nèi)容為:
{ // MacOS "cmd": [ "zsh", "-c", "clang++ '${file}' -std=c++17 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && ${file_path}/../bin/${file_base_name}" ], "file_regex": "^(..{FNXX==XXFN}*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "encoding": "utf-8", "selector": "source.c, source.c++", "variants": [ { "name": "Run In Terminus", "target": "terminus_exec", "cancel": "terminus_cancel_build", "cmd": [ "zsh", "-c", "clang++ '${file}' -std=c++17 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && ${file_path}/../bin/${file_base_name}" ] }, { "name": "Create Input File", "cmd": [ "zsh", "-c", "touch ${file_path}/../in_out/${file_base_name}.in && open -a Sublime\\ Text ${file_path}/../in_out/${file_base_name}.in" ] }, { "name": "Run In Terminal", "cmd": [ "zsh", "-c", "clang++ '${file}' -std=c++17 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && open -a Terminal.app '${file_path}/../bin/${file_base_name}'" ] }, ] }
注意要保證源文件和bin
文件夾、in_out
文件夾在同一目錄下。
配置LSP + LSP-clangd
安裝這兩個插件,windows和linux需要手動安裝clangd
并添加到path。
mac下安裝clangd
我的mac已經(jīng)自帶了clangd
,安裝好這兩個插件即可實現(xiàn)語法提示; 如果沒有安裝clangd
,通過以下命令安裝:
brew install llvm echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc echo 'export LDFLAGS="-L/usr/local/opt/llvm/lib"' >> ~/.zshrc echo 'export CPPFLAGS="-I/usr/local/opt/llvm/include"' >> ~/.zshrc
Debian(testing)安裝clangd和clang
sudo apt install clangd
, sudo apt install llvm
, sudo apt install clang
windows下安裝llvm
借助scoop, scoop install llvm
然后安裝Visual Stdudio Build Tools,鑒于windows上clang默認的c++庫就是msvc,所以就用這個吧,別折騰mingw
了.
配置LSP-clangd
到Preferences->Package Settings->LSP->Settings
,寫入這樣幾行
{ // 在主頁面只顯示error紅色下劃線 //"show_diagnostics_severity_level": 1, // 代碼提示顯示燈泡圖標 "show_code_actions": "bulb", // 保存時自動格式化 "lsp_format_on_save": true, }
再到Preferences->Package Settings->LSP->Servers->LSP-clangd
中,寫入以下幾行
// Settings in here override those in "LSP-clangd/LSP-clangd.sublime-settings" { "initializationOptions": { // 啟用clang-tidy代碼檢查,可能啟用后warning會比較多,自己看著辦吧 "clangd.clang-tidy": true, // 美化clangd輸出的JSON "clangd.pretty": true, } }
再到project/code
(源文件所在目錄)下新建.clang-tidy
文件,寫入:
Checks: "bugprone-*,\ google-*,\ misc-*,\ modernize-*,\ performance-*,\ readability-*,\ portability-*,\ " HeaderFilterRegex: 'Source/cm[^/]*\.(h|hxx|cxx)$' CheckOptions: - key: modernize-use-default-member-init.UseAssignment value: '1' - key: modernize-use-equals-default.IgnoreMacros value: '0' - key: modernize-use-auto.MinTypeNameLength value: '80'
LSP-clangd默認是使用c++98
來檢查代碼的,要修改為c++17
,需要在項目根目錄下新建.clangd
文件,文件內(nèi)容如下:
CompileFlags: Add: [-std=c++17]
推薦方式為利用cmake
生成compile_commands.json
, CMakeLists.txt
的內(nèi)容如下:
cmake_minimum_required(VERSION 3.22) # Enable C++11 set(CMAKE_CXX_STANDARD 17) # 設(shè)置項目名 project(LeetCpp) # 源文件 aux_source_directory(. SOURCES) # 頭文件 include_directories(.) add_executable(${PROJECT_NAME} ${SOURCES})
通過cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1
生成compile_commands.json
文件,注意要在build
目錄下,且build
的上級目錄存在CMakelists.txt
。
配置clang-format
安裝好clang-format
插件之后,只需在project/code
下新建.clang-format
文件,寫入以下內(nèi)容
抄的網(wǎng)上的,修改了大括號換行的部分(左大括號不單獨位于一行)
# 語言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto Language: Cpp # BasedOnStyle: LLVM # 訪問說明符(public、private等)的偏移 AccessModifierOffset: -2 # 開括號(開圓括號、開尖括號、開方括號)后的對齊: Align, DontAlign, AlwaysBreak(總是在開括號后換行) AlignAfterOpenBracket: Align # 連續(xù)賦值時,對齊所有等號 AlignConsecutiveAssignments: false # 連續(xù)聲明時,對齊所有聲明的變量名 AlignConsecutiveDeclarations: false # 右對齊逃脫換行(使用反斜杠換行)的反斜杠 AlignEscapedNewlines: Right # 水平對齊二元和三元表達式的操作數(shù) AlignOperands: true # 對齊連續(xù)的尾隨的注釋 AlignTrailingComments: true # 不允許函數(shù)聲明的所有參數(shù)在放在下一行 AllowAllParametersOfDeclarationOnNextLine: false # 不允許短的塊放在同一行 AllowShortBlocksOnASingleLine: true # 允許短的case標簽放在同一行 AllowShortCaseLabelsOnASingleLine: true # 允許短的函數(shù)放在同一行: None, InlineOnly(定義在類中), Empty(空函數(shù)), Inline(定義在類中,空函數(shù)), All AllowShortFunctionsOnASingleLine: None # 允許短的if語句保持在同一行 AllowShortIfStatementsOnASingleLine: true # 允許短的循環(huán)保持在同一行 AllowShortLoopsOnASingleLine: true # 總是在返回類型后換行: None, All, TopLevel(頂級函數(shù),不包括在類中的函數(shù)), # AllDefinitions(所有的定義,不包括聲明), TopLevelDefinitions(所有的頂級函數(shù)的定義) AlwaysBreakAfterReturnType: None # 總是在多行string字面量前換行 AlwaysBreakBeforeMultilineStrings: false # 總是在template聲明后換行 AlwaysBreakTemplateDeclarations: true # false表示函數(shù)實參要么都在同一行,要么都各自一行 BinPackArguments: true # false表示所有形參要么都在同一行,要么都各自一行 BinPackParameters: true # 大括號換行,只有當BreakBeforeBraces設(shè)置為Custom時才有效 BraceWrapping: # class定義后面 AfterClass: false # 控制語句后面 AfterControlStatement: false # enum定義后面 AfterEnum: false # 函數(shù)定義后面 AfterFunction: false # 命名空間定義后面 AfterNamespace: false # struct定義后面 AfterStruct: false # union定義后面 AfterUnion: false # extern之后 AfterExternBlock: false # catch之前 BeforeCatch: false # else之前 BeforeElse: false # 縮進大括號 IndentBraces: false # 分離空函數(shù) SplitEmptyFunction: false # 分離空語句 SplitEmptyRecord: false # 分離空命名空間 SplitEmptyNamespace: false # 在二元運算符前換行: None(在操作符后換行), NonAssignment(在非賦值的操作符前換行), All(在操作符前換行) BreakBeforeBinaryOperators: NonAssignment # 在大括號前換行: Attach(始終將大括號附加到周圍的上下文), Linux(除函數(shù)、命名空間和類定義,與Attach類似), # Mozilla(除枚舉、函數(shù)、記錄定義,與Attach類似), Stroustrup(除函數(shù)定義、catch、else,與Attach類似), # Allman(總是在大括號前換行), GNU(總是在大括號前換行,并對于控制語句的大括號增加額外的縮進), WebKit(在函數(shù)前換行), Custom # 注:這里認為語句塊也屬于函數(shù) BreakBeforeBraces: Custom # 在三元運算符前換行 BreakBeforeTernaryOperators: false # 在構(gòu)造函數(shù)的初始化列表的冒號后換行 BreakConstructorInitializers: AfterColon #BreakInheritanceList: AfterColon BreakStringLiterals: false # 每行字符的限制,0表示沒有限制 ColumnLimit: 0 CompactNamespaces: true # 構(gòu)造函數(shù)的初始化列表要么都在同一行,要么都各自一行 ConstructorInitializerAllOnOneLineOrOnePerLine: false # 構(gòu)造函數(shù)的初始化列表的縮進寬度 ConstructorInitializerIndentWidth: 4 # 延續(xù)的行的縮進寬度 ContinuationIndentWidth: 4 # 去除C++11的列表初始化的大括號{后和}前的空格 Cpp11BracedListStyle: true # 繼承最常用的指針和引用的對齊方式 DerivePointerAlignment: false # 固定命名空間注釋 FixNamespaceComments: true # 縮進case標簽 IndentCaseLabels: false IndentPPDirectives: None # 縮進寬度 IndentWidth: 4 # 函數(shù)返回類型換行時,縮進函數(shù)聲明或函數(shù)定義的函數(shù)名 IndentWrappedFunctionNames: false # 保留在塊開始處的空行 KeepEmptyLinesAtTheStartOfBlocks: false # 連續(xù)空行的最大數(shù)量 MaxEmptyLinesToKeep: 1 # 命名空間的縮進: None, Inner(縮進嵌套的命名空間中的內(nèi)容), All NamespaceIndentation: None # 指針和引用的對齊: Left, Right, Middle PointerAlignment: Right # 允許重新排版注釋 ReflowComments: true # 允許排序#include SortIncludes: false # 允許排序 using 聲明 SortUsingDeclarations: false # 在C風格類型轉(zhuǎn)換后添加空格 SpaceAfterCStyleCast: false # 在Template 關(guān)鍵字后面添加空格 SpaceAfterTemplateKeyword: true # 在賦值運算符之前添加空格 SpaceBeforeAssignmentOperators: true # SpaceBeforeCpp11BracedList: true # SpaceBeforeCtorInitializerColon: true # SpaceBeforeInheritanceColon: true # 開圓括號之前添加一個空格: Never, ControlStatements, Always SpaceBeforeParens: ControlStatements # SpaceBeforeRangeBasedForLoopColon: true # 在空的圓括號中添加空格 SpaceInEmptyParentheses: false # 在尾隨的評論前添加的空格數(shù)(只適用于//) SpacesBeforeTrailingComments: 1 # 在尖括號的<后和>前添加空格 SpacesInAngles: false # 在C風格類型轉(zhuǎn)換的括號中添加空格 SpacesInCStyleCastParentheses: false # 在容器(ObjC和JavaScript的數(shù)組和字典等)字面量中添加空格 SpacesInContainerLiterals: true # 在圓括號的(后和)前添加空格 SpacesInParentheses: false # 在方括號的[后和]前添加空格,lamda表達式和未指明大小的數(shù)組的聲明不受影響 SpacesInSquareBrackets: false # 標準: Cpp03, Cpp11, Auto Standard: Cpp11 # tab寬度 TabWidth: 4 # 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always UseTab: Never
命令行使用sublime text4打開文件
mac下添加軟連接: ln /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
debian下執(zhí)行sudo ln -s /opt/sublime_text/sublime_text /usr/local/bin/subl
之后就能用subl test.cpp
命令來調(diào)用sublime text4打開test.cpp
了。
leetcode相關(guān)配置
安裝leetgo: brew install j178/tap/leetgo
在預(yù)期的leetcode項目目錄下執(zhí)行leetgo init
,然后配置leetgo.yaml
# leetgo project level config, global config is at /Users/zwy/.config/leetgo/config.yaml # for more details, please refer to https://github.com/j178/leetgo # author author: zwyyy456 language: en code: lang: cpp leetcode: site: https://leetcode.com # credentials: # from: browser editor: use: custom command: "subl" # use: vscode
到此這篇關(guān)于配置 Sublime Text4為 C++ 編輯器的文章就介紹到這了,更多相關(guān)Sublime Text4配置C++內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)系列篇二叉樹的遍歷
本章將會詳細講解二叉樹遍歷的四種方式,分別為前序遍歷、中序遍歷、后續(xù)遍歷和層序遍歷。在學(xué)習遍歷之前,會先帶大家回顧一下二叉樹的基本概念2022-02-02C++前綴樹字典樹的學(xué)習與模擬實現(xiàn)代碼示例
這篇文章主要介紹了C++前綴樹字典樹的學(xué)習與模擬實現(xiàn)代碼示例,Trie又被稱為前綴樹、字典樹,所以當然是一棵樹,上面這棵Trie樹包含的字符串集合是{in,inn,int,tea,ten,to},每個節(jié)點的編號是我們?yōu)榱嗣枋龇奖慵由先サ?需要的朋友可以參考下2023-07-07C++中靜態(tài)成員函數(shù)訪問非靜態(tài)成員的實例
這篇文章主要介紹了C++中靜態(tài)成員函數(shù)訪問非靜態(tài)成員的實例的相關(guān)資料,需要的朋友可以參考下2017-07-07