Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Code folding

From Wikipedia, the free encyclopedia
Tool of editors for programming, scripting and markup
Not to be confused withConstant folding.
Code folding example onPHP code withVim

Code ortext folding, or less commonlyholophrasting,[1] is a feature of somegraphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest. It is typically used with documents which have a naturaltree structure consisting of nested elements. Other names for these features includeexpand and collapse,code hiding, andoutlining. InMicrosoft Word, the feature is called "collapsible outlining".

Many user interfaces providedisclosure widgets for code folding in a sidebar, indicated for example by a triangle that points sideways (if collapsed) or down (if expanded), or by a[-] box for collapsible (expanded) text, and a[+] box for expandable (collapsed) text.

Code folding is found intext editors,source code editors, andIDEs. The folding structure typically follows thesyntax tree of the program defined by thecomputer language. It may also be defined by levels ofindentation, or be specified explicitly using an in-band marker (saved as part of the source code) or out-of-band.

Text folding is a similar feature used on ordinary text, where the nested elements consist of paragraphs, sections, oroutline levels. Programs offering this includefolding editors,outliners, and someword processors.

Data folding is found in somehex editors and is used to structure a binary file or hide inaccessible data sections.[2]

Folding is also frequently used indata comparison, to select one version or another, or only the differences.

History

[edit]

The earliest known example of code folding in an editor is inNLS.[3] Probably the first widely available folding editor was the 1974 Structured Programming Facility (SPF) editor forIBM 370 mainframes, which could hide lines based on their indentation. It displayed on character-mapped 3270 terminals.[4] It was very useful for prolix languages likeCOBOL. It evolved into the Interactive System Productivity Facility (ISPF).

Use

[edit]

Code folding has varioususe patterns, primarily organizing code or hiding less useful information so one can focus on more important information. Common patterns follow.[5]

Outlining

[edit]

Most basically, applications use code folding to outline source code, collapsing each block to a single line. This can be only top-level blocks like functions and classes, nested blocks like nested functions and methods, or all blocks, notably control-flow blocks. This allows one to get an overview of code, easily navigating and rearranging it, and to drill down into more detail as needed, without being distracted by other code. Viewing-wise, this allows one to quickly see a list of all functions (without their bodies), while navigation-wise this replaces extensive paging past long functions – or searching for the target – with going directly to the next function.

Hiding boilerplate code

[edit]

Some languages or libraries require extensiveboilerplate code. This results in extremely long code, which can obscure the main point. Further, substantive code can be lost in the boilerplate.

For example, in Java a single private field with a getter and setter requires at least 3 lines, if each is on a separate line:

privateStringname=null;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}

This expands to 10 lines with conventional function line breaks and spacing between functions (including trailing newline):

privateStringname=null;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}

Documentation with Javadoc expands this to 20 lines:

/** * Property <code>name</code>  readable/writable. */privateStringname=null;/** * Getter for property <code>name</code> */publicStringgetName(){returnname;}/** * Setter for property <code>name</code>. * @param name */publicvoidsetName(Stringname){this.name=name;}

If there are many such fields, the result can easily be hundreds of lines of code with very little "interesting" content – code folding can reduce this to a single line per field, or even to a single line for all fields. Further, if all routine fields are folded, but non-routine fields (where getter or setter is not just returning or assigning a private field) are not folded, it becomes easier to see the substantive code.

Collapsing metadata

[edit]

Metadata can be lengthy, and is generally less important than the data it is describing. Collapsing metadata allows one to primarily focus on the data, not the metadata. For example, a long list ofattributes inC# may be manually collapsed as follows:[6]

#region Attributes[Browsable(false)][MergableProperty(false)][DefaultValue(null)][PersistenceMode(PersistenceMode.InnerProperty)][TemplateContainer(typeof(MyType))][TemplateInstance(TemplateInstance.Single)]#endregionpublicITemplateContentTemplate{get;set;}

The resulting code displays as:

AttributespublicITemplateContentTemplate{get;set;}

Collapsing comments

[edit]

Comments are a form of human-readable metadata, and lengthy comments can disrupt the flow of code. This can be the case either for a long comment for a short section of code, such as a paragraph to explain one line, or comments fordocumentation generators, such asJavadoc orXML Documentation. Code folding allows one to have long comments, but to display them only when required. In cases where a long comment has a single summary line, such as Python docstrings, the summary can still be displayed when the section is collapsed, allowing a summary/detailed view.

Showing structure or sandwich code in structured programming

[edit]

