Skip to content

Commit

Permalink
feat: now you can quickly figure out how dep was installed by the rem…
Browse files Browse the repository at this point in the history
…ove dep code action title
  • Loading branch information
zardoy committed Apr 14, 2023
1 parent a5f9d46 commit ae20ad2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
91 changes: 49 additions & 42 deletions src/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const registerCodeActions = () => {
vscode.languages.registerCodeActionsProvider(
getExtensionSetting('codeActions.enableLanguages').map(language => ({ language, scheme: 'file' })),
{
// eslint-disable-next-line complexity
async provideCodeActions(document, range, { diagnostics }) {
const problem = diagnostics[0]
const hasMissingImport = problem && problem.source === 'ts' && problem.code === 2307
Expand All @@ -31,50 +32,56 @@ export const registerCodeActions = () => {
if (builtinModules.includes(moduleName) || moduleName.startsWith('./')) return

const codeActions: vscode.CodeAction[] = []
// TODO check for existence
codeActions.push(
...(hasMissingImport
? []
: [
{
title: 'Open README to side',
command: {
command: getExtensionCommandId('openPackageReadmePreview'),
// TODO investigate on titles
title: '',
arguments: [moduleName],
},
kind: vscode.CodeActionKind.Empty,
},
{
title: 'Open Repository',
command: {
command: getExtensionCommandId('openPackageRepository'),
title: '',
arguments: [moduleName],
},
kind: vscode.CodeActionKind.Empty,
},
]),
{
title: 'Open on NPM',
command: {
command: getExtensionCommandId('openOnNpm'),
title: '',
arguments: [moduleName],
if (hasMissingImport) {
const { packageJson = {} } = await readPackageJsonWithMetadata({ type: 'closest' }).catch(() => ({}))
let foundType
for (const depType of ['dependencies', 'devDependencies', 'optionalDependencies'])
if (moduleName in packageJson[depType] ?? {}) {
foundType = depType
break
}

// TODO-low check for existence
codeActions.push(
{
title: 'Open README to side',
command: {
command: getExtensionCommandId('openPackageReadmePreview'),
title: '',
arguments: [moduleName],
},
kind: vscode.CodeActionKind.Empty,
},
{
title: 'Open Repository',
command: {
command: getExtensionCommandId('openPackageRepository'),
title: '',
arguments: [moduleName],
},
kind: vscode.CodeActionKind.Empty,
},
kind: vscode.CodeActionKind.Empty,
},
{
title: 'Remove with PNPM',
command: {
command: getExtensionCommandId('removePackages'),
title: '',
arguments: [[moduleName]],
{
title: 'Open on NPM',
command: {
command: getExtensionCommandId('openOnNpm'),
title: '',
arguments: [moduleName],
},
kind: vscode.CodeActionKind.Empty,
},
kind: vscode.CodeActionKind.Empty,
},
)
{
// todo use multiple find up packageJson and disable this code action if not found
title: `Remove ${foundType ? `from ${foundType}` : 'module'}`,
command: {
command: getExtensionCommandId('removePackages'),
title: '',
arguments: [[moduleName]],
},
kind: vscode.CodeActionKind.Empty,
},
)
}

if (hasMissingImport || hasMissingTypes) {
const addModuleFix = (module: string, type: 'dependency' | 'devDependency', isPreferred = true) => {
Expand Down
2 changes: 2 additions & 0 deletions src/commands-core/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const packageManagerCommand = async (_inputArg: {
return msg
}

// eslint-disable-next-line default-case
switch (subcommand) {
case 'add':
msg += 'Installing'
Expand All @@ -117,6 +118,7 @@ export const packageManagerCommand = async (_inputArg: {
msg += 'Linking'
break
}

msg += packages.length > 4 ? ` ${packages.length} packages` : `: ${packages.join(', ')}`
if (flags.includes('-D')) msg += ' as dev'
return msg
Expand Down

0 comments on commit ae20ad2

Please sign in to comment.