Skip to content

Commit ae20ad2

Browse files
committed
feat: now you can quickly figure out how dep was installed by the remove dep code action title
1 parent a5f9d46 commit ae20ad2

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

src/codeActions.ts

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const registerCodeActions = () => {
1414
vscode.languages.registerCodeActionsProvider(
1515
getExtensionSetting('codeActions.enableLanguages').map(language => ({ language, scheme: 'file' })),
1616
{
17+
// eslint-disable-next-line complexity
1718
async provideCodeActions(document, range, { diagnostics }) {
1819
const problem = diagnostics[0]
1920
const hasMissingImport = problem && problem.source === 'ts' && problem.code === 2307
@@ -31,50 +32,56 @@ export const registerCodeActions = () => {
3132
if (builtinModules.includes(moduleName) || moduleName.startsWith('./')) return
3233

3334
const codeActions: vscode.CodeAction[] = []
34-
// TODO check for existence
35-
codeActions.push(
36-
...(hasMissingImport
37-
? []
38-
: [
39-
{
40-
title: 'Open README to side',
41-
command: {
42-
command: getExtensionCommandId('openPackageReadmePreview'),
43-
// TODO investigate on titles
44-
title: '',
45-
arguments: [moduleName],
46-
},
47-
kind: vscode.CodeActionKind.Empty,
48-
},
49-
{
50-
title: 'Open Repository',
51-
command: {
52-
command: getExtensionCommandId('openPackageRepository'),
53-
title: '',
54-
arguments: [moduleName],
55-
},
56-
kind: vscode.CodeActionKind.Empty,
57-
},
58-
]),
59-
{
60-
title: 'Open on NPM',
61-
command: {
62-
command: getExtensionCommandId('openOnNpm'),
63-
title: '',
64-
arguments: [moduleName],
35+
if (hasMissingImport) {
36+
const { packageJson = {} } = await readPackageJsonWithMetadata({ type: 'closest' }).catch(() => ({}))
37+
let foundType
38+
for (const depType of ['dependencies', 'devDependencies', 'optionalDependencies'])
39+
if (moduleName in packageJson[depType] ?? {}) {
40+
foundType = depType
41+
break
42+
}
43+
44+
// TODO-low check for existence
45+
codeActions.push(
46+
{
47+
title: 'Open README to side',
48+
command: {
49+
command: getExtensionCommandId('openPackageReadmePreview'),
50+
title: '',
51+
arguments: [moduleName],
52+
},
53+
kind: vscode.CodeActionKind.Empty,
54+
},
55+
{
56+
title: 'Open Repository',
57+
command: {
58+
command: getExtensionCommandId('openPackageRepository'),
59+
title: '',
60+
arguments: [moduleName],
61+
},
62+
kind: vscode.CodeActionKind.Empty,
6563
},
66-
kind: vscode.CodeActionKind.Empty,
67-
},
68-
{
69-
title: 'Remove with PNPM',
70-
command: {
71-
command: getExtensionCommandId('removePackages'),
72-
title: '',
73-
arguments: [[moduleName]],
64+
{
65+
title: 'Open on NPM',
66+
command: {
67+
command: getExtensionCommandId('openOnNpm'),
68+
title: '',
69+
arguments: [moduleName],
70+
},
71+
kind: vscode.CodeActionKind.Empty,
7472
},
75-
kind: vscode.CodeActionKind.Empty,
76-
},
77-
)
73+
{
74+
// todo use multiple find up packageJson and disable this code action if not found
75+
title: `Remove ${foundType ? `from ${foundType}` : 'module'}`,
76+
command: {
77+
command: getExtensionCommandId('removePackages'),
78+
title: '',
79+
arguments: [[moduleName]],
80+
},
81+
kind: vscode.CodeActionKind.Empty,
82+
},
83+
)
84+
}
7885

7986
if (hasMissingImport || hasMissingTypes) {
8087
const addModuleFix = (module: string, type: 'dependency' | 'devDependency', isPreferred = true) => {

src/commands-core/packageManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const packageManagerCommand = async (_inputArg: {
106106
return msg
107107
}
108108

109+
// eslint-disable-next-line default-case
109110
switch (subcommand) {
110111
case 'add':
111112
msg += 'Installing'
@@ -117,6 +118,7 @@ export const packageManagerCommand = async (_inputArg: {
117118
msg += 'Linking'
118119
break
119120
}
121+
120122
msg += packages.length > 4 ? ` ${packages.length} packages` : `: ${packages.join(', ')}`
121123
if (flags.includes('-D')) msg += ' as dev'
122124
return msg

0 commit comments

Comments
 (0)