|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import delay from 'delay' |
| 3 | +import dedent from 'string-dedent' |
| 4 | + |
| 5 | +import { expect } from 'chai' |
| 6 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error |
| 7 | +//@ts-ignore |
| 8 | +import type { Configuration } from '../../src/configurationType' |
| 9 | +import { fromFixtures, replaceEditorText } from './utils' |
| 10 | + |
| 11 | +describe('Outline', () => { |
| 12 | + const content = dedent/* tsx */ ` |
| 13 | + const classes = { |
| 14 | + header: '...', |
| 15 | + title: '...' |
| 16 | + } |
| 17 | + function A() { |
| 18 | + return <Notification className="test another" id="yes"> |
| 19 | + before |
| 20 | + <div id="ok"> |
| 21 | + <div /> |
| 22 | + <span class="good" /> |
| 23 | + </div> |
| 24 | + after |
| 25 | + </Notification> |
| 26 | + } |
| 27 | + ` |
| 28 | + |
| 29 | + let document: vscode.TextDocument |
| 30 | + const editor = () => vscode.window.activeTextEditor! |
| 31 | + |
| 32 | + const getOutline = async () => vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri) as any |
| 33 | + |
| 34 | + // let editor: vscode.TextEditor |
| 35 | + const startPos = new vscode.Position(0, 0) |
| 36 | + before(async () => { |
| 37 | + const configKey: keyof Configuration = 'patchOutline' |
| 38 | + const configValue: Configuration['patchOutline'] = true |
| 39 | + await vscode.workspace.getConfiguration('tsEssentialPlugins').update(configKey, configValue, vscode.ConfigurationTarget.Global) |
| 40 | + await delay(600) |
| 41 | + await vscode.workspace |
| 42 | + .openTextDocument({ |
| 43 | + content, |
| 44 | + language: 'typescriptreact', |
| 45 | + }) |
| 46 | + .then(async newDocument => { |
| 47 | + document = newDocument |
| 48 | + /* editor = */ await vscode.window.showTextDocument(document) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('Outline untitled works', async () => { |
| 53 | + console.time('get outline') |
| 54 | + const data = await getOutline() |
| 55 | + console.timeEnd('get outline') |
| 56 | + expect(simplifyOutline(data)).to.deep.equal([ |
| 57 | + { |
| 58 | + name: 'A', |
| 59 | + children: [ |
| 60 | + { |
| 61 | + name: 'Notification.test.another#yes', |
| 62 | + children: [ |
| 63 | + { |
| 64 | + name: 'div#ok', |
| 65 | + children: [ |
| 66 | + { |
| 67 | + name: 'div', |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'span.good', |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ]) |
| 79 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor') |
| 80 | + }) |
| 81 | + |
| 82 | + describe('Outline in js project', () => { |
| 83 | + it('Initial', async () => { |
| 84 | + await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(fromFixtures('test-project-js'))) |
| 85 | + await delay(500) |
| 86 | + await vscode.commands.executeCommand('vscode.open', vscode.Uri.file(fromFixtures('test-project-js/src/index.jsx'))) |
| 87 | + await delay(600) |
| 88 | + document = editor().document |
| 89 | + const data = await getOutline() |
| 90 | + expect(simplifyOutline(data)).to.deep.equal(jsProjectExpectedOutline()) |
| 91 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor') |
| 92 | + }) |
| 93 | + |
| 94 | + it('Reopen', async () => { |
| 95 | + await delay(500) |
| 96 | + await vscode.commands.executeCommand('vscode.open', vscode.Uri.file(fromFixtures('test-project-js/src/index.jsx'))) |
| 97 | + await delay(600) |
| 98 | + document = editor().document |
| 99 | + const data = await getOutline() |
| 100 | + expect(simplifyOutline(data)).to.deep.equal(jsProjectExpectedOutline()) |
| 101 | + }) |
| 102 | + |
| 103 | + it('Text change', async () => { |
| 104 | + const searchText = 'NavBar' |
| 105 | + const componentEndPos = document.positionAt(document.getText().indexOf(searchText) + searchText.length) |
| 106 | + await replaceEditorText(editor(), new vscode.Range(componentEndPos.translate(0, -1), componentEndPos), '2') |
| 107 | + await delay(500) |
| 108 | + const data = await getOutline() |
| 109 | + expect(simplifyOutline(data)).to.deep.equal(jsProjectExpectedOutline('NavBa2')) |
| 110 | + }) |
| 111 | + |
| 112 | + it('Text change right after TSServer restart', async () => { |
| 113 | + void vscode.commands.executeCommand('typescript.restartTsServer') |
| 114 | + const searchText = 'NavBa2' |
| 115 | + const componentEndPos = document.positionAt(document.getText().indexOf(searchText) + searchText.length) |
| 116 | + await editor().edit(builder => { |
| 117 | + builder.replace(new vscode.Range(componentEndPos.translate(0, -1), componentEndPos), '3') |
| 118 | + }) |
| 119 | + await delay(800) |
| 120 | + const data = await getOutline() |
| 121 | + expect(simplifyOutline(data)).to.deep.equal(jsProjectExpectedOutline('NavBa3')) |
| 122 | + }) |
| 123 | + |
| 124 | + it('Text change with no syntax server', async () => { |
| 125 | + await vscode.workspace.getConfiguration('typescript').update('tsserver.useSyntaxServer', 'never', vscode.ConfigurationTarget.Global) |
| 126 | + // void vscode.commands.executeCommand('typescript.restartTsServer') |
| 127 | + await delay(300) |
| 128 | + const searchText = 'NavBa3' |
| 129 | + const componentEndPos = document.positionAt(document.getText().indexOf(searchText) + searchText.length) |
| 130 | + await editor().edit(builder => { |
| 131 | + builder.replace(new vscode.Range(componentEndPos.translate(0, -1), componentEndPos), '4') |
| 132 | + }) |
| 133 | + await delay(800) |
| 134 | + const data = await getOutline() |
| 135 | + expect(simplifyOutline(data)).to.deep.equal(jsProjectExpectedOutline('NavBa4')) |
| 136 | + }).timeout(8000) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +const jsProjectExpectedOutline = (navbarPosName = 'NavBar') => [ |
| 141 | + { |
| 142 | + name: 'Component', |
| 143 | + children: [ |
| 144 | + { |
| 145 | + name: 'div.main__wrap', |
| 146 | + children: [ |
| 147 | + { |
| 148 | + name: 'main.container', |
| 149 | + children: [ |
| 150 | + { |
| 151 | + name: 'div.card__box', |
| 152 | + children: [ |
| 153 | + { |
| 154 | + name: navbarPosName, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: 'Counters', |
| 158 | + }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | +] |
| 168 | + |
| 169 | +const simplifyOutline = (items: Array<vscode.SymbolInformation & vscode.DocumentSymbol>) => { |
| 170 | + const newItems: Array<{ name: any; children? }> = [] |
| 171 | + for (const { children, name } of items) { |
| 172 | + if (name === 'classes') continue |
| 173 | + newItems.push({ name, ...(children?.length ? { children: simplifyOutline(children as any) } : {}) }) |
| 174 | + } |
| 175 | + |
| 176 | + return newItems |
| 177 | +} |
0 commit comments