coc-markdownlint 1.35.0 → 1.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/engine.js +211 -0
- package/lib/index.js +67 -82
- package/package.json +143 -112
- package/snippets/snippets.json +15 -0
package/lib/engine.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MarkdownlintEngine = void 0;
|
|
7
|
+
const coc_nvim_1 = require("coc.nvim");
|
|
8
|
+
const deep_extend_1 = __importDefault(require("deep-extend"));
|
|
9
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
11
|
+
const markdownlint_1 = require("markdownlint");
|
|
12
|
+
const markdownlint_rule_helpers_1 = require("markdownlint-rule-helpers");
|
|
13
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
14
|
+
const rc_1 = __importDefault(require("rc"));
|
|
15
|
+
const projectConfigFiles = [".markdownlint.json", ".markdownlint.yaml", ".markdownlint.yml"];
|
|
16
|
+
const configFileParsers = [JSON.parse, js_yaml_1.default.load];
|
|
17
|
+
class MarkdownlintEngine {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.fixAllCommandName = "markdownlint.fixAll";
|
|
20
|
+
this.source = "markdownlint";
|
|
21
|
+
this.outputChannel = coc_nvim_1.window.createOutputChannel(this.source);
|
|
22
|
+
this.diagnosticCollection = coc_nvim_1.languages.createDiagnosticCollection(this.source);
|
|
23
|
+
this.config = {};
|
|
24
|
+
}
|
|
25
|
+
outputLine(message) {
|
|
26
|
+
if (this.outputChannel) {
|
|
27
|
+
this.outputChannel.appendLine(`[${new Date().toLocaleTimeString()}] ${message}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async parseConfig() {
|
|
31
|
+
try {
|
|
32
|
+
this.config = (0, rc_1.default)(this.source, {});
|
|
33
|
+
this.outputLine(`Info: global config: ${JSON.stringify((0, rc_1.default)(this.source, {}))}`);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
this.outputLine(`Error: global config parse failed: ${e}`);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
for (const projectConfigFile of projectConfigFiles) {
|
|
40
|
+
const fullPath = node_path_1.default.join(coc_nvim_1.workspace.root, projectConfigFile);
|
|
41
|
+
if (node_fs_1.default.existsSync(fullPath)) {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
const projectConfig = (0, markdownlint_1.readConfigSync)(fullPath, configFileParsers);
|
|
44
|
+
this.config = (0, deep_extend_1.default)(this.config, projectConfig);
|
|
45
|
+
this.outputLine(`Info: local config: ${fullPath}, ${JSON.stringify(projectConfig)}`);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
this.outputLine(`Error: local config parse failed: ${e}`);
|
|
52
|
+
}
|
|
53
|
+
const cocConfig = coc_nvim_1.workspace.getConfiguration("markdownlint").get("config");
|
|
54
|
+
if (cocConfig) {
|
|
55
|
+
this.config = (0, deep_extend_1.default)(this.config, cocConfig);
|
|
56
|
+
this.outputLine(`Info: config from coc-settings.json: ${JSON.stringify(cocConfig)}`);
|
|
57
|
+
}
|
|
58
|
+
this.outputLine(`Info: full config: ${JSON.stringify(this.config)}`);
|
|
59
|
+
}
|
|
60
|
+
markdownlintWrapper(document) {
|
|
61
|
+
const options = {
|
|
62
|
+
resultVersion: 3,
|
|
63
|
+
config: this.config,
|
|
64
|
+
// customRules: customRules,
|
|
65
|
+
strings: {
|
|
66
|
+
[document.uri]: document.getText(),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
let results = [];
|
|
70
|
+
try {
|
|
71
|
+
results = (0, markdownlint_1.sync)(options)[document.uri];
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
this.outputLine(`Error: lint exception: ${e}`);
|
|
75
|
+
}
|
|
76
|
+
return results || [];
|
|
77
|
+
}
|
|
78
|
+
async provideCodeActions(document, range, context) {
|
|
79
|
+
const doc = coc_nvim_1.workspace.getDocument(document.uri);
|
|
80
|
+
const wholeRange = coc_nvim_1.Range.create(0, 0, doc.lineCount, 0);
|
|
81
|
+
let whole = false;
|
|
82
|
+
if (range.start.line === wholeRange.start.line &&
|
|
83
|
+
range.start.character === wholeRange.start.character &&
|
|
84
|
+
range.end.line === wholeRange.end.line &&
|
|
85
|
+
range.end.character === wholeRange.end.character) {
|
|
86
|
+
whole = true;
|
|
87
|
+
}
|
|
88
|
+
const codeActions = [];
|
|
89
|
+
const fixInfoDiagnostics = [];
|
|
90
|
+
for (const diagnostic of context.diagnostics) {
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
if (diagnostic.fixInfo) {
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
const lineNumber = diagnostic.fixInfo.lineNumber - 1 || diagnostic.range.start.line;
|
|
95
|
+
const line = await coc_nvim_1.workspace.getLine(document.uri, lineNumber);
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
const newText = (0, markdownlint_rule_helpers_1.applyFix)(line, diagnostic.fixInfo, "\n");
|
|
98
|
+
const edit = { changes: {} };
|
|
99
|
+
if (typeof newText === "string") {
|
|
100
|
+
const range = coc_nvim_1.Range.create(lineNumber, 0, lineNumber, line.length);
|
|
101
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
102
|
+
edit.changes[document.uri] = [coc_nvim_1.TextEdit.replace(range, newText)];
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
106
|
+
edit.changes[document.uri] = [coc_nvim_1.TextEdit.del(diagnostic.range)];
|
|
107
|
+
}
|
|
108
|
+
const title = `Fix: ${diagnostic.message.split(":")[0]}`;
|
|
109
|
+
const action = {
|
|
110
|
+
title,
|
|
111
|
+
edit,
|
|
112
|
+
diagnostics: [...context.diagnostics],
|
|
113
|
+
};
|
|
114
|
+
fixInfoDiagnostics.push(diagnostic);
|
|
115
|
+
if (!whole) {
|
|
116
|
+
codeActions.push(action);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (range.start.line === range.end.line && range.start.character === 0) {
|
|
121
|
+
// <!-- markdownlint-disable-next-line -->
|
|
122
|
+
const edit = coc_nvim_1.TextEdit.insert(coc_nvim_1.Position.create(range.start.line, 0), "<!-- markdownlint-disable-next-line -->\n");
|
|
123
|
+
codeActions.push({
|
|
124
|
+
title: "Disable markdownlint for current line",
|
|
125
|
+
edit: {
|
|
126
|
+
changes: {
|
|
127
|
+
[doc.uri]: [edit],
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (whole) {
|
|
133
|
+
// <!-- markdownlint-disable-file -->
|
|
134
|
+
const edit = coc_nvim_1.TextEdit.insert(coc_nvim_1.Position.create(0, 0), "<!-- markdownlint-disable-file -->\n");
|
|
135
|
+
codeActions.push({
|
|
136
|
+
title: "Disable markdownlint for current file",
|
|
137
|
+
edit: {
|
|
138
|
+
changes: {
|
|
139
|
+
[doc.uri]: [edit],
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (fixInfoDiagnostics.length) {
|
|
145
|
+
const title = "Fix All error found by markdownlint";
|
|
146
|
+
const sourceFixAllAction = {
|
|
147
|
+
title,
|
|
148
|
+
kind: coc_nvim_1.CodeActionKind.SourceFixAll,
|
|
149
|
+
diagnostics: fixInfoDiagnostics,
|
|
150
|
+
command: {
|
|
151
|
+
title,
|
|
152
|
+
command: this.fixAllCommandName,
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
codeActions.push(sourceFixAllAction);
|
|
156
|
+
}
|
|
157
|
+
return codeActions;
|
|
158
|
+
}
|
|
159
|
+
lint(document) {
|
|
160
|
+
if (document.languageId !== "markdown") {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
this.diagnosticCollection.set(document.uri);
|
|
164
|
+
const results = this.markdownlintWrapper(document);
|
|
165
|
+
if (!results.length) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const diagnostics = [];
|
|
169
|
+
for (const result of results) {
|
|
170
|
+
const ruleDescription = result.ruleDescription;
|
|
171
|
+
let message = `${result.ruleNames.join("/")}: ${ruleDescription}`;
|
|
172
|
+
if (result.errorDetail) {
|
|
173
|
+
message += ` [${result.errorDetail}]`;
|
|
174
|
+
}
|
|
175
|
+
const start = coc_nvim_1.Position.create(result.lineNumber - 1, 0);
|
|
176
|
+
const end = coc_nvim_1.Position.create(result.lineNumber - 1, 0);
|
|
177
|
+
if (result.errorRange) {
|
|
178
|
+
start.character = result.errorRange[0] - 1;
|
|
179
|
+
end.character = start.character + result.errorRange[1];
|
|
180
|
+
}
|
|
181
|
+
const range = coc_nvim_1.Range.create(start, end);
|
|
182
|
+
const diagnostic = coc_nvim_1.Diagnostic.create(range, message);
|
|
183
|
+
diagnostic.severity = coc_nvim_1.DiagnosticSeverity.Warning;
|
|
184
|
+
diagnostic.source = this.source;
|
|
185
|
+
// @ts-ignore
|
|
186
|
+
diagnostic.fixInfo = result.fixInfo;
|
|
187
|
+
diagnostics.push(diagnostic);
|
|
188
|
+
}
|
|
189
|
+
this.diagnosticCollection.set(document.uri, diagnostics);
|
|
190
|
+
}
|
|
191
|
+
async fixAll(document) {
|
|
192
|
+
const results = this.markdownlintWrapper(document);
|
|
193
|
+
if (!results.length) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const text = document.getText();
|
|
197
|
+
const fixedText = (0, markdownlint_rule_helpers_1.applyFixes)(text, results);
|
|
198
|
+
if (text !== fixedText) {
|
|
199
|
+
const doc = coc_nvim_1.workspace.getDocument(document.uri);
|
|
200
|
+
const end = coc_nvim_1.Position.create(doc.lineCount - 1, doc.getline(doc.lineCount - 1).length);
|
|
201
|
+
const edit = {
|
|
202
|
+
changes: {
|
|
203
|
+
[document.uri]: [coc_nvim_1.TextEdit.replace(coc_nvim_1.Range.create(coc_nvim_1.Position.create(0, 0), end), fixedText)],
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
await coc_nvim_1.workspace.applyEdit(edit);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.MarkdownlintEngine = MarkdownlintEngine;
|
|
211
|
+
//# sourceMappingURL=engine.js.map
|