| 133 | } |
| 134 | |
| 135 | export async function setupWorkspace( |
| 136 | args: { |
| 137 | config?: Partial<IConfiguration>; |
| 138 | fileExtension?: string; |
| 139 | fileContent?: string[]; |
| 140 | forceNewFile?: boolean; |
| 141 | disableCleanUp?: boolean; |
| 142 | } = {}, |
| 143 | ): Promise<void> { |
| 144 | await ExCommandLine.loadHistory(new TestExtensionContext()); |
| 145 | |
| 146 | const newFile = |
| 147 | vscode.window.activeTextEditor === undefined || |
| 148 | vscode.window.activeTextEditor.document.isUntitled || |
| 149 | vscode.window.visibleTextEditors.length > 1 || |
| 150 | (args.fileExtension && |
| 151 | !vscode.window.activeTextEditor.document.fileName.endsWith(args.fileExtension)) || |
| 152 | args.forceNewFile; |
| 153 | const fileContent = (args.fileContent ?? []).join(os.EOL); |
| 154 | |
| 155 | if (newFile) { |
| 156 | if (!args.disableCleanUp) await cleanUpWorkspace(); |
| 157 | const filePath = await createFile({ |
| 158 | fileExtension: args.fileExtension, |
| 159 | contents: fileContent, |
| 160 | }); |
| 161 | const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(filePath)); |
| 162 | await vscode.window.showTextDocument(doc); |
| 163 | } |
| 164 | |
| 165 | const config = new Configuration(args?.config); |
| 166 | await reloadConfiguration(config); |
| 167 | |
| 168 | const activeTextEditor = vscode.window.activeTextEditor; |
| 169 | assert.ok(activeTextEditor); |
| 170 | |
| 171 | activeTextEditor.options.tabSize = config.tabstop; |
| 172 | activeTextEditor.options.insertSpaces = config.expandtab; |
| 173 | |
| 174 | if (!newFile) { |
| 175 | assert.ok( |
| 176 | await activeTextEditor.edit((builder) => { |
| 177 | builder.replace(TextEditor.getDocumentRange(activeTextEditor.document), fileContent); |
| 178 | }), |
| 179 | 'Edit failed', |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | if (!args.disableCleanUp) ModeHandlerMap.clear(); |
| 184 | } |
| 185 | |
| 186 | export async function cleanUpWorkspace(): Promise<void> { |
| 187 | await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |