| 12 | import {waitFor} from './utils'; |
| 13 | |
| 14 | export async function run() { |
| 15 | const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath; |
| 16 | if (!workspaceRoot) { |
| 17 | throw new Error('No workspace root found'); |
| 18 | } |
| 19 | |
| 20 | const testFilePath = path.join(workspaceRoot, 'app.ts'); |
| 21 | const uri = vscode.Uri.file(testFilePath); |
| 22 | |
| 23 | console.log(`Open file: ${testFilePath}`); |
| 24 | const document = await vscode.workspace.openTextDocument(uri); |
| 25 | await vscode.window.showTextDocument(document); |
| 26 | |
| 27 | // Test 1: Definition of 'name' in template |
| 28 | // Line 34 (0-indexed 33): <div>Hello {{ name }}</div> |
| 29 | // 'n' is at character 18 |
| 30 | const namePosition = new vscode.Position(33, 18); |
| 31 | console.log(`Querying definition for name at Position(33, 18)...`); |
| 32 | |
| 33 | // Wait for server to be ready |
| 34 | const nameDefinitions = await waitFor( |
| 35 | () => |
| 36 | vscode.commands.executeCommand<vscode.Location[] | vscode.LocationLink[]>( |
| 37 | 'vscode.executeDefinitionProvider', |
| 38 | uri, |
| 39 | namePosition, |
| 40 | ), |
| 41 | (defs) => !!defs && defs.length > 0, |
| 42 | ); |
| 43 | |
| 44 | assert.ok( |
| 45 | nameDefinitions && nameDefinitions.length > 0, |
| 46 | 'Should return at least one definition for name', |
| 47 | ); |
| 48 | |
| 49 | const nameDef = nameDefinitions[0]; |
| 50 | const nameUri = 'uri' in nameDef ? nameDef.uri : nameDef.targetUri; |
| 51 | const nameRange = 'range' in nameDef ? nameDef.range : nameDef.targetRange; |
| 52 | |
| 53 | console.log(`Definition URI for name: ${nameUri.toString()}`); |
| 54 | console.log(`Definition Range for name: ${nameRange.start.line}:${nameRange.start.character}`); |
| 55 | |
| 56 | assert.strictEqual(nameUri.fsPath, testFilePath, 'Definition should be in the same file'); |
| 57 | // name is declared on line 63 (0-indexed 62): name = 'Angular LS Test'; |
| 58 | assert.strictEqual(nameRange.start.line, 62, 'Definition line should be 62'); |
| 59 | |
| 60 | // Test 2: Definition of 'isSidenavOpen' in click handler |
| 61 | // Line 40 (0-indexed 39): (click)="isSidenavOpen.set(false)" |
| 62 | // 'i' is at character 17 |
| 63 | const sidenavPosition = new vscode.Position(39, 17); |
| 64 | console.log(`Querying definition for isSidenavOpen at Position(39, 17)...`); |
| 65 | const sidenavDefs = await vscode.commands.executeCommand< |
| 66 | vscode.Location[] | vscode.LocationLink[] |
| 67 | >('vscode.executeDefinitionProvider', uri, sidenavPosition); |
| 68 | |
| 69 | assert.ok( |
| 70 | sidenavDefs && sidenavDefs.length > 0, |
| 71 | 'Should return at least one definition for isSidenavOpen', |