show sublime bookmarks (#951)

* remove redundant setGutterMarker optimization

* show sublime bookmarks
This commit is contained in:
tophf 2020-06-01 07:54:49 +03:00 committed by GitHub
parent 39c62e684e
commit a1b0eb7df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View File

@ -18,6 +18,12 @@
outline: -webkit-focus-ring-color auto 5px; outline: -webkit-focus-ring-color auto 5px;
outline-offset: -2px; outline-offset: -2px;
} }
.CodeMirror-bookmark {
background: linear-gradient(to right, currentColor, transparent);
position: absolute;
width: 2em;
opacity: .5;
}
@supports (-moz-appearance:none) { @supports (-moz-appearance:none) {
/* restrict to FF */ /* restrict to FF */
.CodeMirror-focused { .CodeMirror-focused {

View File

@ -8,6 +8,8 @@
prefs.reset('editor.keyMap'); prefs.reset('editor.keyMap');
} }
const CM_BOOKMARK = 'CodeMirror-bookmark';
const CM_BOOKMARK_GUTTER = CM_BOOKMARK + 'gutter';
const defaults = { const defaults = {
autoCloseBrackets: prefs.get('editor.autoCloseBrackets'), autoCloseBrackets: prefs.get('editor.autoCloseBrackets'),
mode: 'css', mode: 'css',
@ -15,6 +17,7 @@
lineWrapping: prefs.get('editor.lineWrapping'), lineWrapping: prefs.get('editor.lineWrapping'),
foldGutter: true, foldGutter: true,
gutters: [ gutters: [
CM_BOOKMARK_GUTTER,
'CodeMirror-linenumbers', 'CodeMirror-linenumbers',
'CodeMirror-foldgutter', 'CodeMirror-foldgutter',
...(prefs.get('editor.linter') ? ['CodeMirror-lint-markers'] : []), ...(prefs.get('editor.linter') ? ['CodeMirror-lint-markers'] : []),
@ -242,22 +245,27 @@
CodeMirror.commands[name] = (...args) => editor[name](...args); CodeMirror.commands[name] = (...args) => editor[name](...args);
} }
// speedup: reuse the old folding marks const elBookmark = document.createElement('div');
// TODO: remove when https://github.com/codemirror/CodeMirror/pull/6010 is shipped in /vendor elBookmark.className = CM_BOOKMARK;
const {setGutterMarker} = CodeMirror.prototype; elBookmark.textContent = '\u00A0';
CodeMirror.prototype.setGutterMarker = function (line, gutterID, value) { const clearMarker = function () {
const o = this.state.foldGutter.options; const line = this.lines[0];
if (typeof o.indicatorOpen === 'string' || CodeMirror.TextMarker.prototype.clear.apply(this);
typeof o.indicatorFolded === 'string') { if (!line.markedSpans.some(span => span.marker.sublimeBookmark)) {
const old = line.gutterMarkers && line.gutterMarkers[gutterID]; this.doc.setGutterMarker(line, CM_BOOKMARK_GUTTER, null);
// old className can contain other names set by CodeMirror so we'll use classList
if (old && value && old.classList.contains(value.className) ||
!old && !value) {
return line;
}
} }
return setGutterMarker.apply(this, arguments);
}; };
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;
},
});
// CodeMirror convenience commands // CodeMirror convenience commands
Object.assign(CodeMirror.commands, { Object.assign(CodeMirror.commands, {