• 使用editorconfig配置你的编辑器


    摘要:

      在团队开发中,统一的代码格式是必要的。但是不同开发人员使用的编辑工具可能不同,这样就造成代码的differ。今天给大家分享一个很好的方法来使不同的编辑器保持一样的风格。

      不同的编辑器也有设置代码风格的,例如我们前端人员最喜欢使用的sublime text 2编辑器。你可以在preferences中选择Settings-User,然后输入自定义的风格,比如:

    {

      "default_line_ending": "unix",

      "font_size": 14,

      "ignored_packages":

      [

        "Vintage"

      ],

      "tab_size": 4,

      "translate_tabs_to_spaces": true

    }

    今天我们用editorconfig来实现多种编辑器的代码风格统一。

    简介:

      EditorConfig帮助开发人员定义和维护一致的编码风格在不同的编辑器和IDE。EditorConfig项目包含一个文件格式定义编码风格和文本编辑器插件的集合。EditorConfig文件易于阅读并且他们与版本控制器很好地合作。

    editorconfig文件:

      下面是一个editorconfig文件例子,用于设置Python和JavaScript行尾和缩进风格的配置文件。

    # EditorConfig is awesome: http://EditorConfig.org
    
    # top-most EditorConfig file
    root = true
    
    # Unix-style newlines with a newline ending every file
    [*]
    end_of_line = lf
    insert_final_newline = true
    
    # Matches multiple files with brace expansion notation
    # Set default charset
    [*.{js,py}]
    charset = utf-8
    
    # 4 space indentation
    [*.py]
    indent_style = space
    indent_size = 4
    
    # Tab indentation (no size specified)
    [*.js]
    indent_style = tab
    
    # Indentation override for all JS under lib directory
    [lib/**.js]
    indent_style = space
    indent_size = 2
    
    # Matches the exact files either package.json or .travis.yml
    [{package.json,.travis.yml}]
    indent_style = space
    indent_size = 2

    点击此处寻找更多的实例:

    怎样安装editorconfig文件:

      当打开一个文件时,EditorConfig插件会在打开文件的目录和其每一级父目录查找.editorconfig文件,直到有一个配置文件root=trueEditorConfig配置文件从上往下读取,并且路径最近的文件最后被读取。匹配的配置属性按照属性应用在代码上,所以最接近代码文件的属性优先级最高。

    注意:Windows 用户在项目根目录创建.editorconfig文件,可以先创建“.editorconfig.”文件,系统会自动重名为.editorconfig

    文件格式:

      EditorConfig文件使用INI格式。斜杠(/)作为路径分隔符,#或者;作为注释。EditorConfig文件使用UTF-8格式、CRLFLF作为换行符。

    通配符:

    * 匹配除/之外的任意字符串
    ** 匹配任意字符串
    ? 匹配任意单个字符
    [name] 匹配name字符
    [!name] 匹配非name字符
    {s1,s2,s3} 匹配任意给定的字符串(since 0.11.0)

    属性:

      indent_style: 设置缩进风格,tab或者空格。tab是hard tabs,space为soft tabs。

      indent_size: 缩进的宽度,即列数,整数。如果indent_style为tab,则此属性默认为tab_width。

      tab_ 设置tab的列数。默认是indent_size。

      end_of_line: 换行符,lf、cr和crlf

      charset: 编码,latin1utf-8utf-8-bomutf-16beutf-16le,不建议使用utf-8-bom。

      trim_trailing_whitespace: 设为true表示会除去换行行首的任意空白字符。

      insert_final_newline: 设为true表明使文件以一个空白行结尾

      root: 表明是最顶层的配置文件,发现设为true时,才会停止查找.editorconfig文件。

    点击此处查看更多属性

    注意:属性不区分大小写

    实例:

      下面以sublime text为例,测试editorconfig是否起作用。首先需要给sublime安装EditorConfig插件,然后在项目的根目录新建文件".editorconfig",内容如下:

    # EditorConfig is awesome: <a onclick="javascript:pageTracker._trackPageview('/outgoing/EditorConfig.org');" href="http://EditorConfig.org">http://EditorConfig.org</a>
    
    # top-most EditorConfig file
    root = true
    
    # Unix-style newlines with a newline ending every file
    [*]
    end_of_line = lf
    insert_final_newline = true
    
    # Matches multiple files with brace expansion notation
    # Set default charset
    [*.{js,html,css}]
    charset = utf-8
    
    # Tab indentation (no size specified)
    [*.js]
    indent_style = tab
    tab_width = 50

    我特意将tab的width修改的很大,你可以重新打开编辑器测试下tab。

    附录:

      https://github.com/editorconfig?page=1

  • 相关阅读:
    b_lc_数组的最大公因数排序(并查集+质因数分解优化)
    Coggle 30 Days of ML:结构化赛题:天池二手车交易价格预测(二)
    Coggle 30 Days of ML:结构化赛题:天池二手车交易价格预测(一)
    漫谈
    漫谈
    漫谈
    漫谈
    漫谈
    漫谈
    漫谈
  • 原文地址:https://www.cnblogs.com/xiyangbaixue/p/4201490.html
Copyright © 2020-2023  润新知