Skip to content

Commit 4153e18

Browse files
committed
Changed to ingore empty lines with a selection
1 parent 92a53b7 commit 4153e18

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
**Nested** is a VSCode extension designed to make working with **Markdown** files easier. With **Nested**, you can quickly **nest and un-nest items** in your Markdown documents by adding or removing **4 spaces** at the beginning of selected lines.
66

7+
## Key Features
8+
- **Add 4 spaces** to the beginning of selected lines to **nest** items.
9+
- **Remove 4 spaces** from the beginning of selected lines to **un-nest** items.
10+
- **Keyboard shortcuts** for both actions.
11+
- **No indentation** is applied to **empty lines**.
12+
713
## Installation
814

915
### Option 1: Install from the VSCode Marketplace:
@@ -36,8 +42,8 @@
3642
- **Headings**: Adjust nested headings in Markdown documents.
3743
- **Code blocks**: Quickly indent or un-indent code or inline code sections.
3844

39-
## License:
45+
## License
4046
This extension is licensed under the MIT License.
4147

42-
## Contributing:
48+
## Contributing
4349
If you'd like to contribute to **Nested**, feel free to fork the repository and submit a pull request!
Binary file not shown.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nested",
33
"displayName": "Nested",
44
"description": "A VSCode extension for nesting and un-nesting items in Markdown.",
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"publisher": "AdamOvera",
77
"engines": {
88
"vscode": "^1.99.0"
@@ -29,13 +29,13 @@
2929
"keybindings": [
3030
{
3131
"command": "nested.addIndent",
32-
"key": "ctrl+shift+space",
32+
"key": "ctrl+shift+space",
3333
"mac": "cmd+shift+space",
3434
"when": "editorTextFocus"
3535
},
3636
{
3737
"command": "nested.removeIndent",
38-
"key": "ctrl+shift+backspace",
38+
"key": "ctrl+shift+backspace",
3939
"mac": "cmd+shift+backspace",
4040
"when": "editorTextFocus"
4141
}
@@ -65,4 +65,4 @@
6565
"@vscode/test-cli": "^0.0.10",
6666
"@vscode/test-electron": "^2.4.1"
6767
}
68-
}
68+
}

src/extension.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ import * as vscode from 'vscode';
22

33
export function activate(context: vscode.ExtensionContext) {
44

5-
// Command to add 4 spaces to the beginning of selected lines
5+
// Command to add 4 spaces to the beginning of selected lines (skip empty lines)
66
let addSpacesCommand = vscode.commands.registerCommand('nested.addIndent', () => {
77
const editor = vscode.window.activeTextEditor;
88

99
if (editor) {
1010
const selection = editor.selection;
1111
const selectedText = editor.document.getText(selection);
1212

13-
// Add 4 spaces to the beginning of each line in the selection
14-
const indentedText = selectedText.split('\n').map(line => ' ' + line).join('\n');
13+
// Add 4 spaces to the beginning of each line in the selection, but skip empty lines
14+
const indentedText = selectedText.split('\n').map(line => {
15+
// Skip empty lines
16+
if (line.trim() === "") {
17+
return line; // Return the line unchanged if it's empty
18+
}
19+
return ' ' + line; // Add 4 spaces to non-empty lines
20+
}).join('\n');
1521

1622
// Replace the selected text with the indented version
1723
editor.edit(editBuilder => {
@@ -20,16 +26,23 @@ export function activate(context: vscode.ExtensionContext) {
2026
}
2127
});
2228

23-
// Command to remove 4 spaces from the beginning of selected lines
29+
// Command to remove 4 spaces from the beginning of selected lines (skip empty lines)
2430
let removeSpacesCommand = vscode.commands.registerCommand('nested.removeIndent', () => {
2531
const editor = vscode.window.activeTextEditor;
2632

2733
if (editor) {
2834
const selection = editor.selection;
2935
const selectedText = editor.document.getText(selection);
3036

31-
// Remove 4 spaces from the beginning of each line in the selection
32-
const unindentedText = selectedText.split('\n').map(line => line.startsWith(' ') ? line.slice(4) : line).join('\n');
37+
// Remove 4 spaces from the beginning of each line in the selection, but skip empty lines
38+
const unindentedText = selectedText.split('\n').map(line => {
39+
// Skip empty lines
40+
if (line.trim() === "") {
41+
return line; // Return the line unchanged if it's empty
42+
}
43+
// Remove 4 spaces from non-empty lines that start with 4 spaces
44+
return line.startsWith(' ') ? line.slice(4) : line;
45+
}).join('\n');
3346

3447
// Replace the selected text with the unindented version
3548
editor.edit(editBuilder => {

0 commit comments

Comments
 (0)