程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mac M1使用Sublime格式化Python,C++,JSON的最佳方式

編輯:Python

tags: Tips Sublime

寫在前面

越來越離不開Sublime Text(下稱ST) 這款輕量級跨平台編輯器了, 滿足我對輕量化編輯代碼的所有要求, 並不像VSCode一樣臃腫, 內存占用也不大, 但是有一點不太好的就是現在插件市場的插件很多都不維護了, 就導致現在大多數開發者都轉向vscode陣營了(我不想做大多數).

我可是放不下這款超級棒的編輯器的, 不管是刷LeetCode還是寫小的測試程序, 都讓我覺得非常舒服. 但是最近遇到了一個問題, 就是在ST中寫C++時候代碼格式化總是不好用, 一開始我用的是一款叫做CoolFormat的插件, 但是遇到C++代碼總是提示Cannot format this file, 網上找到的推薦插件是SublimeAStyleFormatter1, 據說用來格式化C-like代碼都比較好, 並且內置了astyle引擎, 可以定制代碼段的格式化風格, 具體的話可以看看官網2.

如果你用的是arm Mac的話, 這裡安裝之後還得配置一下, 因為這個插件好久不更新了, 對於armMac的支持還沒有, 不過看到了issue3中的解決方案, 我成功實現了C++代碼的ST代碼格式化方法.

順帶提一下Python和JSON的格式化, 都有對應的插件, 比較方便.

Sublime Text 4: Build 4126
MacOS 12.3.1 (arm64)

C++代碼格式化(SublimeAStyleFormatter)

直接調出packageControl(command+shift+P), 輸入insp進入Package Install, 輸入包名稱即可安裝.

之後就是配置在arm Mac中成功運行Astyle:

* cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/SublimeAStyleFormatter/pyastyle/python3/
* mkdir _darwin
* cd _darwin
* touch __init__.py
* pip install pyastyle# 這裡我用到的Python是Mac中自帶的
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pyastyle in /Users/xxx/Library/Python/3.8/lib/python/site-packages (1.1.5)
* which pip
/Users/xxx/Library/Python/3.8/bin//pip
* cp /Users/xxx/Library/Python/3.8/lib/python/site-packages/pyastyle.cpython-38-darwin.so pyastyle.so
* cd ..
* ls
__init__.py _linux_x86 _local_arch _win32
_darwin _linux_x86_64 _macosx_universal _win64
* subl __init__.py # 編輯這個文件, 注釋掉所有的內容, 加入內容, 修改後如下
# try:
# from ._local_arch.pyastyle import *
# platform = "Local arch"
# except ImportError:
# try:
# from ._linux_x86_64.pyastyle import *
# platform = "Linux 64 bits"
# except ImportError:
# try:
# from ._linux_x86.pyastyle import *
# platform = "Linux 32 bits"
# except ImportError:
# try:
# from ._win64.pyastyle import *
# platform = "Windows 64 bits"
# except ImportError:
# try:
# from ._win32.pyastyle import *
# platform = "Windows 32 bits"
# except ImportError:
# try:
# from ._macosx_universal.pyastyle import *
# platform = "MacOS X Universal"
# except ImportError:
# raise ImportError(
# "Could not find a suitable pyastyle binary for your platform and architecture.")
try:
from ._darwin.pyastyle import *
platform = "MacOS X Darwin"
except ImportError:
raise ImportError(
"Could not find a suitable pyastyle binary for your platform and architecture.")

然後重啟ST, 打開一個C++文件,可以看到AstyleFormatterFormat已經可以點擊了.

快捷鍵配置

這裡我設置了三個快捷鍵, 如下:

其中python的格式化和JSON的格式化分別通過AnacondajsFormat這兩個插件完成的.

{

"keys": ["super+alt+/"], //format C/C++
"command": "astyleformat",
"context": [{

"key": "astyleformat_is_enabled",
"operator": "equal",
"operand": ""
}]
}, {

"keys": ["super+k", "super+f"],
"command": "astyleformat",
"args": {

"selection_only": true
},
"context": [{

"key": "astyleformat_is_enabled",
"operator": "equal",
"operand": ""
}]
}, {

"keys": [
"command+i"
],
"command": "reindent"
}, {

"keys": [
"ctrl+shift+s"
],
"command": "auto_save"
}, {

"keys": [
"super+alt+j" // format json
],
"command": "js_format"
}, {

"command": "anaconda_auto_format",
"keys": ["super+alt+l"], //format python
"context": [{

"key": "selector",
"operator": "equal",
"operand": "source.python"
}]
},

Ref


  1. SublimeAStyleFormatter - Packages - Package Control; ︎

  2. Artistic Style (sourceforge.net); ︎

  3. Formatting Not Working - M1 Macs (darwin/arm64) Unsupported · Issue #95 · timonwong/SublimeAStyleFormatter (github.com); ︎


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved