2018-10-11 12:00:25 +00:00
|
|
|
/* global linter editorWorker */
|
2018-10-10 16:54:38 +00:00
|
|
|
/* exported createMetaCompiler */
|
2018-10-01 14:03:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function createMetaCompiler(cm) {
|
|
|
|
const updateListeners = [];
|
|
|
|
let meta = null;
|
|
|
|
let metaIndex = null;
|
|
|
|
let cache = [];
|
|
|
|
|
|
|
|
linter.register((text, options, _cm) => {
|
|
|
|
if (_cm !== cm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const match = text.match(/\/\*\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i);
|
|
|
|
if (!match) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (match[0] === meta && match.index === metaIndex) {
|
|
|
|
return cache;
|
|
|
|
}
|
2018-10-01 14:49:16 +00:00
|
|
|
return editorWorker.metalint(match[0])
|
|
|
|
.then(({metadata, errors}) => {
|
2018-10-01 15:01:21 +00:00
|
|
|
if (errors.every(err => err.code === 'unknownMeta')) {
|
2018-10-01 14:49:16 +00:00
|
|
|
for (const cb of updateListeners) {
|
|
|
|
cb(metadata);
|
|
|
|
}
|
2018-10-01 14:03:17 +00:00
|
|
|
}
|
2018-10-01 14:49:16 +00:00
|
|
|
cache = errors.map(err =>
|
|
|
|
({
|
|
|
|
from: cm.posFromIndex((err.index || 0) + match.index),
|
|
|
|
to: cm.posFromIndex((err.index || 0) + match.index),
|
2018-10-01 15:14:56 +00:00
|
|
|
message: err.code && chrome.i18n.getMessage(`meta_${err.code}`, err.args) || err.message,
|
2018-10-01 14:49:16 +00:00
|
|
|
severity: err.code === 'unknownMeta' ? 'warning' : 'error'
|
|
|
|
})
|
|
|
|
);
|
2018-10-01 14:03:17 +00:00
|
|
|
meta = match[0];
|
|
|
|
metaIndex = match.index;
|
|
|
|
return cache;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
onUpdated: cb => updateListeners.push(cb)
|
|
|
|
};
|
|
|
|
}
|