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

Commit 33d9d34

Browse files
author
Vlad Barosan
committed
further small improvements
1 parent 6e3393c commit 33d9d34

5 files changed

Lines changed: 39 additions & 52 deletions

File tree

src/goGenerateTests.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ export async function generateTestCurrentFunction(): Promise<boolean> {
9191

9292
const functions = await getFunctions(editor.document);
9393
const selection = editor.selection;
94-
const currentFunction: vscode.DocumentSymbol = functions.find(func => {
95-
if (selection && func.range.contains(selection.start)) {
96-
return true;
97-
}
98-
return false;
99-
});
94+
const currentFunction: vscode.DocumentSymbol = functions.find(func => selection && func.range.contains(selection.start));
10095

10196
if (!currentFunction) {
10297
vscode.window.showInformationMessage('No function found at cursor.');

src/goImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function getImports(document: vscode.TextDocument): Promise<string[]> {
3838
return [];
3939
}
4040
// import names will be of the form "math", so strip the quotes in the beginning and the end
41-
let imports = symbols[0].children
41+
const imports = symbols[0].children
4242
.filter(x => x.kind === vscode.SymbolKind.Namespace)
4343
.map(x => x.name.substr(1, x.name.length - 2));
4444
return imports;

src/goOutline.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export function runGoOutline(options: GoOutlineOptions, token: vscode.Cancellati
9797
});
9898
}
9999
if (err) return resolve(null);
100-
let result = stdout.toString();
101-
let decls = <GoOutlineDeclaration[]>JSON.parse(result);
100+
const result = stdout.toString();
101+
const decls = <GoOutlineDeclaration[]>JSON.parse(result);
102102
return resolve(decls);
103103
} catch (e) {
104104
reject(e);
@@ -130,14 +130,12 @@ function convertToCodeSymbols(
130130
(decls || []).forEach(decl => {
131131
if (!includeImports && decl.type === 'import') return;
132132

133-
let label = decl.label;
134133

135-
if (label === '_' && decl.type === 'variable') return;
136-
137-
if (decl.receiverType) {
138-
label = '(' + decl.receiverType + ').' + label;
139-
}
134+
if (decl.label === '_' && decl.type === 'variable') return;
140135

136+
const label = decl.receiverType
137+
? `(${decl.receiverType}).${decl.label}`
138+
: decl.label;
141139

142140
const start = byteOffsetToDocumentOffset(decl.start - 1);
143141
const end = byteOffsetToDocumentOffset(decl.end - 1);

src/goPackages.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ interface Cache {
1414
let gopkgsNotified: boolean = false;
1515
let cacheTimeout: number = 5000;
1616

17-
let gopkgsSubscriptions: Map<string, GopkgsDone[]> = new Map<string, GopkgsDone[]>();
18-
let gopkgsRunning: Set<string> = new Set<string>();
17+
const gopkgsSubscriptions: Map<string, GopkgsDone[]> = new Map<string, GopkgsDone[]>();
18+
const gopkgsRunning: Set<string> = new Set<string>();
1919

20-
let allPkgsCache: Map<string, Cache> = new Map<string, Cache>();
20+
const allPkgsCache: Map<string, Cache> = new Map<string, Cache>();
2121

22-
let pkgRootDirs = new Map<string, string>();
22+
const pkgRootDirs = new Map<string, string>();
2323

2424
function gopkgs(workDir?: string): Promise<Map<string, string>> {
2525
const gopkgsBinPath = getBinPath('gopkgs');
@@ -81,7 +81,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
8181
pkgs.set(pkgPath, pkgName);
8282
});
8383

84-
let timeTaken = Date.now() - t0;
84+
const timeTaken = Date.now() - t0;
8585
/* __GDPR__
8686
"gopkgs" : {
8787
"tool" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
@@ -127,28 +127,26 @@ function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> {
127127
* @argument workDir. The workspace directory of the project.
128128
* @returns Map<string, string> mapping between package import path and package name
129129
*/
130-
export function getAllPackages(workDir: string): Promise<Map<string, string>> {
131-
let cache = allPkgsCache.get(workDir);
132-
let useCache = cache && (new Date().getTime() - cache.lastHit) < cacheTimeout;
130+
export async function getAllPackages(workDir: string): Promise<Map<string, string>> {
131+
const cache = allPkgsCache.get(workDir);
132+
const useCache = cache && (new Date().getTime() - cache.lastHit) < cacheTimeout;
133133
if (useCache) {
134134
cache.lastHit = new Date().getTime();
135135
return Promise.resolve(cache.entry);
136136
}
137137

138-
return getAllPackagesNoCache(workDir).then((pkgs) => {
139-
if (!pkgs || pkgs.size === 0) {
140-
if (!gopkgsNotified) {
141-
vscode.window.showInformationMessage('Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.');
142-
gopkgsNotified = true;
143-
}
138+
const pkgs = await getAllPackagesNoCache(workDir);
139+
if (!pkgs || pkgs.size === 0) {
140+
if (!gopkgsNotified) {
141+
vscode.window.showInformationMessage('Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.');
142+
gopkgsNotified = true;
144143
}
145-
146-
allPkgsCache.set(workDir, {
147-
entry: pkgs,
148-
lastHit: new Date().getTime()
149-
});
150-
return pkgs;
144+
}
145+
allPkgsCache.set(workDir, {
146+
entry: pkgs,
147+
lastHit: new Date().getTime()
151148
});
149+
return pkgs;
152150
}
153151

154152
/**
@@ -160,52 +158,48 @@ export function getAllPackages(workDir: string): Promise<Map<string, string>> {
160158
*/
161159
export function getImportablePackages(filePath: string, useCache: boolean = false): Promise<Map<string, string>> {
162160
filePath = fixDriveCasingInWindows(filePath);
163-
let getAllPackagesPromise: Promise<Map<string, string>>;
164-
let fileDirPath = path.dirname(filePath);
161+
const fileDirPath = path.dirname(filePath);
165162

166163
let foundPkgRootDir = pkgRootDirs.get(fileDirPath);
167-
let workDir = foundPkgRootDir || fileDirPath;
168-
let cache = allPkgsCache.get(workDir);
164+
const workDir = foundPkgRootDir || fileDirPath;
165+
const cache = allPkgsCache.get(workDir);
169166

170-
if (useCache && cache) {
171-
getAllPackagesPromise = Promise.race([getAllPackages(workDir), cache.entry]);
172-
} else {
173-
getAllPackagesPromise = getAllPackages(workDir);
174-
}
167+
const getAllPackagesPromise: Promise<Map<string, string>> = useCache && cache
168+
? Promise.race([getAllPackages(workDir), cache.entry])
169+
: getAllPackages(workDir);
175170

176171
return Promise.all([isVendorSupported(), getAllPackagesPromise]).then(([vendorSupported, pkgs]) => {
177-
let pkgMap = new Map<string, string>();
172+
const pkgMap = new Map<string, string>();
178173
if (!pkgs) {
179174
return pkgMap;
180175
}
181176

182-
let currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
177+
const currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
183178
pkgs.forEach((pkgName, pkgPath) => {
184179
if (pkgName === 'main') {
185180
return;
186181
}
187182

188-
189183
if (!vendorSupported || !currentWorkspace) {
190184
pkgMap.set(pkgPath, pkgName);
191185
return;
192186
}
193187

194188
if (!foundPkgRootDir) {
195189
// try to guess package root dir
196-
let vendorIndex = pkgPath.indexOf('/vendor/');
190+
const vendorIndex = pkgPath.indexOf('/vendor/');
197191
if (vendorIndex !== -1) {
198192
foundPkgRootDir = path.join(currentWorkspace, pkgPath.substring(0, vendorIndex).replace('/', path.sep));
199193
pkgRootDirs.set(fileDirPath, foundPkgRootDir);
200194
}
201195
}
202196

203-
let relativePkgPath = getRelativePackagePath(fileDirPath, currentWorkspace, pkgPath);
197+
const relativePkgPath = getRelativePackagePath(fileDirPath, currentWorkspace, pkgPath);
204198
if (!relativePkgPath) {
205199
return;
206200
}
207201

208-
let allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath);
202+
const allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath);
209203
if (allowToImport) {
210204
pkgMap.set(relativePkgPath, pkgName);
211205
}

src/goReferencesCodelens.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export class GoReferencesCodeLensProvider extends GoBaseCodeLensProvider {
3434

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

0 commit comments

Comments
 (0)