From 2d5af4033043692dc42f6ec126b0a0c6baec2acc Mon Sep 17 00:00:00 2001 From: Rokas Brazdzionis Date: Thu, 23 Mar 2017 16:05:46 +0200 Subject: [PATCH] Make regexps more general just to filter out lines --- .../angular-html-definition-provider.ts | 19 ++-- test/_test_files/foo.component.html | 4 + test/_test_files/foo.component.ts | 4 +- .../angular-html-definition-provider.test.ts | 90 ++++++++++++------- 4 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/providers/angular-html-definition-provider.ts b/src/providers/angular-html-definition-provider.ts index ba40edf..182cdd7 100644 --- a/src/providers/angular-html-definition-provider.ts +++ b/src/providers/angular-html-definition-provider.ts @@ -14,15 +14,24 @@ export class AngularHtmlDefinitionProvider implements DefinitionProvider { const regexps = [ // Interpolation. ex: {{ myProp }} - /{{\s*(\w+)[\.\s\w]*}}/g, + /{{(.*)}}/g, - // Input attributes. ex: [...]="myProp" - /\[\(?[\w\.\-?]*\)?\]=\"!?(\w+)[\.\w]*\"/g, + // Input attributes. ex: [(...)]="myProp" + /\[\(?[\w\.\-?]*\)?\]=\"(.*)"/g, // Output attributes. ex: (...)="myMethod(...)" - /\([\w\.]*\)=\"(\w+)\([\S]*\)\"/g + /\([\w\.]*\)=\"(.*)\"/g, + + // Structural attributes. ex: *ngIf="myProp" + /\*\w+=\"(.*)\"/g ]; - let propertyName: string = utils.parseByLocationRegexps(lineText, position.character, regexps); + let expressionMatch: string = utils.parseByLocationRegexps(lineText, position.character, regexps); + if (!expressionMatch) return false; + + let range = document.getWordRangeAtPosition(position); + if (!range) return false; + + let propertyName = document.getText(range); const componentFilePath = document.fileName.substr(0, document.fileName.lastIndexOf('.')) + '.ts'; diff --git a/test/_test_files/foo.component.html b/test/_test_files/foo.component.html index 8c8bb66..6f7e1af 100644 --- a/test/_test_files/foo.component.html +++ b/test/_test_files/foo.component.html @@ -1,4 +1,8 @@
{{ myProperty }} + +
+ + {{ myProperty | myPipe }}
\ No newline at end of file diff --git a/test/_test_files/foo.component.ts b/test/_test_files/foo.component.ts index ea210b1..9ee890e 100644 --- a/test/_test_files/foo.component.ts +++ b/test/_test_files/foo.component.ts @@ -5,7 +5,7 @@ }) export class FooComponent { myProperty = ''; - myLongProperty: string; + @Input() myLongProperty: string; constructor() { } @@ -16,4 +16,6 @@ export class FooComponent { myMethod() { // do smth } + + good = false; } diff --git a/test/providers/angular-html-definition-provider.test.ts b/test/providers/angular-html-definition-provider.test.ts index cec302b..27b1254 100644 --- a/test/providers/angular-html-definition-provider.test.ts +++ b/test/providers/angular-html-definition-provider.test.ts @@ -10,43 +10,65 @@ suite('AngularHtmlDefinitionProvider', () => { await openFileInVscode(templateFilePath); }); - test('should resolve interpolation', async () => { - const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', - vscode.Uri.file(templateFilePath), new vscode.Position(1, 8)); - - assert.notEqual(result[0], undefined, 'definition did not resolve'); - assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); - assert.equal(result[0].range.start.line, 6, 'wrong line position'); - assert.equal(result[0].range.start.character, 12, 'wrong character position'); - }); + suite('should resolve property/method', () => { + test('from interpolation', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(1, 8)); - test('should resolve one way binded input', async () => { - const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', - vscode.Uri.file(templateFilePath), new vscode.Position(2, 40)); + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 6, 'wrong line position'); + assert.equal(result[0].range.start.character, 12, 'wrong character position'); + }); - assert.notEqual(result[0], undefined, 'definition did not resolve'); - assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); - assert.equal(result[0].range.start.line, 11, 'wrong line position'); - assert.equal(result[0].range.start.character, 14, 'wrong character position'); - }); + test('from interpolation with pipe', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(6, 9)); - test('should resolve two way binded input', async () => { - const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', - vscode.Uri.file(templateFilePath), new vscode.Position(2, 62)); + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 6, 'wrong line position'); + assert.equal(result[0].range.start.character, 12, 'wrong character position'); + }); - assert.notEqual(result[0], undefined, 'definition did not resolve'); - assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); - assert.equal(result[0].range.start.line, 7, 'wrong line position'); - assert.equal(result[0].range.start.character, 16, 'wrong character position'); - }); - - test('should resolve output', async () => { - const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', - vscode.Uri.file(templateFilePath), new vscode.Position(2, 86)); - - assert.notEqual(result[0], undefined, 'definition did not resolve'); - assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); - assert.equal(result[0].range.start.line, 15, 'wrong line position'); - assert.equal(result[0].range.start.character, 10, 'wrong character position'); + test('from one way binded input attribute', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(2, 40)); + + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 11, 'wrong line position'); + assert.equal(result[0].range.start.character, 14, 'wrong character position'); + }); + + test('from two way binded input attribute', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(2, 62)); + + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 7, 'wrong line position'); + assert.equal(result[0].range.start.character, 25, 'wrong character position'); + }); + + test('from output attribute', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(2, 86)); + + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 15, 'wrong line position'); + assert.equal(result[0].range.start.character, 10, 'wrong character position'); + }); + + test('from structural attribute', async () => { + const result = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', + vscode.Uri.file(templateFilePath), new vscode.Position(4, 15)); + + assert.notEqual(result[0], undefined, 'definition did not resolve'); + assert.equal(result[0].uri.fsPath, workspaceFilePath('foo.component.ts'), 'wrong file resolution'); + assert.equal(result[0].range.start.line, 19, 'wrong line position'); + assert.equal(result[0].range.start.character, 6, 'wrong character position'); + }); }); });