Skip to content

Commit 642f5e2

Browse files
committed
feat: reactUnstableActions
1 parent a87203b commit 642f5e2

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@
740740
"devDependencies": {
741741
"@types/lodash": "^4.14.190",
742742
"@types/node": "^16.11.26",
743-
"@types/vscode": "^1.66.0",
743+
"@types/vscode": "^1.76.0",
744744
"@zardoy/tsconfig": "^1.4.0",
745745
"eslint": "^8.11.0",
746746
"eslint-config-zardoy": "^0.2.11",

pnpm-lock.yaml

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/reactUnstableActions.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as vscode from 'vscode'
2+
import { makeOutlineChainFromPos } from '@zardoy/vscode-utils/build/outline'
3+
4+
export default () => {
5+
vscode.languages.registerCodeActionsProvider(['javascriptreact', 'typescriptreact'], {
6+
async provideCodeActions(document, range, context, token) {
7+
document.getWordRangeAtPosition(range.start, /<[A-Z][-\da-zA-Z]+/)
8+
const diagnostic = context.diagnostics.find(d => d.message.startsWith('Cannot find name'))
9+
if (!diagnostic) return
10+
const componentName = /'(.+?)'/.exec(diagnostic.message)?.[1]
11+
if (!componentName) return
12+
const outline = await vscode.commands.executeCommand<vscode.DocumentSymbol[] | undefined>('vscode.executeDocumentSymbolProvider', document.uri)
13+
if (!outline) return
14+
const topItem = makeOutlineChainFromPos(outline, range.start)[0]
15+
if (!topItem) return
16+
const edit = new vscode.WorkspaceEdit()
17+
const textEdit = new vscode.SnippetTextEdit(
18+
new vscode.Range(topItem.range.end, topItem.range.end),
19+
new vscode.SnippetString(`\n\nfunction ${componentName}() {\n\treturn \${1:<div>$0</div>}\n}\n`),
20+
)
21+
edit.set(document.uri, [textEdit])
22+
return [
23+
{
24+
title: 'Declare Component Below',
25+
kind: vscode.CodeActionKind.QuickFix,
26+
isPreferred: true,
27+
edit,
28+
},
29+
]
30+
},
31+
})
32+
}

0 commit comments

Comments
 (0)