From a6e30c72b8ae9a333310e5cc8893ed9e274cf3df Mon Sep 17 00:00:00 2001 From: nisedo <73041332+nisedo@users.noreply.github.com> Date: Wed, 10 Dec 2025 10:03:11 +0100 Subject: [PATCH] feat: support reverting only selected text in Revert Tree String to Text Previously, the "Revert Tree String to Text" command would always revert the entire document. This change makes the command revert only the selected text when a selection exists, while preserving the original behavior of reverting the entire file when no text is selected. --- CHANGELOG.md | 4 ++++ src/extension.ts | 30 +++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5168367..c0e7c97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## [Unreleased] +### Changed + +- `Revert Tree String to Text` now only reverts selected text when a selection exists, instead of always reverting the entire file + ## [2.0.0] - 2024-12-22 Finally found my vscode market account😭. This version mainly improve configuration names, docs and coding styles, with some breaking changes([!15](https://github.com/aprilandjan/vscode-ascii-tree-generator/pull/15) by [@mahyarmirrashed](https://github.com/mahyarmirrashed)). diff --git a/src/extension.ts b/src/extension.ts index a84594d..84a8f61 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -123,16 +123,28 @@ export function activate(context: vscode.ExtensionContext) { ); return; } - const text = editor.document.getText(); + + let text: string; + let range: vscode.Range; + + if (editor.selection.isEmpty) { + // No selection: revert entire document + text = editor.document.getText(); + const firstLine = editor.document.lineAt(0); + const lastLine = editor.document.lineAt(editor.document.lineCount - 1); + range = new vscode.Range( + 0, + firstLine.range.start.character, + editor.document.lineCount - 1, + lastLine.range.end.character + ); + } else { + // Selection exists: revert only selected text + text = editor.document.getText(editor.selection); + range = editor.selection; + } + const reverted = revertTreeString(text); - const firstLine = editor.document.lineAt(0); - const lastLine = editor.document.lineAt(editor.document.lineCount - 1); - const range = new vscode.Range( - 0, - firstLine.range.start.character, - editor.document.lineCount - 1, - lastLine.range.end.character - ); editor.edit((edit) => { edit.replace(range, reverted); });