首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VS代码不缩进4个空格,但仅用于单个文件

VS代码不缩进4个空格,但仅用于单个文件
EN

Stack Overflow用户
提问于 2020-06-02 21:46:49
回答 1查看 2.3K关注 0票数 2

我有一个打字稿项目,我正在编辑与VS代码。VS代码的settings.json中有以下设置

代码语言:javascript
复制
{
    "window.zoomLevel": 0,
    "extensions.ignoreRecommendations": false,
    "editor.formatOnSave": true,
    "javascript.updateImportsOnFileMove.enabled": "always",
    "files.autoSave": "off",
    "workbench.iconTheme": "material-icon-theme"
    "editor.tabSize" : 4
}

此外,该项目使用ESLint,.eslintrc.js

代码语言:javascript
复制
module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint/eslint-plugin'],
  extends: [
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
  root: true,
  env: {
    node: true,
    jest: true,
  },
  rules: {
    '@typescript-eslint/interface-name-prefix': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    "prefer-const": 0,
    "no-use-before-define": 0,
    "no-useless-escape": 0,
    "no-lone-blocks": 0,
    "no-empty-pattern": 0,
    "no-sequences": 0,
    "no-path-concat": 0,
    "no-extend-native": 0,
    "import/no-duplicates": 0,
    "no-unexpected-multiline": 0,
    "camelcase": 0,
    "func-call-spacing": [2, "never"],
    "space-before-function-paren": [2, { "anonymous": "always", "named": "never", "asyncArrow": "always" }],
    "no-array-constructor": 0,
    "no-mixed-operators": 0,
    "no-prototype-builtins": 0,
    "brace-style": [2, "1tbs"],
    "block-spacing": 0,
    "indent": [2, 4, {
      "SwitchCase": 1,
      "VariableDeclarator": 1,
      "outerIIFEBody": 1,
      "MemberExpression": 1,
      "FunctionDeclaration": { "parameters": 1, "body": 1 },
      "FunctionExpression": { "parameters": 1, "body": 1 },
      "CallExpression": { "arguments": 1 },
      "ArrayExpression": 1,
      "ObjectExpression": 1,
      "ImportDeclaration": 1,
      "flatTernaryExpressions": false,
      "ignoreComments": false
    }],
    "eqeqeq": 0,
    "semi": 0,
    "prefer-promise-reject-errors": 0,
    "no-undef": 0,
    "no-return-assign": 0,
    "no-unused-vars": 0,
    "no-unused-expressions": 0,
  },
};

...and a .prettierrc

代码语言:javascript
复制
{
  "singleQuote": true,
  "trailingComma": "all"
  "tabWidth": 4,
}

我的项目主要由.ts类型记录扩展名文件组成。现在,所有这些都很好。我可以更改我的代码,并在保存时,VS代码将自动格式化代码相应.

然而,我有一个文件,特别是拒绝自动格式化为4空间选项卡,并坚持使用2空间选项卡。

app.controller.ts

代码语言:javascript
复制
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

首先,保存在VS代码中不会因为某种原因对此文件进行格式化更改。我尝试过运行npm run lint,它确实捕获了缩进错误。

然后,运行npm run lint --fix将得到以下结果:

代码语言:javascript
复制
@Controller()
export class AppController {
    constructor(private readonly appService: AppService) { }

  @Get()
    getHello(): string {
        return this.appService.getHello();
    }
}

如您所知,constructorgetHello函数被适当缩进,但装饰器没有。此外,进入文件和编辑,我仍然有一个2的标签大小。

如何使该文件符合4的选项卡大小?

提醒您,除了这个文件之外,其他所有文件都可以使用这个文件。

EN

回答 1

Stack Overflow用户

发布于 2021-01-04 05:57:57

还可以尝试在编辑器中强制使用选项卡宽度:

settings.json

代码语言:javascript
复制
"[typescript]": {
    "editor.tabSize": 4,
    "editor.detectIndentation": false,
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62161901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档