Skip to content

Commit 1372f2a

Browse files
committed
v25.2 - Fix the delete action
1 parent 9b71c37 commit 1372f2a

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project are documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.25.2] - 2023-10-16
9+
10+
### Fixed
11+
12+
- Fix the delete action. It was doing an error-prone regex replacement. Parse the file as JSON and use `delete` instead.
13+
814
## [0.25.0, 0.25.1] - 2023-10-16
915

1016
### Changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"description": "View and edit all your snippets in one purty place. Yee-haw!",
99
"icon": "img/logo.png",
10-
"version": "0.25.1",
10+
"version": "0.25.2",
1111
"engines": {
1212
"vscode": "^1.4.0",
1313
"node": ">=12.0.0"

src/snippets-editor.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable class-methods-use-this */
12
// @ts-nocheck
23
/* eslint-disable import/no-unresolved, no-useless-escape, prefer-destructuring, no-template-curly-in-string */
34
const vscode = require("vscode");
@@ -48,20 +49,14 @@ class SnippetsEditor {
4849
/**
4950
* @async
5051
* Delete a snippet from a snippet file.
51-
* @param {vscode.Uri} uri Uri of snippet file
52+
* @param {vscode.Uri} uri URI of the snippet file you want to delete the snippet from.
5253
* @param {String} snippetName The name of the snippet to delete
5354
* @returns {Promise} The TextEditor that contains the user snippets document.
5455
*/
5556
async deleteSnippet(uri, snippetName) {
56-
let text = await SnippetsFetcher.getData(uri.fsPath);
57-
58-
let escapedSnippetName = Util.escapeStringRegexp(snippetName);
59-
let regex = new RegExp(
60-
`[\r\n]*?"${escapedSnippetName}".*?:.*?\\{.*?\\},{0,1}`,
61-
"ms"
62-
);
63-
let newText = text.replace(regex, "");
64-
await fs.promises.writeFile(uri.fsPath, newText);
57+
let json = await SnippetsFetcher.getJson(uri.fsPath);
58+
delete json[snippetName];
59+
await fs.promises.writeFile(uri.fsPath, JSON.stringify(json, null, 2));
6560
}
6661
}
6762

src/snippets-fetcher.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ class SnippetsFetcher {
149149
return Promise.all(json);
150150
}
151151

152+
/**
153+
* Get the contents of the files as JSON objects. The files can be JSON or JSONC
154+
* files (Microsoft JSON with comments standard).
155+
* @param {Array} filepaths
156+
* @returns {Promise} A Promise with an array of JSON objects.
157+
*/
158+
static async getJson(filepath) {
159+
let data = await SnippetsFetcher.getData(filepath);
160+
let json = await jsonc.parse(data);
161+
return json;
162+
}
163+
152164
/**
153165
* @async
154166
* @static

0 commit comments

Comments
 (0)