@bhsd/codemirror-mediawiki 2.31.0 → 3.0.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/README.md +170 -24
- package/dist/codemirror.d.ts +72 -47
- package/dist/codemirror.js +169 -208
- package/dist/color.d.ts +1 -6
- package/dist/color.js +1 -8
- package/dist/config.js +4 -4
- package/dist/escape.js +2 -2
- package/dist/fold.d.ts +53 -6
- package/dist/fold.js +130 -133
- package/dist/indent.d.ts +5 -1
- package/dist/indent.js +2 -1
- package/dist/javascript.d.ts +1 -0
- package/dist/javascript.js +2 -4
- package/dist/keybindings.js +1 -0
- package/dist/linter.js +43 -8
- package/dist/lintsource.d.ts +14 -0
- package/dist/lintsource.js +159 -0
- package/dist/main.min.js +25 -24
- package/dist/matchBrackets.d.ts +1 -1
- package/dist/matchTag.js +2 -2
- package/dist/mw.min.js +30 -29
- package/dist/mwConfig.js +0 -3
- package/dist/statusBar.d.ts +1 -1
- package/dist/token.d.ts +1 -1
- package/dist/token.js +11 -16
- package/dist/vue.d.ts +3 -0
- package/dist/vue.js +14 -0
- package/dist/wiki.min.js +29 -28
- package/i18n/en.json +2 -3
- package/i18n/zh-hans.json +2 -3
- package/i18n/zh-hant.json +2 -3
- package/package.json +10 -7
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { cssLanguage } from '@codemirror/lang-css';
|
|
2
|
+
import { javascriptLanguage } from '@codemirror/lang-javascript';
|
|
3
|
+
import { getWikiLinter, getJsLinter, getCssLinter, getJsonLinter, getLuaLinter } from './linter';
|
|
4
|
+
import { posToIndex } from './hover';
|
|
5
|
+
/**
|
|
6
|
+
* 获取Linter选项
|
|
7
|
+
* @param opt Linter选项
|
|
8
|
+
* @param runtime 是否为运行时选项
|
|
9
|
+
*/
|
|
10
|
+
const getOpt = (opt, runtime) => typeof opt === 'function' ? opt(runtime) : opt;
|
|
11
|
+
/**
|
|
12
|
+
* 获取指定行列的位置
|
|
13
|
+
* @param doc 文档
|
|
14
|
+
* @param line 行号
|
|
15
|
+
* @param column 列号
|
|
16
|
+
* @param from 子语言起始位置
|
|
17
|
+
*/
|
|
18
|
+
const pos = (doc, line, column, from = 0) => {
|
|
19
|
+
if (from === 0) {
|
|
20
|
+
return posToIndex(doc, { line: line - 1, character: column - 1 });
|
|
21
|
+
}
|
|
22
|
+
const lineDesc = doc.lineAt(from);
|
|
23
|
+
return posToIndex(doc, {
|
|
24
|
+
line: lineDesc.number + line - 2,
|
|
25
|
+
character: (line === 1 ? from - lineDesc.from : 0) + column - 1,
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
export const getWikiLintSource = async (opt, v) => {
|
|
29
|
+
const wikiLint = await getWikiLinter(await getOpt(opt), v);
|
|
30
|
+
return async ({ doc }) => (await wikiLint(doc.toString(), await getOpt(opt, true)))
|
|
31
|
+
.map(({ severity, code, message, range: r, from, to, data = [], source }) => ({
|
|
32
|
+
source: source,
|
|
33
|
+
from: from ?? posToIndex(doc, r.start),
|
|
34
|
+
to: to ?? posToIndex(doc, r.end),
|
|
35
|
+
severity: severity === 2 ? 'warning' : 'error',
|
|
36
|
+
message: source === 'Stylelint' ? message : `${message} (${code})`,
|
|
37
|
+
actions: data.map(({ title, range, newText }) => ({
|
|
38
|
+
name: title,
|
|
39
|
+
apply(view) {
|
|
40
|
+
view.dispatch({
|
|
41
|
+
changes: {
|
|
42
|
+
from: posToIndex(doc, range.start),
|
|
43
|
+
to: posToIndex(doc, range.end),
|
|
44
|
+
insert: newText,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
})),
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
51
|
+
const getRange = (doc, line, column, endLine, endColumn, f = 0, t = Infinity) => {
|
|
52
|
+
const start = pos(doc, line, column, f);
|
|
53
|
+
return {
|
|
54
|
+
from: start,
|
|
55
|
+
to: endLine === undefined ? Math.min(t, start + 1) : pos(doc, endLine, endColumn, f),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const jsLintSource = (esLint, code, opt, doc, f = 0, t) => esLint(code, opt)
|
|
59
|
+
.map(({ ruleId, message, severity, line, column, endLine, endColumn, fix, suggestions = [] }) => {
|
|
60
|
+
const diagnostic = {
|
|
61
|
+
source: 'ESLint',
|
|
62
|
+
message: message + (ruleId ? ` (${ruleId})` : ''),
|
|
63
|
+
severity: severity === 1 ? 'warning' : 'error',
|
|
64
|
+
...getRange(doc, line, column, endLine, endColumn, f, t),
|
|
65
|
+
};
|
|
66
|
+
if (fix || suggestions.length > 0) {
|
|
67
|
+
diagnostic.actions = [
|
|
68
|
+
...fix ? [{ name: 'fix', fix }] : [],
|
|
69
|
+
...suggestions.map(suggestion => ({ name: 'suggestion', fix: suggestion.fix })),
|
|
70
|
+
].map(({ name, fix: { range: [from, to], text } }) => ({
|
|
71
|
+
name,
|
|
72
|
+
apply(view) {
|
|
73
|
+
view.dispatch({ changes: { from: from + f, to: to + f, insert: text } });
|
|
74
|
+
},
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
return diagnostic;
|
|
78
|
+
});
|
|
79
|
+
const cssLintSource = async (styleLint, code, opt, doc, f = 0, t) => {
|
|
80
|
+
let option = opt ?? {};
|
|
81
|
+
if (!('extends' in option || 'rules' in option)) {
|
|
82
|
+
option = { rules: option };
|
|
83
|
+
}
|
|
84
|
+
return (await styleLint(code, option))
|
|
85
|
+
.map(({ text, severity, line, column, endLine, endColumn, fix }) => {
|
|
86
|
+
const diagnostic = {
|
|
87
|
+
source: 'Stylelint',
|
|
88
|
+
message: text,
|
|
89
|
+
severity,
|
|
90
|
+
...getRange(doc, line, column, endLine, endColumn, f, t),
|
|
91
|
+
};
|
|
92
|
+
if (fix) {
|
|
93
|
+
diagnostic.actions = [
|
|
94
|
+
{
|
|
95
|
+
name: 'fix',
|
|
96
|
+
apply(view) {
|
|
97
|
+
view.dispatch({
|
|
98
|
+
changes: { from: fix.range[0] + f, to: fix.range[1] + f, insert: fix.text },
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
return diagnostic;
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
export const getJsLintSource = async (opt) => {
|
|
108
|
+
const esLint = await getJsLinter();
|
|
109
|
+
const lintSource = async ({ doc }) => jsLintSource(esLint, doc.toString(), await getOpt(opt), doc);
|
|
110
|
+
lintSource.fixer = (doc, rule) => esLint.fixer(doc.toString(), rule);
|
|
111
|
+
return lintSource;
|
|
112
|
+
};
|
|
113
|
+
export const getCssLintSource = async (opt) => {
|
|
114
|
+
const styleLint = await getCssLinter();
|
|
115
|
+
const lintSource = async ({ doc }) => cssLintSource(styleLint, doc.toString(), await getOpt(opt), doc);
|
|
116
|
+
lintSource.fixer = async (doc, rule) => styleLint.fixer(doc.toString(), rule);
|
|
117
|
+
return lintSource;
|
|
118
|
+
};
|
|
119
|
+
export const getVueLintSource = async (opt) => {
|
|
120
|
+
const styleLint = await getCssLinter(), esLint = await getJsLinter();
|
|
121
|
+
return async (state) => {
|
|
122
|
+
const { doc } = state, option = await getOpt(opt) ?? {}, js = option['js'], css = option['css'];
|
|
123
|
+
return [
|
|
124
|
+
...(await Promise.all(cssLanguage.findRegions(state)
|
|
125
|
+
.map(({ from, to }) => cssLintSource(styleLint, state.sliceDoc(from, to), css, doc, from, to)))).flat(),
|
|
126
|
+
...javascriptLanguage.findRegions(state)
|
|
127
|
+
.flatMap(({ from, to }) => jsLintSource(esLint, state.sliceDoc(from, to), js, doc, from, to)),
|
|
128
|
+
];
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
export const getJsonLintSource = () => {
|
|
132
|
+
const jsonLint = getJsonLinter();
|
|
133
|
+
return ({ doc }) => {
|
|
134
|
+
const [e] = jsonLint(doc.toString());
|
|
135
|
+
if (e) {
|
|
136
|
+
const { message, severity, line, column, position } = e;
|
|
137
|
+
let from = 0;
|
|
138
|
+
if (position) {
|
|
139
|
+
from = Number(position);
|
|
140
|
+
}
|
|
141
|
+
else if (line && column) {
|
|
142
|
+
from = pos(doc, Number(line), Number(column));
|
|
143
|
+
}
|
|
144
|
+
return [{ message, severity, from, to: from }];
|
|
145
|
+
}
|
|
146
|
+
return [];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
export const getLuaLintSource = async () => {
|
|
150
|
+
const luaLint = await getLuaLinter();
|
|
151
|
+
return async ({ doc }) => (await luaLint(doc.toString()))
|
|
152
|
+
.map(({ line, column, end_column: endColumn, msg: message, severity }) => ({
|
|
153
|
+
source: 'Luacheck',
|
|
154
|
+
message,
|
|
155
|
+
severity: severity === 1 ? 'warning' : 'error',
|
|
156
|
+
from: pos(doc, line, column),
|
|
157
|
+
to: pos(doc, line, endColumn + 1),
|
|
158
|
+
}));
|
|
159
|
+
};
|