Skip to content
| Marketplace
Sign in
Visual Studio Code>Programming Languages>TCL Expect FormatterNew to Visual Studio Code? Get it now.
TCL Expect Formatter

TCL Expect Formatter

Klaus Gerlicher

|
6 installs
| (0) | Free
Formats TCL expect files according to GDB/GNU coding standards
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

TCL Expect Formatter

A Visual Studio Code extension that formats TCL expect files according to GDB/GNU coding standards.

Features

  • Automatic Formatting: Format TCL expect files with proper indentation and GNU-style conventions
  • Advanced Syntax Highlighting: Professional-grade syntax highlighting with 200+ GDB-specific procedures
  • GDB Testsuite Support: Comprehensive highlighting for gdb.exp library functions with categorized coloring
  • Machine Interface (MI) Support: Special highlighting for GDB Machine Interface commands and variable objects
  • Configurable Options: Customizable formatting preferences to match your project requirements
  • GNU Standards Compliance: Follows GNU coding standards including 79-character line limits
  • String-Aware Formatting: Preserves content within quoted strings while formatting code structure
  • Expect Command Support: Special formatting for expect-specific commands like spawn, send, expect, interact

Installation

From VSIX (Recommended for development)

  1. Clone or download this repository
  2. Install dependencies:
    npm install
    
  3. Compile the extension:
    npm run compile
    
  4. Package the extension:
    npx vsce package
    
  5. Install the generated .vsix file in VS Code:
    • Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
    • Type "Extensions: Install from VSIX"
    • Select the generated .vsix file

From VS Code Marketplace

(Once published)

  1. Open VS Code
  2. Go to Extensions (Ctrl+Shift+X)
  3. Search for "TCL Expect Formatter"
  4. Click Install

Usage

Automatic Formatting

The extension automatically detects files with the following extensions:

  • .exp
  • .expect
  • .tcl (when containing expect commands)

To format a file:

  1. Open a TCL expect file
  2. Right-click and select "Format Document"
  3. Or use the keyboard shortcut: Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac)
  4. Or use the Command Palette: Ctrl+Shift+P → "Format Document"

Manual Formatting

You can also use the dedicated command:

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  2. Type "TCL Expect Formatter: Format TCL Expect File"
  3. Press Enter

VS Code Integration Benefits

Enhanced Development Experience

  • IntelliSense: Auto-completion for GDB testsuite procedures
  • Quick Info: Hover over functions to see context information
  • Go to Symbol: Navigate through procedures and test functions
  • Bracket Matching: Automatic closing of braces, brackets, and quotes
  • Comment Toggling: Use Ctrl+/ to toggle line comments

Code Navigation

  • Function Outline: View all procedures in the Explorer sidebar
  • Symbol Search: Ctrl+Shift+O to jump to any function
  • Breadcrumbs: See current scope and navigate file structure
  • Folding: Collapse/expand code blocks and regions

Visual Improvements

  • Color Coding: Different colors for different types of GDB procedures
  • Semantic Highlighting: Context-aware highlighting based on function category
  • Error Prevention: Visual distinction between similar function names
  • Readability: Enhanced contrast for complex expect patterns and regex

Configuration

Configure the formatter through VS Code settings (File → Preferences → Settings):

{
  "tclExpectFormatter.indentSize": 4,
  "tclExpectFormatter.maxLineLength": 79,
  "tclExpectFormatter.insertFinalNewline": true,
  "tclExpectFormatter.trimTrailingWhitespace": true
}

Available Settings

Setting Type Default Description
indentSize number 4 Number of spaces for indentation
maxLineLength number 79 Maximum line length (GNU standard)
insertFinalNewline boolean true Insert final newline at end of file
trimTrailingWhitespace boolean true Remove trailing whitespace

Syntax Highlighting Configuration

The extension provides comprehensive syntax highlighting that you can customize to match your preferences:

VS Code Color Theme Settings

The extension uses standard TextMate scopes, so you can customize colors through your VS Code color theme:

Built-in Theme Customization

  1. Open VS Code Settings (Ctrl+, or Cmd+,)
  2. Search for "colorCustomizations"
  3. Add custom colors for specific scopes:
{
  "workbench.colorCustomizations": {
    "editor.tokenColorCustomizations": {
      "textMateRules": [
        {
          "scope": "support.function.gdb.core.tcl",
          "settings": {
            "foreground": "#ff6b6b",
            "fontStyle": "bold"
          }
        },
        {
          "scope": "support.function.gdb.breakpoint.tcl",
          "settings": {
            "foreground": "#ff9500",
            "fontStyle": "bold"
          }
        },
        {
          "scope": "support.function.gdb.assertion.tcl", 
          "settings": {
            "foreground": "#51cf66",
            "fontStyle": "bold"
          }
        },
        {
          "scope": "support.function.gdb.session.tcl",
          "settings": {
            "foreground": "#fd7e14",
            "fontStyle": "bold"
          }
        },
        {
          "scope": "support.function.mi.session.tcl",
          "settings": {
            "foreground": "#339af0",
            "fontStyle": "italic"
          }
        }
      ]
    }
  }
}

