If you fi nd code focus really useful, even temporarily, and are tired of moving your cursor over the code folding ribbon, you can have code focus follow your current text selection by c
Trang 1134 ❘ CHAPTER 7 SYNTAX-AWARE EDITING
CODE FOLDING
Code folding highlights blocks of structured text and allows them to be collapsed — or
“ folded ” — so that you can concentrate on the higher - level structure of your code Code folding
is supported in all C - like languages (C + + , Objective - C, Java) and XML, as shown in Figure 7 - 5
To see and use code folding, you must have the Show Code Folding Ribbon option turned on
in the Text Editing pane of the Xcode Preferences
FIGURE 7-5
The code folding ribbon appears between the gutter and the text The depth of the blocks in your
text is graphed using gradations of grey; the darker the ribbon, the deeper the text in that line is
nested Hovering your cursor over the ribbon presents collapse controls for the block of text that
begins on that line
Code Focus
Code focus highlights a block of text in the editor, making it easier to see what the block
encompasses, as shown in the bottom window in Figure 7 - 5 Code focus appears when you hover
your cursor over the code folding ribbon Uncheck the Code Focus option in the preferences to
disable this behavior
If you fi nd code focus really useful, even temporarily, and are tired of moving your cursor over the
code folding ribbon, you can have code focus follow your current text selection by choosing View
➪ Code Folding ➪ Focus Follows Selection (Control + Option + Command + F) Now, the code focus
animation will continuously highlight the block containing the current selection or insertion point
Invoke the command a second time to toggle the feature off again
Trang 2Folding Code
You have four ways to fold a block of code, listed in the following table
Collapse button in ribbon Collapses highlighted block View ➪ Code Folding ➪ Fold Collapses block containing the current
selection View ➪ Code Folding ➪ Fold Methods/Functions Collapses the outmost block of every method
or function View ➪ Code Folding ➪ Fold Comment Blocks Collapses all multi - line comments
Folded code is collapsed and replaced with an ellipsis placeholder, as shown in Figure 7 - 6
FIGURE 7-6
The Fold command folds the block containing the current text selection or insertion point, and
is the only fold action that uses the current selection The Collapse button folds the highlighted block in the ribbon, and the other two commands apply globally to the entire fi le
Folding comment blocks only applies to multi - line comments (like / … * / ) The command does not recognize, or fold, successive lines of single - line comments ( // )
Have no fear; the text in the folded region is still there Including a folded block placeholder in a text selection includes all of the text in that block, just as if it weren ’ t folded
Unfolding Code
You have six ways to unfold code, listed in the following table
Trang 3136 ❘ CHAPTER 7 SYNTAX-AWARE EDITING
View ➪ Code Folding ➪ Unfold Expands blocks contained in the selected text
or in the selected block View ➪ Code Folding ➪ Unfold
Methods/Functions
Expands the outmost block of every method
or function View ➪ Code Folding ➪ Unfold Comment Blocks Expands all multi - line comments
View ➪ Code Folding ➪ Unfold All Expands all collapsed blocks
Display any text in a collapsed block Expands the block containing the text
The fi rst four actions are symmetric complements to the code folding actions in the previous section
In addition, the Unfold All command unfolds all collapsed blocks of code Finally, any action that
you take that causes some text to be selected or revealed — searching for text or jumping to a
symbol defi nition — automatically expands the block so that the text is visible
INDENTING TEXT
Over the years, programmers have developed coding styles that visually refl ect the structured nature
of the languages in which they are programming One of these conventions is indenting These
conventions have become so common and consistent, that many can be automated
Indenting is accomplished by starting a line with tab or space characters Using spaces to indent
lines is the most predictable method, but creates unnecessarily large fi les Tab characters are more
effi cient, but are problematic because there is no standard that dictates how many column positions
a tab character represents If you decide that tab stops should be every four columns and you
open a text fi le from a programmer who thought tabs should be every eight columns, the text won ’ t
line up correctly You ’ ll achieve the greatest compatibility by adopting four - column tab stops, which
seems to be the most common setting these days You can also alter the tab width for individual
fi les The default tab settings for fi les are confi gured in the Indentation tab of the
Xcode Preferences
Xcode ’ s tab width, indenting, and auto - formatting behaviors are all set globally in the preference ’ s
Indenting pane, shown in Figure 7 - 7 The tab widths are default for fi les, but individual fi les can
override these global settings The remaining options in this preferences pane are global and apply
to all source fi les
Trang 4Setting Tab Width and Indent Width
Xcode makes a distinction between the width of the tab stops and the amount by which each line
of code is indented The two numbers do not have to be the same The Tab Width setting, shown
in Figure 7 - 7, determines the width of the tab stops in characters The Indent Width setting is the number of columns ’ worth of white space added to the beginning of a line to indent it one level
Xcode uses the most effi cient combination of spaces and tabs that it can If the indent width is an integer multiple of the tab width, only tab characters are used to indent lines If not, a combination
of tabs and spaces is used to achieve the desired indentation Say you have a tab width of 8 and an indent width of 4 To indent a line one level requires four space characters Indenting two levels would cause these spaces to be replaced by a single tab
The global tab and indent width settings can be overridden for an individual fi le either in the Info window for the fi le, or by choosing the View ➪ Text ➪ Tab Settings command, both of which are shown in Figure 7 - 8
FIGURE 7-7
Trang 5138 ❘ CHAPTER 7 SYNTAX-AWARE EDITING
The Editor Uses Tabs option permits the use of tab characters to indent lines Turning it off forces
Xcode to use spaces for all indenting This is the safest option when you ’ re sharing code with other
programmers who might not be using the same tab width, but it also makes your fi les substantially
larger I also think it makes deleting the white space in lines awkward, and also makes it easier to
inadvertently align text at odd indentations
The Reset to Text Editing Defaults button returns the fi le to using the global settings in the
Indentation preferences panel File - specifi c editor settings are maintained for the fi le in its project
document The settings will not be remembered for fi les that are not assets of a project, and the
settings are independent of other projects
Automatic Indentation
Automatic indenting occurs whenever you start a new line by pressing the Return key The editor
inserts the same amount of indentation on the new line as was present on the line above it This
feature is always active To circumvent it for a single line, type Option ⫹ Return at the end of the
line Option + Return starts a new line with no indentation There is no simple option to turn off
auto - indentation globally, but it can be defeated by changing the key binding for the Return key, as
explained in Chapter 23
Syntax - Aware Indentation
Enabling Syntax - Aware Indenting in the Indentation pane of the Xcode Preferences turns on a
number of additional features that will aid you in indenting source code and forming code blocks
You can have Xcode do any of the following:
Automatically indent lines when you press the Tab key Automatically indent code blocks
Automatically create a balanced code block when you type a { character Automatically re - indent a line when you type one of several punctuation characters
Automatically indent C - style comments Each feature can be independently confi gured or disabled, as desired
Syntax - Aware Tab Indenting
The Tab Indents option determines what action occurs when you press the Tab key It has three
settings, as described in the following table
➤
➤
➤
➤
➤
Trang 6TAB INDENTS SETTING TAB KEY ACTION
In Leading White Space
When the text cursor is at the beginning, in, or immediately to the right of any white space at the beginning of the line, pressing the Tab key causes the entire line to be indented one level When not in this leading non – white space portion of the line, the Tab key inserts a single tab character
Always Regardless of where the cursor position is within the line, pressing the Tab
key causes the line to be re - indented
Never Tab key inserts a tab character
The In Leading White Space setting is the most useful when you ’ re programming in block -structured languages At the beginning of the line, pressing the Tab key indents the line Within the line, typically at the end of a statement where you want to begin a comment, the Tab key simply inserts a tab character
“ White space ” refers to the non - graphic, printable characters in the Unicode character set Practically, that means the tab, space, and end - of - line characters, but, technically, it also includes characters like the non - breaking space (Unicode
subsequent characters in the fi le to be shifted to new lines or column positions, but are not themselves visible In other words, they just leave a white space on the page
Note that the fi rst two settings are not variations on one another Xcode has two indentation functions: indent and re - indent Indent increases the indentation level of a line by inserting more white space Re - indention looks at the indention of the line with respect to the indentation of the line that precedes it, and uses syntax - aware rules to determine what its indentation level should be
Repeatedly indenting a line continues to shift the line further and further to the right Repeatedly
re - indenting a line does nothing Until you change the contents of the line, or the preceding line, Xcode repeatedly reformats the text to the same indentation level
You can circumvent the settings for the Tab key at any time by using Option + Tab or Control + I
Option + Tab always inserts a tab character, regardless of the syntax - aware tab settings Control + I always performs a re - indent of the currently selected line, or lines, and is the same as the Edit ➪ Format ➪ Re - indent command
Indenting Solitary “ { ”
Trang 7140 ❘CHAPTER 7 SYNTAX-AWARE EDITING
{
}
{
}
}
The indentation amount is always relative to the indentation of the previous line, not the indentation
level of the line containing the leading { character In other words, if the Indent Solo “ { ” setting is 4
characters and the line was already indented four characters more than the previous line, a leading
{ changes nothing In fact, the indentation level could decrease if the Indent Solo “ { ” setting is lower
than the nominal indentation amount for the line
Automatically Close { } Blocks
The Automatically Insert Closing “ } ” option does
two things when you type a { character: it inserts
a line with a matching } ahead of the cursor, and
then it advances to the next line as if the Return
key had been pressed The effect is simple: typing
a { creates a balanced block with the cursor
positioned at the fi rst line of the block Figure 7 - 9
shows an if statement before and after a single
{ character was typed
Controlling When Automatic
Indentation Occurs
The Automatically Indented Characters setting
selects the characters that trigger an automatic
re - indentation of the current line When a
character in the set is checked, typing one of FIGURE 7-9
Trang 8those characters is the same as invoking the Edit ➪ Format ➪ Re - indent command after typing the
character Note that this is a re indent , not an indent; Xcode reinterprets the line using its syntax
-aware rules and determines the appropriate indentation level for the line based on its new content
This may, or may not, change the indentation of the line This re - indentation occurs after the
character has been inserted Thus, the Return key calculates the indentation of the new line it just created, not the line the Return key was pressed on
Indenting Comments
The two // Comments options apply to C/C + + - style comments when a line is re - indented They are not applied when comments are being typed The Indent // Comments option treats // comments like any other program statement and indents them to the same level as the surrounding code
If off, // comments are left at whatever indentation level the programmer typed them The Align Consecutive // Comments option indents a // comment that appears alone on a line to the same indentation level as the // comment in the previous line This is most signifi cant when // comments are started to the right of a program statement and continued in another // comment on the next line The following three listings illustrate the effects of the two comment formatting options:
No // Comment Formatting
int main (int argc, char * const argv[]) {
// Check the environment
char * server_addr = getenv(“SERVER”);
if (server_addr==NULL) exit(3); // terminate immediately // returning a status of 3
Indent // Comments Only
int main (int argc, char * const argv[]) {
// Check the environment char * server_addr = getenv(“SERVER”);
if (server_addr==NULL) exit(3); // terminate immediately // returning a status of 3
Indent // Comments and Align Consecutive // Comments
int main (int argc, char * const argv[])
Trang 9142 ❘ CHAPTER 7 SYNTAX-AWARE EDITING
exit(3); // terminate immediately
// returning a status of 3
When you use both comment formatting options, you should get in the habit of inserting a blank
line before starting a new C/C + + - style comment on its own line Otherwise, it may get indented to
the level of the comment on the previous line
Re - indenting Text
As mentioned earlier, re - indenting text uses the language - specifi c rules to determine the appropriate
indentation level for each line This can happen automatically as you type, depending on which
syntax - aware indentation options you have enabled, or manually when you use Control + I or the
Edit ➪ Format ➪ Re - indent command It also happens whenever text is pasted from the clipboard
You can always manually alter the indentation level of a line, or lines, using the Edit ➪ Format ➪
Shift Right (Command + ]) and Edit ➪ Format ➪ Shift Left (Command + [) commands These either
increase or decrease the indentation of every line in the selection by indentation width Lines that are
already at the left margin cannot be decreased any further This is just a manual adjustment of the
amount of white space at the beginning of the line A subsequent re - indent recalculates the appropriate
indentation level based on the language rules, undoing any effect of Shift Right or Shift Left
CODE COMPLETION
The most sophisticated feature of syntax aware editing is Code Completion, Xcode ’ s auto
-completion technology Code -completion analyzes the context of the code you are typing and
interactively offers suggestions for completing the statement Code completion supports the C,
C + + , Objective - C, Objective - C + + , Java, and AppleScript languages It works by assembling a list of
symbol names from the Code Sense index that would be appropriate at that point in your code If
you are starting a new statement, the appropriate symbols will include any global variable, function,
class name, or language keyword, but if you typed a class or structure name, the appropriate symbols
would be only the member functions or instance variables in that class or structure Figure 7 - 10
shows the available completions for the CGSize structure nested inside a CGRect structure
FIGURE 7-10