Structured programming consists of nested blocks of code, and long blocks of code – such as long switch statements – can obscure the overall structure. Code folding allows one to see the overall structure and expand to a specific level. Further, in some uses, particularly strict structured programming (single function exit), there are code patterns that are hard to see when looking at expanded code. For example, inresource management in structured programming, one generally acquires a resource, followed by a block of code using the resource, and finishing with releasing the resource. The acquisition/release pairing is hard to see if there is a long block of code in between, but easy to see if the intervening block is folded. Similarly, in conditional code likeif...then...else, secondary blocks may be far from the condition statement.

Grouping code

[edit]

Fold groups can be used to group code, either by explicit grouping – similar to comment blocks separating a module into sections, or class members into associated groups – or implicitly, such as by automatically grouping class members by access level.

Hiding legacy code

[edit]

Legacy code – or any code that a developer does not wish to view or change at a given point in time – can be folded away so that programmers can concentrate on the code under consideration.

Hiding in-source data tables

[edit]

Conventions

[edit]

In order to support code folding, the text editor must provide a mechanism for identifying "folding points" within a text file. Some text editors provide this mechanism automatically, while others provide defaults that can either be overridden or augmented by the user.

There are various mechanisms, coarsely divided as automatic and manual – do they require any specification by the programmer? Folding points are usually determined with one or more of the following mechanisms. Each of these has its own distinct advantages and difficulties, and it is essentially up to the developers who create the text editor software to decide which to implement. Text editors that provide support for multiple folding mechanisms typically allow the user to choose which is most appropriate for the file being edited.

Syntax-dependent

[edit]

Syntax-dependent folding points are those that rely on the content of the file being edited in order to specify where specific folding regions should begin and end. Syntax-based folding points are typically defined around any or all of the standard sub-features of themarkup language orprogramming language in use. These are desirable due to being automatic and agreeing with code structure, but may require significant work to implement, and time to compute when editing a file.

Indentation-based

[edit]

Indentation-based folding points are generally specified by the position and sequence of non-printing whitespace, such as tabs and spaces, within the text. This is most often used as a simple form of syntax-based folding, as indentation almost always reflects nesting level inindent styles for structured programming languages.

This convention is particularly suitable to syntaxes that have anoff-side rule, so the structure largely agrees with the indent. Examples includePython and text files that require indentation as a rule by themselves. However, even in these cases, structure does not exactly agree with indent, such as inline continuation, and thus syntax-dependent folding is preferred.

Token-based

[edit]

Token-based folding points are specified using specialdelimiters that serve no other purpose in the text than to identify the boundaries of folding points. This convention can be compared to indentation-based folding points, where printable characters are used instead of whitespace. The most common delimiter tokens are{{{ to begin the folded section, and}}} to end it.

Another notable token is#region (C# directives), respectively#Region (Visual Basic directives), used inMicrosoft Visual StudioCode Editor. These are treated syntactically ascompiler directives, though they do not affect compilation.

As a manual method, token-based folding allows discretion in grouping code based on arbitrary criteria, such as "functions related to a given task", which cannot be inferred from syntactic analysis.

Token-based folding requires in-band signalling, with folding tokens essentially being structured comments, and unlike other methods, are present in the source code and visible to other programmers. This allows them to be shared, but also requires their use (or preservation) by all programmers working on a particular file, and can cause friction and maintenance burden.

User-specified

[edit]

User-specified folding allows the user to fold sections of text using a generic selection method, but without changing the source code (out-of-band), instead being specified only in the editor. For example, a programmer may select some lines of text and specify that they should be folded. Folded text might be anonymous or named, and this may be preserved across editing sessions or discarded. Unlike token-based folding, this does not change the source text – it thus is not shared with other editors of the file, and is not visible in the code.

Examples

[edit]

The following document contains folding tokens ({{{ ... }}}):

 Heading 1 {{{ Body }}} Heading 2 {{{ Body }}} Heading 3 {{{ Body }}}