Available Highlighting Scopes

The extension defines these specific scopes you can customize:

GDB Function Categories

Scope Functions Description
support.function.gdb.core.tcl gdb_test, gdb_test_multiple, gdb_test_sequence Core test functions
support.function.gdb.session.tcl clean_restart, gdb_start, gdb_exit, gdb_spawn Session management
support.function.gdb.execution.tcl runto, gdb_continue, arrive_at_instruction Program execution
support.function.gdb.assertion.tcl pass, fail, xfail, gdb_assert Test assertions
support.function.gdb.compilation.tcl gdb_compile, build_executable, prepare_for_testing Compilation functions
support.function.gdb.breakpoint.tcl gdb_breakpoint, delete_breakpoints, gdb_get_line_number Breakpoint functions
support.function.gdb.values.tcl get_integer_valueof, get_var_address, get_sizeof Value retrieval
support.function.gdb.communication.tcl send_gdb, gdb_expect, gdb_expect_list Communication
support.function.gdb.utility.tcl with_test_prefix, multi_line, string_to_regexp Utility functions
support.function.gdb.platform.tcl is_x86_64_m64_target, allow_cplus_tests Platform detection
support.function.gdb.language.tcl set_lang_fortran, allow_ada_tests Language support
support.function.gdb.requirements.tcl require, allow_hw_breakpoint_tests Requirements/feature tests

Machine Interface (MI)

  • support.function.mi.session.tcl - MI session management (mi_gdb_start, mi_clean_restart)
  • support.function.mi.testing.tcl - MI testing functions (mi_gdb_test, mi_run_cmd)
  • support.function.mi.breakpoint.tcl - MI breakpoints (mi_create_breakpoint, mi_delete_breakpoints)
  • support.function.mi.varobj.tcl - MI variable objects (mi_create_varobj, mi_varobj_update)

Language Elements

  • keyword.control.expect.tcl - Expect commands (expect, send, spawn)
  • keyword.control.tcl - TCL control structures (if, while, for, proc)
  • variable.other.tcl - Variables ($var, ${complex_var})
  • string.quoted.double.tcl - Double-quoted strings
  • comment.line.number-sign.tcl - Comments

Theme-Specific Configuration

For Dark Themes

{
  "workbench.colorCustomizations": {
    "[Default Dark+]": {
      "editor.tokenColorCustomizations": {
        "textMateRules": [
          {
            "scope": "support.function.gdb.core.tcl",
            "settings": {
              "foreground": "#4FC1FF",
              "fontStyle": "bold"
            }
          },
          {
            "scope": "support.function.gdb.assertion.tcl",
            "settings": {
              "foreground": "#52C41A", 
              "fontStyle": "bold"
            }
          }
        ]
      }
    }
  }
}

For Light Themes

{
  "workbench.colorCustomizations": {
    "[Default Light+]": {
      "editor.tokenColorCustomizations": {
        "textMateRules": [
          {
            "scope": "support.function.gdb.core.tcl",
            "settings": {
              "foreground": "#0066CC",
              "fontStyle": "bold"
            }
          }
        ]
      }
    }
  }
}

Quick Color Scheme Examples

Professional Scheme

