Skip to content

VBAToolKit feature : Normalization of source files

lucas-v edited this page Sep 25, 2013 · 4 revisions

As a lot of VBA coders know, the casing of identifiers is managed by the VBA IDE.

The problem

The VBA IDE automatically changes the casing of identifiers to keep it consistent among all the open modules.
The casing it chooses depends on the first occurrence of the identifier found by the IDE.

This is an annoying feature when working with version control, as the diffs are polluted by casing changes.

The solution

VBAToolKit has a function to normalize the casing of identifiers in a VBA source file, and automatically normalizes the source files of the modules it exports when saving.

How it works

VTK stores a list of identifiers with the desired casing.

An identifier is a token composed of alphanumeric characters and underscores that has to start with a letter.
For example :

  • in Err.number, Err and number are identifiers.
  • in foo(bar), foo and bar are identifiers.
  • _foobar is not an identifier, as it starts with an underscore.

The content of the source files is put through a lexical analyzer, so that all the identifiers found are compared case-unsensitively to the list stored by VTK. If an identifier matches an element of the list, it is replaced by this element, properly cased.

Comments and strings in the code (between quotes) are left untouched. A word in a comment or a string matching an identifier in the list will have its casing left as is.

Manage the list

The list is stored in the module vtkNormalize of VBAToolKit. It is stored as a String where the identifiers are separated by commas.

    properlyCasedIdentifiersString = _
    "Err" & "," & _
    "Dim" & "," & _
    ""

The simplest way of adding an identifier to the list is to copy and paste a line inside the declaration and change the identifier. To remove one, remove its line.