Skip to content

Commit

Permalink
More robust
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Jan 25, 2024
1 parent 6b1d8a1 commit 9cce317
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/common/src/ide/types/FileSystem.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export interface FileSystem {
/**
* Reads a file that comes bundled with Cursorless, with the utf-8 encoding.
* {@link path} is expected to be relative to the root of the extension
* bundle.
* bundle. If the file doesn't exist, returns `undefined`.
*
* Note that in development mode, it is possible to supply an absolute
* path to a file on the local filesystem, for things like hot-reloading.
* Note that in development mode, it is possible to supply an absolute path to
* a file on the local filesystem, for things like hot-reloading.
*
* @param path The path of the file to read
* @returns The contents of path, decoded as UTF-8
*/
readBundledFile(path: string): Promise<string>;
readBundledFile(path: string): Promise<string | undefined>;

/**
* Recursively watch a directory for changes.
Expand Down
46 changes: 30 additions & 16 deletions packages/cursorless-engine/src/languages/LanguageDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,13 @@ export class LanguageDefinition {
): Promise<LanguageDefinition | undefined> {
const languageQueryPath = join(queryDir, `${languageId}.scm`);

let rawLanguageQueryString;
try {
rawLanguageQueryString = await readQueryFileAndImports(
fileSystem,
languageQueryPath,
);
} catch (err) {
if (
err instanceof Error &&
"code" in err &&
err.code === "FileNotFound"
) {
return undefined;
}
throw err;
const rawLanguageQueryString = await readQueryFileAndImports(
fileSystem,
languageQueryPath,
);

if (rawLanguageQueryString == null) {
return undefined;
}

const rawQuery = treeSitter
Expand Down Expand Up @@ -122,7 +114,29 @@ async function readQueryFileAndImports(
continue;
}

const rawQuery = await fileSystem.readBundledFile(queryPath);
let rawQuery = await fileSystem.readBundledFile(queryPath);

if (rawQuery == null) {
if (queryPath === languageQueryPath) {
// If this is the main query file, then we know that this language
// just isn't defined using new-style queries
return undefined;
}

showError(
ide().messages,
"LanguageDefinition.readQueryFileAndImports.queryNotFound",
`Could not find imported query file ${queryPath}`,
);

if (ide().runMode === "test") {
throw new Error("Invalid import statement");
}

// If we're not in test mode, we just ignore the import and continue
rawQuery = "";
}

rawQueryStrings[queryPath] = rawQuery;
matchAll(
rawQuery,
Expand Down
25 changes: 18 additions & 7 deletions packages/cursorless-vscode/src/ide/vscode/VscodeFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,29 @@ export class VscodeFileSystem implements FileSystem {
/**
* Reads a file that comes bundled with Cursorless, with the utf-8 encoding.
* {@link path} is expected to be relative to the root of the extension
* bundle.
* bundle. If the file doesn't exist, returns `undefined`.
*
* Note that in development mode, it is possible to supply an absolute
* path to a file on the local filesystem, for things like hot-reloading.
* Note that in development mode, it is possible to supply an absolute path to
* a file on the local filesystem, for things like hot-reloading.
*
* @param path The path of the file to read
* @returns The contents of path, decoded as UTF-8
*/
public async readBundledFile(path: string): Promise<string> {
return this.decoder.decode(
await vscode.workspace.fs.readFile(this.resolveBundledPath(path)),
);
public async readBundledFile(path: string): Promise<string | undefined> {
try {
return this.decoder.decode(
await vscode.workspace.fs.readFile(this.resolveBundledPath(path)),
);
} catch (err) {
if (
err instanceof Error &&
"code" in err &&
err.code === "FileNotFound"
) {
return undefined;
}
throw err;
}
}

private resolveBundledPath(path: string) {
Expand Down

0 comments on commit 9cce317

Please sign in to comment.