Skip to content

Commit 2dacf8b

Browse files
committed
feat: command for stylex snippet. for stylex users!
1 parent 465115a commit 2dacf8b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@
323323
{
324324
"command": "fixedGitStageFile",
325325
"title": "Fixed Git Stage File"
326+
},
327+
{
328+
"command": "stylexSnippet",
329+
"title": "Stylex Snippet"
326330
}
327331
],
328332
"configuration": {

src/features/stylexSnippet.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as vscode from 'vscode'
2+
import { registerExtensionCommand } from 'vscode-framework'
3+
4+
export default () => {
5+
registerExtensionCommand('stylexSnippet', async () => {
6+
const editor = vscode.window.activeTextEditor
7+
if (!editor) return
8+
let { line } = editor.selection.active
9+
let regexMatch
10+
const predicateRegex = /const ([\w\d]+) = stylex.create\({/i
11+
while (line > 0) {
12+
const { text } = editor.document.lineAt(line)
13+
if (predicateRegex.test(text)) {
14+
regexMatch = predicateRegex.exec(text)![1]!
15+
break
16+
}
17+
18+
line--
19+
}
20+
21+
if (!regexMatch) return
22+
const insideProps = editor.document.lineAt(editor.selection.active).text.includes('...stylex.props')
23+
const snippet = insideProps
24+
? new vscode.SnippetString().appendText(`${regexMatch}.`)
25+
: new vscode.SnippetString().appendText(`{...stylex.props(${regexMatch}.`).appendTabstop(0).appendText(')}')
26+
27+
await editor.insertSnippet(snippet)
28+
await vscode.commands.executeCommand('editor.action.triggerSuggest')
29+
})
30+
}

0 commit comments

Comments
 (0)