A simple VS Code extension that copies selected text with formatted line numbers and an optional file path header.
Features
Line Numbering:
Prepends each line of the selected text with its corresponding line number. The line numbers are formatted to align based on the total number of lines (e.g., 2 digits for 10–99 lines, 3 digits for 100–999 lines).
Optional File Path:
When enabled, the extension can insert the relative file path as the first line of the copied text.
Customizable Spacing:
Configurable option to add extra spaces after the line numbers for better readability.
Requirements
VS Code version 1.96.0 or later.
A workspace with a valid file open.
Extension Settings
This extension contributes the following settings to VS Code:
copyWithLineNumbers.addFilePath Type: Boolean Default: false Description: Include the file's relative path as a header in the copied text.
copyWithLineNumbers.addExtraSpaces Type: Boolean Default: true Description: Insert two extra spaces after each line number.
How It Works
Select Text:
Highlight the code or text in your editor.
Run Command:
Trigger the command Copy with line numbers from the Command Palette (Cmd+Shift+P on macOS or Ctrl+Shift+P on other platforms).
Copy to Clipboard:
The extension processes the selection by:
Calculating the appropriate width for line numbers based on the total lines (using logarithmic calculation).
Formatting each line with a padded line number and optional extra spaces.
Optionally prepending the file’s relative path.
Copying the final formatted text to the clipboard.
Paste:
You can now paste the formatted text wherever needed.
Debugging & Development
Running the Extension:
Press F5 to launch a new VS Code window with your extension loaded and test your changes.
Testing:
A sample unit test is provided in src/test/formatting.test.ts. Run tests using the VS Code testing view or via the command line with npm run test.
Release Notes
0.0.1:
Initial release of the extension with customizable line numbering and optional file path header.