Skip to content

Commit 7dd82a6

Browse files
authored
feat: introduce pinia store register code action (#14)
1 parent fdf6a9b commit 7dd82a6

6 files changed

+106
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"devDependencies": {
6868
"@types/node": "^17.0.21",
69-
"@types/vscode": "^1.65.0",
69+
"@types/vscode": "^1.72.0",
7070
"@zardoy/tsconfig": "^1.4.0",
7171
"eslint": "^8.10.0",
7272
"eslint-config-zardoy": "^0.2.10",

pnpm-lock.yaml

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

src/configurationType.ts

+16
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ export type Configuration = {
3030
* @default "preserve"
3131
*/
3232
copyComponentNameCase: 'preserve' | 'camelCase'
33+
/**
34+
* Whether to enable code action for quick store registration in computed field
35+
* @default true
36+
*/
37+
enablePiniaStoreRegistrationCodeAction: boolean
38+
/**
39+
* Pinia stores import path
40+
* @default ".*?stores\\/.*"
41+
*/
42+
piniaStorePathRegex: string
43+
/**
44+
* (advanced): Invokes `String.prototype.replace` on the imported store name
45+
* Call signature: `<importedStoreName>.replace(new Regexp(pattern), replacement)`
46+
* @default null
47+
*/
48+
piniaStoreComputedNameTransform: null | { pattern: RegExp | string; replacement: string }
3349
}

src/extension.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { registerFindReferences } from './findReferences'
77
import { registerGotoDefinition } from './gotoDefinition'
88
import { registerHover } from './hover'
99
import { registerTemplateCompletion } from './templateCompletion'
10+
import { registerPiniaCodeactions } from './piniaCodeActions'
1011
import focusVuexMapper from './commands/focusVuexMapper'
1112
import { registerCopyComponentName } from './commands/copyComponentName'
1213

@@ -22,4 +23,5 @@ export const activate = () => {
2223
registerCssClasesFromTemplate()
2324
focusVuexMapper()
2425
registerCopyComponentName()
26+
registerPiniaCodeactions()
2527
}

src/piniaCodeActions.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as vscode from 'vscode'
2+
import { getExtensionSetting } from 'vscode-framework'
3+
import { getComputedOutline } from './util'
4+
5+
export const registerPiniaCodeactions = () => {
6+
vscode.languages.registerCodeActionsProvider('vue', {
7+
async provideCodeActions(document, range, context, token) {
8+
if (!getExtensionSetting('enablePiniaStoreRegistrationCodeAction')) return
9+
10+
const pos = range.start
11+
const codeActions = [] as vscode.CodeAction[]
12+
const piniaStorePathRegex = getExtensionSetting('piniaStorePathRegex')
13+
14+
const piniaStoreMatch = new RegExp(`(import (.+)\\s+from )(['"]${piniaStorePathRegex}['"].?)`).exec(document.lineAt(pos.line).text)
15+
16+
if (piniaStoreMatch) {
17+
const computedOutline = await getComputedOutline(document.uri)
18+
if (!computedOutline) return
19+
// const str = `\${TM_CURRENT_LINE/import (.+)\\s+from ['"]${piniaStorePathRegex}['"].?/$1/}`
20+
const { range: computedRange } = computedOutline
21+
22+
const customNameTransform = getExtensionSetting('piniaStoreComputedNameTransform')
23+
const computedName = customNameTransform
24+
? piniaStoreMatch[2]!.replace(new RegExp(customNameTransform?.pattern), customNameTransform?.replacement)
25+
: piniaStoreMatch[2]!
26+
27+
const snippetStirng = `\t\${1:${computedName}}() {\n\t\treturn defineStore('\${2:${piniaStoreMatch[2]!}}', \${3:${piniaStoreMatch[2]!}})()\n\t},$0\n`
28+
29+
const workspaceEdit = new vscode.WorkspaceEdit()
30+
if (computedRange.isSingleLine)
31+
// computed: {}; case
32+
workspaceEdit.set(document.uri, [
33+
new vscode.SnippetTextEdit(
34+
new vscode.Range(computedRange.start, computedRange.end),
35+
new vscode.SnippetString(`computed: {\n${snippetStirng}}`),
36+
),
37+
])
38+
else
39+
workspaceEdit.set(document.uri, [
40+
new vscode.SnippetTextEdit(
41+
new vscode.Range(computedRange.start.translate(1), computedRange.start.translate(1)),
42+
new vscode.SnippetString(snippetStirng),
43+
),
44+
])
45+
46+
codeActions.push({
47+
title: 'Register pinia store in computed',
48+
kind: vscode.CodeActionKind.QuickFix,
49+
isPreferred: true,
50+
edit: workspaceEdit,
51+
})
52+
}
53+
54+
return codeActions
55+
},
56+
})
57+
}

src/util.ts

+13
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ export const getComponentNameOutline = async (documentUri: vscode.Uri) => {
2222

2323
return componentNameOutline
2424
}
25+
26+
export const getComputedOutline = async (documentUri: vscode.Uri) => {
27+
const outline = await getNormalizedVueOutline(documentUri)
28+
29+
const computedOutline = outline
30+
?.find(({ name }) => name === 'script')
31+
?.children.find(({ name }) => name === 'default')
32+
?.children.find(({ name }) => name === 'computed')
33+
34+
if (!computedOutline) console.warn("Can' get computed outline")
35+
36+
return computedOutline
37+
}

0 commit comments

Comments
 (0)