WebStorm 2021.2 Help

Scratch files草稿文件

Sometimes you may need to create temporary notes or draft up some code outside of the project context. 有时,您可能需要在项目上下文之外创建临时注释或起草一些代码。Instead of switching to a different application for this purpose, you can use scratch files and scratch buffers.您可以使用“草稿文件”和“草稿文件缓冲区”,而不是为此目的切换到其他应用程序。

  • Scratch files are fully functional, runnable, and debuggable files, which support syntax highlighting, code completion, and all other features for the corresponding file type. 草稿文件”是功能齐全、可运行和可调试的文件,支持语法突出显示、代码完成以及相应文件类型的所有其他功能。For example, while working on one project, you may come up with an idea for a method that you could later use in another project. 例如,在处理一个项目时,您可能会想到一种方法,以后可以在另一个项目中使用。You can create a scratch file with a draft of the method, which is not stored in your project directory but can be accessed and opened from another project. 您可以使用该方法的草稿创建一个草稿文件,该文件不存储在您的项目目录中,但可以从另一个项目访问和打开。You can use scratch files to draft code constructs, HTTP requests, JSON documents, and so on.您可以使用草稿文件来起草代码构造、HTTP请求、JSON文档等。

  • Scratch buffers are simple text files without any coding assistance features. 草稿文件缓冲区”是没有任何编码辅助功能的简单文本文件。Scratch buffers can be used for simple task lists and notes to yourself. 草稿文件缓冲区可用于简单的任务列表和个人备忘。They are also not stored in the project directory but can be accessed and opened from another project. 它们也不存储在项目目录中,但可以从另一个项目访问和打开。You can create up to five scratch buffers with default names, which are rotated and reused by clearing the content.您最多可以创建五个具有默认名称的草稿文件缓冲区,这些缓冲区通过清除内容进行旋转和重用。

Scratch files and buffers are stored in the IDE configuration directory under scratches.草稿文件和缓冲区存储在IDE配置目录下的scratches下。

Create scratch files创建草稿文件

  1. From the main menu, select File | New | Scratch File or press Ctrl+Alt+Shift+Insert.从主菜单中,选择“文件”|“新建”|“草稿文件”或按Ctrl+Alt+Shift+Insert

  2. Select the language of the scratch file. 选择“草稿文件的语言”。Scratch files of the same type are automatically numbered and added to the Scratches and Consoles directory of the Project tool window.相同类型的草稿文件将自动编号并添加到项目工具窗口的“草稿文件和控制台”目录中。

Alternatively, you can create a new scratch file with the contents of the current selection in the editor. 或者,可以在编辑器中使用当前选择的内容创建新的草稿文件。Select some text or code, press Alt+Enter and then select Create new scratch file from selection. 选择一些文本或代码,按Alt+Enter,然后选择“从所选内容创建新的草稿文件”。WebStorm will attempt to detect the language of the selected fragment and use the appropriate type and extension. WebStorm将尝试检测所选片段的语言,并使用适当的类型和扩展名。If it can't detect the language of the selection, WebStorm will create the file with the same type and extension as the original file.如果WebStorm无法检测到选择的语言,它将创建与原始文件具有相同类型和扩展名的文件。

WebStorm numbers scratch files sequentially starting from 1. WebStorm编号从1开始按顺序抓取文件。If you close a tab with an empty scratch file, WebStorm deletes it.如果关闭带有空草稿文件的选项卡,WebStorm会将其删除。

Create scratch buffers创建草稿文件缓冲区

  • There is no dedicated menu item for the action to create a new scratch buffer, but you can use the Find Action popup Ctrl+Shift+A and run the New Scratch Buffer action.该操作没有专用的菜单项来创建新的草稿文件缓冲区,但您可以使用“查找操作”弹出菜单Ctrl+Shift+A并运行“新的草稿文件缓冲区”操作。

Find the New Scratch Buffer action

