Skip to content

Improve UX around educational notes #1423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
"title": "Create New Project...",
"category": "Swift"
},
{
"command": "swift.openEducationalNote",
"title": "Open Educational Note...",
"category": "Swift"
},
{
"command": "swift.newFile",
"title": "Create New Swift File...",
Expand Down Expand Up @@ -943,6 +948,10 @@
{
"command": "swift.coverAllTests",
"when": "swift.isActivated"
},
{
"command": "swift.openEducationalNote",
"when": "false"
}
],
"editor/context": [
Expand Down
18 changes: 18 additions & 0 deletions src/DiagnosticsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ export class DiagnosticsManager implements vscode.Disposable {
d1 => isSwiftc(d1) && !!removedDiagnostics.find(d2 => isEqual(d1, d2))
);
}

for (const diagnostic of newDiagnostics) {
if (
diagnostic.code &&
typeof diagnostic.code !== "string" &&
typeof diagnostic.code !== "number"
) {
if (diagnostic.code.target.fsPath.endsWith(".md")) {
diagnostic.code = {
target: vscode.Uri.parse(
`command:swift.openEducationalNote?${encodeURIComponent(JSON.stringify(diagnostic.code.target))}`
),
value: "More Information...",
};
}
}
}

// Append the new diagnostics we just received
allDiagnostics.push(...newDiagnostics);
this.allDiagnostics.set(uri.fsPath, allDiagnostics);
Expand Down
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { openInExternalEditor } from "./commands/openInExternalEditor";
import { switchPlatform } from "./commands/switchPlatform";
import { insertFunctionComment } from "./commands/insertFunctionComment";
import { createNewProject } from "./commands/createNewProject";
import { openEducationalNote } from "./commands/openEducationalNote";
import { openPackage } from "./commands/openPackage";
import { resolveDependencies } from "./commands/dependencies/resolve";
import { resetPackage } from "./commands/resetPackage";
Expand Down Expand Up @@ -199,6 +200,9 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
vscode.commands.registerCommand(Commands.SHOW_NESTED_DEPENDENCIES_LIST, () =>
updateDependenciesViewList(ctx, false)
),
vscode.commands.registerCommand("swift.openEducationalNote", uri =>
openEducationalNote(uri)
),
];
}

Expand Down
24 changes: 24 additions & 0 deletions src/commands/openEducationalNote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021-2025 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";

/**
* Handle the user requesting to show an educational note.
*
* The default behaviour is to open it in a markdown preview to the side.
*/
export async function openEducationalNote(markdownFile: vscode.Uri | undefined): Promise<void> {
await vscode.commands.executeCommand("markdown.showPreviewToSide", markdownFile);
}
111 changes: 111 additions & 0 deletions test/integration-tests/DiagnosticsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FolderContext } from "../../src/FolderContext";
import { Version } from "../../src/utilities/version";
import { Workbench } from "../../src/utilities/commands";
import { activateExtensionForSuite, folderInRootWorkspace } from "./utilities/testutilities";
import { expect } from "chai";

const isEqual = (d1: vscode.Diagnostic, d2: vscode.Diagnostic) => {
return (
Expand Down Expand Up @@ -555,6 +556,116 @@ suite("DiagnosticsManager Test Suite", async function () {
await swiftConfig.update("diagnosticsCollection", undefined);
});

suite("markdownLinks", () => {
let diagnostic: vscode.Diagnostic;

setup(async () => {
workspaceContext.diagnostics.clear();
diagnostic = new vscode.Diagnostic(
new vscode.Range(new vscode.Position(1, 8), new vscode.Position(1, 8)), // Note swiftc provides empty range
"Cannot assign to value: 'bar' is a 'let' constant",
vscode.DiagnosticSeverity.Error
);
diagnostic.source = "SourceKit";
});

test("ignore strings", async () => {
diagnostic.code = "string";

// Now provide identical SourceKit diagnostic
workspaceContext.diagnostics.handleDiagnostics(
mainUri,
DiagnosticsManager.isSourcekit,
[diagnostic]
);

// check diagnostic hasn't changed
assertHasDiagnostic(mainUri, diagnostic);

const diagnostics = vscode.languages.getDiagnostics(mainUri);
const matchingDiagnostic = diagnostics.find(findDiagnostic(diagnostic));

expect(matchingDiagnostic).to.have.property("code", "string");
});

test("ignore numbers", async () => {
diagnostic.code = 1;

// Now provide identical SourceKit diagnostic
workspaceContext.diagnostics.handleDiagnostics(
mainUri,
DiagnosticsManager.isSourcekit,
[diagnostic]
);

// check diagnostic hasn't changed
assertHasDiagnostic(mainUri, diagnostic);

const diagnostics = vscode.languages.getDiagnostics(mainUri);
const matchingDiagnostic = diagnostics.find(findDiagnostic(diagnostic));

expect(matchingDiagnostic).to.have.property("code", 1);
});

test("target without markdown link", async () => {
const diagnosticCode = {
value: "string",
target: vscode.Uri.file("/some/path/md/readme.txt"),
};
diagnostic.code = diagnosticCode;

// Now provide identical SourceKit diagnostic
workspaceContext.diagnostics.handleDiagnostics(
mainUri,
DiagnosticsManager.isSourcekit,
[diagnostic]
);

// check diagnostic hasn't changed
assertHasDiagnostic(mainUri, diagnostic);

const diagnostics = vscode.languages.getDiagnostics(mainUri);
const matchingDiagnostic = diagnostics.find(findDiagnostic(diagnostic));

expect(matchingDiagnostic).to.have.property("code", diagnostic.code);
});

test("target with markdown link", async () => {
const pathToMd = "/some/path/md/readme.md";
diagnostic.code = {
value: "string",
target: vscode.Uri.file(pathToMd),
};

workspaceContext.diagnostics.handleDiagnostics(
mainUri,
DiagnosticsManager.isSourcekit,
[diagnostic]
);

const diagnostics = vscode.languages.getDiagnostics(mainUri);
const matchingDiagnostic = diagnostics.find(findDiagnostic(diagnostic));

expect(matchingDiagnostic).to.have.property("code");
expect(matchingDiagnostic?.code).to.have.property("value", "More Information...");

if (
matchingDiagnostic &&
matchingDiagnostic.code &&
typeof matchingDiagnostic.code !== "string" &&
typeof matchingDiagnostic.code !== "number"
) {
expect(matchingDiagnostic.code.target.scheme).to.equal("command");
expect(matchingDiagnostic.code.target.path).to.equal(
"swift.openEducationalNote"
);
expect(matchingDiagnostic.code.target.query).to.contain(pathToMd);
} else {
assert.fail("Diagnostic target not replaced with markdown command");
}
});
});

suite("keepAll", () => {
setup(async () => {
await swiftConfig.update("diagnosticsCollection", "keepAll");
Expand Down
Loading