Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/languageFeatures/diagnostics.ts
5237 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { CommandManager } from '../commandManager';
8
import { isMarkdownFile } from '../util/file';
9
10
11
// Copied from markdown language service
12
export enum DiagnosticCode {
13
link_noSuchReferences = 'link.no-such-reference',
14
link_noSuchHeaderInOwnFile = 'link.no-such-header-in-own-file',
15
link_noSuchFile = 'link.no-such-file',
16
link_noSuchHeaderInFile = 'link.no-such-header-in-file',
17
}
18
19
20
class AddToIgnoreLinksQuickFixProvider implements vscode.CodeActionProvider {
21
22
private static readonly _addToIgnoreLinksCommandId = '_markdown.addToIgnoreLinks';
23
24
private static readonly _metadata: vscode.CodeActionProviderMetadata = {
25
providedCodeActionKinds: [
26
vscode.CodeActionKind.QuickFix
27
],
28
};
29
30
public static register(selector: vscode.DocumentSelector, commandManager: CommandManager): vscode.Disposable {
31
const reg = vscode.languages.registerCodeActionsProvider(selector, new AddToIgnoreLinksQuickFixProvider(), AddToIgnoreLinksQuickFixProvider._metadata);
32
const commandReg = commandManager.register({
33
id: AddToIgnoreLinksQuickFixProvider._addToIgnoreLinksCommandId,
34
execute(resource: vscode.Uri, path: string) {
35
const settingId = 'validate.ignoredLinks';
36
const config = vscode.workspace.getConfiguration('markdown', resource);
37
const paths = new Set(config.get<string[]>(settingId, []));
38
paths.add(path);
39
config.update(settingId, [...paths], vscode.ConfigurationTarget.WorkspaceFolder);
40
}
41
});
42
return vscode.Disposable.from(reg, commandReg);
43
}
44
45
provideCodeActions(document: vscode.TextDocument, _range: vscode.Range | vscode.Selection, context: vscode.CodeActionContext, _token: vscode.CancellationToken): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> {
46
const fixes: vscode.CodeAction[] = [];
47
48
for (const diagnostic of context.diagnostics) {
49
switch (diagnostic.code) {
50
case DiagnosticCode.link_noSuchReferences:
51
case DiagnosticCode.link_noSuchHeaderInOwnFile:
52
case DiagnosticCode.link_noSuchFile:
53
case DiagnosticCode.link_noSuchHeaderInFile: {
54
// eslint-disable-next-line local/code-no-any-casts
55
const hrefText = (diagnostic as any).data?.hrefText;
56
if (hrefText) {
57
const fix = new vscode.CodeAction(
58
vscode.l10n.t("Exclude '{0}' from link validation.", hrefText),
59
vscode.CodeActionKind.QuickFix);
60
61
fix.command = {
62
command: AddToIgnoreLinksQuickFixProvider._addToIgnoreLinksCommandId,
63
title: '',
64
arguments: [document.uri, hrefText],
65
};
66
fixes.push(fix);
67
}
68
break;
69
}
70
}
71
}
72
73
return fixes;
74
}
75
}
76
77
function registerMarkdownStatusItem(selector: vscode.DocumentSelector, commandManager: CommandManager): vscode.Disposable {
78
const statusItem = vscode.languages.createLanguageStatusItem('markdownStatus', selector);
79
80
const enabledSettingId = 'validate.enabled';
81
const commandId = '_markdown.toggleValidation';
82
83
const commandSub = commandManager.register({
84
id: commandId,
85
execute: (enabled: boolean) => {
86
vscode.workspace.getConfiguration('markdown').update(enabledSettingId, enabled);
87
}
88
});
89
90
const update = () => {
91
const activeDoc = vscode.window.activeTextEditor?.document;
92
const markdownDoc = activeDoc && isMarkdownFile(activeDoc) ? activeDoc : undefined;
93
94
const enabled = vscode.workspace.getConfiguration('markdown', markdownDoc).get(enabledSettingId);
95
if (enabled) {
96
statusItem.text = vscode.l10n.t('Markdown link validation enabled');
97
statusItem.command = {
98
command: commandId,
99
arguments: [false],
100
title: vscode.l10n.t('Disable'),
101
tooltip: vscode.l10n.t('Disable validation of Markdown links'),
102
};
103
} else {
104
statusItem.text = vscode.l10n.t('Markdown link validation disabled');
105
statusItem.command = {
106
command: commandId,
107
arguments: [true],
108
title: vscode.l10n.t('Enable'),
109
tooltip: vscode.l10n.t('Enable validation of Markdown links'),
110
};
111
}
112
};
113
update();
114
115
return vscode.Disposable.from(
116
statusItem,
117
commandSub,
118
vscode.workspace.onDidChangeConfiguration(e => {
119
if (e.affectsConfiguration('markdown.' + enabledSettingId)) {
120
update();
121
}
122
}),
123
);
124
}
125
126
export function registerDiagnosticSupport(
127
selector: vscode.DocumentSelector,
128
commandManager: CommandManager,
129
): vscode.Disposable {
130
return vscode.Disposable.from(
131
AddToIgnoreLinksQuickFixProvider.register(selector, commandManager),
132
registerMarkdownStatusItem(selector, commandManager),
133
);
134
}
135
136