Visual Studio Code 和 TSLint:代码换行超过 80 个字符

J. *_*ers 10 word-wrap typescript tslint visual-studio-code prettier

我在 Visual Studio Code 中使用带有 TSLint 和 Prettier 的 TypeScript 来编写 React Native 应用程序。

我试图将我的编辑器配置为将每行代码自动包装为 100 个字符。但保存后总是 80 个字符,而不是 100 个。

以下是我更改的设置:

"prettier.tslintIntegration": true,
"prettier.printWidth": 100,
"editor.renderIndentGuides": true,
"editor.rulers": [100],
"editor.wordWrapColumn": 100,
"editor.formatOnSave": true
Run Code Online (Sandbox Code Playgroud)

这是我的tslint.json

{
  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
  "rules": {
    // "jsx-no-lambda": false,
    "member-access": false,
    "interface-name": false,
    "max-line-length": [true, 100]
  }
}
Run Code Online (Sandbox Code Playgroud)

即使我以这种方式配置了所有内容,我的代码仍然会自动换行 80 个字符。我怎样才能让它停止?

如果我的行超过 100 个字符,我会收到 linting 错误,因此该tslint.json设置似乎有效。

奖励:完成 VSCode 设置,以防我遗漏了什么:

{
  "telemetry.enableTelemetry": false,
  "workbench.colorTheme": "One Dark Pro",
  "workbench.iconTheme": "vscode-icons",
  "window.zoomLevel": 0,
  "prettier.eslintIntegration": true,
  "prettier.tslintIntegration": true,
  "prettier.printWidth": 100,
  "editor.renderIndentGuides": true,
  "editor.rulers": [100],
  "[javascript]": {
    "editor.tabSize": 2
  },
  "[typescript]": {
    "editor.tabSize": 2
  },
  "[typescriptreact]": {
    "editor.tabSize": 2
  },
  "[json]": {
    "editor.tabSize": 2
  },
  "workbench.colorCustomizations": {
    // "statusBar.background": "#272b33",
    // "panel.background": "#30353f",
    // "sideBar.background": "#2a2b33",
    "editor.background": "#2c313a"
  },
  "todohighlight.keywords": [
    {
      "text": "DEBUG:",
      "color": "#fff",
      "backgroundColor": "limegreen",
      "overviewRulerColor": "grey"
    },
    {
      "text": "NOTE:",
      "color": "#fff",
      "backgroundColor": "mediumslateblue",
      "overviewRulerColor": "grey"
    },
    {
      "text": "REVIEW:",
      "color": "#fff",
      "backgroundColor": "dodgerblue",
      "overviewRulerColor": "grey"
    },
    {
      "text": "XXX:",
      "color": "#fff",
      "backgroundColor": "orangered",
      "overviewRulerColor": "grey"
    }
  ],
  "editor.wordWrapColumn": 100,
  "editor.formatOnSave": true
}
Run Code Online (Sandbox Code Playgroud)

val*_*tev 8

这两个应该够了:

"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 100
Run Code Online (Sandbox Code Playgroud)

"editor.wordWrap"您的设置中似乎缺少。在 vscode 中,此设置控制换行策略:“wordWrapColumn”表示在"editor.wordWrapColumn"设置时换行。

您还可以尝试"editor.wordWrap": "bounded"哪个将尊重“wordWrapColumn”,但如果您的视口少于您定义的列数,则也可以换行。

UPD:根据我们在评论中的讨论,似乎更漂亮不尊重其"printWidth"设置。可能有两个最可能的原因:

  1. 本期:https : //github.com/prettier/prettier-vscode/issues/595
  2. 定义配置选项的优先级:https : //github.com/prettier/prettier-vscode#prettiers-settings。特别是,它首先搜索更漂亮的配置文件,而不是.editorconfig文件,然后才搜索 vscode 设置。

作为一种解决方法,您可以尝试在更漂亮的项目配置文件或 editorconfig 文件中实际定义此设置,并检查 vscode 插件是否适用于其中任何一个。


Sha*_*Ali 8

找到或添加名为 .prettierrc.js 的文件并按printWidth如下所示添加

module.exports = {
  bracketSpacing: false,
  jsxBracketSameLine: true,
  singleQuote: true,
  trailingComma: 'all',
  arrowParens: 'avoid',
  printWidth: 150, // <== add this
};
Run Code Online (Sandbox Code Playgroud)


ind*_*eet 5

我找到了对我有用的最简单的方法。进入设置搜索Print WidthPrettier: Print Width根据您的需要设置,默认情况下它是 80,我将其更改为 150,它对我有用。并在您的 settings.json 中添加以下内容 "editor.wordWrap": "wordWrapColumn", "editor.wordWrapColumn": 150, "prettier.printWidth": 150

在此处输入图片说明


Man*_*ddy 5

我安装了 prettier,仅添加以下行就足以解决问题:

"prettier.printWidth": 120,
Run Code Online (Sandbox Code Playgroud)

一般缠绕长度为 80 和 120,但有些使用 150。

您可以在以下任一设置中添加上述内容:

项目工作区设置:

  • .vscode\settings.json

用户设置:

  • %UserProfile%\AppData\Roaming\Code\User\settings.json

上面的路径适用于 Windows 操作系统,但类似的路径也适用于其他操作系统。