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

Commit

Permalink
Ensure completion to respond faster (#1209)
Browse files Browse the repository at this point in the history
* Ensure not more than 1sec delivery on completion

* Lint warning: add whitespace

* Speed up listPackages test

* Do not wait importable packages ready for more than 1sec

* Remove unused variable
  • Loading branch information
uudashr authored and ramya-rao-a committed Sep 14, 2017
1 parent dd92750 commit 9618aa0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import vscode = require('vscode');
import cp = require('child_process');
import { dirname, basename } from 'path';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars } from './util';
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, timeout } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getTextEditForAddImport } from './goImport';
import { getImportablePackages } from './goPackages';
Expand Down Expand Up @@ -214,10 +214,8 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}
// TODO: Shouldn't lib-path also be set?
private ensureGoCodeConfigured(): Thenable<void> {
let setPkgsList = getImportablePackages(vscode.window.activeTextEditor.document.fileName).then(pkgMap => {
this.pkgsList = pkgMap;
return;
});
let importablePkgsPromise = getImportablePackages(vscode.window.activeTextEditor.document.fileName).then(pkgMap => this.pkgsList = pkgMap);
let setPkgsList = Promise.race([timeout(1000).then(() => this.pkgsList), importablePkgsPromise]);

let setGocodeProps = new Promise<void>((resolve, reject) => {
let gocode = getBinPath('gocode');
Expand Down
9 changes: 8 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,18 @@ export class LineBuffer {
}
}

export function timeout(millis): Promise<void> {
return new Promise<void>((resolve, reject) => {
setTimeout(() => resolve(), millis);
});
}

/**
* Exapnds ~ to homedir in non-Windows platform and resolves ${workspaceRoot}
*/
export function resolvePath(inputPath: string): string {
if (!inputPath || !inputPath.trim()) return inputPath;
inputPath = inputPath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath);
return resolveHomeDir(inputPath);
}
}

9 changes: 3 additions & 6 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,10 @@ It returns the number of bytes written and any write error encountered.
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);
return Promise.all([includeImportedPkgs, excludeImportedPkgs]).then(([pkgsInclude, pkgsExclude]) => {
assert.equal(pkgsInclude.indexOf('fmt') > -1, true);
assert.equal(pkgsExclude.indexOf('fmt') > -1, false);
});
excludeImportedPkgs.then(pkgs => {
assert.equal(pkgs.indexOf('fmt') > -1, false);
});
return Promise.all([includeImportedPkgs, excludeImportedPkgs]);
});
}).then(() => done(), done);
});
Expand Down

0 comments on commit 9618aa0

Please sign in to comment.