Skip to content

Commit 5321c28

Browse files
New snippet lib
1 parent 9515360 commit 5321c28

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

packages/cursorless-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"lodash-es": "^4.17.21",
3131
"moo": "0.5.2",
3232
"nearley": "2.20.1",
33-
"talon-snippets": "1.2.0",
33+
"talon-snippets": "1.3.0",
3434
"uuid": "^10.0.0",
3535
"zod": "3.23.8"
3636
},

packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippetCommunity.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
type TextEditor,
88
} from "@cursorless/common";
99
import {
10-
getHeaderSnippet,
1110
parseSnippetFile,
1211
serializeSnippetFile,
13-
type SnippetDocument,
12+
type Snippet,
13+
type SnippetFile,
14+
type SnippetHeader,
1415
type SnippetVariable,
1516
} from "talon-snippets";
1617
import type { Snippets } from "../../core/Snippets";
@@ -129,36 +130,35 @@ export default class GenerateSnippetCommunity {
129130
const snippetLines = constructSnippetBody(snippetBodyText, linePrefix);
130131

131132
let editableEditor: EditableTextEditor;
132-
let snippetDocuments: SnippetDocument[];
133+
let snippetFile: SnippetFile = { snippets: [] };
133134

134135
if (ide().runMode === "test") {
135136
// If we're testing, we just overwrite the current document
136137
editableEditor = ide().getEditableTextEditor(editor);
137-
snippetDocuments = [];
138138
} else {
139139
// Otherwise, we create and open a new document for the snippet
140140
editableEditor = ide().getEditableTextEditor(
141141
await this.snippets.openNewSnippetFile(snippetName, directory),
142142
);
143-
snippetDocuments = parseSnippetFile(editableEditor.document.getText());
143+
snippetFile = parseSnippetFile(editableEditor.document.getText());
144144
}
145145

146146
await editableEditor.setSelections([
147147
editableEditor.document.range.toSelection(false),
148148
]);
149149

150-
const headerSnippet = getHeaderSnippet(snippetDocuments);
151-
152150
/** The next placeholder index to use for the meta snippet */
153151
let currentPlaceholderIndex = 1;
154152

153+
const { header } = snippetFile;
154+
155155
const phrases =
156-
headerSnippet?.phrases != null
156+
snippetFile.header?.phrases != null
157157
? undefined
158158
: [`${PLACEHOLDER}${currentPlaceholderIndex++}`];
159159

160160
const createVariable = (variable: Variable): SnippetVariable => {
161-
const hasPhrase = headerSnippet?.variables?.some(
161+
const hasPhrase = header?.variables?.some(
162162
(v) => v.name === variable.name && v.wrapperPhrases != null,
163163
);
164164
return {
@@ -169,22 +169,22 @@ export default class GenerateSnippetCommunity {
169169
};
170170
};
171171

172-
const snippet: SnippetDocument = {
173-
name: headerSnippet?.name === snippetName ? undefined : snippetName,
172+
const snippet: Snippet = {
173+
name: header?.name === snippetName ? undefined : snippetName,
174174
phrases,
175-
languages: getSnippetLanguages(editor, headerSnippet),
175+
languages: getSnippetLanguages(editor, header),
176176
body: snippetLines,
177177
variables: variables.map(createVariable),
178178
};
179179

180-
snippetDocuments.push(snippet);
180+
snippetFile.snippets.push(snippet);
181181

182182
/**
183183
* This is the text of the meta-snippet in Textmate format that we will
184184
* insert into the new document where the user will fill out their snippet
185185
* definition
186186
*/
187-
const metaSnippetText = serializeSnippetFile(snippetDocuments)
187+
const metaSnippetText = serializeSnippetFile(snippetFile)
188188
// Escape dollar signs in the snippet text so that they don't get used as
189189
// placeholders in the meta snippet
190190
.replace(/\$/g, "\\$")
@@ -205,7 +205,7 @@ export default class GenerateSnippetCommunity {
205205

206206
function getSnippetLanguages(
207207
editor: TextEditor,
208-
header: SnippetDocument | undefined,
208+
header: SnippetHeader | undefined,
209209
): string[] | undefined {
210210
if (header?.languages?.includes(editor.document.languageId)) {
211211
return undefined;

packages/cursorless-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@
12891289
"lodash-es": "^4.17.21",
12901290
"nearley": "2.20.1",
12911291
"semver": "^7.6.3",
1292-
"talon-snippets": "1.2.0",
1292+
"talon-snippets": "1.3.0",
12931293
"tinycolor2": "1.6.0",
12941294
"trie-search": "2.0.0",
12951295
"uuid": "^10.0.0",

packages/cursorless-vscode/src/migrateSnippets.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as fs from "node:fs/promises";
66
import * as path from "node:path";
77
import {
88
serializeSnippetFile,
9-
type SnippetDocument,
9+
type SnippetFile,
1010
type SnippetVariable,
1111
} from "talon-snippets";
1212
import * as vscode from "vscode";
@@ -31,20 +31,20 @@ export async function migrateSnippets(
3131
async function migrateFile(targetDirectory: string, filePath: string) {
3232
const fileName = path.basename(filePath, ".cursorless-snippets");
3333
const snippetFile = await readLegacyFile(filePath);
34-
const communitySnippetFile: SnippetDocument[] = [];
34+
const communitySnippetFile: SnippetFile = { snippets: [] };
3535

3636
for (const snippetName in snippetFile) {
3737
const snippet = snippetFile[snippetName];
3838

39-
communitySnippetFile.push({
39+
communitySnippetFile.header = {
4040
name: snippetName,
41+
description: snippet.description,
4142
variables: parseVariables(snippet.variables),
4243
insertionScopes: snippet.insertionScopeTypes,
43-
description: snippet.description,
44-
});
44+
};
4545

4646
for (const def of snippet.definitions) {
47-
communitySnippetFile.push({
47+
communitySnippetFile.snippets.push({
4848
body: def.body.map((line) => line.replaceAll("\t", " ")),
4949
languages: def.scope?.langIds,
5050
variables: parseVariables(def.variables),
@@ -98,10 +98,7 @@ async function readLegacyFile(filePath: string): Promise<SnippetMap> {
9898
return JSON.parse(content);
9999
}
100100

101-
async function writeCommunityFile(
102-
snippetFile: SnippetDocument[],
103-
filePath: string,
104-
) {
101+
async function writeCommunityFile(snippetFile: SnippetFile, filePath: string) {
105102
const snippetText = serializeSnippetFile(snippetFile);
106103
const file = await fs.open(filePath, "w");
107104
await file.write(snippetText);

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)