Refactoring the TTextPrinter Component


In the last section, you saw how to use the Declare Variable command to refactor the GetText method and create a new variable from an undeclared identifier. In this portion of the chapter, you'll learn a bit more about refactoring.

Refactoring is the name of a set of commands that enable you to restructure your source code without changing its functionality. Besides the Declare Variable refactoring that you've already used, you're going to see how to use the following refactorings:

  • Rename Symbol

  • Extract Resourcestring

  • Extract Method

Renaming Symbols

Rename Symbol refactoring enables you to rename an identifier and all its references. For instance, to rename the Font property PrinterFont, right-click the Font identifier in the Code Editor and select Refactoring ® Rename property 'Font'. Selecting the Rename property 'Font' command displays the following dialog box.

image from book
Figure 27-9: The Rename symbol dialog box

If the View references before refactoring check box is checked, two things occur:

  • The IDE displays the Refactorings window (see Figure 27-10), which contains the list of all code portions that are affected by the selected refactoring.

    image from book
    Figure 27-10: The Refactorings window

  • The refactoring itself is postponed until you choose to either apply or discard it.

When you apply the refactoring by clicking the Refactor button on the Refactorings window, the refactoring remains in the list, in case you opt to undo it. If you don't plan to undo the applied refactoring, you can remove it by clicking either the Remove Refactoring or Remove All Refactorings buttons.

Sync Edit

Delphi's Code Editor also allows you to rename an identifier by switching to the Sync Edit mode. If the identifier is local and you can easily select it, you should rename it using the Sync Edit feature. If the identifier you wish to rename is a field, a property, or a similar identifier that is used throughout the unit, you should rename it using Rename Symbol refactoring.

To enter the Sync Edit mode, you first have to select a portion of source code in the Code Editor. When you select a block of code, the Code Editor's left margin displays the Sync Edit icon as shown in Figure 27-11.

image from book
Figure 27-11: The Sync Edit icon

To enter the Sync Edit mode, click the Sync Edit icon in the Code Editor's left margin. When you enter the Sync Edit mode, the Code Editor outlines all duplicate identifiers that can be renamed (see Figure 27-12). To rename an identifier and all of its occurrences in the selected portion of the code, simply type over the existing identifier.

image from book
Figure 27-12: The Sync Edit mode

Extracting Resource Strings

Extract Resource String refactoring enables you to extract a string from the source code and create a resource string in the resourcestring section of the unit. Extract Resource String refactoring also creates the resourcestring section if one doesn't exist. Modifying the strings in the resourcestring section doesn't require you to recompile the application.

For instance, if you want to extract the 'class(' string from the Special- Cases function, right-click on the string and select Refactoring ® Extract Resource String 'class(' on the context menu. The Extract Resource String dialog box that appears allows you to define the resourcestring's name.

image from book
Figure 27-13: Extracting resource strings

If you click OK in the Extract Resource String dialog box, it will make the following changes to the source code:

implementation resourcestring   StrClass = 'class('; ... function SpecialCases(const AReservedWord: string): Boolean; var   lowWord: string; begin   lowWord := LowerCase(AReservedWord);   Result := (Pos(StrClass, lowWord) > 0) or (Pos('array[', lowWord) > 0); end;

Extracting Methods

The refactoring that you are most likely to use is Extract Method. It allows you to select a portion of a routine and extract it to another one. The TTextPrinter's Print method is the perfect target for Extract Method refactoring, because it is large and contains a code segment that can very successfully be extracted to another method. This code segment deals with applying the font settings for comments and reserved words.

image from book
Figure 27-14: The code segment that should be extracted with Extract Method refactoring

To extract the selected code segment from the Print method to another method, right-click the selected code segment and select Refactoring ®Extract Method on the context menu. The Extract Method dialog box that appears (see Figure 27-15) allows you to see what the new method will look like and to rename it. You should name the new method ApplyFormatting and press OK to have Extract Method refactoring create it.

image from book
Figure 27-15: The Extract Method dialog box

Extract Method refactoring makes the following changes to the source code:

TTextPrinter = class(TComponent) private   { Private declarations }   procedure ApplyFormatting(comment: TCommentType; oneWord: string); end; ... procedure TTextPrinter.ApplyFormatting(comment: TCommentType;   oneWord: string); begin   with Printer.Canvas.Font do   begin     { assume we're printing "normal text" }     Style := [];     Color := clBlack;     { check for a reserved word, but only if neither a              single-line nor a block comment are opened }     if (comment = ctNoComment) and IsReservedWord(oneWord) then     begin       Style := Style + [fsBold];       Color := clNavy;     end;     { check if the word is inside a block or a single-line              comment and render appropriately }     if comment <> ctNoComment then     begin       Style := Style + [fsItalic];       Color := clGreen;     end;   end; end; procedure TTextPrinter.Print; begin     ...       else if Pos('{', oneWord) > 0 then         comment := ctBlockComment;       ApplyFormatting(comment, oneWord);       { get the word's width }       wordWidth := Printer.Canvas.TextWidth(oneWord);     ... end;



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net