forked from emmyforeignreal/vscode-markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
1108 lines (1040 loc) · 35.9 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
// Minimal requires (requires that may not be needed are inlined to avoid startup cost)
const vscode = require("vscode");
const path = require("node:path");
const {promisify} = require("node:util");
// Constants
const extensionDisplayName = "markdownlint";
const configFileGlob = ".markdownlint.{jsonc,json,yaml,yml,cjs}";
const optionsFileGlob = ".markdownlint-cli2.{jsonc,yaml,cjs}";
const markdownlintJson = ".markdownlint.json";
const configFileNames = [
".markdownlint.jsonc",
markdownlintJson,
".markdownlint.yaml",
".markdownlint.yml",
".markdownlint.cjs"
];
const ignoreFileName = ".markdownlintignore";
const markdownLanguageId = "markdown";
// Untitled/unsaved document
const schemeUntitled = "untitled";
// Standard file system workspace
const schemeFile = "file";
// Used by extensions:
// GitHub Repositories (github.remotehub)
// Remote Repositories (ms-vscode.remote-repositories)
const schemeVscodeVfs = "vscode-vfs";
// Used by @vscode/test-web when testing web extensions
const schemeVscodeTestWeb = "vscode-test-web";
// Used by GistPad (vsls-contrib.gistfs)
const schemeGist = "gist";
// Schemes that are okay to lint (as part of a workspace or independently)
const schemeSupported = new Set([
schemeUntitled,
schemeFile,
schemeVscodeVfs,
schemeVscodeTestWeb,
schemeGist
]);
// Schemes that are file system-like (support probing for configuration files)
const schemeFileSystemLike = new Set([
schemeFile,
schemeVscodeVfs,
schemeVscodeTestWeb
]);
const configParsers = [
(content) => JSON.parse(require("jsonc-parser").stripComments(content)),
(content) => require("js-yaml").load(content)
];
const codeActionKindQuickFix = vscode.CodeActionKind.QuickFix;
const codeActionKindSourceFixAll = vscode.CodeActionKind.SourceFixAll;
const codeActionKindSourceFixAllExtension = codeActionKindSourceFixAll.append(extensionDisplayName);
const defaultConfig = {
"MD013": false
};
const clickForInfo = "More information about ";
const clickToFix = "Fix this violation of ";
const fixLineCommandName = "markdownlint.fixLine";
const fixAllCommandTitle = `Fix all supported ${extensionDisplayName} violations in the document`;
const fixAllCommandName = "markdownlint.fixAll";
const lintWorkspaceCommandName = "markdownlint.lintWorkspace";
const openConfigFileCommandName = "markdownlint.openConfigFile";
const toggleLintingCommandName = "markdownlint.toggleLinting";
const lintAllTaskName = `Lint all Markdown files in the workspace with ${extensionDisplayName}`;
const problemMatcherName = `$${extensionDisplayName}`;
const clickForConfigureInfo = `Details about configuring ${extensionDisplayName} rules`;
const clickForConfigureUrl = "https://github.com/DavidAnson/vscode-markdownlint#configure";
const errorExceptionPrefix = "ERROR: Exception while linting with markdownlint-cli2:\n";
const openCommand = "vscode.open";
const sectionConfig = "config";
const sectionCustomRules = "customRules";
const sectionFocusMode = "focusMode";
const sectionIgnore = "ignore";
const sectionLintWorkspaceGlobs = "lintWorkspaceGlobs";
const sectionRun = "run";
const applicationConfigurationSections = [ sectionFocusMode ];
const throttleDuration = 500;
const customRuleExtensionPrefixRe = /^\{([^}]+)\}\/(.*)$/iu;
const driveLetterRe = /^[A-Za-z]:[/\\]/;
const networkShareRe = /^\\\\[^\\]+\\/;
const firstSegmentRe = /^\/{1,2}[^/]+\//;
// Variables
const applicationConfiguration = {};
const ruleNameToInformationUri = {};
let outputChannel = null;
let diagnosticCollection = null;
let runMap = {};
let ignores = null;
let lintingEnabled = true;
const throttle = {
"document": null,
"timeout": null
};
// Converts to a POSIX-style path
// eslint-disable-next-line id-length
function posixPath (p) {
return p.split(path.sep).join(path.posix.sep);
}
// Gets the workspace folder Uri (if any) for the document Uri (if specified)
function getWorkspaceFolderUri (documentUri) {
if (documentUri) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(documentUri);
return workspaceFolder ?
workspaceFolder.uri :
vscode.Uri.joinPath(documentUri, "..");
}
const workspaceFolders = vscode.workspace.workspaceFolders;
return (workspaceFolders && (workspaceFolders.length > 0)) ?
workspaceFolders[0].uri :
null;
}
// A Node-like fs object implemented using vscode.workspace.fs
class FsWrapper {
// Returns true
static fwTrue () {
return true;
}
// Returns false
static fwFalse () {
return false;
}
// Returns a Uri of fwFolderUri with the specified path segment
fwFolderUriWithPathSegment (pathSegment) {
// Fix drive letter issues on Windows
let posixPathSegment = posixPath(pathSegment);
if (driveLetterRe.test(posixPathSegment)) {
// eslint-disable-next-line unicorn/prefer-ternary
if (
this.fwFolderUri.path.startsWith("/") &&
driveLetterRe.test(this.fwFolderUri.path.slice(1))
) {
// Both paths begin with Windows drive letter, make it consistent
posixPathSegment = `/${posixPathSegment}`;
} else {
// Folder path does not start with Windows drive letter, remove it
posixPathSegment = posixPathSegment.replace(driveLetterRe, "/");
}
}
// Fix network share issues on Windows (possibly in addition to drive letter issues)
if (networkShareRe.test(this.fwFolderUri.fsPath)) {
// Path segment has the computer name prefixed, remove it
posixPathSegment = posixPathSegment.replace(firstSegmentRe, "/");
}
// Return consistently-formatted Uri with specified path
return this.fwFolderUri.with({"path": posixPathSegment});
}
// Implements fs.access via vscode.workspace.fs
fwAccess (pathSegment, mode, callback) {
// eslint-disable-next-line no-param-reassign
callback = callback || mode;
vscode.workspace.fs.stat(
this.fwFolderUriWithPathSegment(pathSegment)
).then(
() => callback(null),
callback
);
}
// Implements fs.readdir via vscode.workspace.fs
fwReaddir (pathSegment, options, callback) {
// eslint-disable-next-line no-param-reassign
callback = callback || options;
vscode.workspace.fs.readDirectory(
this.fwFolderUriWithPathSegment(pathSegment)
).then(
(namesAndTypes) => {
const namesOrDirents = namesAndTypes.map(
(nameAndType) => {
const [
name,
fileType
] = nameAndType;
return options.withFileTypes ?
{
/* eslint-disable multiline-ternary, no-bitwise */
"isBlockDevice": FsWrapper.fwFalse,
"isCharacterDevice": FsWrapper.fwFalse,
"isDirectory": (fileType & vscode.FileType.Directory) ? FsWrapper.fwTrue : FsWrapper.fwFalse,
"isFIFO": FsWrapper.fwFalse,
"isFile": (fileType & vscode.FileType.File) ? FsWrapper.fwTrue : FsWrapper.fwFalse,
"isSocket": FsWrapper.fwFalse,
"isSymbolicLink":
(fileType & vscode.FileType.SymbolicLink) ? FsWrapper.fwTrue : FsWrapper.fwFalse,
/* eslint-enable multiline-ternary, no-bitwise */
name
} :
name;
}
);
callback(null, namesOrDirents);
},
callback
);
}
// Implements fs.readFile via vscode.workspace.fs
fwReadFile (pathSegment, options, callback) {
// eslint-disable-next-line no-param-reassign
callback = callback || options;
vscode.workspace.fs.readFile(
this.fwFolderUriWithPathSegment(pathSegment)
).then(
(bytes) => callback(null, new TextDecoder().decode(bytes)),
callback
);
}
// Implements fs.stat via vscode.workspace.fs
fwStat (pathSegment, options, callback) {
// eslint-disable-next-line no-param-reassign
callback = callback || options;
vscode.workspace.fs.stat(
this.fwFolderUriWithPathSegment(pathSegment)
).then(
(fileStat) => {
// Stub required properties for fast-glob
/* eslint-disable dot-notation, multiline-ternary, no-bitwise */
fileStat["isBlockDevice"] = FsWrapper.fwFalse;
fileStat["isCharacterDevice"] = FsWrapper.fwFalse;
fileStat["isDirectory"] = (fileStat.type & vscode.FileType.Directory) ? FsWrapper.fwTrue : FsWrapper.fwFalse;
fileStat["isFIFO"] = FsWrapper.fwFalse;
fileStat["isFile"] = (fileStat.type & vscode.FileType.File) ? FsWrapper.fwTrue : FsWrapper.fwFalse;
fileStat["isSocket"] = FsWrapper.fwFalse;
fileStat["isSymbolicLink"] =
(fileStat.type & vscode.FileType.SymbolicLink) ? FsWrapper.fwTrue : FsWrapper.fwFalse;
/* eslint-enable dot-notation, multiline-ternary, no-bitwise */
callback(null, fileStat);
},
callback
);
}
// Constructs a new instance
constructor (folderUri) {
this.fwFolderUri = folderUri;
this.access = this.fwAccess.bind(this);
this.readdir = this.fwReaddir.bind(this);
this.readFile = this.fwReadFile.bind(this);
this.stat = this.fwStat.bind(this);
this.lstat = this.stat;
this.promises = {};
this.promises.access = promisify(this.fwAccess).bind(this);
this.promises.readFile = promisify(this.fwReadFile).bind(this);
this.promises.stat = promisify(this.fwStat).bind(this);
}
}
// A Node-like fs object for a "null" file system
class FsNull {
// Implements fs.access/readdir/readFile/stat
static fnError (pathSegment, modeOrOptions, callback) {
// eslint-disable-next-line no-param-reassign
callback = callback || modeOrOptions;
callback(new Error("FsNull.fnError"));
}
// Constructs a new instance
constructor () {
this.access = FsNull.fnError;
this.readdir = this.access;
this.readFile = this.access;
this.stat = this.access;
this.lstat = this.access;
this.promises = {};
this.promises.access = promisify(FsNull.fnError);
this.promises.readFile = this.promises.access;
this.promises.stat = this.promises.access;
}
}
// A VS Code Pseudoterminal for linting a workspace and emitting the results
class LintWorkspacePseudoterminal {
constructor () {
this.writeEmitter = new vscode.EventEmitter();
this.closeEmitter = new vscode.EventEmitter();
}
get onDidWrite () {
return this.writeEmitter.event;
}
get onDidClose () {
return this.closeEmitter.event;
}
open () {
// eslint-disable-next-line unicorn/no-keyword-prefix
const {newLineRe} = require("markdownlint/helpers");
const logString = (message) => this.writeEmitter.fire(
`${message.split(newLineRe).join("\r\n")}\r\n`
);
lintWorkspace(logString)
.finally(() => this.close());
}
close () {
this.writeEmitter.dispose();
this.closeEmitter.fire();
this.closeEmitter.dispose();
}
}
// Writes date and message to the output channel
function outputLine (message, show) {
const datePrefix = "[" + (new Date()).toLocaleTimeString() + "] ";
outputChannel.appendLine(datePrefix + message);
if (show) {
outputChannel.show(true);
}
}
// Returns rule configuration from user/workspace configuration
async function getConfig (fs, configuration, uri) {
let userWorkspaceConfig = configuration.get(sectionConfig);
// Bootstrap extend behavior into readConfig
if (userWorkspaceConfig && userWorkspaceConfig.extends) {
const userWorkspaceConfigMetadata = configuration.inspect(sectionConfig);
const workspaceFolderUri = getWorkspaceFolderUri(uri);
const useHomedir =
(userWorkspaceConfigMetadata.globalValue &&
(userWorkspaceConfigMetadata.globalValue.extends === userWorkspaceConfig.extends)) ||
!workspaceFolderUri ||
(workspaceFolderUri.scheme !== schemeFile);
const os = require("node:os");
const homedir = os && os.homedir && os.homedir();
const workspaceFolderFsPath = workspaceFolderUri && posixPath(workspaceFolderUri.fsPath);
// eslint-disable-next-line multiline-ternary
const extendBase = ((useHomedir && homedir) ? homedir : workspaceFolderFsPath) || "";
const {expandTildePath} = require("markdownlint/helpers");
let expanded = expandTildePath(userWorkspaceConfig.extends, os);
if (homedir) {
expanded = expanded.replace(/\${userHome}/g, homedir);
}
if (workspaceFolderFsPath) {
expanded = expanded.replace(/\${workspaceFolder}/g, workspaceFolderFsPath);
}
const extendPath = path.resolve(extendBase, expanded);
try {
const markdownlint = require("markdownlint").promises;
const extendConfig = await markdownlint.readConfig(extendPath, configParsers, fs);
userWorkspaceConfig = {
...extendConfig,
...userWorkspaceConfig
};
} catch {
// Ignore error
}
}
return {
...defaultConfig,
...userWorkspaceConfig
};
}
// Returns ignore configuration for user/workspace
function getIgnores (document) {
if (!Array.isArray(ignores)) {
ignores = [];
let ignoreFile = ignoreFileName;
// Handle "ignore" configuration
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
const ignoreValue = configuration.get(sectionIgnore);
if (Array.isArray(ignoreValue)) {
for (const ignorePath of ignoreValue) {
const ignoreRe = require("minimatch").makeRe(ignorePath, {
"dot": true,
"nocomment": true
});
if (ignoreRe) {
ignores.push((file) => ignoreRe.test(file));
}
}
} else if (typeof ignoreValue === "string") {
ignoreFile = ignoreValue;
}
// Handle .markdownlintignore
const workspaceFolderUri = getWorkspaceFolderUri(document.uri);
if (workspaceFolderUri) {
const ignoreFileUri = vscode.Uri.joinPath(
workspaceFolderUri,
ignoreFile
);
vscode.workspace.fs.stat(ignoreFileUri).then(
() => vscode.workspace.fs.readFile(ignoreFileUri).then(
(ignoreBytes) => {
const ignoreString = new TextDecoder().decode(ignoreBytes);
const ignore = require("ignore").default;
const ignoreInstance = ignore().add(ignoreString);
ignores.push((file) => ignoreInstance.ignores(file));
clearDiagnosticsAndLintVisibleFiles();
}
),
() => null
);
}
}
return ignores;
}
// Clears the ignore list
function clearIgnores (eventUri) {
const source = eventUri ?
`"${eventUri.fsPath}"` :
"setting";
outputLine(`INFO: Resetting ignore cache due to ${source} change.`);
ignores = null;
if (eventUri) {
clearDiagnosticsAndLintVisibleFiles();
}
}
// Returns custom rule configuration for user/workspace
function getCustomRules (configuration) {
const customRulesPaths = configuration.get(sectionCustomRules);
const customRules = customRulesPaths.map((rulePath) => {
const match = customRuleExtensionPrefixRe.exec(rulePath);
if (match) {
const [
,
extensionName,
relativePath
] = match;
const extension = vscode.extensions.getExtension(extensionName);
if (extension) {
// eslint-disable-next-line no-param-reassign
rulePath = posixPath(
path.resolve(extension.extensionPath, relativePath)
);
}
}
return rulePath;
});
return customRules;
}
// Gets the default markdown-it plugins for markdownlint-cli2
function getDefaultMarkdownItPlugins () {
return [
[
require("markdown-it-texmath"),
{
"engine": {
"renderToString": () => ""
}
}
]
];
}
// Gets the value of the optionsDefault parameter to markdownlint-cli2
async function getOptionsDefault (fs, workspaceConfiguration, config) {
return {
"config": config || await getConfig(fs, workspaceConfiguration),
"customRules": getCustomRules(workspaceConfiguration),
"markdownItPlugins": getDefaultMarkdownItPlugins()
};
}
// Gets the value of the optionsOverride parameter to markdownlint-cli2
function getOptionsOverride () {
return {
"fix": false
};
}
// Gets the value of the noRequire parameter to markdownlint-cli2
function getNoRequire (scheme) {
const isTrusted = vscode.workspace.isTrusted;
const isSchemeFile = (scheme === schemeFile);
const isDesktop = Boolean(require("node:os").platform());
return !isTrusted || !isSchemeFile || !isDesktop;
}
// Wraps getting options and calling into markdownlint-cli2
async function markdownlintWrapper (document) {
// Prepare markdownlint-cli2 parameters
const scheme = document.uri.scheme;
const independentDocument = !schemeFileSystemLike.has(scheme);
const name = posixPath(document.uri.fsPath);
const workspaceFolderUri = getWorkspaceFolderUri(document.uri);
const fs = independentDocument ?
new FsNull() :
new FsWrapper(workspaceFolderUri);
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
const config = await getConfig(fs, configuration, document.uri);
const directory = independentDocument ?
null :
(workspaceFolderUri ?
posixPath(workspaceFolderUri.fsPath) :
path.posix.dirname(name));
const argv = independentDocument ?
[] :
[ `:${name}` ];
const contents = independentDocument ?
"nonFileContents" :
"fileContents";
let results = [];
// eslint-disable-next-line func-style
const captureResultsFormatter = (options) => {
results = options.results;
};
const parameters = {
fs,
directory,
argv,
[contents]: {
[name]: document.getText()
},
"noErrors": true,
"noGlobs": true,
"noRequire": getNoRequire(scheme),
"optionsDefault": await getOptionsDefault(fs, configuration, config),
"optionsOverride": {
...getOptionsOverride(),
"outputFormatters": [ [ captureResultsFormatter ] ]
}
};
// Invoke markdownlint-cli2
const {"main": markdownlintCli2} = require("markdownlint-cli2");
return markdownlintCli2(parameters)
.catch((error) => outputLine(errorExceptionPrefix + error.stack, true))
.then(() => results);
// If necessary some day to filter results by matching file name...
// .then(() => results.filter((result) => isSchemeUntitled || (result.fileName === path.posix.relative(directory, name))))
}
// Returns if the document is Markdown
function isMarkdownDocument (document) {
return (
(document.languageId === markdownLanguageId) &&
schemeSupported.has(document.uri.scheme)
);
}
// Lints Markdown files in the workspace folder tree
async function lintWorkspace (logString) {
const workspaceFolderUri = getWorkspaceFolderUri();
if (workspaceFolderUri) {
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, workspaceFolderUri);
const fs = new FsWrapper(workspaceFolderUri);
const parameters = {
fs,
"argv": configuration.get(sectionLintWorkspaceGlobs),
"directory": posixPath(workspaceFolderUri.fsPath),
"logMessage": logString,
"logError": logString,
"noErrors": true,
"noRequire": getNoRequire(workspaceFolderUri.scheme),
"optionsDefault": await getOptionsDefault(fs, configuration),
"optionsOverride": getOptionsOverride()
};
const {"main": markdownlintCli2} = require("markdownlint-cli2");
return markdownlintCli2(parameters)
.catch((error) => logString(errorExceptionPrefix + error.stack));
}
throw new Error("No workspace folder.");
}
// Runs the lintWorkspace task to lint all Markdown files in the workspace
function lintWorkspaceViaTask () {
return vscode.tasks.fetchTasks({"type": extensionDisplayName})
.then((tasks) => {
const lintWorkspaceTask = tasks.find((task) => task.name === lintAllTaskName);
return lintWorkspaceTask ?
vscode.tasks.executeTask(lintWorkspaceTask) :
Promise.reject(new Error("Unable to fetch task."));
});
}
// Lints a Markdown document
function lint (document) {
if (!lintingEnabled || !isMarkdownDocument(document)) {
return;
}
// Check ignore list
const diagnostics = [];
let task = Promise.resolve();
const relativePath = vscode.workspace.asRelativePath(document.uri, false);
const normalizedPath = relativePath.split(path.sep).join("/");
if (getIgnores(document).every((ignoreTest) => !ignoreTest(normalizedPath))) {
// Lint
task = markdownlintWrapper(document)
.then((results) => {
const {activeTextEditor} = vscode.window;
for (const result of results) {
// Create Diagnostics
const lineNumber = result.lineNumber;
const focusMode = applicationConfiguration[sectionFocusMode];
const focusModeRange = (!Number.isInteger(focusMode) || (focusMode < 0)) ?
0 :
focusMode;
if (
(applicationConfiguration[sectionFocusMode] === false) ||
!activeTextEditor ||
(activeTextEditor.document !== document) ||
(activeTextEditor.selection.active.line < (lineNumber - focusModeRange - 1)) ||
(activeTextEditor.selection.active.line > (lineNumber + focusModeRange - 1))
) {
const ruleName = result.ruleNames[0];
const ruleDescription = result.ruleDescription;
const ruleInformationUri = result.ruleInformation && vscode.Uri.parse(result.ruleInformation);
ruleNameToInformationUri[ruleName] = ruleInformationUri;
let message = result.ruleNames.join("/") + ": " + ruleDescription;
if (result.errorDetail) {
message += " [" + result.errorDetail + "]";
}
let range = document.lineAt(lineNumber - 1).range;
if (result.errorRange) {
const start = result.errorRange[0] - 1;
const end = start + result.errorRange[1];
range = range.with(range.start.with(undefined, start), range.end.with(undefined, end));
}
const diagnostic = new vscode.Diagnostic(range, message, vscode.DiagnosticSeverity.Warning);
diagnostic.code = ruleInformationUri ?
{
"value": ruleName,
"target": ruleInformationUri
} :
ruleName;
diagnostic.source = extensionDisplayName;
// @ts-ignore
diagnostic.fixInfo = result.fixInfo;
diagnostics.push(diagnostic);
}
}
});
}
// Publish
task.then(() => diagnosticCollection.set(document.uri, diagnostics));
}
// Implements CodeActionsProvider.provideCodeActions to provide information and fix rule violations
function provideCodeActions (document, range, codeActionContext) {
const codeActions = [];
// eslint-disable-next-line func-style
const addToCodeActions = (action) => {
if (!codeActionContext.only || codeActionContext.only.contains(action.kind)) {
codeActions.push(action);
}
};
const diagnostics = codeActionContext.diagnostics || [];
const extensionDiagnostics = diagnostics.filter((diagnostic) => diagnostic.source === extensionDisplayName);
for (const diagnostic of extensionDiagnostics) {
const ruleName = diagnostic.code.value || diagnostic.code;
const ruleNameAlias = diagnostic.message.split(":")[0];
// Provide code action to fix the violation
if (diagnostic.fixInfo) {
const fixTitle = clickToFix + ruleNameAlias;
const fixAction = new vscode.CodeAction(fixTitle, codeActionKindQuickFix);
fixAction.command = {
"title": fixTitle,
"command": fixLineCommandName,
"arguments": [
diagnostic.range.start.line,
diagnostic.fixInfo
]
};
fixAction.diagnostics = [ diagnostic ];
fixAction.isPreferred = true;
addToCodeActions(fixAction);
}
// Provide code action for information about the violation
const ruleInformationUri = ruleNameToInformationUri[ruleName];
if (ruleInformationUri) {
const infoTitle = clickForInfo + ruleNameAlias;
const infoAction = new vscode.CodeAction(infoTitle, codeActionKindQuickFix);
infoAction.command = {
"title": infoTitle,
"command": openCommand,
"arguments": [ ruleInformationUri ]
};
infoAction.diagnostics = [ diagnostic ];
addToCodeActions(infoAction);
}
}
if (extensionDiagnostics.length > 0) {
// eslint-disable-next-line func-style
const registerFixAllCodeAction = (codeActionKind) => {
// Register a "fix all" code action
const sourceFixAllAction = new vscode.CodeAction(
fixAllCommandTitle,
codeActionKind
);
sourceFixAllAction.command = {
"title": fixAllCommandTitle,
"command": fixAllCommandName
};
addToCodeActions(sourceFixAllAction);
};
registerFixAllCodeAction(codeActionKindSourceFixAllExtension);
registerFixAllCodeAction(codeActionKindQuickFix);
// Add information about configuring rules
const configureInfoAction = new vscode.CodeAction(clickForConfigureInfo, codeActionKindQuickFix);
configureInfoAction.command = {
"title": clickForConfigureInfo,
"command": openCommand,
"arguments": [ vscode.Uri.parse(clickForConfigureUrl) ]
};
addToCodeActions(configureInfoAction);
}
return codeActions;
}
// Fixes violations of a rule on a line
function fixLine (lineIndex, fixInfo) {
return new Promise((resolve, reject) => {
const editor = vscode.window.activeTextEditor;
if (editor && fixInfo) {
const {applyFix} = require("markdownlint/helpers");
const document = editor.document;
const lineNumber = fixInfo.lineNumber || (lineIndex + 1);
const {text, range} = document.lineAt(lineNumber - 1);
const fixedText = applyFix(text, fixInfo, "\n");
return editor.edit((editBuilder) => {
if (typeof fixedText === "string") {
editBuilder.replace(range, fixedText);
} else {
let deleteRange = range;
if (lineNumber === 1) {
if (document.lineCount > 1) {
const nextLine = document.lineAt(range.end.line + 1);
deleteRange = range.with({"end": nextLine.range.start});
}
} else {
const previousLine = document.lineAt(range.start.line - 1);
deleteRange = range.with({"start": previousLine.range.end});
}
editBuilder.delete(deleteRange);
}
})
.then(() => {
// Remove inappropriate selection that may have been added by editBuilder.replace
const cursorPosition = editor.selection.active;
editor.selection = new vscode.Selection(cursorPosition, cursorPosition);
})
.then(resolve, reject);
}
return resolve();
});
}
// Fixes all violations in the active document
function fixAll () {
return new Promise((resolve, reject) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
if (isMarkdownDocument(document)) {
return markdownlintWrapper(document)
.then((errors) => {
const {applyFixes} = require("markdownlint/helpers");
const text = document.getText();
const fixedText = applyFixes(text, errors);
return (text === fixedText) ?
null :
editor.edit((editBuilder) => {
const start = document.lineAt(0).range.start;
const end = document.lineAt(document.lineCount - 1).range.end;
editBuilder.replace(new vscode.Range(start, end), fixedText);
});
})
.then(resolve, reject);
}
}
return resolve();
});
}
// Formats a range of the document (applying fixes)
function formatDocument (document, range) {
return new Promise((resolve, reject) => {
if (isMarkdownDocument(document)) {
return markdownlintWrapper(document)
.then((errors) => {
const rangeErrors = errors.filter((error) => {
const {fixInfo} = error;
if (fixInfo) {
// eslint-disable-next-line unicorn/consistent-destructuring
const line = error.lineNumber - 1;
return ((range.start.line <= line) && (line <= range.end.line));
}
return false;
});
const {applyFixes} = require("markdownlint/helpers");
const text = document.getText();
const fixedText = applyFixes(text, rangeErrors);
const start = document.lineAt(0).range.start;
const end = document.lineAt(document.lineCount - 1).range.end;
return (text === fixedText) ?
[] :
[ vscode.TextEdit.replace(new vscode.Range(start, end), fixedText) ];
})
.then(resolve, reject);
}
return resolve();
});
}
// Creates or opens the markdownlint configuration file for the workspace
function openConfigFile () {
const workspaceFolderUri = getWorkspaceFolderUri();
if (workspaceFolderUri) {
Promise.all(configFileNames.map((configFileName) => {
const fileUri = vscode.Uri.joinPath(workspaceFolderUri, configFileName);
return vscode.workspace.fs.stat(fileUri).then(
() => fileUri,
() => null
);
})).then((fileUris) => {
const validFilePaths = fileUris.filter((filePath) => filePath !== null);
if (validFilePaths.length > 0) {
// File exists, open it
vscode.window.showTextDocument(validFilePaths[0]);
} else {
// File does not exist, create one
const fileUri = vscode.Uri.joinPath(workspaceFolderUri, markdownlintJson);
const untitledFileUri = fileUri.with({"scheme": schemeUntitled});
vscode.window.showTextDocument(untitledFileUri).then(
(editor) => {
editor.edit((editBuilder) => {
editBuilder.insert(
new vscode.Position(0, 0),
JSON.stringify(defaultConfig, null, 2)
);
});
}
);
}
});
}
}
// Toggles linting on/off
function toggleLinting () {
lintingEnabled = !lintingEnabled;
clearDiagnosticsAndLintVisibleFiles();
}
// Clears diagnostics and lints all visible files
function clearDiagnosticsAndLintVisibleFiles (eventUri) {
if (eventUri) {
outputLine(`INFO: Re-linting due to "${eventUri.fsPath}" change.`);
}
diagnosticCollection.clear();
lintVisibleFiles();
}
// Lints all visible files
function lintVisibleFiles () {
didChangeVisibleTextEditors(vscode.window.visibleTextEditors);
}
// Returns the run setting for the document
function getRun (document) {
const name = document.uri.toString();
// Use cached configuration if present for file
if (runMap[name]) {
return runMap[name];
}
// Read workspace configuration
const configuration = vscode.workspace.getConfiguration(extensionDisplayName, document.uri);
runMap[name] = configuration.get(sectionRun);
outputLine("INFO: Linting for \"" + name + "\" will be run \"" + runMap[name] + "\".");
return runMap[name];
}
// Clears the map of run settings
function clearRunMap () {
runMap = {};
}
// Suppresses a pending lint for the specified document
function suppressLint (document) {
if (throttle.timeout && (document === throttle.document)) {
clearTimeout(throttle.timeout);
throttle.document = null;
throttle.timeout = null;
}
}
// Requests a lint of the specified document
function requestLint (document) {
suppressLint(document);
throttle.document = document;
throttle.timeout = setTimeout(() => {
// Do not use throttle.document in this function; it may have changed
lint(document);
suppressLint(document);
}, throttleDuration);
}
// Reads all application-scoped configuration settings
function getApplicationConfiguration () {
const configuration = vscode.workspace.getConfiguration(extensionDisplayName);
for (const section of applicationConfigurationSections) {
applicationConfiguration[section] = configuration.get(section);
}
}
// Handles the onDidChangeActiveTextEditor event
function didChangeActiveTextEditor () {
if (applicationConfiguration[sectionFocusMode] !== false) {
lintVisibleFiles();
}
}
// Handles the onDidChangeTextEditorSelection event
function didChangeTextEditorSelection (change) {
const document = change.textEditor.document;
if (
isMarkdownDocument(document) &&
(applicationConfiguration[sectionFocusMode] !== false)
) {
requestLint(document);
}
}
// Handles the onDidChangeVisibleTextEditors event
function didChangeVisibleTextEditors (textEditors) {
for (const textEditor of textEditors) {
lint(textEditor.document);
}
}
// Handles the onDidOpenTextDocument event
function didOpenTextDocument (document) {
if (isMarkdownDocument(document)) {
lint(document);
suppressLint(document);
}
}
// Handles the onDidChangeTextDocument event
function didChangeTextDocument (change) {
const document = change.document;
if (isMarkdownDocument(document) && (getRun(document) === "onType")) {
requestLint(document);
}
}
// Handles the onDidSaveTextDocument event
function didSaveTextDocument (document) {
if (isMarkdownDocument(document) && (getRun(document) === "onSave")) {
lint(document);
suppressLint(document);
}
}
// Handles the onDidCloseTextDocument event
function didCloseTextDocument (document) {
suppressLint(document);
diagnosticCollection.delete(document.uri);
}
// Handles the onDidChangeConfiguration event
function didChangeConfiguration (change) {
if (!change || change.affectsConfiguration(extensionDisplayName)) {
outputLine("INFO: Resetting configuration cache due to setting change.");
getApplicationConfiguration();
clearRunMap();
clearIgnores();
clearDiagnosticsAndLintVisibleFiles();
}
}
// Handles the onDidGrantWorkspaceTrust event
function didGrantWorkspaceTrust () {
didChangeConfiguration();
}
function activate (context) {
// Create OutputChannel
outputChannel = vscode.window.createOutputChannel(extensionDisplayName);
context.subscriptions.push(outputChannel);
// Get application-level configuration
getApplicationConfiguration();
// Hook up to workspace events
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(didChangeActiveTextEditor),
vscode.window.onDidChangeTextEditorSelection(didChangeTextEditorSelection),
vscode.window.onDidChangeVisibleTextEditors(didChangeVisibleTextEditors),
vscode.workspace.onDidOpenTextDocument(didOpenTextDocument),
vscode.workspace.onDidChangeTextDocument(didChangeTextDocument),
vscode.workspace.onDidSaveTextDocument(didSaveTextDocument),
vscode.workspace.onDidCloseTextDocument(didCloseTextDocument),
vscode.workspace.onDidChangeConfiguration(didChangeConfiguration),
vscode.workspace.onDidGrantWorkspaceTrust(didGrantWorkspaceTrust)
);