简体   繁体   English

如何格式化 Visual Studio Code 项目中的所有文件?

[英]How do I format all files in a Visual Studio Code project?

Is there a way to format all files in a project without formatting each one individually?有没有一种方法可以格式化项目中的所有文件而无需单独格式化每个文件?

Use the extension called ”Format Files” .使用名为“Format Files”的扩展名 Here are the steps:以下是步骤:

  1. Download the extension called ”Format Files” on VSCode.在 VSCode 上下载名为“Format Files”的扩展程序。
  2. Select and open the folder with files to format on VSCode.选择并打开包含要在 VSCode 上格式化的文件的文件夹。
  3. Press Ctrl+Shift+P to open command palette.按 Ctrl+Shift+P 打开命令面板。
  4. Enter "Start Format Files: Workspace" and select this option.输入“开始格式化文件:工作区”并选择此选项。

Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files来源: https ://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files

This works for me这对我有用

Install prettier:安装更漂亮:

npm init 
npm i prettier

Add following script in package.json:package.json中添加以下脚本

"pretty": "prettier --write \"./**/*.{js,jsx,json}\"" 

In this case only, i need to format my .js .jsx and .json files.仅在这种情况下,我需要格式化我的 .js .jsx 和 .json 文件。

Run script:运行脚本:

npm run pretty

The simplest solution that I have found is as below.我发现的最简单的解决方案如下。

  • Install prettier in vscode.在 vscode 中安装 prettier。
  • Create the .prettierrc file and configure it the way you want.创建 .prettierrc 文件并按照您想要的方式对其进行配置。
  • Run following command in vscode console.在 vscode 控制台中运行以下命令。

npx prettier --write "**/*.ts" (Add the file type regex as per the need) npx prettier --write "**/*.ts" (根据需要添加文件类型正则表达式)

I was out of luck finding an extension that was doing this the way I was expecting it so I made one.我很不幸地找到了一个以我期望的方式执行此操作的扩展,所以我做了一个。 I suggest you take a look at the extension I just made :我建议你看看我刚刚做的扩展:

https://marketplace.visualstudio.com/items?itemName=lacroixdavid1.vscode-format-context-menu#overview https://marketplace.visualstudio.com/items?itemName=lacroixdavid1.vscode-format-context-menu#overview

It might still have some issues, feel free to report them or to contribute.它可能仍然存在一些问题,请随时报告或贡献。

With npm , just run npx prettier --write .使用npm ,只需运行npx prettier --write . in the terminal if your language is supported如果您的语言受支持,则在终端中

You can use the extension Command on All Files您可以在所有文件上使用扩展命令

Add this to your settings.json将此添加到您的settings.json

"commandOnAllFiles.commands": {
    "Format File": {
      "command": "editor.action.formatDocument",
      "includeFileExtensions": [".js",".ts",".css"]
    }
  }

From the command palette select command Apply 1 or more commands to all files in the Workspace and select Format File from the Quickpick list.从命令面板选择命令Apply 1 or more commands to all files in the Workspace并从 Quickpick 列表中选择Format File Wait till you see an information message (lower right) that all files have been processed.等到您看到所有文件都已处理的信息消息(右下角)。

I do a simply trick:我做了一个简单的把戏:

  1. download this extension https://marketplace.visualstudio.com/items?itemName=ApceHHypocrite.OpenAllFiles下载此扩展https://marketplace.visualstudio.com/items?itemName=ApceHHypocrite.OpenAllFiles
  2. open all files打开所有文件
  3. set "editor.formatOnSave": true设置“editor.formatOnSave”:真
  4. save all files保存所有文件

Hope it helps希望能帮助到你

As @herrbischoff said, there is currently no way to format all files in a project.正如@herrbischoff 所说,目前无法格式化项目中的所有文件。
However it would be a useful feature.但是,这将是一个有用的功能。

What it can do is format all unsaved files by having auto-save and auto-format on.它可以做的是通过打开自动保存和自动格式化来格式化所有未保存的文件。

