Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Sort import packages by std lib first #2803

Merged
merged 10 commits into from
Nov 4, 2019
9 changes: 4 additions & 5 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export async function listPackages(excludeImportedPkgs: boolean = false): Promis
? await getImports(vscode.window.activeTextEditor.document)
: [];
const pkgMap = await getImportablePackages(vscode.window.activeTextEditor.document.fileName, true);

return Array.from(pkgMap.keys())
.filter(pkg => !importedPkgs.some(imported => imported === pkg))
.sort();
.filter(pkg => !importedPkgs.some(imported => imported === pkg));
//.sort();
}

/**
Expand Down Expand Up @@ -100,15 +99,15 @@ export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
}
}

export function addImport(arg: {importPath: string, from: string}) {
export function addImport(arg: { importPath: string, from: string }) {
const p = (arg && arg.importPath) ? Promise.resolve(arg.importPath) : askUserForImport();
p.then(imp => {
/* __GDPR__
"addImportCmd" : {
"from" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
sendTelemetryEvent('addImportCmd', { from: (arg && arg.from) || 'cmd'});
sendTelemetryEvent('addImportCmd', { from: (arg && arg.from) || 'cmd' });
const edits = getTextEditForAddImport(imp);
if (edits && edits.length > 0) {
const edit = new vscode.WorkspaceEdit();
Expand Down
25 changes: 18 additions & 7 deletions src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const allPkgsCache: Map<string, Cache> = new Map<string, Cache>();

const pkgRootDirs = new Map<string, string>();


function gopkgs(workDir?: string): Promise<Map<string, string>> {
const gopkgsBinPath = getBinPath('gopkgs');
if (!path.isAbsolute(gopkgsBinPath)) {
Expand All @@ -35,7 +36,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {

const t0 = Date.now();
return new Promise<Map<string, string>>((resolve, reject) => {
const args = ['-format', '{{.Name}};{{.ImportPath}}'];
const args = ['-format', '{{.Name}};{{.ImportPath}};{{.Dir}}'];
if (workDir) {
args.push('-workDir', workDir);
}
Expand Down Expand Up @@ -79,13 +80,23 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
});
return resolve(pkgs);
}

const stdPkgs = new Map<string, string>();
const otherPkg = new Map<string, string>();
output.split('\n').forEach((pkgDetail) => {
if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) return;
const [pkgName, pkgPath] = pkgDetail.trim().split(';');
pkgs.set(pkgPath, pkgName);
const [pkgName, pkgPath, pkgDir] = pkgDetail.trim().split(';');
if (pkgDir.includes(process.env['GOROOT'])) {
stdPkgs.set(pkgPath, pkgName);
} else {
otherPkg.set(pkgPath, pkgName);
}
});

Array.from(stdPkgs.keys()).forEach((val)=>{
pkgs.set(val, stdPkgs.get(val));
})
Array.from(otherPkg.keys()).forEach((val)=>{
pkgs.set(val, otherPkg.get(val));
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramya-rao-a This looks very crude to me :) esp maintaining two maps and merging them. Could you suggest a way to improve it? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of maintaining 2 different maps, we can go back to the single map as before.
That map uses string -> string at the moment
We can change that to string -> PackageInfo where PackageInfo is an interface

export interface PackageInfo {
   name string;
   isStd boolean;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it extendable such that in the future if we want to extract more info from the gopkgs tool, we can do so.

const timeTaken = Date.now() - t0;
/* __GDPR__
"gopkgs" : {
Expand All @@ -103,7 +114,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> {
return new Promise<Map<string, string>>((resolve, reject) => {
// Use subscription style to guard costly/long running invocation
const callback = function(pkgMap: Map<string, string>) {
const callback = function (pkgMap: Map<string, string>) {
resolve(pkgMap);
};

Expand Down Expand Up @@ -299,4 +310,4 @@ function isAllowToImportPackage(toDirPath: string, currentWorkspace: string, pkg
return toDirPath.startsWith(rootProjectForInternalPkg + path.sep) || toDirPath === rootProjectForInternalPkg;
}
return true;
}
}