Skip to content

Commit

Permalink
Unexpected behavior when no text is selected with JSON options
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
carlocardella committed Jan 25, 2023
1 parent cce3b50 commit a901d35
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ See the [list of open enhancements on GitHub](https://github.com/carlocardella/v

## Log

## [2.13.1] - 2023-01-25

### Fixed

* [#38 Unexpected behavior when no text is selected with JSON options](https://github.com/carlocardella/vscode-TextToolbox/issues/38)

## [2.13.0] - 2023-01-24

### Fixed
Expand Down
35 changes: 27 additions & 8 deletions src/modules/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getActiveEditor, getTextFromSelection } from "./helpers";
import * as jsonic from "jsonic";
import { Selection } from "vscode";

/**
* Transforms a text string into proper JSON format, optionally can fix syntax errors.
Expand All @@ -13,7 +14,15 @@ export async function stringifyJson(fixJson: boolean): Promise<void> {
}

editor.edit((editBuilder) => {
editor.selections.forEach((s) => {
let selections: Selection[] = [];

if (editor.selection.isEmpty) {
selections.push(new Selection(0, 0, editor.document.lineCount, editor.document.lineAt(editor.document.lineCount - 1).text.length));
} else {
selections = editor.selections.map((s) => s);
}

selections.forEach((s) => {
let newJson: string = "";
let selectionText = getTextFromSelection(editor, s);
if (fixJson) {
Expand All @@ -40,7 +49,15 @@ export async function minifyJson(): Promise<void> {
}

editor.edit((editBuilder) => {
editor.selections.forEach((s) => {
let selections: Selection[] = [];

if (editor.selection.isEmpty) {
selections.push(new Selection(0, 0, editor.document.lineCount, editor.document.lineAt(editor.document.lineCount - 1).text.length));
} else {
selections = editor.selections.map((s) => s);
}

selections.forEach((s) => {
let selectionText = getTextFromSelection(editor, s);
let newJson = JSON.stringify(jsonic(selectionText!));

Expand All @@ -57,13 +74,15 @@ export function escapeWin32PathInJson() {
return;
}

editor.edit((editBuilder) => {
editor.selections.forEach((selection) => {
const text = getTextFromSelection(editor, selection);
const newText = text!.replace(/\\/g, "\\\\");
editBuilder.replace(selection, newText);
if (!editor.selection.isEmpty) {
editor.edit((editBuilder) => {
editor.selections.forEach((selection) => {
const text = getTextFromSelection(editor, selection);
const newText = text!.replace(/\\/g, "\\\\");
editBuilder.replace(selection, newText);
});
});
});
}

return;
}

0 comments on commit a901d35

Please sign in to comment.