How It All Works
This page describes the internal architecture of DFRefactor, for developers who add functions to the library. Users of the program do not need to read it.
The refactoring engine
The engine (cRefactorEngine, global handle ghoEngine) processes the source. When a run starts it receives a struct holding:
- the selected refactoring functions
- the set of source files to process
- run-time settings - Read Only, count-only, backup configuration
cRefactorEngine extends BusinessProcess. Errors that occur during a run do not stop the run. They are saved in the StatLog table, and you can read them afterwards with Tools > Logs > Error Log....
The class chain
cRefactorApplication handles startup, settings and workspace management. The function library itself is a chain of five classes, each adding one layer:
|
Class |
What it adds |
|
cRefactorUtilities |
Low-level reusable primitives - stack helpers, string and array utilities, scope-keyword detection, keyword-array lookups. |
|
cRefactorLexer |
The tokenizer, plus comment and string-literal detection, and the lexer state a function can ask about (in a procedure, in a class, inside a multi-line string). |
|
cRefactorExpressionParser |
Expression extraction - parenthesised and legacy gt/ge/lt/le/eq/ne - and boolean-indicator parsing. |
|
cBaseFuncLib |
Initialization and error handling: _InitializeFuncLib, _AddAllKeyWords, _ClearRefactoringProperties. |
|
cRefactorFuncLib |
RegisterInterface and {Description} meta-tag parsing. |
oRefactorFuncLib (global handle ghoFuncLib) is the instantiated object built on that chain, and holds every public refactoring function. New functions are added there.
The most important inherited helper is the tokenizer in cRefactorLexer: given one source line it produces a struct array of tokens - keyword, identifier, operator, string literal, comment. Refactoring functions work with these tokens rather than with plain text. This is why they work correctly with unusual spacing, comments and text in quotes.
How functions reach the list
When the program starts, it reads the meta-tags of the functions in oRefactorFuncLib.pkg. Every function marked { Published = True } is included, and the Functions table is updated from its meta-tags; no separate registration is needed. Add the function and its meta-tags, and restart the program; the function is then shown in Select Functions.
Refactoring function types
Each function declares its type with a { MethodType = ... } meta-tag inside its { Description } block. The engine uses it to decide what data to pass to the function:
|
MethodType |
Signature |
Per-call scope |
Documented on |
|
eStandardFunction |
String ByRef sLine, String sParameter, Returns Boolean |
One source line |
|
|
eRemoveFunction |
String ByRef sLine, String sParameter, Returns Boolean |
One source line, for deletions |
|
|
eEditorFunction |
String[] ByRef asCode, String sParameter, Returns Boolean |
Whole file, via the Scintilla editor |
|
|
eOtherFunction |
String[] ByRef asCode, String sParameter, Returns Integer |
Whole file, as a string array |
|
|
eOtherFunctionAll |
String[] ByRef asFiles, String sParameter, Returns Integer |
All selected files at once |
|
|
eReportFunction |
String[] ByRef asCode, String sParameter, Returns Integer |
Whole file; no modifications permitted |
|
|
eReportFunctionAll |
String[] ByRef asFiles, String sParameter, Returns Integer |
All selected files at once; no modifications permitted |
The Boolean that a Standard, Remove or Editor function returns tells whether the line or the file was changed. The Integer that an Other or Report function returns is a count, for example of changed lines or reported files.
Two notes on the table: the engine always passes sParameter, also to a Remove function. A function that does not need it can declare it and ignore it, or leave it out of the declaration, as two of the included Remove functions do. No function in the library currently uses eReportFunction: all reports are written as eReportFunctionAll, because a report is nearly always more useful for all selected files than for one file at a time.
Function parameters
A refactoring function takes at most two:
- The first always contains the source code: one line, or an array of lines, depending on the MethodType.
- The second, optional, is sParameter, which gets its value from the Parameter column in Select Functions. Declare its valid values with { EnumList }, its first-use default with { InitialValue }, and per-value help text with { ParamHelp }.
{ InitialValue } is only used the first time. A value that the user has chosen is kept when the program is restarted; the startup check uses the initial value only when the saved value is empty, or is no longer one of the { EnumList } values.
Adding a new function
- Add it to AppSrc/oRefactorFuncLib.pkg - or AppSrc/UserDefinedRefactorFunctions.pkg for personal additions, which can be marked { Private = True } to keep them out of the JSON export.
- Decorate it:
{ Published = True }
{ Description = """
Human-readable description of what the function does.
{ MethodType = eStandardFunction }
{ SummaryText = Short post-run summary text }
{ FnGroup = Modernization }
""" }
Every line inside the triple quotes must be indented at least as far as the closing """. If a line starts further to the left, the DataFlex 26 compiler stops without any error message, and the .err file is empty.
- { FnGroup } places it in the Select Functions grid - Modernization, Cleanup (shown as Tidy Up) or Unicode64 (shown as x64 + Unicode). Without it, the function is placed in Other.
- If the function fixes a compiler message, add { ModRecipe = "<msg#> | <match-text> | [category]" } so the Analyzer can offer it. The category is AutoFix, Suggest or Explain. Repeat the tag for a function that fixes more than one message.
- Add { EnumList }, { InitialValue } and { ParamHelp } if it takes an sParameter.
- { FnFamily = <name> } groups very similar converters into one table on their type's page, instead of giving each one its own section. Use it when the example explains everything and the descriptions would be nearly the same.
- { NoDoc = True } leaves a function out of this help, for example a function that the engine runs automatically on every line, or a function that is only meant for the developer.
- Add unit tests - a fixture in AppSrc/Tests/, or oUnit_Tests.pkg - and run UnitTests64.exe (compiled from UnitTests.src) to check nothing regressed.
Need a refactoring function that isn't in the library? RDC Tools International can design, implement and unit-test one for your codebase - an AI-assisted, author-reviewed process. See SUPPORT.md in the home folder, or the Feedback page.
Log files
When a run is finished, the engine writes its results to the main log, DFRefactorLogFile.txt, in the workspace's DFRefactor_Backup sub-folder. You can open it with Read log on the dashboard.
Some functions write their own logs; UnusedSourceFiles, for example, lists the files that are not used anywhere. You can open these logs with Read other logs, or with Tools > Logs > Other Log Files....
Errors that occur during a run are saved in the StatLog table, and you can read them with Tools > Logs > Error Log....