【问题标题】:GitHub breaks Visual Studio indentationGitHub 打破了 Visual Studio 缩进
【发布时间】:2018-01-13 03:48:30
【问题描述】:

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

这是一个例子:

以及它在 GitHub 中的外观:

Indentation in GitHub

【问题讨论】:

    标签: visual-studio github indentation


    【解决方案1】:

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

    其次,默认情况下,GitHub 将选项卡显示为 8 个字符。 所以尝试查看相同的 GitHub 页面,但在其 URL 的末尾添加:?ts=4

    即:

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

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


    作为mentioned here,您可以添加.editorconfig(如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 文件使用制表符缩进。

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

    editorconfig: 使用制表符缩进文本文件

    签字人:Hans Jerry Illikainen

    以前,.editorconfig 没有为文本文件指定缩进样式。

    但是,快速查看类似缩进的间距表明制表符在文档中更常见:

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

    请注意,有很多文件使用单个空格缩进列表延续(和其他内容) - 如果第一次搜索是在没有固定量词的情况下进行的,结果看起来会非常不同。
    但是,结果确实符合我在编辑 Git 文档时的轶事经验。

    此提交将*.txt 添加到.editorconfig 作为扩展名,应使用制表符缩进。

    【讨论】:

    • 哇,太好了!有没有办法在我的仓库中设置默认的“ts=4”?阅读我的代码的人不会考虑使用它,他们会因为看到不可读的代码而受苦。编辑:我总是使用制表符作为缩进。
    • @razzorflame 不确定要点,但尝试添加.editorconfig。请参阅我编辑的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2012-03-07
    相关资源
    最近更新 更多