GitHub 打破 Visual Studio 缩进

GitHub breaks Visual Studio indentation

我想知道为什么我的代码在推送到 GitHub 后变得混乱。
例如,当我缩进某些 class 的成员时,它们都是对齐的,在 Visual Studio 中看起来不错,在 GitHub 中看起来很丑。

这是一个例子:

它在 GitHub 中的样子:

Indentation in GitHub

首先,确保所有缩进都是真正的制表符,而不是空格。

其次,默认情况下,GitHub 会将制表符显示为 8 个字符。 因此,尝试查看相同的 Git 中心页面,但在其 URL 的末尾添加:?ts=4

即:

https://gist.github.com/razzorflame/ef776ddef260608bc1a8799090af629e?ts=4

或者... configure your Visual Studio to use a tab width of 8(虽然不理想)。


作为 mentioned here, you can add a .editorconfig (like this one 的要点)与:

root = true

[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
[*.{js,jsx,html,sass}]
charset = utf-8
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false 

然后 GitHub 应该使用正确的宽度 (4) 显示标签。


作为 .editorconfig 的使用说明,Git 本身与 Git 2.26(2020 年第一季度)告诉 .editorconfig 在这个项目中,*.txt 文件用制表符缩进。

参见 commit 7047f75 (05 Jan 2020) by Hans Jerry Illikainen (illikainen)
(由 Junio C Hamano -- gitster -- in commit 34246a1 合并,2020 年 1 月 30 日)

editorconfig: indent text files with tabs

Signed-off-by: Hans Jerry Illikainen

Previously, the .editorconfig did not specify an indentation style for text files.

However, a quick look for indentation-like spacing suggest that tabs are more common for documentation:

$ git grep -Pe '^ {4}' -- '*.txt' |wc -l 
2683 
$ git grep -Pe '^\t' -- '*.txt' |wc -l 
14011

Note that there are a lot of files that indent list continuations (and other things) with a single space -- if the first search was made without the fixed quantifier the result would look very different.
However, the result does correspond with my anecdotal experience when editing Git documentation.

This commit adds *.txt to .editorconfig as an extension that should be indented with tabs.