When loaded into a folding editor, the outline structure will be shown:

 Heading 1 {{{ ... Heading 2 {{{ ... Heading 3 {{{ ...

Usually clicking on the{{{ marks makes the appropriate body text appear.

Software with code folding capability

[edit]

One of the earliest folding editors wasSTET, an editor written for theVM/CMS operating system in 1977 byMike Cowlishaw. STET is a text editor (for documentation, programs, etc.) which folds files on the basis of blocks of lines; any block of lines can be folded and replaced by a name line (which in turn can be part of a block which itself can then be folded).

A folding editor appeared in theoccamIDEcirca 1983, which was called theInmos Transputer Development System (TDS)[7],.[8] The "f" editor (in list below) probably is the most intact legacy from this work.

TheMacintosh computer historically had a number ofsource code editors that "folded" portions of code via "disclosure triangles". TheUserLand Software product Frontier is a scripting environment that has this capability.[9]

Folding is provided by many modern text editors, andsyntax-based or semantics-based folding is now a component of manysoftware development environments. Editors include:

NameTokenIndentationSyntaxUser
ABAP EditorYes?Yes?
AkelPad??Yes?
Anjuta IDE?YesYes?
Atom[a]?Yes?Yes
BBEdit??Yes?
BracketsPlug-inYesYesNo
CodeanywhereYesYesYes?
CodenvyYesYesYes?
Code::Blocks IDEYesYesYesYes
Cubic IDEYesYesYesYes
CudaText????
Delphi IDEYes?Yes?
Dreamweaver???Yes
Eclipse??Yes?
EditPlusNoYesNoNo
EmacsYes[b]?[c]Yes[d]Yes[e]
EmEditor Professional?YesYes?
FlashDevelop IDE??Yes?
geany?YesYes?
geditYesYesYes?
ISPF?Yes?Yes
JEDYesYes[f]?No
jEditYesYesYesYes
KateYesYesYesYes
MATLABNoNoYesNo
MS Visual StudioYesYesYesYes
NetBeans IDEYesYesYesYes
Notepad++?YesYesYes
NuSphere PHPEd??YesYes
Qt Creator??Yes?
SciTEYesYesYes?
STET[g]????
TextMateYesYesYesYes
UltraEditNoNoYesYes
VimYesYesYesYes
Visual Expert??Yes?
Visual Studio CodeYesYesYesNo
XcodeYesYesYesYes
Zend Studio????


Other editors

[edit]

See also

[edit]

Notes

[edit]
  1. ^http://flight-manual.atom.io/using-atom/sections/folding/
  2. ^Token-based folding is implemented by thefolding minor mode. One can also useoutline andallout minor modes for sectioning program sources.
  3. ^One can use theset-selective-display function in Emacs to hide lines based on the indentation level, as suggested in theUniversal code folding note.
  4. ^Syntax-dependent folding is supported by theoutline andallout modesfor special dedicatedoutline-syntaxes; by thehideshow minor mode for some programming languages; also, by thesemantic-tag-folding minor mode and thesenator-fold-tag command forsyntaxes supported bysemantic (a component of CEDET), as well as bydoc-mode forJavaDoc orDoxygen comments, byTeX-fold-mode,sgml-fold-element command,nxml-outln library in the corresponding language-specific modes, and possibly in other modes for particular syntaxes. Sometimes, the standard simpleoutline minor mode is used to simulate syntax-based folding, cf. theuse of it in properly indented Emacs Lisp source code, theuse of it (see near the end of the page) for properly indented HTML. Several folding mechanisms are unified by thefold-dwim interface. See alsoCategoryHideStuff.
  5. ^Folding of user-selected regions in Emacs is implemented by thehide-region-hide command.
  6. ^Theset_selective_display function may be used to hide lines indented beyond a specified amount.
  7. ^STET may have been the first text editor that supported folding[citation needed]

References

[edit]
  1. ^Simon Gauvin, Omid Banyasad, "Transparency, holophrasting, and automatic layout applied to control structures for visual dataflow programming languages", inProceedings of the 2006 ACM symposium on Software visualization, p. 67–75
  2. ^"HxD - Freeware Hex Editor and Disk Editor".mh-nexus. Retrieved2007-04-30.
  3. ^Marcel (Jul 9, 2012),"The Mother of All Demos, presented by Douglas Engelbart (1968)",YouTube, retrieved2019-12-29
  4. ^Saint-flour, Gilbert (25 Jun 1998)."ISPF history".Planet MVS. Retrieved2015-10-27.
  5. ^Atwood 2008.
  6. ^Rob (March 19, 2014)."Now, this is interesting, because I tend to use #region more to HIDE THE CRAP (XML documentation etc, long list of attributes etc.) making it easier to see the important code…".The Problem With Code Folding. Coding Horror Discussion.Archived from the original on Aug 6, 2020.
  7. ^North American Transputer Users Group. Conference (2nd : 1989 : Durham, N.C.) (1990).Transputer research and applications, 2 : NATUG-2, proceedings of the Second Conference of the North American Transputer Users Group, October 18-19, 1989, Durham, NC. Board, John A., Duke University. Amsterdam: IOS Press. p. 85.ISBN 9051990278.OCLC 35478471.{{cite book}}: CS1 maint: numeric names: authors list (link)
  8. ^Cormie, David (1986)."INMOS Technical Note 03 - Getting started with the TDS"(PDF).transputer.net. Retrieved2019-07-19.
  9. ^"Outliners.com". Archived fromthe original on 2006-12-23. Retrieved2006-12-27.
  10. ^LEXX – A programmable structured editorIBM Journal of Research and Development, Vol 31, No. 1, 1987, IBM Reprint order number G322-0151

External links

[edit]
Wikimedia Commons has media related toCode folding.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Code_folding&oldid=1305109443"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp