-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
765 additions
and
155 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
cursorless-snippets/functionDeclaration.cursorless-snippets
This file contains 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,49 @@ | ||
{ | ||
"functionDeclaration": { | ||
"definitions": [ | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"typescript", | ||
"typescriptreact", | ||
"javascript", | ||
"javascriptreact" | ||
] | ||
}, | ||
"body": [ | ||
"function $name($parameterList) {", | ||
"\t$body", | ||
"}" | ||
], | ||
"variables": { | ||
"name": { | ||
"formatter": "camelCase" | ||
} | ||
} | ||
}, | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"python" | ||
] | ||
}, | ||
"body": [ | ||
"def $name($parameterList):", | ||
"\t$body" | ||
], | ||
"variables": { | ||
"name": { | ||
"formatter": "snakeCase" | ||
} | ||
} | ||
} | ||
], | ||
"description": "Function declaration", | ||
"variables": { | ||
"body": { | ||
"wrapperScopeType": "statement" | ||
} | ||
}, | ||
"insertionScopeType": "statement" | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
"alternative": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeType": "statement" | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
"consequence": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeType": "statement" | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
"exceptBody": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeType": "statement" | ||
} | ||
} | ||
} |
This file contains 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 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,159 @@ | ||
import { commands, DecorationRangeBehavior } from "vscode"; | ||
import { | ||
Action, | ||
ActionPreferences, | ||
ActionReturnValue, | ||
Graph, | ||
TypedSelection, | ||
} from "../typings/Types"; | ||
import displayPendingEditDecorations from "../util/editDisplayUtils"; | ||
import { ensureSingleEditor } from "../util/targetUtils"; | ||
import { SnippetParser } from "../vendor/snippet/snippetParser"; | ||
import { | ||
findMatchingSnippetDefinition, | ||
transformSnippetVariables, | ||
} from "../util/snippet"; | ||
import textFormatters from "../core/textFormatters"; | ||
import { SnippetDefinition, Snippet } from "../typings/snippet"; | ||
import { | ||
callFunctionAndUpdateSelectionInfos, | ||
callFunctionAndUpdateSelections, | ||
getSelectionInfo, | ||
} from "../core/updateSelections/updateSelections"; | ||
|
||
export default class InsertSnippet implements Action { | ||
private snippetParser = new SnippetParser(); | ||
|
||
getTargetPreferences(snippetName: string): ActionPreferences[] { | ||
const snippet = this.graph.snippets.getSnippet(snippetName); | ||
|
||
if (snippet == null) { | ||
throw new Error(`Couldn't find snippet ${snippetName}`); | ||
} | ||
|
||
const defaultScopeType = snippet.insertionScopeType; | ||
|
||
return [ | ||
{ | ||
insideOutsideType: "outside", | ||
modifier: | ||
defaultScopeType == null | ||
? undefined | ||
: { | ||
type: "containingScope", | ||
scopeType: defaultScopeType, | ||
includeSiblings: false, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
constructor(private graph: Graph) { | ||
this.run = this.run.bind(this); | ||
} | ||
|
||
async run( | ||
[targets]: [TypedSelection[]], | ||
snippetName: string, | ||
substitutions: Record<string, string> | ||
): Promise<ActionReturnValue> { | ||
const snippet = this.graph.snippets.getSnippet(snippetName)!; | ||
|
||
const editor = ensureSingleEditor(targets); | ||
|
||
// Find snippet definition matching context. | ||
// NB: We only look at the first target to create our context. This means | ||
// that if there are two snippets that match two different contexts, and | ||
// the two targets match those two different contexts, we will just use the | ||
// snippet that matches the first context for both targets | ||
const definition = findMatchingSnippetDefinition( | ||
targets[0], | ||
snippet.definitions | ||
); | ||
|
||
if (definition == null) { | ||
throw new Error("Couldn't find matching snippet definition"); | ||
} | ||
|
||
const parsedSnippet = this.snippetParser.parse(definition.body.join("\n")); | ||
|
||
const formattedSubstitutions = | ||
substitutions == null | ||
? undefined | ||
: formatSubstitutions(snippet, definition, substitutions); | ||
|
||
transformSnippetVariables(parsedSnippet, null, formattedSubstitutions); | ||
|
||
const snippetString = parsedSnippet.toTextmateString(); | ||
|
||
await displayPendingEditDecorations( | ||
targets, | ||
this.graph.editStyles.pendingModification0 | ||
); | ||
|
||
const targetSelections = targets.map( | ||
(target) => target.selection.selection | ||
); | ||
|
||
// TODO: Fix "insert before" once we have the new update selections code | ||
// TODO: Remove undo stop once we have the new update selections code | ||
await this.graph.actions.setSelection.run([targets]); | ||
|
||
// NB: We do this to auto insert the delimiter if necessary | ||
await this.graph.actions.replace.run([targets], [""]); | ||
|
||
const targetSelectionInfos = targetSelections.map((selection) => | ||
getSelectionInfo( | ||
editor.document, | ||
selection, | ||
DecorationRangeBehavior.OpenOpen | ||
) | ||
); | ||
|
||
// NB: We used the command "editor.action.insertSnippet" instead of calling editor.insertSnippet | ||
// because the latter doesn't support special variables like CLIPBOARD | ||
const [updatedTargetSelections] = await callFunctionAndUpdateSelectionInfos( | ||
this.graph.rangeUpdater, | ||
() => | ||
commands.executeCommand("editor.action.insertSnippet", { | ||
snippet: snippetString, | ||
}), | ||
editor.document, | ||
[targetSelectionInfos] | ||
); | ||
|
||
return { | ||
thatMark: updatedTargetSelections.map((selection) => ({ | ||
editor, | ||
selection, | ||
})), | ||
}; | ||
} | ||
} | ||
function formatSubstitutions( | ||
snippet: Snippet, | ||
definition: SnippetDefinition, | ||
substitutions: Record<string, string> | ||
) { | ||
return Object.fromEntries( | ||
Object.entries(substitutions).map(([variableName, value]) => { | ||
const formatterName = | ||
(definition.variables ?? {})[variableName]?.formatter ?? | ||
(snippet.variables ?? {})[variableName]?.formatter; | ||
|
||
if (formatterName == null) { | ||
return [variableName, value]; | ||
} | ||
|
||
const formatter = textFormatters[formatterName]; | ||
|
||
if (formatter == null) { | ||
throw new Error( | ||
`Couldn't find formatter ${formatterName} for variable ${variableName}` | ||
); | ||
} | ||
|
||
return [variableName, formatter(value.split(" "))]; | ||
}) | ||
); | ||
} |
This file contains 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.