The most complete pycharm tutorial (a) code folding for the--pycharm editor feature

Source: Internet
Author: User

1. Theme

In some cases, if some code is less important, we can collapse it into one line through the Pycharm code folding feature. In the next section we will introduce the basic usage of code folding functionality.

2, the code can be folded contour line and folding switch

First, let's look at the folding line on the left side of the code. This thin line is displayed on the left side of the code, marking the code block area. When the code is in an open state, the beginning and end of the line show the folding switch and; When the code block is folded, the two switch flags are merged into one, the collapsed code displays only the first row, and the other lines are hidden behind the three-point number. Click the sign of the plus sign to expand the collapsed code.

Hover the mouse pointer over the folding line, and the collapsed line is bold:

Note that here we can cancel the collapse of the first display. Single Open Settings dialog box, under the Editor node, click the code Folding page to cancel the show code folding outline check box. When the folding line is not visible, we can only use the menu command (Code | Folding | Expand/collapse) or shortcut keys to achieve the collapse of the code.

3, browse the hidden code after folding

If you want to be able to view the contents of the collapsed code without expanding the code block, the operation is very simple, just hover the mouse pointer over the three-point number, and Pycharm will pop up a temporary window to show the contents of the collapsed code:

Pycharm also defaults to display a complete code block that exceeds the current editing area. As the following illustration shows: The code section at the beginning of the file is not visible in the current editing environment (beyond the current edit box display). But we don't have to drag the scroll bar to browse that part of the display, just hover the mouse over the code block end tag, and Pycharm will automatically pop up the window to fully display the current block of code:

4, the default code block collapse rule

By default, the implementation portion of the class and function is marked first, and the collapse is done for this block of code, which collapses a class, a function, by default.

5. Folding arbitrary Code piece

Suppose you want to fold a few scattered programs that do not belong to the default collapsible code block (not a complete class or function).

First select the piece of code you want to collapse

Then do the following (select one of three):

Select Code in main Menu | Folding | Fold selection/remove Region menu command.

Right-click the selected code slice and select Folding in the shortcut menu | Fold Selection/remove Region

Press the ctrl+period shortcut key

The selected code slice is collapsed and hidden.

It should be noted that the selected code slice should be in the class or function body, if we select a class or function at the beginning of the definition of the part of the code can not be folded hidden.

6, use two line annotation to annotate the code

Pycharm provides two types of logical code block wrapping annotation Surround:

Visual Studio Mode:

NetBeans mode:

The specific actions are as follows:

(1) Select the code block to surround the annotation

(2) The following operation two select a

Using Code | Surround with main menu command

Press the ctrl+alt+t

(3) Select the annotation style you want in the pop-up shortcut menu:

(4) Enter some necessary annotation descriptions

When you wrap a piece of code around a comment in this way, the collapse switch appears to the left of the comment line:

Click the collapse switch to collapse the corresponding code slice, showing only the added wrapping comment information:

See here for more information on the code folding feature.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.