Skip to content

Commit

Permalink
Make regexps more general just to filter out lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dzonatan committed Mar 23, 2017
1 parent d713075 commit 2d5af40
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
19 changes: 14 additions & 5 deletions src/providers/angular-html-definition-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 4 additions & 0 deletions test/_test_files/foo.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<div>
{{ myProperty }}
<input type="text" [class.error]="myGetter" [(ngModel)]="myLongProperty" (click)="myMethod($event)">

<div *ngIf="good"></div>

{{ myProperty | myPipe }}
</div>
4 changes: 3 additions & 1 deletion test/_test_files/foo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
})
export class FooComponent {
myProperty = '';
myLongProperty: string;
@Input() myLongProperty: string;

constructor() { }

Expand All @@ -16,4 +16,6 @@ export class FooComponent {
myMethod() {
// do smth
}

good = false;
}
90 changes: 56 additions & 34 deletions test/providers/angular-html-definition-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,65 @@ suite('AngularHtmlDefinitionProvider', () => {
await openFileInVscode(templateFilePath);
});

test('should resolve interpolation', async () => {
const result = await vscode.commands.executeCommand<vscode.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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.Location[]>('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');
});
});
});

0 comments on commit 2d5af40

Please sign in to comment.