From a1b0eb7df1f05c0a9e2ee0ecf204a5ab192a219a Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 1 Jun 2020 07:54:49 +0300 Subject: [PATCH] show sublime bookmarks (#951) * remove redundant setGutterMarker optimization * show sublime bookmarks --- edit/codemirror-default.css | 6 ++++++ edit/codemirror-default.js | 36 ++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/edit/codemirror-default.css b/edit/codemirror-default.css index 61dd10f2..6adbf962 100644 --- a/edit/codemirror-default.css +++ b/edit/codemirror-default.css @@ -18,6 +18,12 @@ outline: -webkit-focus-ring-color auto 5px; outline-offset: -2px; } +.CodeMirror-bookmark { + background: linear-gradient(to right, currentColor, transparent); + position: absolute; + width: 2em; + opacity: .5; +} @supports (-moz-appearance:none) { /* restrict to FF */ .CodeMirror-focused { diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index 3f8afc6d..4b43b378 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -8,6 +8,8 @@ prefs.reset('editor.keyMap'); } + const CM_BOOKMARK = 'CodeMirror-bookmark'; + const CM_BOOKMARK_GUTTER = CM_BOOKMARK + 'gutter'; const defaults = { autoCloseBrackets: prefs.get('editor.autoCloseBrackets'), mode: 'css', @@ -15,6 +17,7 @@ lineWrapping: prefs.get('editor.lineWrapping'), foldGutter: true, gutters: [ + CM_BOOKMARK_GUTTER, 'CodeMirror-linenumbers', 'CodeMirror-foldgutter', ...(prefs.get('editor.linter') ? ['CodeMirror-lint-markers'] : []), @@ -242,22 +245,27 @@ CodeMirror.commands[name] = (...args) => editor[name](...args); } - // speedup: reuse the old folding marks - // TODO: remove when https://github.com/codemirror/CodeMirror/pull/6010 is shipped in /vendor - const {setGutterMarker} = CodeMirror.prototype; - CodeMirror.prototype.setGutterMarker = function (line, gutterID, value) { - const o = this.state.foldGutter.options; - if (typeof o.indicatorOpen === 'string' || - typeof o.indicatorFolded === 'string') { - const old = line.gutterMarkers && line.gutterMarkers[gutterID]; - // 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; - } + 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); } - 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 Object.assign(CodeMirror.commands, {