Bug 1463674 - Add shadow autocompletion text capability to the source editor; r=bgrins. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Thu, 05 Jul 2018 16:18:53 +0200
changeset 818819 aa728e3ee3254e2ef7eb1768e3f8621decca0535
parent 818818 4d57be89965691948609c9037ef60d8850cea8af
child 818820 d35caaf19b14d9a5cfbddaf58d20bc6c7aeb4aaa
push id116354
push userbmo:nchevobbe@mozilla.com
push dateMon, 16 Jul 2018 16:01:04 +0000
reviewersbgrins
bugs1463674
milestone63.0a1
Bug 1463674 - Add shadow autocompletion text capability to the source editor; r=bgrins. This functionnality is used in the webconsole to display to the user what will be inserted if they hit Tab. Since CodeMirror does not provide such feature, we take advantage of markText to put the autocompletion text in a title attribute and then display it using a CSS after pseudo element. This way, we don't have to run any complex computation for positioning nor styling. MozReview-Commit-ID: 1tFdlR51418
devtools/client/sourceeditor/editor.js
devtools/client/themes/webconsole.css
--- a/devtools/client/sourceeditor/editor.js
+++ b/devtools/client/sourceeditor/editor.js
@@ -1267,16 +1267,32 @@ Editor.prototype = {
 
     if (this.config.autocomplete && Services.prefs.getBoolPref(AUTOCOMPLETE)) {
       this.initializeAutoCompletion(this.config.autocompleteOpts);
     } else {
       this.destroyAutoCompletion();
     }
   },
 
+  setAutoCompletionText: function(text) {
+    const cursor = this.getCursor();
+    const cm = editors.get(this);
+    const className = "cm-auto-complete-shadow-text";
+
+    cm.getAllMarks().forEach(mark => {
+      if (mark.className === className) {
+        mark.clear();
+      }
+    });
+
+    if (text) {
+      cm.markText({...cursor, ch: cursor.ch - 1}, cursor, { className, title: text });
+    }
+  },
+
   /**
    * Extends an instance of the Editor object with additional
    * functions. Each function will be called with context as
    * the first argument. Context is a {ed, cm} object where
    * 'ed' is an instance of the Editor object and 'cm' is an
    * instance of the CodeMirror object. Example:
    *
    * function hello(ctx, name) {
--- a/devtools/client/themes/webconsole.css
+++ b/devtools/client/themes/webconsole.css
@@ -377,16 +377,21 @@ textarea.jsterm-input-node:focus {
   border-radius: 0 0 4px 4px;
 }
 
 /* Unset the bottom right radius on the jsterm inputs when the sidebar is visible */
 :root[platform="mac"]  .jsterm-cm .sidebar ~ .jsterm-input-container > .CodeMirror {
   border-bottom-right-radius: 0;
 }
 
+.jsterm-cm .cm-auto-complete-shadow-text::after {
+  content: attr(title);
+  color: var(--theme-comment);
+}
+
 /* Security styles */
 
 .message.security > .indent {
   border-inline-end: solid red 6px;
 }
 
 .message.security.error > .icon::before {
   background-position: -12px -48px;