Skip to content

Commit bdb7c92

Browse files
authored
Fix comment completion tests (#1735)
* Try to fix comment completion tests Issues using the `activeTextEditor` property so we'll "show", preserving focus, so that we get an editor instance. Also don't update position unless edit completes * Use a workspace edit * Don't show document in tests * Cleanup * Test cleanup
1 parent 5a34d40 commit bdb7c92

File tree

3 files changed

+14
-55
lines changed

3 files changed

+14
-55
lines changed

src/editor/CommentCompletion.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
6060
document: vscode.TextDocument,
6161
position: vscode.Position
6262
) {
63-
const editor = vscode.window.visibleTextEditors.find(
64-
e => e.document.uri.toString() === document.uri.toString()
65-
);
66-
if (!editor || editor.document.isClosed) {
67-
return;
68-
}
6963
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
7064
const lineText = document.lineAt(position.line).text;
7165
// Continue the comment if its a white space only line, or if VS Code has already continued
@@ -75,17 +69,13 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
7569
? [lineText, lineText, ""]
7670
: /^(\s*)\/\/\s(.+)/.exec(lineText);
7771
if (match) {
78-
await editor.edit(
79-
edit => {
80-
edit.replace(
81-
new vscode.Range(position.line, 0, position.line, match[0].length),
82-
`${match[1]}/// ${match[2]}`
83-
);
84-
},
85-
{ undoStopBefore: false, undoStopAfter: true }
72+
const edit = new vscode.WorkspaceEdit();
73+
edit.replace(
74+
document.uri,
75+
new vscode.Range(position.line, 0, position.line, match[0].length),
76+
`${match[1]}/// ${match[2]}`
8677
);
87-
const newPosition = new vscode.Position(position.line, match[1].length + 4);
88-
editor.selection = new vscode.Selection(newPosition, newPosition);
78+
await vscode.workspace.applyEdit(edit);
8979
}
9080
}
9181
}

test/integration-tests/BackgroundCompilation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import * as assert from "assert";
1616
import * as vscode from "vscode";
1717
import { WorkspaceContext } from "../../src/WorkspaceContext";
1818
import { testAssetUri } from "../fixtures";
19-
import { Workbench } from "../../src/utilities/commands";
2019
import { activateExtensionForTest, updateSettings } from "./utilities/testutilities";
20+
import { closeAllEditors } from "../utilities/commands";
2121

2222
suite("BackgroundCompilation Test Suite", () => {
2323
let workspaceContext: WorkspaceContext;
@@ -33,7 +33,7 @@ suite("BackgroundCompilation Test Suite", () => {
3333
});
3434

3535
suiteTeardown(async () => {
36-
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
36+
await closeAllEditors();
3737
});
3838

3939
test("build all on save @slow", async () => {

test/integration-tests/editor/CommentCompletion.test.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import * as assert from "assert";
1616
import * as vscode from "vscode";
1717
import { CommentCompletionProviders } from "../../../src/editor/CommentCompletion";
18-
import { Workbench } from "../../../src/utilities/commands";
1918

2019
suite("CommentCompletion Test Suite", () => {
2120
let provider: CommentCompletionProviders;
@@ -223,7 +222,12 @@ suite("CommentCompletion Test Suite", () => {
223222
]);
224223
});
225224

226-
test("Comment insertion", async () => {
225+
test("Comment insertion", async function () {
226+
if (process.platform === "linux") {
227+
// Linux tests are having issues with open text editors
228+
this.skip();
229+
}
230+
227231
const { document, positions } = await openDocument(`
228232
/// 1️⃣
229233
func foo(bar: Int, baz: String) -> Data throws { return Data() }`);
@@ -393,12 +397,6 @@ suite("CommentCompletion Test Suite", () => {
393397
});
394398

395399
suite("Document Comment Completion", function () {
396-
setup(function () {
397-
if (process.platform === "linux") {
398-
// Skip as these tests access the active test editor which will sometimes crash on Linux.
399-
this.skip();
400-
}
401-
});
402400
test("Should not provide completions on first line", async () => {
403401
const { document, positions } = await openDocument(`1️⃣
404402
public func foo() {}`);
@@ -473,27 +471,6 @@ public func foo() {}`);
473471
assert.deepEqual(documentText, originalText, "Document text should not change");
474472
});
475473

476-
test("Should handle case when no active text editor", async () => {
477-
const { document: doc, positions } = await openDocument(`
478-
/// aaa
479-
1️⃣
480-
public func foo() {}`);
481-
482-
const position = positions["1️⃣"];
483-
484-
// Close all editors to simulate no active text editor
485-
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
486-
487-
// This should not throw an error
488-
const items = await provider.docCommentCompletion.provideCompletionItems(doc, position);
489-
490-
assert.equal(
491-
items,
492-
undefined,
493-
"Should not provide completions when no active text editor"
494-
);
495-
});
496-
497474
test("Should handle when previous line has // but not ///", async () => {
498475
const { document, positions } = await openDocument(`
499476
// aaa
@@ -521,9 +498,6 @@ public func foo() {}`);
521498

522499
const position = positions["1️⃣"];
523500

524-
// Show the document to ensure there's an active editor
525-
await vscode.window.showTextDocument(document);
526-
527501
const items = await provider.docCommentCompletion.provideCompletionItems(
528502
document,
529503
position
@@ -545,9 +519,6 @@ public func foo() {}`);
545519

546520
const position = positions["1️⃣"];
547521

548-
// Show the document to ensure there's an active editor
549-
await vscode.window.showTextDocument(document);
550-
551522
const originalText = document.getText();
552523
const items = await provider.docCommentCompletion.provideCompletionItems(
553524
document,
@@ -609,8 +580,6 @@ public func foo() {}`);
609580
content: purgedContent,
610581
});
611582

612-
await vscode.window.showTextDocument(doc);
613-
614583
return { document: doc, positions };
615584
}
616585
});

0 commit comments

Comments
 (0)