2018-11-07 06:09:29 +00:00
|
|
|
/* global CodeMirror prefs loadScript editor $ template */
|
2017-09-24 08:54:21 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
// CodeMirror miserably fails on keyMap='' so let's ensure it's not
|
|
|
|
if (!prefs.get('editor.keyMap')) {
|
|
|
|
prefs.reset('editor.keyMap');
|
|
|
|
}
|
|
|
|
|
2020-06-01 04:54:49 +00:00
|
|
|
const CM_BOOKMARK = 'CodeMirror-bookmark';
|
|
|
|
const CM_BOOKMARK_GUTTER = CM_BOOKMARK + 'gutter';
|
2017-09-24 08:54:21 +00:00
|
|
|
const defaults = {
|
2018-11-07 06:09:29 +00:00
|
|
|
autoCloseBrackets: prefs.get('editor.autoCloseBrackets'),
|
2017-09-24 08:54:21 +00:00
|
|
|
mode: 'css',
|
|
|
|
lineNumbers: true,
|
2017-12-02 22:52:46 +00:00
|
|
|
lineWrapping: prefs.get('editor.lineWrapping'),
|
2017-09-24 08:54:21 +00:00
|
|
|
foldGutter: true,
|
|
|
|
gutters: [
|
2020-06-01 04:54:49 +00:00
|
|
|
CM_BOOKMARK_GUTTER,
|
2017-09-24 08:54:21 +00:00
|
|
|
'CodeMirror-linenumbers',
|
|
|
|
'CodeMirror-foldgutter',
|
|
|
|
...(prefs.get('editor.linter') ? ['CodeMirror-lint-markers'] : []),
|
|
|
|
],
|
|
|
|
matchBrackets: true,
|
|
|
|
hintOptions: {},
|
|
|
|
lintReportDelay: prefs.get('editor.lintReportDelay'),
|
|
|
|
styleActiveLine: true,
|
2018-11-07 06:09:29 +00:00
|
|
|
theme: prefs.get('editor.theme'),
|
2017-09-24 08:54:21 +00:00
|
|
|
keyMap: prefs.get('editor.keyMap'),
|
2017-12-08 02:45:27 +00:00
|
|
|
extraKeys: Object.assign(CodeMirror.defaults.extraKeys || {}, {
|
2017-09-24 08:54:21 +00:00
|
|
|
// independent of current keyMap
|
|
|
|
'Alt-Enter': 'toggleStyle',
|
|
|
|
'Alt-PageDown': 'nextEditor',
|
2017-12-24 08:20:37 +00:00
|
|
|
'Alt-PageUp': 'prevEditor',
|
|
|
|
'Ctrl-Pause': 'toggleEditorFocus',
|
2017-12-08 02:45:27 +00:00
|
|
|
}),
|
2017-12-02 23:02:22 +00:00
|
|
|
maxHighlightLength: 100e3,
|
2017-09-24 08:54:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(CodeMirror.defaults, defaults, prefs.get('editor.options'));
|
|
|
|
|
|
|
|
// 'basic' keymap only has basic keys by design, so we skip it
|
|
|
|
|
|
|
|
const extraKeysCommands = {};
|
|
|
|
Object.keys(CodeMirror.defaults.extraKeys).forEach(key => {
|
|
|
|
extraKeysCommands[CodeMirror.defaults.extraKeys[key]] = true;
|
|
|
|
});
|
|
|
|
if (!extraKeysCommands.jumpToLine) {
|
|
|
|
CodeMirror.keyMap.sublime['Ctrl-G'] = 'jumpToLine';
|
|
|
|
CodeMirror.keyMap.emacsy['Ctrl-G'] = 'jumpToLine';
|
|
|
|
CodeMirror.keyMap.pcDefault['Ctrl-J'] = 'jumpToLine';
|
|
|
|
CodeMirror.keyMap.macDefault['Cmd-J'] = 'jumpToLine';
|
|
|
|
}
|
|
|
|
if (!extraKeysCommands.autocomplete) {
|
|
|
|
// will be used by 'sublime' on PC via fallthrough
|
|
|
|
CodeMirror.keyMap.pcDefault['Ctrl-Space'] = 'autocomplete';
|
|
|
|
// OSX uses Ctrl-Space and Cmd-Space for something else
|
|
|
|
CodeMirror.keyMap.macDefault['Alt-Space'] = 'autocomplete';
|
|
|
|
// copied from 'emacs' keymap
|
|
|
|
CodeMirror.keyMap.emacsy['Alt-/'] = 'autocomplete';
|
|
|
|
// 'vim' and 'emacs' define their own autocomplete hotkeys
|
|
|
|
}
|
|
|
|
if (!extraKeysCommands.blockComment) {
|
2018-03-03 20:31:21 +00:00
|
|
|
CodeMirror.keyMap.sublime['Shift-Ctrl-/'] = 'commentSelection';
|
2017-09-24 08:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.appVersion.includes('Windows')) {
|
2017-12-17 19:00:51 +00:00
|
|
|
// 'pcDefault' keymap on Windows should have F3/Shift-F3/Ctrl-R
|
2017-09-24 08:54:21 +00:00
|
|
|
if (!extraKeysCommands.findNext) {
|
|
|
|
CodeMirror.keyMap.pcDefault['F3'] = 'findNext';
|
|
|
|
}
|
|
|
|
if (!extraKeysCommands.findPrev) {
|
|
|
|
CodeMirror.keyMap.pcDefault['Shift-F3'] = 'findPrev';
|
|
|
|
}
|
2017-12-17 19:00:51 +00:00
|
|
|
if (!extraKeysCommands.replace) {
|
|
|
|
CodeMirror.keyMap.pcDefault['Ctrl-R'] = 'replace';
|
|
|
|
}
|
2017-09-24 08:54:21 +00:00
|
|
|
|
|
|
|
// try to remap non-interceptable Ctrl-(Shift-)N/T/W hotkeys
|
|
|
|
['N', 'T', 'W'].forEach(char => {
|
|
|
|
[
|
|
|
|
{from: 'Ctrl-', to: ['Alt-', 'Ctrl-Alt-']},
|
|
|
|
// Note: modifier order in CodeMirror is S-C-A
|
|
|
|
{from: 'Shift-Ctrl-', to: ['Ctrl-Alt-', 'Shift-Ctrl-Alt-']}
|
|
|
|
].forEach(remap => {
|
|
|
|
const oldKey = remap.from + char;
|
|
|
|
Object.keys(CodeMirror.keyMap).forEach(keyMapName => {
|
|
|
|
const keyMap = CodeMirror.keyMap[keyMapName];
|
|
|
|
const command = keyMap[oldKey];
|
|
|
|
if (!command) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
remap.to.some(newMod => {
|
|
|
|
const newKey = newMod + char;
|
|
|
|
if (!(newKey in keyMap)) {
|
|
|
|
delete keyMap[oldKey];
|
|
|
|
keyMap[newKey] = command;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:01:27 +00:00
|
|
|
Object.assign(CodeMirror.mimeModes['text/css'].propertyKeywords, {
|
2020-09-25 10:51:03 +00:00
|
|
|
'content-visibility': true,
|
2020-09-22 10:56:53 +00:00
|
|
|
'overflow-anchor': true,
|
2020-08-13 17:58:09 +00:00
|
|
|
'overscroll-behavior': true,
|
2017-11-23 10:01:27 +00:00
|
|
|
});
|
2017-12-19 21:42:03 +00:00
|
|
|
Object.assign(CodeMirror.mimeModes['text/css'].colorKeywords, {
|
|
|
|
'darkgrey': true,
|
|
|
|
'darkslategrey': true,
|
|
|
|
'dimgrey': true,
|
|
|
|
'lightgrey': true,
|
|
|
|
'lightslategrey': true,
|
|
|
|
'slategrey': true,
|
|
|
|
});
|
2017-11-23 09:51:16 +00:00
|
|
|
|
2017-09-24 08:54:21 +00:00
|
|
|
const MODE = {
|
2018-04-19 08:12:23 +00:00
|
|
|
less: {
|
|
|
|
family: 'css',
|
|
|
|
value: 'text/x-less',
|
|
|
|
isActive: cm =>
|
|
|
|
cm.doc.mode &&
|
|
|
|
cm.doc.mode.name === 'css' &&
|
|
|
|
cm.doc.mode.helperType === 'less',
|
|
|
|
},
|
2017-09-24 08:54:21 +00:00
|
|
|
stylus: 'stylus',
|
|
|
|
uso: 'css'
|
|
|
|
};
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
CodeMirror.defineExtension('setPreprocessor', function (preprocessor, force = false) {
|
2017-11-22 00:38:29 +00:00
|
|
|
const mode = MODE[preprocessor] || 'css';
|
2018-04-19 08:12:23 +00:00
|
|
|
const isActive = mode.isActive || (
|
|
|
|
cm => cm.doc.mode === mode ||
|
|
|
|
cm.doc.mode && (cm.doc.mode.name + (cm.doc.mode.helperType || '') === mode)
|
|
|
|
);
|
|
|
|
if (!force && isActive(this)) {
|
2017-11-26 21:45:21 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2018-04-19 08:12:23 +00:00
|
|
|
if ((mode.family || mode) === 'css') {
|
|
|
|
// css.js is always loaded via html
|
|
|
|
this.setOption('mode', mode.value || mode);
|
2017-11-26 21:45:21 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return loadScript(`/vendor/codemirror/mode/${mode}/${mode}.js`).then(() => {
|
2017-11-22 00:38:29 +00:00
|
|
|
this.setOption('mode', mode);
|
|
|
|
});
|
2017-11-01 03:00:41 +00:00
|
|
|
});
|
2017-11-14 23:50:53 +00:00
|
|
|
|
|
|
|
CodeMirror.defineExtension('isBlank', function () {
|
|
|
|
// superfast checking as it runs only until the first non-blank line
|
|
|
|
let isBlank = true;
|
|
|
|
this.doc.eachLine(line => {
|
|
|
|
if (line.text && line.text.trim()) {
|
|
|
|
isBlank = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return isBlank;
|
|
|
|
});
|
2017-12-22 13:23:20 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
// editor commands
|
|
|
|
for (const name of ['save', 'toggleStyle', 'nextEditor', 'prevEditor']) {
|
2018-11-21 15:47:28 +00:00
|
|
|
CodeMirror.commands[name] = (...args) => editor[name](...args);
|
2018-04-18 16:33:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:54:49 +00:00
|
|
|
const elBookmark = document.createElement('div');
|
|
|
|
elBookmark.className = CM_BOOKMARK;
|
|
|
|
elBookmark.textContent = '\u00A0';
|
|
|
|
const clearMarker = function () {
|
|
|
|
const line = this.lines[0];
|
|
|
|
CodeMirror.TextMarker.prototype.clear.apply(this);
|
|
|
|
if (!line.markedSpans.some(span => span.marker.sublimeBookmark)) {
|
|
|
|
this.doc.setGutterMarker(line, CM_BOOKMARK_GUTTER, null);
|
2019-09-25 08:44:33 +00:00
|
|
|
}
|
|
|
|
};
|
2020-06-01 04:54:49 +00:00
|
|
|
const {markText} = CodeMirror.prototype;
|
|
|
|
Object.assign(CodeMirror.prototype, {
|
|
|
|
markText() {
|
|
|
|
const marker = markText.apply(this, arguments);
|
|
|
|
if (marker.sublimeBookmark) {
|
|
|
|
this.doc.setGutterMarker(marker.lines[0], CM_BOOKMARK_GUTTER, elBookmark.cloneNode(true));
|
|
|
|
marker.clear = clearMarker;
|
|
|
|
}
|
|
|
|
return marker;
|
|
|
|
},
|
|
|
|
});
|
2019-09-25 08:44:33 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
// CodeMirror convenience commands
|
|
|
|
Object.assign(CodeMirror.commands, {
|
|
|
|
toggleEditorFocus,
|
|
|
|
jumpToLine,
|
|
|
|
commentSelection,
|
|
|
|
});
|
2017-12-22 13:23:20 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function jumpToLine(cm) {
|
|
|
|
const cur = cm.getCursor();
|
|
|
|
const oldDialog = $('.CodeMirror-dialog', cm.display.wrapper);
|
|
|
|
if (oldDialog) {
|
|
|
|
// close the currently opened minidialog
|
|
|
|
cm.focus();
|
2017-12-22 13:23:20 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
// make sure to focus the input in newly opened minidialog
|
|
|
|
// setTimeout(() => {
|
|
|
|
// $('.CodeMirror-dialog', section).focus();
|
|
|
|
// });
|
|
|
|
cm.openDialog(template.jumpToLine.cloneNode(true), str => {
|
|
|
|
const m = str.match(/^\s*(\d+)(?:\s*:\s*(\d+))?\s*$/);
|
|
|
|
if (m) {
|
|
|
|
cm.setCursor(m[1] - 1, m[2] ? m[2] - 1 : cur.ch);
|
|
|
|
}
|
|
|
|
}, {value: cur.line + 1});
|
|
|
|
}
|
2017-12-23 01:42:20 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function commentSelection(cm) {
|
|
|
|
cm.blockComment(cm.getCursor('from'), cm.getCursor('to'), {fullLines: false});
|
|
|
|
}
|
2017-12-22 13:23:20 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function toggleEditorFocus(cm) {
|
|
|
|
if (!cm) return;
|
|
|
|
if (cm.hasFocus()) {
|
|
|
|
setTimeout(() => cm.display.input.blur());
|
|
|
|
} else {
|
|
|
|
cm.focus();
|
|
|
|
}
|
2017-12-22 13:23:20 +00:00
|
|
|
}
|
2017-09-24 08:54:21 +00:00
|
|
|
})();
|
2017-11-23 13:28:55 +00:00
|
|
|
|
2017-11-25 01:29:56 +00:00
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
CodeMirror.hint && (() => {
|
2017-11-23 13:28:55 +00:00
|
|
|
const USO_VAR = 'uso-variable';
|
|
|
|
const USO_VALID_VAR = 'variable-3 ' + USO_VAR;
|
|
|
|
const USO_INVALID_VAR = 'error ' + USO_VAR;
|
2018-02-28 02:15:28 +00:00
|
|
|
const RX_IMPORTANT = /(i(m(p(o(r(t(a(nt?)?)?)?)?)?)?)?)?(?=\b|\W|$)/iy;
|
2018-04-04 08:37:39 +00:00
|
|
|
const RX_VAR_KEYWORD = /(^|[^-\w\u0080-\uFFFF])var\(/iy;
|
|
|
|
const RX_END_OF_VAR = /[\s,)]|$/g;
|
2020-07-14 20:25:19 +00:00
|
|
|
const RX_CONSUME_PROP = /[-\w]*\s*:\s?|$/y;
|
2017-11-23 13:28:55 +00:00
|
|
|
|
2017-11-25 01:29:56 +00:00
|
|
|
const originalHelper = CodeMirror.hint.css || (() => {});
|
2018-04-03 18:39:09 +00:00
|
|
|
const helper = cm => {
|
2018-01-14 13:16:31 +00:00
|
|
|
const pos = cm.getCursor();
|
|
|
|
const {line, ch} = pos;
|
2017-11-23 13:28:55 +00:00
|
|
|
const {styles, text} = cm.getLineHandle(line);
|
2018-01-14 13:16:31 +00:00
|
|
|
if (!styles) return originalHelper(cm);
|
|
|
|
const {style, index} = cm.getStyleAtPos({styles, pos: ch}) || {};
|
|
|
|
if (style && (style.startsWith('comment') || style.startsWith('string'))) {
|
2017-11-25 01:29:56 +00:00
|
|
|
return originalHelper(cm);
|
2017-11-23 13:28:55 +00:00
|
|
|
}
|
2018-04-03 18:39:09 +00:00
|
|
|
|
|
|
|
// !important
|
2018-02-28 02:15:28 +00:00
|
|
|
if (text[ch - 1] === '!' && /i|\W|^$/i.test(text[ch] || '')) {
|
2018-01-14 13:16:31 +00:00
|
|
|
RX_IMPORTANT.lastIndex = ch;
|
|
|
|
return {
|
|
|
|
list: ['important'],
|
|
|
|
from: pos,
|
|
|
|
to: {line, ch: ch + RX_IMPORTANT.exec(text)[0].length},
|
|
|
|
};
|
|
|
|
}
|
2018-04-03 18:39:09 +00:00
|
|
|
|
2018-01-14 13:16:31 +00:00
|
|
|
let prev = index > 2 ? styles[index - 2] : 0;
|
|
|
|
let end = styles[index];
|
2018-04-03 18:39:09 +00:00
|
|
|
|
|
|
|
// #hex colors
|
2018-01-14 13:16:31 +00:00
|
|
|
if (text[prev] === '#') {
|
|
|
|
return {list: [], from: pos, to: pos};
|
|
|
|
}
|
2018-04-03 18:39:09 +00:00
|
|
|
|
|
|
|
// adjust cursor position for /*[[ and ]]*/
|
2018-01-14 13:16:31 +00:00
|
|
|
const adjust = text[prev] === '/' ? 4 : 0;
|
|
|
|
prev += adjust;
|
|
|
|
end -= adjust;
|
|
|
|
const leftPart = text.slice(prev, ch);
|
2018-04-03 18:39:09 +00:00
|
|
|
|
|
|
|
// --css-variables
|
|
|
|
const startsWithDoubleDash = text[prev] === '-' && text[prev + 1] === '-';
|
|
|
|
if (startsWithDoubleDash ||
|
2018-04-04 08:37:39 +00:00
|
|
|
leftPart === '(' && testAt(RX_VAR_KEYWORD, Math.max(0, prev - 4), text)) {
|
2018-04-03 18:39:09 +00:00
|
|
|
// simplified regex without CSS escapes
|
|
|
|
const RX_CSS_VAR = new RegExp(
|
|
|
|
'(?:^|[\\s/;{])(' +
|
|
|
|
(leftPart.startsWith('--') ? leftPart : '--') +
|
|
|
|
(leftPart.length <= 2 ? '[a-zA-Z_\u0080-\uFFFF]' : '') +
|
|
|
|
'[-0-9a-zA-Z_\u0080-\uFFFF]*)',
|
|
|
|
'gm');
|
|
|
|
const cursor = cm.getSearchCursor(RX_CSS_VAR, null, {caseFold: false, multiline: false});
|
|
|
|
const list = new Set();
|
|
|
|
while (cursor.findNext()) {
|
|
|
|
list.add(cursor.pos.match[1]);
|
|
|
|
}
|
|
|
|
if (!startsWithDoubleDash) {
|
|
|
|
prev++;
|
|
|
|
}
|
2018-04-04 08:37:39 +00:00
|
|
|
RX_END_OF_VAR.lastIndex = prev;
|
|
|
|
end = RX_END_OF_VAR.exec(text).index;
|
2018-04-03 18:39:09 +00:00
|
|
|
return {
|
|
|
|
list: [...list.keys()].sort(),
|
|
|
|
from: {line, ch: prev},
|
|
|
|
to: {line, ch: end},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!editor || !style || !style.includes(USO_VAR)) {
|
2020-05-01 05:52:18 +00:00
|
|
|
// add ":" after a property name
|
|
|
|
const res = originalHelper(cm);
|
|
|
|
const state = res && cm.getTokenAt(pos).state.state;
|
|
|
|
if (state === 'block' || state === 'maybeprop') {
|
|
|
|
res.list = res.list.map(str => str + ': ');
|
2020-07-14 20:25:19 +00:00
|
|
|
RX_CONSUME_PROP.lastIndex = res.to.ch;
|
|
|
|
res.to.ch += RX_CONSUME_PROP.exec(text)[0].length;
|
2020-05-01 05:52:18 +00:00
|
|
|
}
|
|
|
|
return res;
|
2018-04-03 18:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// USO vars in usercss mode editor
|
2018-11-07 06:09:29 +00:00
|
|
|
const vars = editor.getStyle().usercssData.vars;
|
|
|
|
const list = vars ?
|
|
|
|
Object.keys(vars).filter(name => name.startsWith(leftPart)) : [];
|
2018-01-14 13:16:31 +00:00
|
|
|
return {
|
|
|
|
list,
|
|
|
|
from: {line, ch: prev},
|
|
|
|
to: {line, ch: end},
|
|
|
|
};
|
2018-04-03 18:39:09 +00:00
|
|
|
};
|
|
|
|
CodeMirror.registerHelper('hint', 'css', helper);
|
|
|
|
CodeMirror.registerHelper('hint', 'stylus', helper);
|
2017-11-23 13:28:55 +00:00
|
|
|
|
|
|
|
const hooks = CodeMirror.mimeModes['text/css'].tokenHooks;
|
|
|
|
const originalCommentHook = hooks['/'];
|
|
|
|
hooks['/'] = tokenizeUsoVariables;
|
|
|
|
|
|
|
|
function tokenizeUsoVariables(stream) {
|
|
|
|
const token = originalCommentHook.apply(this, arguments);
|
|
|
|
if (token[1] !== 'comment') {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
const {string, start, pos} = stream;
|
|
|
|
// /*[[install-key]]*/
|
|
|
|
// 01234 43210
|
|
|
|
if (string[start + 2] === '[' &&
|
|
|
|
string[start + 3] === '[' &&
|
|
|
|
string[pos - 3] === ']' &&
|
|
|
|
string[pos - 4] === ']') {
|
2017-11-29 10:21:31 +00:00
|
|
|
const vars = typeof editor !== 'undefined' && (editor.getStyle().usercssData || {}).vars;
|
2017-12-06 03:29:54 +00:00
|
|
|
const name = vars && string.slice(start + 4, pos - 4);
|
|
|
|
if (vars && Object.hasOwnProperty.call(vars, name.endsWith('-rgb') ? name.slice(0, -4) : name)) {
|
2017-11-23 13:28:55 +00:00
|
|
|
token[0] = USO_VALID_VAR;
|
|
|
|
} else {
|
|
|
|
token[0] = USO_INVALID_VAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
2018-04-04 08:37:39 +00:00
|
|
|
|
|
|
|
function testAt(rx, index, text) {
|
|
|
|
if (!rx) return false;
|
|
|
|
rx.lastIndex = index;
|
|
|
|
return rx.test(text);
|
|
|
|
}
|
2017-11-23 13:28:55 +00:00
|
|
|
})();
|