|
1 | 1 | import { builtinModules } from 'module'
|
2 | 2 | import vscode from 'vscode'
|
3 |
| -import { getExtensionCommandId } from 'vscode-framework' |
| 3 | +import { getExtensionCommandId, getExtensionSetting, getExtensionSettingId } from 'vscode-framework' |
4 | 4 | import { readPackageJsonWithMetadata } from './commands-core/packageJson'
|
5 | 5 | import { AddPackagesArg } from './commands/addPackages'
|
6 | 6 |
|
7 | 7 | export const registerCodeActions = () => {
|
8 |
| - vscode.languages.registerCodeActionsProvider( |
9 |
| - ['typescript', 'typescriptreact', 'javascript', 'javascriptreact', 'vue'].map(language => ({ language, scheme: 'file' })), |
| 8 | + // TODo refactor with helper |
| 9 | + const registerCodeActionsInner = () => vscode.languages.registerCodeActionsProvider( |
| 10 | + getExtensionSetting('codeActions.enableLanguages').map(language => ({ language, scheme: 'file' })), |
10 | 11 | {
|
11 |
| - async provideCodeActions(document, range, { triggerKind, diagnostics }, token) { |
12 |
| - if (triggerKind === vscode.CodeActionTriggerKind.Automatic) return |
| 12 | + async provideCodeActions(document, range, { diagnostics }) { |
13 | 13 | const problem = diagnostics[0]
|
14 | 14 | const hasMissingImport = problem && problem.source === 'ts' && problem.code === 2307
|
15 | 15 | const hasMissingTypes = problem && problem.source === 'ts' && problem.code === 7016
|
16 | 16 | // TODO also check end
|
17 | 17 | const pos = range.start
|
18 | 18 | const lineText = document.lineAt(pos.line).text
|
19 |
| - // faster than ask TS lang server |
| 19 | + // TODO support skypack imports e.g. https://cdn.skypack.dev/canvas-confetti |
20 | 20 | const regexs = [/(import .*)(['"].*['"])/, /(} from )(['"].*['"])/]
|
21 | 21 | let moduleNameIndex: number | undefined
|
22 | 22 | let moduleName: string | undefined
|
| 23 | + console.log(0) |
23 | 24 | for (const regex of regexs) {
|
24 | 25 | const result = regex.exec(lineText)
|
25 |
| - moduleNameIndex = result?.[1]?.length |
26 |
| - moduleName = result?.[2] |
27 |
| - if (result) break |
| 26 | + if (!result) continue |
| 27 | + console.log(1, result) |
| 28 | + moduleNameIndex = result[1]!.length |
| 29 | + moduleName = getModuleName(result[2]!.slice(1, -1)) |
| 30 | + console.log(2, moduleName) |
| 31 | + break; |
28 | 32 | }
|
29 | 33 |
|
30 | 34 | if (!moduleName) return
|
31 | 35 |
|
32 | 36 | // TODO also detect remaps (paths) from tsconfig.json
|
33 |
| - moduleName = moduleName.slice(1, -1) |
34 | 37 | if (builtinModules.includes(moduleName) || moduleName.startsWith('./')) return
|
35 | 38 | if (pos.character < moduleNameIndex!) {
|
36 | 39 | console.log('pos')
|
@@ -120,4 +123,15 @@ export const registerCodeActions = () => {
|
120 | 123 | // documentation
|
121 | 124 | },
|
122 | 125 | )
|
| 126 | + |
| 127 | + let disposable = registerCodeActionsInner() |
| 128 | + |
| 129 | + vscode.workspace.onDidChangeConfiguration(({affectsConfiguration}) => { |
| 130 | + if (affectsConfiguration(getExtensionSettingId('codeActions.enableLanguages'))) { |
| 131 | + disposable.dispose() |
| 132 | + disposable = registerCodeActionsInner() |
| 133 | + } |
| 134 | + }) |
123 | 135 | }
|
| 136 | + |
| 137 | +const getModuleName = (importPath: string) => /^(@[a-z\d-~][a-z\d-._~]*\/)?[a-z\d-~][a-z\d-._~]*/.exec(importPath)?.[0] |
0 commit comments