Skip to content

Commit

Permalink
feat: show pkg desc on vscode not builtin dependencies node
Browse files Browse the repository at this point in the history
  • Loading branch information
tjx666 committed Jun 30, 2024
1 parent 8b72007 commit e76d32f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/hoverTooltips/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class DependenciesHoverProvider implements HoverProvider {
const pkgNameAndVersion = await getPkgNameAndVersionFromDocPosition(document, position);
if (!pkgNameAndVersion) return;

const { name, version } = pkgNameAndVersion;
const { name, version, dependenciesNodePath } = pkgNameAndVersion;
const info = await getPackageInfo(name, {
packageInstallDir: await findPkgInstallDir(name, document.uri.fsPath),
searchVersionRange: version,
Expand All @@ -27,7 +27,11 @@ export class DependenciesHoverProvider implements HoverProvider {
if (!info) return;

const pkgHoverContentsCreator = getPkgHoverContentsCreator();
const hoverContents = pkgHoverContentsCreator.generate(info);
const isVscodeBuiltinShowDesc =
!dependenciesNodePath.includes('.') && dependenciesNodePath.endsWith('dependencies');
const hoverContents = pkgHoverContentsCreator.generate(info, {
showDescription: !isVscodeBuiltinShowDesc,
});
return new Hover(hoverContents);
}
}
17 changes: 9 additions & 8 deletions src/utils/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ export async function getPkgNameAndVersionFromDocPosition(
const nameNode = findNodeAtOffset(root, document.offsetAt(position));
if (!nameNode || !nameNode.parent) return;

const dependenciesNodes =
configuration.packageJsonDependenciesCodeLens.dependenciesNodePaths.map((path) =>
findNodeAtLocation(root, path.split('.')),
);

const versionNode = nameNode.parent.children![1];
const isHoverOverDependency =
let isHoverOverDependency =
nameNode.type === 'string' &&
versionNode.type === 'string' &&
nameNode.parent?.type === 'property' &&
nameNode.parent.children?.length === 2 &&
nameNode === nameNode.parent.children![0] &&
dependenciesNodes.includes(nameNode.parent?.parent);
nameNode === nameNode.parent.children![0];
const dependenciesNodePath =
configuration.packageJsonDependenciesCodeLens.dependenciesNodePaths.find((path) => {
const node = findNodeAtLocation(root, path.split('.'));
return node === nameNode.parent?.parent;
});
isHoverOverDependency = isHoverOverDependency && !!dependenciesNodePath;
if (!isHoverOverDependency) return;

const pkgName = nameNode.value;
Expand All @@ -77,6 +77,7 @@ export async function getPkgNameAndVersionFromDocPosition(
return {
nameNode,
versionNode,
dependenciesNodePath: dependenciesNodePath!,
name: pkgName,
version: versionNode.value!,
};
Expand Down
3 changes: 3 additions & 0 deletions test-workspace/pkg-json-codelens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"bbb": "233",
"vscode": "1.1.37",
"execa": "^7.1.1"
},
"resolutions": {
"axios": "0.28.1"
}
}

0 comments on commit e76d32f

Please sign in to comment.