Skip to content

Commit

Permalink
v1.5.1 - Fix error when no workspace is open
Browse files Browse the repository at this point in the history
  • Loading branch information
robole committed Nov 18, 2023
1 parent b5c069d commit 1bb9a2f
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 60 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ 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).

## [1.5.1] - 2023-11-18

### Fixed

- A "cannot read properties of undefined (reading '0')" error is thrown when the command `Snippets Ranger: Show me that dur Range, Partner` is run and no workspace is open. Fixed this scenario.

### Changed

- If a snippets files has invalid data or is empty, the file is show in the view with the description "No snippets".
- Improved error handling for rejected promises from `glob`.

## [1.5.0] - 2023-11-10

### Changed
Expand Down
80 changes: 62 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.webp",
"version": "1.5.0",
"version": "1.5.1",
"engines": {
"vscode": "^1.4.0",
"node": ">=12.0.0"
Expand Down
42 changes: 31 additions & 11 deletions src/core/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ function fetch(globalStoragePath) {
* @returns {Array} An array of `SnippetsFile` objects
*/
async function fetchProjectSnippetsFiles() {
let projectPaths = await env.getProjectSnippetPaths();
let snippetsFiles = await fetchSnippetsFiles(projectPaths);
let paths = [];

try {
paths = await env.getProjectSnippetPaths();
} catch (error) {
console.log(`No project files. Error: ${error}`);
}

let snippetsFiles = await fetchSnippetsFiles(paths);
return snippetsFiles;
}

Expand All @@ -19,8 +26,15 @@ function fetch(globalStoragePath) {
* @returns {Array} An array of `SnippetsFile` objects
*/
async function fetchUserSnippetsFiles() {
let userPaths = await env.getUserSnippetPaths(globalStoragePath);
let snippetsFiles = fetchSnippetsFiles(userPaths);
let paths = [];

try {
paths = await env.getUserSnippetPaths(globalStoragePath);
} catch (error) {
console.log(`No user files. Error: ${error}`);
}

let snippetsFiles = fetchSnippetsFiles(paths);
return snippetsFiles;
}

Expand All @@ -29,8 +43,15 @@ function fetch(globalStoragePath) {
* @returns {Array} An array of `SnippetsFile` objects
*/
async function fetchAppSnippetsFiles() {
let appPaths = await env.getAppSnippetPaths();
let snippetsFiles = await fetchSnippetsFiles(appPaths);
let paths = [];

try {
paths = await env.getAppSnippetPaths();
} catch (error) {
console.log(`No app files. Error: ${error}`);
}

let snippetsFiles = await fetchSnippetsFiles(paths);
return snippetsFiles;
}

Expand Down Expand Up @@ -70,15 +91,14 @@ function fetch(globalStoragePath) {

async function fetchSnippetsFiles(paths) {
let array = [];

let parsedCollectionPromised = paths.map((path) => parser.parse(path));
let collection = await Promise.all(parsedCollectionPromised);

collection.forEach((snippetsJson, index) => {
if (snippetsJson !== undefined) {
let snippetsArray = toSnippetsArray(snippetsJson);
let snippetsFile = new SnippetsFile(paths[index], snippetsArray);
array.push(snippetsFile);
}
let snippetsArray = toSnippetsArray(snippetsJson);
let snippetsFile = new SnippetsFile(paths[index], snippetsArray);
array.push(snippetsFile);
});

return array;
Expand Down
13 changes: 10 additions & 3 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ const jsonc = require("jsonc-parser");

/**
* @async
* Asynchronously parse the entire contents of a file as JSON. The file can be formatted as JSON or
* Asynchronously parse the contents of a file as JSON. The file can be formatted as JSON or
* JSONC (Microsoft's JSON with comments standard).
* @param {String} path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @returns {Object}
*/
async function parse(path) {
let data = await fs.promises.readFile(path, { encoding: "utf-8" });
let json = await jsonc.parse(data);
let json = {};

try {
let data = await fs.promises.readFile(path, { encoding: "utf-8" });
json = await jsonc.parse(data);
} catch (err) {
console.error(`Parsing error: ${err.message}`);
}

return json;
}

Expand Down
22 changes: 12 additions & 10 deletions src/core/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ const Extension = require("../model/extension");
function toSnippetsArray(snippetsObj) {
let array = [];

Object.entries(snippetsObj).forEach(([key, value]) => {
let snippet = new Snippet(
key,
value.prefix,
value.body,
value.description,
value.scope
);
array.push(snippet);
});
if (snippetsObj !== undefined) {
Object.entries(snippetsObj).forEach(([key, value]) => {
let snippet = new Snippet(
key,
value.prefix,
value.body,
value.description,
value.scope
);
array.push(snippet);
});
}

return array;
}
Expand Down
1 change: 0 additions & 1 deletion src/helper/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");

/**
* If the file exists, it does nothing. If the file does not exist, it will create a new file with an empty object.
Expand Down
44 changes: 29 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,42 @@ const styles = require("./view/styles.css");
async function activate(context) {
context.subscriptions.push(
vscode.commands.registerCommand("snippets-ranger.show", async () => {
let view = View(context);

let fetcher = fetch(context.globalStoragePath);

let projectSnippetsFiles = await fetcher.fetchProjectSnippetsFiles();
let userSnippetsFiles = await fetcher.fetchUserSnippetsFiles();
let appSnippetsFiles = await fetcher.fetchAppSnippetsFiles();
let extensions = await fetcher.fetchExtensions();

view.load(
projectSnippetsFiles,
userSnippetsFiles,
appSnippetsFiles,
extensions
);
await showView(context);
}),

vscode.commands.registerCommand("snippets-ranger.add", async () => {
await addSnippet();
})
);
}

async function showView(context) {
let view = View(context);
let { globalStoragePath } = context;

if (globalStoragePath === undefined || globalStoragePath === "") {
await vscode.window.showErrorMessage(
`The context.globalStoragePath variable is not defined: ${globalStoragePath}`
);

return;
}

let fetcher = fetch(globalStoragePath);

let projectSnippetsFiles = await fetcher.fetchProjectSnippetsFiles();
let userSnippetsFiles = await fetcher.fetchUserSnippetsFiles();
let appSnippetsFiles = await fetcher.fetchAppSnippetsFiles();
let extensions = await fetcher.fetchExtensions();

view.load(
projectSnippetsFiles,
userSnippetsFiles,
appSnippetsFiles,
extensions
);
}

// this method is called when your extension is deactivated
function deactivate() {}

Expand Down
4 changes: 3 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# To Do

1. Can I make the extension more resilient? What about funky data?
1. This snippet extension - https://open-vsx.org/extension/hollowtree/vue-snippets - appears to cause an error. Why?

## Changes to consider

1. Support for multi-root workspaces. For multi-root workspaces, a sub workspace can have its own snippets in its *.vscode* folder. You would need look in the `.code-workspace` to discover the sub workspaces and inspect them.
1. This snippet extension - https://open-vsx.org/extension/hollowtree/vue-snippets - appears to cause an error. Can I make the extension more resilient?
1. Show the shortcuts assigned to any snippets (via command 'insert snippet')?

0 comments on commit 1bb9a2f

Please sign in to comment.