-
-
Notifications
You must be signed in to change notification settings - Fork 88
Added voice command to migrate Cursorless snippet to community format #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
3754e88
Added the ability to generate community snippets
AndreasArvidsson 325d9a6
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 6d48577
Update comment
AndreasArvidsson 2362952
neovim skip generate snippet test
AndreasArvidsson e858d69
Started working on migrating snippets
AndreasArvidsson 92b187e
Added dependency
AndreasArvidsson d92e383
Ordered imports
AndreasArvidsson 5dcc518
Merge branch 'generateSnippets2' into migrateSnippets
AndreasArvidsson bfabb54
Save files
AndreasArvidsson 2ee5a90
Added notification
AndreasArvidsson 7646f6e
Use path return
AndreasArvidsson b585a44
Update packages/cursorless-engine/src/actions/GenerateSnippet/Generat…
phillco 2ac4d44
Reflow comment
AndreasArvidsson d51cea3
Update name to directory
AndreasArvidsson 216309d
we name
AndreasArvidsson 67f6133
Up dit
AndreasArvidsson c980df5
fix
AndreasArvidsson a813db4
Merge branch 'generateSnippets2' into migrateSnippets
AndreasArvidsson cd073e0
Merge branch 'main' into migrateSnippets
AndreasArvidsson 4e185e2
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 821038f
neovim dummy command
AndreasArvidsson 820e3e4
Merge branch 'migrateSnippets' of github.com:cursorless-dev/cursorles…
AndreasArvidsson 456c3e8
fix meta
AndreasArvidsson efeec1b
Update docs
AndreasArvidsson 3f4be9c
Removed todos
AndreasArvidsson 9515360
New version of snippet lib
AndreasArvidsson 5321c28
New snippet lib
AndreasArvidsson 40bc300
Use constant
AndreasArvidsson 1a85c84
Refactor
AndreasArvidsson 555028c
Update packages/cursorless-vscode/src/migrateSnippets.ts
phillco 3995ca4
Update packages/cursorless-org-docs/src/docs/user/experimental/snippe…
phillco 45aa9dc
indent
AndreasArvidsson c6ef011
Merge branch 'migrateSnippets' of github.com:cursorless-dev/cursorles…
AndreasArvidsson 050cbc2
Merge branch 'main' into migrateSnippets
AndreasArvidsson c45a55b
Update packages/cursorless-vscode/src/migrateSnippets.ts
phillco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { | ||
SnippetMap, | ||
SnippetVariable as SnippetVariableLegacy, | ||
} from "@cursorless/common"; | ||
import * as fs from "node:fs/promises"; | ||
import * as path from "node:path"; | ||
import { | ||
serializeSnippetFile, | ||
type SnippetFile, | ||
type SnippetVariable, | ||
} from "talon-snippets"; | ||
import * as vscode from "vscode"; | ||
import { | ||
CURSORLESS_SNIPPETS_SUFFIX, | ||
type VscodeSnippets, | ||
} from "./VscodeSnippets"; | ||
|
||
export async function migrateSnippets( | ||
snippets: VscodeSnippets, | ||
targetDirectory: string, | ||
) { | ||
const userSnippetsDir = snippets.getUserDirectoryStrict(); | ||
const files = await snippets.getSnippetPaths(userSnippetsDir); | ||
|
||
for (const file of files) { | ||
await migrateFile(targetDirectory, file); | ||
} | ||
|
||
await vscode.window.showInformationMessage( | ||
`${files.length} snippet files migrated successfully!`, | ||
); | ||
} | ||
|
||
async function migrateFile(targetDirectory: string, filePath: string) { | ||
const fileName = path.basename(filePath, CURSORLESS_SNIPPETS_SUFFIX); | ||
const snippetFile = await readLegacyFile(filePath); | ||
const communitySnippetFile: SnippetFile = { snippets: [] }; | ||
|
||
for (const snippetName in snippetFile) { | ||
const snippet = snippetFile[snippetName]; | ||
|
||
communitySnippetFile.header = { | ||
name: snippetName, | ||
description: snippet.description, | ||
variables: parseVariables(snippet.variables), | ||
insertionScopes: snippet.insertionScopeTypes, | ||
}; | ||
|
||
for (const def of snippet.definitions) { | ||
communitySnippetFile.snippets.push({ | ||
body: def.body.map((line) => line.replaceAll("\t", " ")), | ||
languages: def.scope?.langIds, | ||
variables: parseVariables(def.variables), | ||
// SKIP: def.scope?.scopeTypes | ||
// SKIP: def.scope?.excludeDescendantScopeTypes | ||
}); | ||
} | ||
} | ||
|
||
try { | ||
const destinationPath = path.join(targetDirectory, `${fileName}.snippet`); | ||
await writeCommunityFile(communitySnippetFile, destinationPath); | ||
} catch (error: any) { | ||
if (error.code === "EEXIST") { | ||
const destinationPath = path.join( | ||
targetDirectory, | ||
`${fileName}_CONFLICT.snippet`, | ||
); | ||
await writeCommunityFile(communitySnippetFile, destinationPath); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
function parseVariables( | ||
variables?: Record<string, SnippetVariableLegacy>, | ||
): SnippetVariable[] { | ||
return Object.entries(variables ?? {}).map( | ||
([name, variable]): SnippetVariable => { | ||
return { | ||
name, | ||
wrapperScope: variable.wrapperScopeType, | ||
insertionFormatters: variable.formatter | ||
? [variable.formatter] | ||
: undefined, | ||
// SKIP: variable.description | ||
}; | ||
}, | ||
); | ||
} | ||
|
||
async function readLegacyFile(filePath: string): Promise<SnippetMap> { | ||
const content = await fs.readFile(filePath, "utf8"); | ||
if (content.length === 0) { | ||
return {}; | ||
} | ||
return JSON.parse(content); | ||
} | ||
|
||
async function writeCommunityFile(snippetFile: SnippetFile, filePath: string) { | ||
const snippetText = serializeSnippetFile(snippetFile); | ||
const file = await fs.open(filePath, "wx"); | ||
try { | ||
await file.write(snippetText); | ||
} finally { | ||
await file.close(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.