"textMateRules": [
  {
    "scope": "support.function.gdb.core.tcl",
    "settings": { "foreground": "#0084FF", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.gdb.breakpoint.tcl",
    "settings": { "foreground": "#FF6600", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.gdb.assertion.tcl",
    "settings": { "foreground": "#00C851", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.gdb.session.tcl",
    "settings": { "foreground": "#9C27B0", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.mi.session.tcl",
    "settings": { "foreground": "#FF8800", "fontStyle": "italic" }
  },
  {
    "scope": "keyword.control.expect.tcl",
    "settings": { "foreground": "#AA00FF", "fontStyle": "bold" }
  }
]

High Contrast Scheme

"textMateRules": [
  {
    "scope": "support.function.gdb.core.tcl",
    "settings": { "foreground": "#FF0040", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.gdb.assertion.tcl", 
    "settings": { "foreground": "#00FF00", "fontStyle": "bold" }
  },
  {
    "scope": "support.function.gdb.execution.tcl",
    "settings": { "foreground": "#FFFF00", "fontStyle": "bold" }
  }
]

Semantic Highlighting Control

You can also control semantic highlighting:

{
  "editor.semanticHighlighting.enabled": true,
  "editor.semanticTokenColorCustomizations": {
    "enabled": true
  }
}

The highlighting automatically applies to all .exp, .expect, and .tcl files when the extension is active!

Quick Test - Verify Your Highlighting Works

Create a test file with .exp extension and paste this code to see all the different highlighting categories:

# Test different GDB function categories
gdb_test "print var" "= 42" "core test"           # support.function.gdb.core.tcl
gdb_breakpoint "main"                              # support.function.gdb.breakpoint.tcl  
clean_restart $binfile                             # support.function.gdb.session.tcl
runto_main                                         # support.function.gdb.execution.tcl
pass "test passed"                                 # support.function.gdb.assertion.tcl
gdb_compile "test.c" "test" executable {debug}    # support.function.gdb.compilation.tcl
with_test_prefix "subtest" { gdb_test "print 1" } # support.function.gdb.utility.tcl

# Machine Interface
mi_gdb_start                                       # support.function.mi.session.tcl
mi_create_breakpoint "func"                        # support.function.mi.breakpoint.tcl

# Standard TCL/Expect
expect { "prompt" { send "cmd\r" } }              # keyword.control.expect.tcl
if {$var == "test"} { puts "found" }              # keyword.control.tcl

Each function should have different colors based on your configuration!

Formatting Rules

This extension applies the following GNU/GDB formatting standards:

Indentation

  • Uses 4-space indentation by default
  • Properly indents nested blocks (if, while, for, proc, etc.)
  • Maintains consistent indentation for continuation lines

Line Length

  • Enforces 79-character line limit (GNU standard)
  • Automatically wraps long lines with proper continuation
  • Breaks lines at logical points (operators, spaces, brackets)

Code Structure

  • Formats expect blocks with proper brace alignment
  • Standardizes spacing around operators (=, {, })
  • Ensures consistent formatting for procedure definitions
  • Properly formats control structures (if, while, for, foreach)

Comments

  • Ensures single space after # in comments
  • Preserves comment content while standardizing format
  • Maintains proper indentation for block comments

Example Formatting

Before:

proc test_function{arg1 arg2}{
if{$arg1=="value"}  {
send "command\\r"
expect{
"prompt>"{
send "another command\\r"
}
timeout{puts "Timeout occurred"}
}
}
}

After:

proc test_function {arg1 arg2} {
    if {$arg1 == "value"} {
        send "command\\r"
        expect {
            "prompt>" {
                send "another command\\r"
            }
            timeout {
                puts "Timeout occurred"
            }
        }
    }
}

Syntax Highlighting Features

The extension provides comprehensive syntax highlighting specifically designed for GDB testsuite development:

GDB Testsuite Library Support (200+ Procedures)

Core Testing Functions

  • gdb_test, gdb_test_multiple, gdb_test_sequence, gdb_test_exact
  • gdb_test_no_output, gdb_test_stdio, gdb_test_multiline

Session Management

  • clean_restart, gdb_start, gdb_exit, gdb_spawn
  • gdb_reinitialize_dir, gdb_unload, gdb_interact

Program Execution

  • runto, runto_main, gdb_run_cmd, gdb_continue
  • gdb_continue_to_breakpoint, arrive_at_instruction

Test Assertions

  • pass, fail, xfail, xpass, kfail, kpass
  • untested, unsupported, unresolved
  • gdb_assert, setup_kfail, setup_xfail

Compilation & Building

  • gdb_compile, gdb_compile_ada, gdb_compile_pthreads
  • build_executable, prepare_for_testing, standard_testfile

Machine Interface (MI) Commands

  • mi_gdb_start, mi_gdb_test, mi_run_cmd, mi_create_breakpoint
  • mi_create_varobj, mi_varobj_update, mi_list_varobj_children

Platform & Feature Detection

  • allow_cplus_tests, allow_hw_breakpoint_tests, is_x86_64_m64_target
  • supports_reverse, using_fission, require

Utility Functions

  • with_test_prefix, proc_with_prefix, multi_line
  • string_to_regexp, get_integer_valueof, delete_breakpoints

Advanced Language Features

String Highlighting

  • Variable interpolation: $var, ${complex_var}, $::namespace::var
  • Escape sequences: \n, \r, \\, \{, \}
  • Regex patterns: Special highlighting for -re patterns in expect blocks

TCL Language Support

  • Control structures: if, while, for, foreach, switch, proc
  • Operators: Comparison (==, !=), arithmetic (+, -, *), logical (&&, ||)
  • Data types: Numbers (int, float, hex), booleans, lists, arrays

Code Structure

  • Comments: # line comments with proper scoping
  • Brackets: Context-aware highlighting for [], {}, ()
  • Procedures: Function names and definitions
  • Namespaces: ::namespace::procedure highlighting

Supported TCL/Expect Commands

The formatter and highlighter recognize:

  • Expect commands: expect, send, spawn, interact, exp_continue
  • Control structures: if, while, for, foreach, switch
  • Procedures: proc, namespace
  • Variables: set, global, variable, array
  • I/O commands: puts, gets, open, close

Development

Building from Source

  1. Clone the repository:

    git clone <repository-url>
    cd tcl-expect-formatter
    
  2. Install dependencies:

    npm install
    
  3. Compile TypeScript:

    npm run compile
    
  4. Run in development mode:

    • Press F5 in VS Code to launch Extension Development Host

Testing

The extension includes comprehensive formatting logic that handles:

  • Nested code blocks
  • Line continuation with backslashes
  • Comment preservation and formatting
  • Variable substitution in strings
  • Complex expect patterns

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly with various TCL expect files
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

Changelog

1.0.11

  • Documentation Fix: Updated README to correctly reflect default indent size of 4 spaces
  • Fixed configuration examples and formatting samples to match actual defaults
  • Corrected settings table and description text

1.0.10

  • Bug Fix: Fixed closing brace indentation for incomplete selections
  • When formatting selections that start inside a block (missing the opening brace), closing braces were being over-indented
  • Improved bracket analysis to properly dedent closing braces from starting indentation level

1.0.9

  • Major Rewrite: Redesigned selection formatting with comprehensive bracket analysis
  • Added analyzeBracketStructure() method to map all brackets in selection
  • Uses bracket stack to match opening/closing braces correctly
  • Determines proper indentation based on complete bracket structure
  • Added BracketInfo interface for structured bracket tracking

1.0.8

  • Selection Formatting: Improved bracket indentation for partial selections
  • Refined logic for handling closing braces in partial selections
  • Closing braces that match opening braces within selection: dedent normally
  • Closing braces that close blocks from before selection: dedent to starting level

1.0.7

  • Selection Fix: Fixed bracket indentation for partial selections
  • Added tracking of opening braces within the selection
  • Only dedent closing braces when they have matching opening braces in selection
  • Ensures proper indentation when formatting partial code blocks

1.0.6

  • Operator Fix: Fixed == operator formatting issue
  • The formatter was incorrectly splitting == operators into = = by processing each = character individually
  • Updated formatOperatorsOutsideStrings to properly detect and handle comparison operators (==, !=, <=, >=) as single units
  • Preserves assignment operator (=) formatting

1.0.5

  • Selection Formatting: Fixed format selection indentation issue
  • Improved handling of partial code block selections
  • Better context calculation for range formatting

1.0.4

  • Major Feature: Added comprehensive syntax highlighting for 200+ GDB testsuite procedures
  • GDB Integration: Complete support for gdb.exp library functions with categorized highlighting:
    • Core testing functions (gdb_test, gdb_test_multiple, etc.)
    • Session management (clean_restart, gdb_spawn, etc.)
    • Program execution (runto, gdb_continue, etc.)
    • Compilation and building (gdb_compile, build_executable, etc.)
    • Test assertions (pass, fail, gdb_assert, etc.)
  • Machine Interface Support: Full MI command highlighting (mi_gdb_test, mi_create_varobj, etc.)
  • Advanced Features: Platform detection, feature support, and utility function highlighting
  • Language Configuration: Enhanced auto-completion, bracket matching, and code folding
  • TextMate Grammar: Professional-grade syntax highlighting with context-aware scoping

1.0.3

  • Bug Fix: Fixed whitespace insertion inside quoted strings
  • Formatter now preserves content within strings (e.g., "= \\{0 <repeats 64 times>\\}" no longer becomes "= \\ {0 <repeats 64 times>\\}")
  • Improved string-aware formatting for braces and operators

1.0.2

  • Fixed indentation logic for commands after comments
  • Disabled long line wrapping by default
  • Added CLAUDE.md for development guidance

1.0.1

  • Added LICENSE and package repository information

1.0.0

  • Initial release
  • Basic TCL expect formatting
  • GNU coding standards compliance
  • Configurable formatting options
  • Basic syntax highlighting support
  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft