Skip to content

Commit a36deca

Browse files
committed
fix: properly extract module name from import statements for codeactions. This fix following issues:
- bug where module name were incorrectly extracted when import statement contained path import - now module codeactions isn't showing for non modules (e.g. `~/components/...`)
1 parent 0132b2e commit a36deca

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/codeActions.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
import { builtinModules } from 'module'
22
import vscode from 'vscode'
3-
import { getExtensionCommandId } from 'vscode-framework'
3+
import { getExtensionCommandId, getExtensionSetting, getExtensionSettingId } from 'vscode-framework'
44
import { readPackageJsonWithMetadata } from './commands-core/packageJson'
55
import { AddPackagesArg } from './commands/addPackages'
66

77
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' })),
1011
{
11-
async provideCodeActions(document, range, { triggerKind, diagnostics }, token) {
12-
if (triggerKind === vscode.CodeActionTriggerKind.Automatic) return
12+
async provideCodeActions(document, range, { diagnostics }) {
1313
const problem = diagnostics[0]
1414
const hasMissingImport = problem && problem.source === 'ts' && problem.code === 2307
1515
const hasMissingTypes = problem && problem.source === 'ts' && problem.code === 7016
1616
// TODO also check end
1717
const pos = range.start
1818
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
2020
const regexs = [/(import .*)(['"].*['"])/, /(} from )(['"].*['"])/]
2121
let moduleNameIndex: number | undefined
2222
let moduleName: string | undefined
23+
console.log(0)
2324
for (const regex of regexs) {
2425
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;
2832
}
2933

3034
if (!moduleName) return
3135

3236
// TODO also detect remaps (paths) from tsconfig.json
33-
moduleName = moduleName.slice(1, -1)
3437
if (builtinModules.includes(moduleName) || moduleName.startsWith('./')) return
3538
if (pos.character < moduleNameIndex!) {
3639
console.log('pos')
@@ -120,4 +123,15 @@ export const registerCodeActions = () => {
120123
// documentation
121124
},
122125
)
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+
})
123135
}
136+
137+
const getModuleName = (importPath: string) => /^(@[a-z\d-~][a-z\d-._~]*\/)?[a-z\d-~][a-z\d-._~]*/.exec(importPath)?.[0]

src/configurationType.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ export type Configuration = {
111111
* @default false
112112
*/
113113
'search.includeDeprecated': boolean
114+
/**
115+
* Affects `openPackageReadmePreview`: whether to open README on npmjs, if it can't be found on disk locally.
116+
* @default true
117+
*/
118+
// 'openReadmeRemote': boolean
114119
/**
115120
* From where to pick bin commands
116121
* @default allUpToRoot
@@ -127,6 +132,11 @@ export type Configuration = {
127132
* @default algolia
128133
*/
129134
'search.provder': 'algolia' | 'npms'
135+
/**
136+
* Specify language IDs in which to enable code actions. You can set it to empty array to entirely disable the feature.
137+
* @default ["typescript", "typescriptreact", "javascript", "javascriptreact", "vue"]
138+
*/
139+
'codeActions.enableLanguages': string[]
130140
/** @default true */
131141
'statusbar.showMainScriptStatus': boolean
132142
/** Applies only if project uses yarn 2 or greater and #openWorkspaceAutoInstall# is withoutPrompt. In this case project script, instead of your system-wide package manager will be used

src/configurationTypeCache.jsonc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// GENERATED. DON'T EDIT MANUALLY
2-
// md5hash: ac71363238fe400c9d98e73292062f37
2+
// md5hash: f8909f14c73cab0ae50768abd0169063
33
{
44
"type": "object",
55
"properties": {
@@ -139,6 +139,20 @@
139139
],
140140
"type": "string"
141141
},
142+
"codeActions.enableLanguages": {
143+
"description": "Specify language IDs in which to enable code actions. You can set it to empty array to entirely disable the feature.",
144+
"default": [
145+
"typescript",
146+
"typescriptreact",
147+
"javascript",
148+
"javascriptreact",
149+
"vue"
150+
],
151+
"type": "array",
152+
"items": {
153+
"type": "string"
154+
}
155+
},
142156
"statusbar.showMainScriptStatus": {
143157
"default": true,
144158
"type": "boolean"
@@ -157,6 +171,7 @@
157171
"required": [
158172
"addPackages.installTypes",
159173
"codeAction.resolveBranchName",
174+
"codeActions.enableLanguages",
160175
"install.clipboardDetection",
161176
"install.packs",
162177
"install.runOnOpen",

0 commit comments

Comments
 (0)