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

Commit

Permalink
further small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Barosan committed Feb 25, 2019
1 parent 6e3393c commit 33d9d34
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
7 changes: 1 addition & 6 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ export async function generateTestCurrentFunction(): Promise<boolean> {

const functions = await getFunctions(editor.document);
const selection = editor.selection;
const currentFunction: vscode.DocumentSymbol = functions.find(func => {
if (selection && func.range.contains(selection.start)) {
return true;
}
return false;
});
const currentFunction: vscode.DocumentSymbol = functions.find(func => selection && func.range.contains(selection.start));

if (!currentFunction) {
vscode.window.showInformationMessage('No function found at cursor.');
Expand Down
2 changes: 1 addition & 1 deletion src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function getImports(document: vscode.TextDocument): Promise<string[]> {
return [];
}
// import names will be of the form "math", so strip the quotes in the beginning and the end
let imports = symbols[0].children
const imports = symbols[0].children
.filter(x => x.kind === vscode.SymbolKind.Namespace)
.map(x => x.name.substr(1, x.name.length - 2));
return imports;
Expand Down
14 changes: 6 additions & 8 deletions src/goOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export function runGoOutline(options: GoOutlineOptions, token: vscode.Cancellati
});
}
if (err) return resolve(null);
let result = stdout.toString();
let decls = <GoOutlineDeclaration[]>JSON.parse(result);
const result = stdout.toString();
const decls = <GoOutlineDeclaration[]>JSON.parse(result);
return resolve(decls);
} catch (e) {
reject(e);
Expand Down Expand Up @@ -130,14 +130,12 @@ function convertToCodeSymbols(
(decls || []).forEach(decl => {
if (!includeImports && decl.type === 'import') return;

let label = decl.label;

if (label === '_' && decl.type === 'variable') return;

if (decl.receiverType) {
label = '(' + decl.receiverType + ').' + label;
}
if (decl.label === '_' && decl.type === 'variable') return;

const label = decl.receiverType
? `(${decl.receiverType}).${decl.label}`
: decl.label;

const start = byteOffsetToDocumentOffset(decl.start - 1);
const end = byteOffsetToDocumentOffset(decl.end - 1);
Expand Down
64 changes: 29 additions & 35 deletions src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ interface Cache {
let gopkgsNotified: boolean = false;
let cacheTimeout: number = 5000;

let gopkgsSubscriptions: Map<string, GopkgsDone[]> = new Map<string, GopkgsDone[]>();
let gopkgsRunning: Set<string> = new Set<string>();
const gopkgsSubscriptions: Map<string, GopkgsDone[]> = new Map<string, GopkgsDone[]>();
const gopkgsRunning: Set<string> = new Set<string>();

let allPkgsCache: Map<string, Cache> = new Map<string, Cache>();
const allPkgsCache: Map<string, Cache> = new Map<string, Cache>();

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

function gopkgs(workDir?: string): Promise<Map<string, string>> {
const gopkgsBinPath = getBinPath('gopkgs');
Expand Down Expand Up @@ -81,7 +81,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
pkgs.set(pkgPath, pkgName);
});

let timeTaken = Date.now() - t0;
const timeTaken = Date.now() - t0;
/* __GDPR__
"gopkgs" : {
"tool" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
Expand Down Expand Up @@ -127,28 +127,26 @@ function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> {
* @argument workDir. The workspace directory of the project.
* @returns Map<string, string> mapping between package import path and package name
*/
export function getAllPackages(workDir: string): Promise<Map<string, string>> {
let cache = allPkgsCache.get(workDir);
let useCache = cache && (new Date().getTime() - cache.lastHit) < cacheTimeout;
export async function getAllPackages(workDir: string): Promise<Map<string, string>> {
const cache = allPkgsCache.get(workDir);
const useCache = cache && (new Date().getTime() - cache.lastHit) < cacheTimeout;
if (useCache) {
cache.lastHit = new Date().getTime();
return Promise.resolve(cache.entry);
}

return getAllPackagesNoCache(workDir).then((pkgs) => {
if (!pkgs || pkgs.size === 0) {
if (!gopkgsNotified) {
vscode.window.showInformationMessage('Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.');
gopkgsNotified = true;
}
const pkgs = await getAllPackagesNoCache(workDir);
if (!pkgs || pkgs.size === 0) {
if (!gopkgsNotified) {
vscode.window.showInformationMessage('Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.');
gopkgsNotified = true;
}

allPkgsCache.set(workDir, {
entry: pkgs,
lastHit: new Date().getTime()
});
return pkgs;
}
allPkgsCache.set(workDir, {
entry: pkgs,
lastHit: new Date().getTime()
});
return pkgs;
}

/**
Expand All @@ -160,52 +158,48 @@ export function getAllPackages(workDir: string): Promise<Map<string, string>> {
*/
export function getImportablePackages(filePath: string, useCache: boolean = false): Promise<Map<string, string>> {
filePath = fixDriveCasingInWindows(filePath);
let getAllPackagesPromise: Promise<Map<string, string>>;
let fileDirPath = path.dirname(filePath);
const fileDirPath = path.dirname(filePath);

let foundPkgRootDir = pkgRootDirs.get(fileDirPath);
let workDir = foundPkgRootDir || fileDirPath;
let cache = allPkgsCache.get(workDir);
const workDir = foundPkgRootDir || fileDirPath;
const cache = allPkgsCache.get(workDir);

if (useCache && cache) {
getAllPackagesPromise = Promise.race([getAllPackages(workDir), cache.entry]);
} else {
getAllPackagesPromise = getAllPackages(workDir);
}
const getAllPackagesPromise: Promise<Map<string, string>> = useCache && cache
? Promise.race([getAllPackages(workDir), cache.entry])
: getAllPackages(workDir);

return Promise.all([isVendorSupported(), getAllPackagesPromise]).then(([vendorSupported, pkgs]) => {
let pkgMap = new Map<string, string>();
const pkgMap = new Map<string, string>();
if (!pkgs) {
return pkgMap;
}

let currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
const currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
pkgs.forEach((pkgName, pkgPath) => {
if (pkgName === 'main') {
return;
}


if (!vendorSupported || !currentWorkspace) {
pkgMap.set(pkgPath, pkgName);
return;
}

if (!foundPkgRootDir) {
// try to guess package root dir
let vendorIndex = pkgPath.indexOf('/vendor/');
const vendorIndex = pkgPath.indexOf('/vendor/');
if (vendorIndex !== -1) {
foundPkgRootDir = path.join(currentWorkspace, pkgPath.substring(0, vendorIndex).replace('/', path.sep));
pkgRootDirs.set(fileDirPath, foundPkgRootDir);
}
}

let relativePkgPath = getRelativePackagePath(fileDirPath, currentWorkspace, pkgPath);
const relativePkgPath = getRelativePackagePath(fileDirPath, currentWorkspace, pkgPath);
if (!relativePkgPath) {
return;
}

let allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath);
const allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath);
if (allowToImport) {
pkgMap.set(relativePkgPath, pkgName);
}
Expand Down
4 changes: 2 additions & 2 deletions src/goReferencesCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class GoReferencesCodeLensProvider extends GoBaseCodeLensProvider {

// Add offset for functions as go-outline returns position at the keyword func instead of func name
if (symbol.kind === vscode.SymbolKind.Function) {
let funcDecl = document.lineAt(position.line).text.substr(position.character);
let match = methodRegex.exec(funcDecl);
const funcDecl = document.lineAt(position.line).text.substr(position.character);
const match = methodRegex.exec(funcDecl);
position = position.translate(0, match ? match[0].length : 5);
}
return new ReferencesCodeLens(document, new vscode.Range(position, position));
Expand Down

0 comments on commit 33d9d34

Please sign in to comment.