EditorConfig Generator
生成.editorconfig文件以确保编辑器之间的代码风格一致。
# EditorConfig — https://editorconfig.org root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true max_line_length = 120 [*.json] indent_size = 2 [*.md] trim_trailing_whitespace = false
关于此工具
EditorConfig是一个配置文件格式,可帮助开发团队在不同编辑器和IDE中保持一致的代码格式风格。当团队成员使用Visual Studio Code、JetBrains IDE、Sublime Text、Vim或其他工具时,.editorconfig文件确保缩进、行结束符、字符集和其他格式规则保持统一,不管每个人偏好使用哪种编辑器。这消除了版本控制系统中的格式冲突,并减少了花在风格讨论上的时间。
要使用此生成器,只需选择您首选的缩进类型(制表符或空格)、缩进大小、行结束符风格(LF、CRLF或自动检测)和字符集。您还可以为Python、JavaScript、JSON或您使用的任何其他语言等特定文件类型配置规则。自定义所有设置后,下载生成的.editorconfig文件并将其放在项目根目录中。您的团队成员的编辑器在打开该项目中的文件时将自动应用这些规则。
EditorConfig通过内置支持或轻量级插件与大多数现代编辑器无缝集成,是在不需要额外构建工具或代码检查工具的情况下强制执行代码风格一致性的最简单方法之一。从事开源项目、企业代码库或协作环境工作的团队获益最多,因为它为格式偏好设置了唯一的真实来源。该工具对于多种编程语言共存的多语言项目特别有用,因为.editorconfig规则可以按文件扩展名定制。
常见问题
代码实现
# Generate a .editorconfig file programmatically
def generate_editorconfig(indent_style="space", indent_size=2, end_of_line="lf",
charset="utf-8", trim_trailing_whitespace=True,
insert_final_newline=True) -> str:
lines = [
"# EditorConfig is awesome: https://editorconfig.org",
"",
"# top-most EditorConfig file",
"root = true",
"",
"[*]",
f"indent_style = {indent_style}",
f"indent_size = {indent_size}",
f"end_of_line = {end_of_line}",
f"charset = {charset}",
f"trim_trailing_whitespace = {str(trim_trailing_whitespace).lower()}",
f"insert_final_newline = {str(insert_final_newline).lower()}",
"",
"[*.md]",
"trim_trailing_whitespace = false",
"",
"[Makefile]",
"indent_style = tab",
"",
"[*.{json,yml,yaml}]",
"indent_size = 2",
]
return "\n".join(lines)
config = generate_editorconfig(indent_style="space", indent_size=4)
print(config)
# Write to file
with open(".editorconfig", "w") as f:
f.write(config)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.