Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"mobxConstructor": {
"definitions": [
{
"scope": {
"langIds": [
"typescript",
"javascript",
"typescriptreact",
"javascriptreact"
]
},
"body": [
"constructor($parameters) {",
"\tmakeAutoObservable(this);",
"}"
]
}
],
"description": "Constructor using makeAutoObservable",
"insertionScopeTypes": [
"namedFunction"
]
},
"constantDeclaration": {
"definitions": [
{
"scope": {
"langIds": [
"typescript",
"javascript",
"typescriptreact",
"javascriptreact"
]
},
"body": [
"const $name = ${value/^([^;]*);?$/$1/};"
],
"variables": {
"name": {
"formatter": "camelCase"
}
}
}
],
"description": "Constant variable declaration",
"insertionScopeTypes": [
"statement"
],
"variables": {
"value": {
"wrapperScopeType": "statement"
}
}
},
"constantDeclarationWithType": {
"definitions": [
{
"scope": {
"langIds": [
"typescript",
"javascript",
"typescriptreact",
"javascriptreact"
]
},
"body": [
"const $name: $type = ${value/^([^;]*);?$/$1/};"
],
"variables": {
"name": {
"formatter": "camelCase"
}
}
}
],
"description": "Constant variable declaration with type",
"insertionScopeTypes": [
"statement"
],
"variables": {
"value": {
"wrapperScopeType": "statement"
}
}
},
"letDeclaration": {
"definitions": [
{
"scope": {
"langIds": [
"typescript",
"javascript",
"typescriptreact",
"javascriptreact"
]
},
"body": [
"let $name = ${value/^([^;]*);?$/$1/};"
],
"variables": {
"name": {
"formatter": "camelCase"
}
}
}
],
"description": "Let variable declaration",
"insertionScopeTypes": [
"statement"
],
"variables": {
"value": {
"wrapperScopeType": "statement"
}
}
},
"letDeclarationWithType": {
"definitions": [
{
"scope": {
"langIds": [
"typescript",
"javascript",
"typescriptreact",
"javascriptreact"
]
},
"body": [
"let $name: $type = ${value/^([^;]*);?$/$1/};"
],
"variables": {
"name": {
"formatter": "camelCase"
}
}
}
],
"description": "Let variable declaration with type",
"insertionScopeTypes": [
"statement"
],
"variables": {
"value": {
"wrapperScopeType": "statement"
}
}
}
}
68 changes: 48 additions & 20 deletions packages/cursorless-vscode/src/migrateSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,26 @@ async function migrateFile(
const fileName = path.basename(filePath, CURSORLESS_SNIPPETS_SUFFIX);
const snippetFile = await readLegacyFile(filePath);
const communitySnippetFile: SnippetFile = { snippets: [] };
const snippetNames = Object.keys(snippetFile);
const useHeader = snippetNames.length === 1;
let hasSkippedSnippet = false;

for (const snippetName in snippetFile) {
for (const snippetName of snippetNames) {
const snippet = snippetFile[snippetName];
const phrase =
spokenForms.insertion[snippetName] ??
spokenForms.insertionWithPhrase[snippetName];
const phrases = phrase ? [phrase] : undefined;

communitySnippetFile.header = {
name: snippetName,
description: snippet.description,
phrases: phrase ? [phrase] : undefined,
variables: parseVariables(spokenForms, snippetName, snippet.variables),
insertionScopes: snippet.insertionScopeTypes,
};
if (useHeader) {
communitySnippetFile.header = {
name: snippetName,
description: snippet.description,
phrases: phrases,
variables: parseVariables(spokenForms, snippetName, snippet.variables),
insertionScopes: snippet.insertionScopeTypes,
};
}

for (const def of snippet.definitions) {
if (
Expand All @@ -91,11 +96,20 @@ async function migrateFile(
continue;
}
communitySnippetFile.snippets.push({
body: def.body.map((line) => line.replaceAll("\t", " ")),
name: useHeader ? undefined : snippetName,
description: useHeader ? undefined : snippet.description,
phrases: useHeader ? undefined : phrases,
insertionScopes: useHeader ? undefined : snippet.insertionScopeTypes,
languages: def.scope?.langIds,
variables: parseVariables(spokenForms, snippetName, def.variables),
variables: parseVariables(
spokenForms,
snippetName,
useHeader ? undefined : snippet.variables,
def.variables,
),
// SKIP: def.scope?.scopeTypes
// SKIP: def.scope?.excludeDescendantScopeTypes
body: def.body.map((line) => line.replaceAll("\t", " ")),
});
}
}
Expand Down Expand Up @@ -131,22 +145,36 @@ async function migrateFile(
function parseVariables(
spokenForms: SpokenForms,
snippetName: string,
variables?: Record<string, SnippetVariableLegacy>,
snippetVariables?: Record<string, SnippetVariableLegacy>,
defVariables?: Record<string, SnippetVariableLegacy>,
): SnippetVariable[] {
return Object.entries(variables ?? {}).map(
([name, variable]): SnippetVariable => {
const map: Record<string, SnippetVariable> = {};

const add = (name: string, variable: SnippetVariableLegacy) => {
if (!map[name]) {
const phrase = spokenForms.wrapper[`${snippetName}.${name}`];
return {
map[name] = {
name,
wrapperPhrases: phrase ? [phrase] : undefined,
wrapperScope: variable.wrapperScopeType,
insertionFormatters: variable.formatter
? getFormatter(variable.formatter)
: undefined,
// SKIP: variable.description
};
},
}
if (variable.wrapperScopeType) {
map[name].wrapperScope = variable.wrapperScopeType;
}
if (variable.formatter) {
map[name].insertionFormatters = getFormatter(variable.formatter);
}
// SKIP: variable.description
};

Object.entries(snippetVariables ?? {}).forEach(([name, variable]) =>
add(name, variable),
);
Object.entries(defVariables ?? {}).forEach(([name, variable]) =>
add(name, variable),
);

return Object.values(map);
}

// Convert Cursorless formatters to Talon community formatters
Expand Down
Loading