GitHub破坏了Visual Studio的缩进

4
我想知道为什么我将代码推送到GitHub后会变得混乱。
例如,当我缩进某个类的成员以使它们对齐时,它在Visual Studio中看起来很好看,但在GitHub中看起来很丑陋。
这是一个例子: Indentation in Visual Studio 而这是在GitHub上的呈现方式: Indentation in GitHub
1个回答

4

首先,请确保您的缩进是实际制表符,而不是空格。

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

即:

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

tabs looks good

或者... 配置您的Visual Studio,使用8个制表符宽度(虽然不是理想的选择)。

此处所提到的,您可以添加一个.editorconfig(像这个一样用于gist):

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文件使用制表符缩进。
请参见提交7047f75(由Hans Jerry Illikainen (illikainen)于2020年1月5日完成)。
(由Junio C Hamano -- gitster --合并于提交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.


1
哇,太棒了!有没有办法在我的代码库中设置默认的“ts=4”?阅读我的代码的人不会考虑使用它,他们将遭受看到难以阅读的代码的痛苦。 编辑:我总是使用制表符作为缩进。 - Poeta Kodu
@razzorflame不确定你是否理解要点,但可以尝试添加一个.editorconfig文件。请看我的编辑后的答案。 - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接