Skip to content

Commit 0c1d8fa

Browse files
committed
Be more error tolerant
1 parent f9af96a commit 0c1d8fa

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/core/Snippets.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { readFile, stat } from "fs/promises";
22
import { cloneDeep, max, merge } from "lodash";
33
import { join } from "path";
4-
import { workspace } from "vscode";
4+
import { window, workspace } from "vscode";
55
import { walkFiles } from "../testUtil/walkAsync";
66
import { Snippet, SnippetMap } from "../typings/snippet";
77
import { Graph } from "../typings/Types";
88
import { mergeStrict } from "../util/object";
9+
import { CURSORLESS_SNIPPETS_SUFFIX } from "./constants";
910

1011
const SNIPPET_DIR_REFRESH_INTERVAL_MS = 1000;
1112

@@ -34,6 +35,8 @@ export class Snippets {
3435
*/
3536
private maxSnippetMtimeMs: number = -1;
3637

38+
private shownErrorMessageForDir: string | null | undefined = null;
39+
3740
constructor(private graph: Graph) {
3841
this.updateUserSnippetsPath();
3942

@@ -63,7 +66,7 @@ export class Snippets {
6366
async init() {
6467
const extensionPath = this.graph.extensionContext.extensionPath;
6568
const snippetsDir = join(extensionPath, "cursorless-snippets");
66-
const snippetFiles = await walkFiles(snippetsDir);
69+
const snippetFiles = await getSnippetPaths(snippetsDir);
6770
this.coreSnippets = mergeStrict(
6871
...(await Promise.all(
6972
snippetFiles.map(async (path) =>
@@ -98,9 +101,25 @@ export class Snippets {
98101
}
99102

100103
async updateUserSnippets() {
101-
const snippetFiles = this.userSnippetsDir
102-
? await walkFiles(this.userSnippetsDir)
103-
: [];
104+
let snippetFiles: string[];
105+
try {
106+
snippetFiles = this.userSnippetsDir
107+
? await getSnippetPaths(this.userSnippetsDir)
108+
: [];
109+
} catch (err) {
110+
if (this.shownErrorMessageForDir !== this.userSnippetsDir) {
111+
window.showErrorMessage(
112+
`Error with cursorless snippets dir "${this.userSnippetsDir}": ${
113+
(err as Error).message
114+
}`
115+
);
116+
}
117+
118+
this.shownErrorMessageForDir = this.userSnippetsDir;
119+
return;
120+
}
121+
122+
this.shownErrorMessageForDir = null;
104123

105124
const maxSnippetMtime =
106125
max(
@@ -117,9 +136,24 @@ export class Snippets {
117136

118137
this.userSnippets = mergeStrict(
119138
...(await Promise.all(
120-
snippetFiles.map(async (path) =>
121-
JSON.parse(await readFile(path, "utf8"))
122-
)
139+
snippetFiles.map(async (path) => {
140+
try {
141+
const content = await readFile(path, "utf8");
142+
143+
if (content.length === 0) {
144+
return {};
145+
}
146+
147+
return JSON.parse(content);
148+
} catch (err) {
149+
window.showErrorMessage(
150+
`Error with cursorless snippets file "${path}": ${
151+
(err as Error).message
152+
}`
153+
);
154+
return {};
155+
}
156+
})
123157
))
124158
);
125159

@@ -185,3 +219,9 @@ export class Snippets {
185219
return this.mergedSnippets[snippetName];
186220
}
187221
}
222+
223+
async function getSnippetPaths(snippetsDir: string) {
224+
return (await walkFiles(snippetsDir)).filter((path) =>
225+
path.endsWith(CURSORLESS_SNIPPETS_SUFFIX)
226+
);
227+
}

src/core/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const CURSORLESS_SNIPPETS_SUFFIX = ".cursorless-snippets";
2+
13
export const SUBWORD_MATCHER = /[A-Z]?[a-z]+|[A-Z]+(?![a-z])|[0-9]+/g;
24

35
export const DEBOUNCE_DELAY = 175;

0 commit comments

Comments
 (0)