|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +/* eslint-disable @typescript-eslint/triple-slash-reference */ |
| 7 | +/// <reference path="../../../../vscode.d.ts" /> |
| 8 | + |
| 9 | +import * as assert from 'assert'; |
| 10 | +import { suite } from 'mocha'; |
| 11 | +import * as path from 'path'; |
| 12 | +import * as sinon from 'sinon'; |
| 13 | +import * as vscode from 'vscode'; |
| 14 | +import * as api from 'vscode-cpptools'; |
| 15 | +import * as apit from 'vscode-cpptools/out/testApi'; |
| 16 | +import * as extension from '../../../../src/LanguageServer/extension'; |
| 17 | +import * as testHelpers from '../../../common/testHelpers'; |
| 18 | + |
| 19 | +suite("Inactive region folding in a multi-root workspace", function (): void { |
| 20 | + let testHook: apit.CppToolsTestHook; |
| 21 | + let firstWorkspaceFolder: vscode.WorkspaceFolder; |
| 22 | + let workspaceFolder: vscode.WorkspaceFolder; |
| 23 | + |
| 24 | + suiteSetup(async function (): Promise<void> { |
| 25 | + await testHelpers.activateCppExtension(); |
| 26 | + const cpptools: apit.CppToolsTestApi = await apit.getCppToolsTestApi(api.Version.latest) |
| 27 | + ?? assert.fail("Could not get CppToolsTestApi"); |
| 28 | + testHook = cpptools.getTestHook(); |
| 29 | + firstWorkspaceFolder = vscode.workspace.workspaceFolders?.[0] |
| 30 | + ?? assert.fail("First workspace folder is unavailable"); |
| 31 | + workspaceFolder = vscode.workspace.workspaceFolders?.[1] |
| 32 | + ?? assert.fail("Second workspace folder is unavailable"); |
| 33 | + }); |
| 34 | + |
| 35 | + suiteTeardown(function (): void { |
| 36 | + testHook.dispose(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("routes manual folding operations by URI during a workspace client switch", async () => { |
| 40 | + const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", workspaceFolder.uri); |
| 41 | + const previousCodeFoldingValue: string | undefined = configuration.inspect<string>("codeFolding")?.globalValue; |
| 42 | + await configuration.update("codeFolding", "enabled", vscode.ConfigurationTarget.Global); |
| 43 | + let releaseEditorChange: (() => void) | undefined; |
| 44 | + let editorChangeStub: sinon.SinonStub | undefined; |
| 45 | + let editorChangePromise: Promise<void> | undefined; |
| 46 | + let editor: vscode.TextEditor | undefined; |
| 47 | + let firstEditor: vscode.TextEditor | undefined; |
| 48 | + try { |
| 49 | + editor = await openFileAndWaitForIntelliSense(); |
| 50 | + await vscode.commands.executeCommand("editor.unfoldAll"); |
| 51 | + |
| 52 | + const firstDocument: vscode.TextDocument = await vscode.workspace.openTextDocument( |
| 53 | + path.join(firstWorkspaceFolder.uri.fsPath, "test.cpp")); |
| 54 | + firstEditor = await vscode.window.showTextDocument(firstDocument); |
| 55 | + await extension.clients.didChangeActiveEditor(firstEditor); |
| 56 | + const firstClient = extension.clients.ActiveClient; |
| 57 | + const owner = extension.clients.getClientFor(editor.document.uri); |
| 58 | + assert.notStrictEqual(firstClient, owner); |
| 59 | + |
| 60 | + const pendingEditorChange: Promise<void> = new Promise(resolve => releaseEditorChange = resolve); |
| 61 | + editorChangeStub = sinon.stub(owner, "didChangeActiveEditor").returns(pendingEditorChange); |
| 62 | + const activeEditor: vscode.TextEditor = await vscode.window.showTextDocument(editor.document, editor.viewColumn, false); |
| 63 | + editorChangePromise = extension.clients.didChangeActiveEditor(activeEditor); |
| 64 | + assert.strictEqual(extension.clients.ActiveClient, firstClient); |
| 65 | + |
| 66 | + await vscode.commands.executeCommand("C_Cpp.FoldAllInactiveRegions"); |
| 67 | + await assertInactiveBranchIsFolded(editor); |
| 68 | + |
| 69 | + await vscode.commands.executeCommand("C_Cpp.UnfoldAllInactiveRegions"); |
| 70 | + await assertInactiveBranchIsUnfolded(editor); |
| 71 | + } finally { |
| 72 | + releaseEditorChange?.(); |
| 73 | + await editorChangePromise; |
| 74 | + editorChangeStub?.restore(); |
| 75 | + if (editor) { |
| 76 | + await closeEditor(editor); |
| 77 | + } |
| 78 | + if (firstEditor) { |
| 79 | + await closeEditor(firstEditor); |
| 80 | + } |
| 81 | + await testHelpers.delay(150); |
| 82 | + await configuration.update("codeFolding", previousCodeFoldingValue, vscode.ConfigurationTarget.Global); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + async function closeEditor(editor: vscode.TextEditor): Promise<void> { |
| 87 | + await vscode.window.showTextDocument(editor.document, editor.viewColumn, false); |
| 88 | + await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); |
| 89 | + } |
| 90 | + |
| 91 | + async function assertInactiveBranchIsFolded(editor: vscode.TextEditor): Promise<void> { |
| 92 | + let line: number = 24; |
| 93 | + for (let i: number = 0; i < 20; ++i) { |
| 94 | + const activeEditor: vscode.TextEditor = await vscode.window.showTextDocument(editor.document, editor.viewColumn, false); |
| 95 | + activeEditor.selection = new vscode.Selection(24, 0, 24, 0); |
| 96 | + await vscode.commands.executeCommand("cursorMove", { to: "down", by: "wrappedLine", value: 1 }); |
| 97 | + line = activeEditor.selection.active.line; |
| 98 | + if (line === 28) { |
| 99 | + return; |
| 100 | + } |
| 101 | + await testHelpers.delay(50); |
| 102 | + } |
| 103 | + |
| 104 | + assert.strictEqual(line, 28); |
| 105 | + } |
| 106 | + |
| 107 | + async function assertInactiveBranchIsUnfolded(editor: vscode.TextEditor): Promise<void> { |
| 108 | + const activeEditor: vscode.TextEditor = await vscode.window.showTextDocument(editor.document, editor.viewColumn, false); |
| 109 | + activeEditor.selection = new vscode.Selection(24, 0, 24, 0); |
| 110 | + await vscode.commands.executeCommand("cursorMove", { to: "down", by: "wrappedLine", value: 1 }); |
| 111 | + assert.strictEqual(activeEditor.selection.active.line, 25); |
| 112 | + } |
| 113 | + |
| 114 | + function waitForIntelliSenseReady(fileName: string): Promise<void> { |
| 115 | + return new Promise<void>((resolve, reject) => { |
| 116 | + const timeout: NodeJS.Timeout = setTimeout(() => { |
| 117 | + listener.dispose(); |
| 118 | + reject(new Error(`Timed out waiting for IntelliSense for ${fileName}`)); |
| 119 | + }, testHelpers.defaultTimeout); |
| 120 | + const listener: vscode.Disposable = testHook.IntelliSenseStatusChanged(result => { |
| 121 | + if (result.filename === fileName && result.status === apit.Status.IntelliSenseReady) { |
| 122 | + clearTimeout(timeout); |
| 123 | + listener.dispose(); |
| 124 | + resolve(); |
| 125 | + } |
| 126 | + }); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + async function openFileAndWaitForIntelliSense(): Promise<vscode.TextEditor> { |
| 131 | + const fileName: string = "code_folding.cpp"; |
| 132 | + const filePath: string = path.join(workspaceFolder.uri.fsPath, fileName); |
| 133 | + const ready: Promise<void> = waitForIntelliSenseReady(fileName); |
| 134 | + const document: vscode.TextDocument = await vscode.workspace.openTextDocument(filePath); |
| 135 | + const editor: vscode.TextEditor = await vscode.window.showTextDocument(document); |
| 136 | + await ready; |
| 137 | + return editor; |
| 138 | + } |
| 139 | +}); |
0 commit comments