Make Variables Global
This extension allows you to easily globalize local variables and manage a global variables section in your files. Works for JavaScript and TypeScript.
Features
1. Globalize Variable
Quickly turn a local variable into a global one. This command will:
- Move the variable declaration to the designated global variables section.
- Leave the assignment in place if there is one.
Global variables section
- If there is a
// GLOBAL VARIABLES
section at the top of the file, the global variables will be placed there.
- If not, it will place the global variables at the top of the file after the
import
statements.
2. Insert Global Variables Marker
Insert a comment to designate where global variables should be placed. This command will:
- Insert a
// GLOBAL VARIABLES
comment at the current cursor position.
- If a global variables marker already exists in the file, it will scroll to its location instead of inserting a new one.
Usage
Globalizing a Variable
- Place your cursor on a line with a variable declaration (e.g.,
let x = 5;
)
- Use one of these methods:
- Press
Ctrl+Shift+G
(or Cmd+Shift+G
on macOS)
- Open the Command Palette (
Ctrl+Shift+P
or Cmd+Shift+P
on macOS), type "Globalize Variable" and select the command
Inserting a Global Variables Marker
- Place your cursor where you want to insert the marker
- Use one of these methods:
- Press
Ctrl+Shift+M
(or Cmd+Shift+M
on macOS)
- Open the Command Palette, type "Insert Global Variables Marker" and select the command
Keyboard Shortcuts
By default, this extension provides the following keyboard shortcuts:
Ctrl+Shift+G
(or Cmd+Shift+G
on macOS): Globalize Variable
Ctrl+Shift+M
(or Cmd+Shift+M
on macOS): Insert Global Variables Marker
You can customize these shortcuts in VS Code:
- Open the Keyboard Shortcuts editor (
File > Preferences > Keyboard Shortcuts
on Windows,Code > Settings > Keyboard Shortcuts
on macOS)
- Search for "Globalize Variable" or "Insert Global Variables Marker"
- Click on the pencil icon next to the command and enter your preferred key binding
Configuration
This extension contributes the following settings:
variableGlobalization.maxLinesToSearch
: The maximum number of lines to search for inserting global variables or finding the global marker from the top of the file. Default is 60.
You can modify this setting in your VS Code settings:
- Open Settings (
File > Preferences > Settings
)
- Search for "Variable Globalization"
- Adjust the "Max Lines to Search" value as needed
Alternatively, you can add this to your settings.json
file:
{
"variableGlobalization.maxLinesToSearch": 100
}
Known Issues
- The extension currently only supports JavaScript and TypeScript files.
- It may not work correctly with complex or multi-line variable declarations.
License
This project is licensed under the MIT License.