From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI User Group Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

LabWindows/CVI Tip: Tools to Automate Source Code Formatting

Create Cleaner Code with Batch Formatting in LabWindows/CVI 2013

To help developers create cleaner, more readable code, LabWindows/CVI 2013 provides customizable batch formatting and auto-indentation tools as well as an API to automate the process of formatting multiple files. For consistency, developers can choose from common indentation and bracket placement styles and use a preview window to see an example of their customized format selections.

  • Method 1: New Formatting Tools in the IDE
  • Method 2: Programatic Formatting using the LabWindows/CVI API

Method 1: New Formatting Tools in the IDE

Find Formatting Options in Editor Preferences

The Editor Preferences dialog box lets you configure Formatting and Tab spacing options for your LabWindows/CVI workspace. Select Options»Editor Preferences. The Enable auto indent checkbox configures LabWindows/CVI to indent text in .c and .h files automatically. Auto indent determines the indentation of code as you type, based on the surrounding context. You can specify indentation options in the Formatting Options dialog box or the Formatting Options (Legacy) dialog box. LabWindows/CVI also provides batch auto indent with the Edit»Format File or Edit»Format Selection menu items. With batch auto indent, you can format entire files or lines of files at one time.

Formatting_Options.png

Fig 1. Access Editor Preferences from the Options menu.

To  maintain consistency with older projects that are loaded in LabWindows/CVI 2013, you can enable the Use CVI legacy formatting option to select formatting options available in versions of LabWindows/CVI prior to 2013. When you enable this option, you can open the Formatting Options (Legacy) dialog box, where you can specify bracket styles for functions and statements. Note that if you enable Use CVI legacy formatting, you cannot automatically format an entire file or selection of lines. If you do not enable Use CVI Legavy formatting, then the Formatting Options button opens the Formatting Options dialog box.

Set Formatting Options

The Formatting Options dialog box provides a preview of code to help you select your preferred Bracket Style (location of curly brackets) and specify which statement types to apply formatting.

Code_formatting_options_CVI_2013.png

Fig 2. Configure bracket styles in the Formatting Options dialog box.

Format your Code

To auto-indent specified lines, select lines of text in the Source window, and select Edit»Format Selection or Right-click in the code and select Format Selection from the menu. To auto-indent an entire file, select Edit»Format File or Right-click in the code and select Format File when no code is highlighted.

Format_Code.png

Fig 3. Right-click in the source file to Format the entire file or selected lines of code.


Format_File.png

Fig 4. Formatting can also be performed from the Edit menu.


Method 2: Programatic Formatting using the LabWindows/CVI API

The LabWindows/CVI 2013 automation API contains new functions that allow you to programmatically perform formatting on speficic files or projects.

Using the SetFormattingOptions function to configure the indentation formatting. This function accepts inputs for bracket style and all the same formatting options available throught the Formatting Options dialog box.

Next, use the FormatFileSelection function to specify a source file and location in the source file to apply indentation formatting using the SetFormattingOptions function. Use the GetFormattingOptions function to get the current indentation format options of the source file.

Demo Code

Use the attached demo code to see an example of programmatic formatting.

FormatDemo.png

Did you find this tip useful? Rate this document or add a comment below.

If you give this a try, share your experience! Add a comment below.

National Instruments
Contributors