Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
fix(general): replace rootpath, add it to DI to minimize vscode loggi…
Browse files Browse the repository at this point in the history
…ng (#244)


Fixes #242
  • Loading branch information
buehler committed Jul 12, 2017
1 parent 3b53d9b commit 2d135c8
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/extension/IoC.ts
Expand Up @@ -18,13 +18,14 @@ import { VscodeExtensionConfig } from './VscodeExtensionConfig';

const container = new IoCContainer();

container.bind(iocSymbols.rootPath).toConstantValue(workspace.rootPath || '');
container.bind(TypeScriptHero).to(TypeScriptHero).inSingletonScope();
container.bind(iocSymbols.configuration).to(VscodeExtensionConfig).inSingletonScope();
container
.bind<DeclarationIndex>(iocSymbols.declarationIndex)
.toDynamicValue((context: interfaces.Context) => {
const parser = context.container.get<TypescriptParser>(iocSymbols.typescriptParser);
return new DeclarationIndex(parser, workspace.rootPath || '');
return new DeclarationIndex(parser, context.container.get<string>(iocSymbols.rootPath));
})
.inSingletonScope();

Expand Down
1 change: 1 addition & 0 deletions src/extension/IoCSymbols.ts
Expand Up @@ -10,4 +10,5 @@ export const iocSymbols = {
codeActionCreators: Symbol('codeActionCreators'),
declarationIndex: Symbol('declarationIndex'),
typescriptParser: Symbol('typescriptParser'),
rootPath: Symbol('rootPath'),
};
Expand Up @@ -6,7 +6,7 @@ import {
NamedImport,
TypescriptParser,
} from 'typescript-parser';
import { Command, Diagnostic, TextDocument, workspace } from 'vscode';
import { Command, Diagnostic, TextDocument } from 'vscode';

import { getAbsolutLibraryName } from '../../common/helpers';
import { iocSymbols } from '../IoCSymbols';
Expand All @@ -25,6 +25,7 @@ export class MissingImplementationInClassCreator extends CodeActionCreator {
constructor(
@inject(iocSymbols.typescriptParser) private parser: TypescriptParser,
@inject(iocSymbols.declarationIndex) private index: DeclarationIndex,
@inject(iocSymbols.rootPath) private rootPath: string,
) {
super();
}
Expand Down Expand Up @@ -77,7 +78,7 @@ export class MissingImplementationInClassCreator extends CodeActionCreator {
const declaration = (parsedDocument.declarations.find(o => o.name === specifier) ||
(this.index.declarationInfos.find(
o => o.declaration.name === specifier &&
o.from === getAbsolutLibraryName(alreadyImported!.libraryName, document.fileName, workspace.rootPath),
o.from === getAbsolutLibraryName(alreadyImported!.libraryName, document.fileName, this.rootPath),
) || { declaration: undefined }).declaration) as (ClassLikeDeclaration & GenericDeclaration) | undefined;

if (commands.some((o: Command) => o.title.indexOf(specifier) >= 0)) {
Expand Down
4 changes: 2 additions & 2 deletions src/extension/extensions/CodeCompletionExtension.ts
Expand Up @@ -8,7 +8,6 @@ import {
languages,
Position,
TextDocument,
workspace,
} from 'vscode';

import { getDeclarationsFilteredByImports } from '../../common/helpers';
Expand All @@ -35,6 +34,7 @@ export class CodeCompletionExtension extends BaseExtension implements Completion
@inject(iocSymbols.loggerFactory) loggerFactory: LoggerFactory,
@inject(iocSymbols.typescriptParser) private parser: TypescriptParser,
@inject(iocSymbols.declarationIndex) private index: DeclarationIndex,
@inject(iocSymbols.rootPath) private rootPath: string,
) {
super(context);
this.logger = loggerFactory('CodeCompletionExtension');
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CodeCompletionExtension extends BaseExtension implements Completion
this.index.declarationInfos,
document.fileName,
parsed.imports,
workspace.rootPath,
this.rootPath,
)
.filter(o => !parsed.declarations.some(d => d.name === o.declaration.name))
.filter(o => !parsed.usages.some(d => d === o.declaration.name));
Expand Down
15 changes: 8 additions & 7 deletions src/extension/extensions/ImportResolveExtension.ts
Expand Up @@ -55,7 +55,7 @@ function compareIgnorePatterns(local: string[], config: string[]): boolean {
* @param {ExtensionConfig} config
* @returns {Promise<string[]>}
*/
export async function findFiles(config: ExtensionConfig): Promise<string[]> {
export async function findFiles(config: ExtensionConfig, rootPath: string): Promise<string[]> {
const searches: PromiseLike<Uri[]>[] = [
workspace.findFiles(
'{**/*.ts,**/*.tsx}',
Expand All @@ -66,8 +66,8 @@ export async function findFiles(config: ExtensionConfig): Promise<string[]> {
let globs: string[] = [];
let ignores = ['**/typings/**'];

if (workspace.rootPath && existsSync(join(workspace.rootPath, 'package.json'))) {
const packageJson = require(join(workspace.rootPath, 'package.json'));
if (rootPath && existsSync(join(rootPath, 'package.json'))) {
const packageJson = require(join(rootPath, 'package.json'));
if (packageJson['dependencies']) {
globs = globs.concat(
Object.keys(packageJson['dependencies']).map(o => `**/node_modules/${o}/**/*.d.ts`),
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function findFiles(config: ExtensionConfig): Promise<string[]> {
uris = uris.map((o, idx) => idx === 0 ?
o.filter(
f => f.fsPath
.replace(workspace.rootPath || '', '')
.replace(rootPath || '', '')
.split(/\\|\//)
.every(p => excludePatterns.indexOf(p) < 0)) :
o,
Expand Down Expand Up @@ -136,6 +136,7 @@ export class ImportResolveExtension extends BaseExtension {
@inject(iocSymbols.configuration) private config: ExtensionConfig,
@inject(iocSymbols.typescriptParser) private parser: TypescriptParser,
@inject(iocSymbols.declarationIndex) private index: DeclarationIndex,
@inject(iocSymbols.rootPath) private rootPath: string,
) {
super(context);
this.logger = loggerFactory('ImportResolveExtension');
Expand Down Expand Up @@ -222,7 +223,7 @@ export class ImportResolveExtension extends BaseExtension {
private async buildIndex(): Promise<void> {
this.statusBarItem.text = resolverSyncing;

const files = await findFiles(this.config);
const files = await findFiles(this.config, this.rootPath);
this.logger.info(`Building index for ${files.length} files.`);
try {
await this.index.buildIndex(files);
Expand Down Expand Up @@ -457,7 +458,7 @@ export class ImportResolveExtension extends BaseExtension {
this.index.declarationInfos,
documentPath,
parsedSource.imports,
workspace.rootPath,
this.rootPath,
).filter(o => o.declaration.name.startsWith(cursorSymbol));

return [
Expand Down Expand Up @@ -486,7 +487,7 @@ export class ImportResolveExtension extends BaseExtension {
this.index.declarationInfos,
documentPath,
parsedDocument.imports,
workspace.rootPath,
this.rootPath,
);

for (const usage of parsedDocument.nonLocalUsages) {
Expand Down
12 changes: 8 additions & 4 deletions src/extension/managers/ImportManager.ts
Expand Up @@ -53,6 +53,10 @@ export class ImportManager implements ObjectManager {
return Container.get<() => TypescriptCodeGenerator>(iocSymbols.generatorFactory)();
}

private static get rootPath(): string {
return Container.get<string>(iocSymbols.rootPath);
}

private importGroups: ImportGroup[];
private imports: Import[] = [];
private userImportDecisions: { [usage: string]: DeclarationInfo[] }[] = [];
Expand Down Expand Up @@ -122,7 +126,7 @@ export class ImportManager implements ObjectManager {
o => declarationInfo.from === getAbsolutLibraryName(
o.libraryName,
this.document.fileName,
workspace.rootPath,
ImportManager.rootPath,
) && o instanceof ImportProxy,
) as ImportProxy;

Expand All @@ -145,14 +149,14 @@ export class ImportManager implements ObjectManager {
imp = new ImportProxy(getRelativeLibraryName(
declarationInfo.from,
this.document.fileName,
workspace.rootPath,
ImportManager.rootPath,
));
(imp as ImportProxy).defaultPurposal = declarationInfo.declaration.name;
} else {
imp = new ImportProxy(getRelativeLibraryName(
declarationInfo.from,
this.document.fileName,
workspace.rootPath,
ImportManager.rootPath,
));
(imp as ImportProxy).specifiers.push(new SymbolSpecifier(declarationInfo.declaration.name));
}
Expand All @@ -177,7 +181,7 @@ export class ImportManager implements ObjectManager {
index.declarationInfos,
this.document.fileName,
this.imports,
workspace.rootPath,
ImportManager.rootPath,
);

for (const usage of this._parsedDocument.nonLocalUsages) {
Expand Down
19 changes: 10 additions & 9 deletions test/extension/extensions/CodeActionExtension.test.ts
Expand Up @@ -28,6 +28,7 @@ class SpyCodeAction implements CodeAction {
describe('CodeActionExtension', () => {

let extension: any;
const rootPath = Container.get<string>(iocSymbols.rootPath);

before(async () => {
const ctx = Container.get<ExtensionContext>(iocSymbols.extensionContext);
Expand All @@ -38,27 +39,27 @@ describe('CodeActionExtension', () => {
await index.buildIndex(
[
join(
workspace.rootPath!,
rootPath,
'server/indices/MyClass.ts',
),
join(
workspace.rootPath!,
rootPath,
'extension/extensions/codeActionExtension/exportedObjects.ts',
),
join(
workspace.rootPath!,
rootPath,
'extension/extensions/codeActionExtension/implementInterfaceOrAbstract.ts',
),
join(
workspace.rootPath!,
rootPath,
'node_modules/fancy-library/FancierLibraryClass.d.ts',
),
],
);

const creators = [
new MissingImportCreator(index as any),
new MissingImplementationInClassCreator(parser, index as any),
new MissingImplementationInClassCreator(parser, index as any, rootPath),
];

extension = new CodeActionExtension(ctx, logger, creators, index as any);
Expand All @@ -67,7 +68,7 @@ describe('CodeActionExtension', () => {
describe('executeCodeAction', () => {

const file = join(
workspace.rootPath!,
rootPath,
'extension/extensions/codeActionExtension/empty.ts',
);
let document: TextDocument;
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('CodeActionExtension', () => {
describe('missingImport', () => {

const file = join(
workspace.rootPath!,
rootPath,
'extension/extensions/codeActionExtension/empty.ts',
);
let document: TextDocument;
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('CodeActionExtension', () => {
});

describe('missingPolymorphicElements', () => {
const file = join(workspace.rootPath!, 'extension/extensions/codeActionExtension/implementInterfaceOrAbstract.ts');
const file = join(rootPath, 'extension/extensions/codeActionExtension/implementInterfaceOrAbstract.ts');
let document: TextDocument;
let documentText: string;

Expand Down Expand Up @@ -428,7 +429,7 @@ describe('CodeActionExtension', () => {
describe('provideCodeActions', () => {

const file = join(
workspace.rootPath!,
rootPath,
'extension/extensions/codeActionExtension/empty.ts',
);
let document: TextDocument;
Expand Down
10 changes: 6 additions & 4 deletions test/extension/extensions/CodeCompletionExtension.test.ts
Expand Up @@ -17,8 +17,10 @@ describe('CodeCompletionExtension', () => {
let extension: CodeCompletionExtension;

before(async () => {
const rootPath = Container.get<string>(iocSymbols.rootPath);

const file = join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/codeCompletionExtension/codeCompletionFile.ts',
);
document = await vscode.workspace.openTextDocument(file);
Expand All @@ -32,17 +34,17 @@ describe('CodeCompletionExtension', () => {
await index.buildIndex(
[
join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/codeCompletionExtension/codeCompletionImports.ts',
),
join(
vscode.workspace.rootPath!,
rootPath,
'server/indices/MyClass.ts',
),
],
);

extension = new CodeCompletionExtension(ctx, logger, parser, index as any);
extension = new CodeCompletionExtension(ctx, logger, parser, index as any, rootPath);
});

it('shoud resolve to null if typing in a string', async () => {
Expand Down
Expand Up @@ -25,8 +25,9 @@ describe('DocumentSymbolStructureExtension', () => {
let document: vscode.TextDocument;

before(async () => {
const rootPath = Container.get<string>(iocSymbols.rootPath);
const file = join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/documentSymbolStructureExtension/documentSymbolFile.ts',
);
document = await vscode.workspace.openTextDocument(file);
Expand All @@ -52,8 +53,9 @@ describe('DocumentSymbolStructureExtension', () => {
});

it('should return a "file not parsable" if it is no ts file', async () => {
const rootPath = Container.get<string>(iocSymbols.rootPath);
const file = join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/documentSymbolStructureExtension/notParsable.txt',
);
const txtDocument = await vscode.workspace.openTextDocument(file);
Expand Down
20 changes: 11 additions & 9 deletions test/extension/extensions/ImportResolveExtension.test.ts
Expand Up @@ -13,11 +13,13 @@ chai.should();

describe('ImportResolveExtension', () => {

let rootPath: string;
let extension: any;

before(async () => {
rootPath = Container.get<string>(iocSymbols.rootPath);
const file = join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/importResolveExtension/addImportToDocument.ts',
);
const document = await vscode.workspace.openTextDocument(file);
Expand All @@ -33,31 +35,31 @@ describe('ImportResolveExtension', () => {
await index.buildIndex(
[
join(
vscode.workspace.rootPath!,
rootPath,
'typings/globals/body-parser/index.d.ts',
),
join(
vscode.workspace.rootPath!,
rootPath,
'server/indices/MyClass.ts',
),
join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/importResolveExtension/sub1/sub2/sub3/subFile.ts',
),
join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/importResolveExtension/sameDirectory.ts',
),
],
);

extension = new ImportResolveExtension(ctx, logger, config, parser, index);
extension = new ImportResolveExtension(ctx, logger, config, parser, index, rootPath);
});

describe('addImportToDocument', () => {

const rootPath = Container.get<string>(iocSymbols.rootPath);
const file = join(
vscode.workspace.rootPath!,
rootPath,
'extension/extensions/importResolveExtension/addImportToDocument.ts',
);
let document: vscode.TextDocument;
Expand Down Expand Up @@ -141,7 +143,7 @@ describe('ImportResolveExtension', () => {

describe('organizeImports', () => {

const file = join(vscode.workspace.rootPath!, 'extension/extensions/importResolveExtension/organizeImports.ts');
const file = join(rootPath, 'extension/extensions/importResolveExtension/organizeImports.ts');
let document: vscode.TextDocument;
let documentText: string;

Expand Down

0 comments on commit 2d135c8

Please sign in to comment.