Skip to content

Commit

Permalink
monaco: parseSnippet RangeError handling (eclipse-theia#12463)
Browse files Browse the repository at this point in the history
Change `parseSnippets` from recursive function to nested for-loops.

Eliminate 'prefix' requirement for `JsonSerializedSnippet.is()`.

Change handling to account for missing 'prefix' parameter.

Add `isFileTemplate` param to Snippet to bring it in line with VS Code.

Signed-off-by: FernandoAscencio <[email protected]>
  • Loading branch information
FernandoAscencio authored and jonah-iden committed May 2, 2023
1 parent 482c21d commit cec878a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/monaco/src/browser/monaco-snippet-suggest-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export class MonacoSnippetSuggestProvider implements monaco.languages.Completion
fromJSON(snippets: JsonSerializedSnippets | undefined, { language, source }: SnippetLoadOptions): Disposable {
const toDispose = new DisposableCollection();
this.parseSnippets(snippets, (name, snippet) => {
const { prefix, body, description } = snippet;
const { isFileTemplate, prefix, body, description } = snippet;
const parsedBody = Array.isArray(body) ? body.join('\n') : body;
const parsedPrefixes = Array.isArray(prefix) ? prefix : [prefix];
const parsedPrefixes = !prefix ? [''] : Array.isArray(prefix) ? prefix : [prefix];

if (typeof parsedBody !== 'string') {
return;
Expand All @@ -181,6 +181,7 @@ export class MonacoSnippetSuggestProvider implements monaco.languages.Completion
}
}
parsedPrefixes.forEach(parsedPrefix => toDispose.push(this.push({
isFileTemplate: Boolean(isFileTemplate),
scopes,
name,
prefix: parsedPrefix,
Expand All @@ -196,7 +197,10 @@ export class MonacoSnippetSuggestProvider implements monaco.languages.Completion
if (JsonSerializedSnippet.is(scopeOrTemplate)) {
accept(name, scopeOrTemplate);
} else {
this.parseSnippets(scopeOrTemplate, accept);
// eslint-disable-next-line @typescript-eslint/no-shadow
for (const [name, template] of Object.entries(scopeOrTemplate)) {
accept(name, template);
}
}
}
}
Expand Down Expand Up @@ -240,18 +244,20 @@ export interface JsonSerializedSnippets {
[name: string]: JsonSerializedSnippet | { [name: string]: JsonSerializedSnippet };
}
export interface JsonSerializedSnippet {
isFileTemplate?: boolean;
body: string | string[];
scope: string;
prefix: string | string[];
scope?: string;
prefix?: string | string[];
description: string;
}
export namespace JsonSerializedSnippet {
export function is(obj: unknown): obj is JsonSerializedSnippet {
return isObject(obj) && 'body' in obj && 'prefix' in obj;
return isObject(obj) && 'body' in obj;
}
}

export interface Snippet {
readonly isFileTemplate: boolean
readonly scopes: string[]
readonly name: string
readonly prefix: string
Expand Down

0 comments on commit cec878a

Please sign in to comment.