typescript - Visual Studio 代码和 TSLint : Code wrap to more than 80 characters

标签 typescript visual-studio-code word-wrap tslint 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

这是我的 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]
  }
}

即使我以这种方式配置了所有内容,我的代码仍然会自动换行大约 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
}

最佳答案

这两个应该足够了:

"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 100

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

您也可以尝试 "editor.wordWrap": "bounded",它会尊重“wordWrapColumn”,但如果您的视口(viewport)少于您定义的列数,也会换行。

UPD:根据我们在评论中的讨论,prettier 似乎不遵守其 "printWidth" 设置。可能有两个最可能的原因:

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

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

关于typescript - Visual Studio 代码和 TSLint : Code wrap to more than 80 characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52815047/

相关文章:

reactjs - 创建-react-app --typescript : Cannot compile namespaces when the '--isolatedModules' flag is provided

asp.net-core - 在 VS Code 中调试类库

angular - 你能在 VS Code 中自动添加 Angular 导入吗?

c - 函数类型 "extern __declspec(dllimport) INT __cdecl"在 C/C++ 或 SWIG 中有意义吗?

vim - Vim 中任意宽度窗口中的 80 个字符软换行

javascript - 如何使用 "in"和/或 "hasOwnProperty"缩小对象类型

javascript - 在 Visual Studio Code 中提取 JS/TS 中的局部变量的键盘快捷键

visual-studio-code - 如何使代码片段仅显示在 Visual Studio Code 中的特定文件扩展名?

HTML/CSS Two Columns/One Row Second Row Wrapping 如果浏览器宽度太小

javascript - 更改 Angular2 中 Select2 的数据和 processResults 上下文