Skip to content

Commit

Permalink
Improve UX around educational notes
Browse files Browse the repository at this point in the history
The Swift compiler contains educational notes to further describe
diagnostics [1]. These educational notes are documented in markdown
files that are contained within the toolchain.

Sourcekit LSP includes a link to the local markdown file when returning
diagnostics that have an associated educational note (as part of the
diagnostic code). The default behaviour in VSCode is to present these
as a link in the diagnostic hover, and open the editor to the markdown
file when the link is clicked.

This PR updates the behaviour for educational notes to instead open
the link using the markdown preview, which shows nicely rendered
content. It also updates the link in the hover to show "More
 Information" instead of the code.

Issue: swiftlang#1395

[1] https://github.com/swiftlang/swift/tree/main/userdocs/diagnostics
  • Loading branch information
daveyc123 committed Mar 7, 2025
1 parent ff3ca28 commit 04a3fcf
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
5 changes: 5 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
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

0 comments on commit 04a3fcf

Please sign in to comment.