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

Commit

Permalink
Do not allow import for already imported pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Oct 6, 2016
1 parent 91637fb commit 27f7681
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import cp = require('child_process');
import { getBinPath } from './goPath';
import { parseFilePrelude } from './util';
import { promptForMissingTool } from './goInstallTools';
import { documentSymbols } from './goOutline';

export function listPackages(): Thenable<string[]> {
return new Promise<string[]>((resolve, reject) => {
export function listPackages(excludeImportedPkgs: boolean = false): Thenable<string[]> {
let importsPromise = excludeImportedPkgs && vscode.window.activeTextEditor ? getImports(vscode.window.activeTextEditor.document.fileName) : Promise.resolve([]);
let pkgsPromise = new Promise<string[]>((resolve, reject) => {
cp.execFile(getBinPath('gopkgs'), [], (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gopkgs');
Expand All @@ -23,10 +25,38 @@ export function listPackages(): Thenable<string[]> {
return resolve(sortedlines);
});
});

return Promise.all<string[]>([importsPromise, pkgsPromise]).then(values => {
let imports = values[0];
let pkgs = values[1];
if (imports.length === 0) {
return pkgs;
}
return pkgs.filter(element => {
return imports.indexOf(element) === -1;
});
});
}

/**
* Returns the imported packages in the given file
*
* @param fileName File system path of the file whose imports need to be returned
* @returns Array of imported package paths wrapped in a promise
*/
export function getImports(fileName: string): Promise<string[]> {
return documentSymbols(fileName).then(symbols => {
if (!symbols || !symbols[0] || !symbols[0].children) {
return [];
}
// imports will be of the form { type: 'import', label: '"math"'}
let imports = symbols[0].children.filter(x => x.type === 'import').map(x => x.label.substr(1, x.label.length - 2));
return imports;
});
}

function askUserForImport(): Thenable<string> {
return listPackages().then(packages => {
return listPackages(true).then(packages => {
return vscode.window.showQuickPick(packages);
});
}
Expand Down
35 changes: 35 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getEditsFromUnifiedDiffStr, getEdits } from '../src/diffUtils';
import jsDiff = require('diff');
import { testCurrentFile } from '../src/goTest';
import { getGoVersion } from '../src/goInstallTools';
import { documentSymbols } from '../src/goOutline';
import { listPackages } from '../src/goImport';

suite('Go Extension Tests', () => {
let gopath = process.env['GOPATH'];
Expand Down Expand Up @@ -278,4 +280,37 @@ encountered.
}).then(() => done(), done);
});

test('Test Outline', (done) => {
let filePath = path.join(fixturePath, 'test.go');
documentSymbols(filePath).then(outlines => {
let packageOutline = outlines[0];
let symbols = packageOutline.children;
let imports = symbols.filter(x => x.type === 'import');
let functions = symbols.filter(x => x.type === 'function');

assert.equal(packageOutline.type, 'package');
assert.equal(packageOutline.label, 'main');
assert.equal(imports[0].label, '"fmt"');
assert.equal(functions[0].label, 'print');
assert.equal(functions[1].label, 'main');
done();
}, done);
});

test('Test listPackages', (done) => {
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
let includeImportedPkgs = listPackages(false);
let excludeImportedPkgs = listPackages(true);
includeImportedPkgs.then(pkgs => {
assert.equal(pkgs.indexOf('fmt') > -1, true);
});
excludeImportedPkgs.then(pkgs => {
assert.equal(pkgs.indexOf('fmt') > -1, false);
});
return Promise.all([includeImportedPkgs, excludeImportedPkgs]);
});
}).then(() => done(), done);
});
});

0 comments on commit 27f7681

Please sign in to comment.