Otherwise you would need a shell script or an extension or some other extern program (like a tslint checker which can auto-correct errors) which is capable of doing this.否则,您将需要一个能够执行此操作的 shell 脚本或扩展程序或其他一些外部程序(例如可以自动纠正错误的 tslint 检查器)。

Had problems with this myself and it sucks to open all files by hand我自己有这个问题,手动打开所有文件很糟糕

There is currently no way to do that nor does it sound like a particularly useful feature to have.目前没有办法做到这一点,听起来也不是一个特别有用的功能。 Or put another way: it would be a useful feature if you could completely trust it, which you can't.或者换一种说法:如果你可以完全信任它,这是一个有用的功能,但你不能。

You would have to put a lot of faith into the auto-formatting logic of the used languages to not screw up and possibly introduce errors.您必须对所用语言的自动格式化逻辑充满信心,以免搞砸并可能引入错误。 You would need to review the changes manually anyway, so this approach should not result in measurable productivity gains.无论如何,您都需要手动查看更改,因此这种方法不应带来可衡量的生产力提升。

If you're working with a seriously f'ed up code base and don't care about possible problems, I would suggest running a simple shell command with the respective languages' CLI formatter.如果您正在使用严重的代码库并且不关心可能出现的问题,我建议您使用相应语言的 CLI 格式化程序运行一个简单的 shell 命令。 Example for C++ code, using clang-format : C++ 代码示例,使用clang-format

find . -iname *.cpp -exec clang-format {} +

This command will find all cpp files recursively and will run them through the formatter with default settings.此命令将递归查找所有 cpp 文件,并使用默认设置通过格式化程序运行它们。

The process is essentially the same for any language, for example JavaScript (with js-beautify ):对于任何语言,该过程基本上都是相同的,例如 JavaScript(使用js-beautify ):

find . -iname *.js -exec js-beautify {} +

Just make sure you review whatever comes out.只要确保你检查出来的任何东西。 Also, it may very well be possible to script this command into VScode — or just run it in the built-in terminal.此外,很可能将此命令编写到 VScode 中,或者直接在内置终端中运行。

if you want format any specific folder then just move the that folder and run command npx prettier --write.如果你想格式化任何特定的文件夹,那么只需移动该文件夹并运行命令npx prettier --write.

eg: if you are in 'xyz/frontend' folder all the file inside will be formatted same if you want to format only src folder under your frontend project the move to src via cd src then run commant npx prettier --write.例如:如果你在 'xyz/frontend' 文件夹中,如果你只想格式化前端项目下的 src 文件夹,则里面的所有文件都将被格式化,通过cd src移动到 src,然后运行命令 npx npx prettier --write. this will only format files inside src这只会格式化 src 中的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在侧栏中显示Visual Studio Code的所有文件 - How do I show all files in the sidebar for Visual Studio Code 如何在 Visual Studio Code 中查找和替换所有匹配项(在所有文件中)? - How do I find and replace all occurrences (in all files) in Visual Studio Code? Visual Studio代码:如何在所有文件的选项卡中显示文件目录? - Visual Studio Code: How do I display the file directory in the tab for all files? 如何在 Visual Studio Code 中列出/搜索特定类型的所有文件 - How do I list/search all files of a specific type in Visual Studio Code 如何在 Visual Studio 代码中查看文件/文件的内容? - How do I see the contents of the file / files in visual studio code? 如何在 Visual Studio Code 中搜索文件? - How do I search for files in Visual Studio Code? 如何在 Visual Studio Code 中为所有用户禁用遥测? - How do I disable telemetry for all users in Visual Studio Code? 如何在 Visual Studio Code 的所有项目文件中快速搜索光标下的符号 - How to quickly search for a symbol under cursor in all project files in Visual Studio Code 如何在 Visual Studio Code 中格式化 python 代码? - How do you format python code in Visual Studio Code? 如何在 Visual Studio Code (VSCode) 中格式化代码? - How do you format code in Visual Studio Code (VSCode)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM