Skip to content

Commit

Permalink
v25.2 - Fix the delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
robole committed Oct 16, 2023
1 parent 9b71c37 commit 1372f2a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.25.2] - 2023-10-16

### Fixed

- Fix the delete action. It was doing an error-prone regex replacement. Parse the file as JSON and use `delete` instead.

## [0.25.0, 0.25.1] - 2023-10-16

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"description": "View and edit all your snippets in one purty place. Yee-haw!",
"icon": "img/logo.png",
"version": "0.25.1",
"version": "0.25.2",
"engines": {
"vscode": "^1.4.0",
"node": ">=12.0.0"
Expand Down
15 changes: 5 additions & 10 deletions src/snippets-editor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable class-methods-use-this */
// @ts-nocheck
/* eslint-disable import/no-unresolved, no-useless-escape, prefer-destructuring, no-template-curly-in-string */
const vscode = require("vscode");
Expand Down Expand Up @@ -48,20 +49,14 @@ class SnippetsEditor {
/**
* @async
* Delete a snippet from a snippet file.
* @param {vscode.Uri} uri Uri of snippet file
* @param {vscode.Uri} uri URI of the snippet file you want to delete the snippet from.
* @param {String} snippetName The name of the snippet to delete
* @returns {Promise} The TextEditor that contains the user snippets document.
*/
async deleteSnippet(uri, snippetName) {
let text = await SnippetsFetcher.getData(uri.fsPath);

let escapedSnippetName = Util.escapeStringRegexp(snippetName);
let regex = new RegExp(
`[\r\n]*?"${escapedSnippetName}".*?:.*?\\{.*?\\},{0,1}`,
"ms"
);
let newText = text.replace(regex, "");
await fs.promises.writeFile(uri.fsPath, newText);
let json = await SnippetsFetcher.getJson(uri.fsPath);
delete json[snippetName];
await fs.promises.writeFile(uri.fsPath, JSON.stringify(json, null, 2));
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/snippets-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ class SnippetsFetcher {
return Promise.all(json);
}

/**
* Get the contents of the files as JSON objects. The files can be JSON or JSONC
* files (Microsoft JSON with comments standard).
* @param {Array} filepaths
* @returns {Promise} A Promise with an array of JSON objects.
*/
static async getJson(filepath) {
let data = await SnippetsFetcher.getData(filepath);
let json = await jsonc.parse(data);
return json;
}

/**
* @async
* @static
Expand Down

0 comments on commit 1372f2a

Please sign in to comment.