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