View the available scratches查看可用的草稿文件

  • To see the list of created scratch files and buffers, open the Project tool window and select Scratches and Consoles | Scratches:要查看已创建的草稿文件和缓冲区的列表,请打开“项目工具”窗口并选择“草稿和控制台”|“草稿”

    Scratches in the Project window

  • By default, scratch files and buffers are sorted alphabetically by their names and next by their extensions (types), which automatically means that buffer files always come first. 默认情况下,草稿文件和缓冲区按名称的字母顺序排序,然后按扩展名(类型)排序,这自动意味着“缓冲区”文件总是排在第一位。To sort scratch files by types, click the Show Options Menu button or right-click the Project tab and select Sort by Type:要按类型对草稿文件排序,请单击the Show Options Menu button或右键单击“项目”选项卡,然后选择“按类型排序”:

    The Sort by Type action
    WebStorm sorts scratches alphabetically based on their extensions:WebStorm根据扩展名按字母顺序对草稿文件进行排序:
    Scratches sorted by type

View documentation for scratches查看文档中的草稿文件

  • Select any scratch file or buffer and press Ctrl+Q to view quick documentation with the location, type, size, creation and modification date of the file.选择任何草稿文件或缓冲区,然后按Ctrl+Q以查看包含文件位置、类型、大小、创建和修改日期的“快速文档”。

Rename scratches重命名草稿文件

  1. In the Project tool window, click Scratches under Scratches and Consoles, select the file to rename, and press Shift+F6.在“项目工具”窗口中,单击“草稿文件和控制台”下的“草稿文件”,选择要重命名的文件,然后按Shift+F6

  2. In the dialog that opens, type the new name. 在打开的对话框中,键入新名称。Note that specifying another extension does not affect the file type, use the Change Language action for that.请注意,指定另一个扩展名不会影响文件类型,请为此使用“更改语言”操作。

See Renaming files and folders for details.有关详细信息,请参阅重命名文件和文件夹

Copy scratches复制草稿文件

  1. In the Project tool window, in the Scratches and Consoles | Scratches directory, select the scratch file to copy.

  2. Press F5. In the dialog that opens, type the new filename. F5。在打开的对话框中,键入新文件名。Here you can also specify another extension to change the file type. 在这里,您还可以指定另一个扩展名来更改文件类型。If necessary, select the folder where you want to save the copy.如有必要,请选择要保存副本的文件夹。

See Copying files and folders for details.有关详细信息,请参阅复制文件和文件夹

Move scratches移动草稿文件

  1. In the Project tool window, open the Scratches and Consoles | Scratches directory and select the scratch file to move.项目工具窗口中,打开“草稿文件和控制台”|“草稿文件目录”并选择要移动的草稿文件。

  2. Press F6 and select the target folder in the dialog that opens.F6键并在打开的对话框中选择目标文件夹。

See Moving files and folders for details.有关详细信息,请参阅移动文件和文件夹

Change the language of scratches

  1. In the Project tool window, click Scratches under Scratches and Consoles, and choose Change Language (<current language>) from the context menu of the scratch file to change the language for.

  2. Select the desired language. Note the following:

    • Four last used items are on top of the list, above the separator.

    • To narrow down the list, start typing the language name.

    • The Change Language action keeps extension in sync, if it exists.

You can assign a shortcut to the Change Language action as described in Configuring keyboard shortcuts.

Run and debug scratches

  1. Install the required packages and add the corresponding import statements to the scratch file. For example, to use JQuery:

    1. Open package.json and add the following packages to the dependencies property:

      "dependencies": { "jquery": "^3.5.0", "jsdom": "^16.2.2", "core-js": "^3.6.5", "nuxt": "^2.14.5" }
    2. From the context menu of package.json, select Run 'npm install'.

    3. Open your scratch file and add the following import statements:

      const {JSDOM} = require("jsdom"); const {window} = new JSDOM(""); const $ = require('jquery')(window);
  2. From the context menu of the scratch file, select Run <scratch_file_name>.

  3. To debug a scratch file, set the breakpoints as required and select Debug <scratch_file_name> from its context menu..

Run an SQL scratch file

To run an SQL scratch file, you need to have a connection to a data source. For this, attach a scratch file to the query console.

When you attach the console, WebStorm resolves the objects in your SQL code. As a result, you can navigate from the SQL code to the database tree.

  • Press Ctrl+Enter or click the Run button in the gutter. In the Sessions window, select a query console.

  • Click the <session>> on> switcher in the upper right corner of the window and select a console. The switcher menu includes consoles that are associated with the same language as your scratch file.

    If you want to attach a console from another DBMS, change the scratch file language. To change a language, right-click any area in the open scratch file and click Change Language.

  • Right-click any area in the open scratch file and click Execute. In the Sessions window, select a query console.

Last modified: 28 July 2021