forked from mirrors/gecko-dev
Differential Revision: https://phabricator.services.mozilla.com/D19641 --HG-- rename : devtools/client/sourceeditor/.eslintrc.js => devtools/client/shared/sourceeditor/.eslintrc.js rename : devtools/client/sourceeditor/README => devtools/client/shared/sourceeditor/README rename : devtools/client/sourceeditor/autocomplete.js => devtools/client/shared/sourceeditor/autocomplete.js rename : devtools/client/sourceeditor/codemirror/LICENSE => devtools/client/shared/sourceeditor/codemirror/LICENSE rename : devtools/client/sourceeditor/codemirror/addon/comment/comment.js => devtools/client/shared/sourceeditor/codemirror/addon/comment/comment.js rename : devtools/client/sourceeditor/codemirror/addon/comment/continuecomment.js => devtools/client/shared/sourceeditor/codemirror/addon/comment/continuecomment.js rename : devtools/client/sourceeditor/codemirror/addon/dialog/dialog.css => devtools/client/shared/sourceeditor/codemirror/addon/dialog/dialog.css rename : devtools/client/sourceeditor/codemirror/addon/dialog/dialog.js => devtools/client/shared/sourceeditor/codemirror/addon/dialog/dialog.js rename : devtools/client/sourceeditor/codemirror/addon/edit/closebrackets.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/closebrackets.js rename : devtools/client/sourceeditor/codemirror/addon/edit/closetag.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/closetag.js rename : devtools/client/sourceeditor/codemirror/addon/edit/continuelist.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/continuelist.js rename : devtools/client/sourceeditor/codemirror/addon/edit/matchbrackets.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/matchbrackets.js rename : devtools/client/sourceeditor/codemirror/addon/edit/matchtags.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/matchtags.js rename : devtools/client/sourceeditor/codemirror/addon/edit/trailingspace.js => devtools/client/shared/sourceeditor/codemirror/addon/edit/trailingspace.js rename : devtools/client/sourceeditor/codemirror/addon/fold/brace-fold.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/brace-fold.js rename : devtools/client/sourceeditor/codemirror/addon/fold/comment-fold.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/comment-fold.js rename : devtools/client/sourceeditor/codemirror/addon/fold/foldcode.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/foldcode.js rename : devtools/client/sourceeditor/codemirror/addon/fold/foldgutter.css => devtools/client/shared/sourceeditor/codemirror/addon/fold/foldgutter.css rename : devtools/client/sourceeditor/codemirror/addon/fold/foldgutter.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/foldgutter.js rename : devtools/client/sourceeditor/codemirror/addon/fold/indent-fold.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/indent-fold.js rename : devtools/client/sourceeditor/codemirror/addon/fold/markdown-fold.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/markdown-fold.js rename : devtools/client/sourceeditor/codemirror/addon/fold/xml-fold.js => devtools/client/shared/sourceeditor/codemirror/addon/fold/xml-fold.js rename : devtools/client/sourceeditor/codemirror/addon/hint/show-hint.js => devtools/client/shared/sourceeditor/codemirror/addon/hint/show-hint.js rename : devtools/client/sourceeditor/codemirror/addon/runmode/runmode.js => devtools/client/shared/sourceeditor/codemirror/addon/runmode/runmode.js rename : devtools/client/sourceeditor/codemirror/addon/search/match-highlighter.js => devtools/client/shared/sourceeditor/codemirror/addon/search/match-highlighter.js rename : devtools/client/sourceeditor/codemirror/addon/search/search.js => devtools/client/shared/sourceeditor/codemirror/addon/search/search.js rename : devtools/client/sourceeditor/codemirror/addon/search/searchcursor.js => devtools/client/shared/sourceeditor/codemirror/addon/search/searchcursor.js rename : devtools/client/sourceeditor/codemirror/addon/selection/active-line.js => devtools/client/shared/sourceeditor/codemirror/addon/selection/active-line.js rename : devtools/client/sourceeditor/codemirror/addon/selection/mark-selection.js => devtools/client/shared/sourceeditor/codemirror/addon/selection/mark-selection.js rename : devtools/client/sourceeditor/codemirror/addon/tern/tern.css => devtools/client/shared/sourceeditor/codemirror/addon/tern/tern.css rename : devtools/client/sourceeditor/codemirror/addon/tern/tern.js => devtools/client/shared/sourceeditor/codemirror/addon/tern/tern.js rename : devtools/client/sourceeditor/codemirror/cmiframe.html => devtools/client/shared/sourceeditor/codemirror/cmiframe.html rename : devtools/client/sourceeditor/codemirror/codemirror.bundle.js => devtools/client/shared/sourceeditor/codemirror/codemirror.bundle.js rename : devtools/client/sourceeditor/codemirror/keymap/emacs.js => devtools/client/shared/sourceeditor/codemirror/keymap/emacs.js rename : devtools/client/sourceeditor/codemirror/keymap/sublime.js => devtools/client/shared/sourceeditor/codemirror/keymap/sublime.js rename : devtools/client/sourceeditor/codemirror/keymap/vim.js => devtools/client/shared/sourceeditor/codemirror/keymap/vim.js rename : devtools/client/sourceeditor/codemirror/lib/codemirror.css => devtools/client/shared/sourceeditor/codemirror/lib/codemirror.css rename : devtools/client/sourceeditor/codemirror/lib/codemirror.js => devtools/client/shared/sourceeditor/codemirror/lib/codemirror.js rename : devtools/client/sourceeditor/codemirror/mode/clike/clike.js => devtools/client/shared/sourceeditor/codemirror/mode/clike/clike.js rename : devtools/client/sourceeditor/codemirror/mode/clojure/clojure.js => devtools/client/shared/sourceeditor/codemirror/mode/clojure/clojure.js rename : devtools/client/sourceeditor/codemirror/mode/coffeescript/coffeescript.js => devtools/client/shared/sourceeditor/codemirror/mode/coffeescript/coffeescript.js rename : devtools/client/sourceeditor/codemirror/mode/css/css.js => devtools/client/shared/sourceeditor/codemirror/mode/css/css.js rename : devtools/client/sourceeditor/codemirror/mode/elm/elm.js => devtools/client/shared/sourceeditor/codemirror/mode/elm/elm.js rename : devtools/client/sourceeditor/codemirror/mode/haxe/haxe.js => devtools/client/shared/sourceeditor/codemirror/mode/haxe/haxe.js rename : devtools/client/sourceeditor/codemirror/mode/htmlmixed/htmlmixed.js => devtools/client/shared/sourceeditor/codemirror/mode/htmlmixed/htmlmixed.js rename : devtools/client/sourceeditor/codemirror/mode/javascript/javascript.js => devtools/client/shared/sourceeditor/codemirror/mode/javascript/javascript.js rename : devtools/client/sourceeditor/codemirror/mode/jsx/jsx.js => devtools/client/shared/sourceeditor/codemirror/mode/jsx/jsx.js rename : devtools/client/sourceeditor/codemirror/mode/wasm/wasm.js => devtools/client/shared/sourceeditor/codemirror/mode/wasm/wasm.js rename : devtools/client/sourceeditor/codemirror/mode/xml/xml.js => devtools/client/shared/sourceeditor/codemirror/mode/xml/xml.js rename : devtools/client/sourceeditor/codemirror/mozilla.css => devtools/client/shared/sourceeditor/codemirror/mozilla.css rename : devtools/client/sourceeditor/css-autocompleter.js => devtools/client/shared/sourceeditor/css-autocompleter.js rename : devtools/client/sourceeditor/debugger.js => devtools/client/shared/sourceeditor/debugger.js rename : devtools/client/sourceeditor/editor-commands-controller.js => devtools/client/shared/sourceeditor/editor-commands-controller.js rename : devtools/client/sourceeditor/editor.js => devtools/client/shared/sourceeditor/editor.js rename : devtools/client/sourceeditor/moz.build => devtools/client/shared/sourceeditor/moz.build rename : devtools/client/sourceeditor/package.json => devtools/client/shared/sourceeditor/package.json rename : devtools/client/sourceeditor/tern/README => devtools/client/shared/sourceeditor/tern/README rename : devtools/client/sourceeditor/tern/browser.js => devtools/client/shared/sourceeditor/tern/browser.js rename : devtools/client/sourceeditor/tern/comment.js => devtools/client/shared/sourceeditor/tern/comment.js rename : devtools/client/sourceeditor/tern/condense.js => devtools/client/shared/sourceeditor/tern/condense.js rename : devtools/client/sourceeditor/tern/def.js => devtools/client/shared/sourceeditor/tern/def.js rename : devtools/client/sourceeditor/tern/ecma5.js => devtools/client/shared/sourceeditor/tern/ecma5.js rename : devtools/client/sourceeditor/tern/infer.js => devtools/client/shared/sourceeditor/tern/infer.js rename : devtools/client/sourceeditor/tern/moz.build => devtools/client/shared/sourceeditor/tern/moz.build rename : devtools/client/sourceeditor/tern/signal.js => devtools/client/shared/sourceeditor/tern/signal.js rename : devtools/client/sourceeditor/tern/tern.js => devtools/client/shared/sourceeditor/tern/tern.js rename : devtools/client/sourceeditor/tern/tests/unit/head_tern.js => devtools/client/shared/sourceeditor/tern/tests/unit/head_tern.js rename : devtools/client/sourceeditor/tern/tests/unit/test_autocompletion.js => devtools/client/shared/sourceeditor/tern/tests/unit/test_autocompletion.js rename : devtools/client/sourceeditor/tern/tests/unit/test_import_tern.js => devtools/client/shared/sourceeditor/tern/tests/unit/test_import_tern.js rename : devtools/client/sourceeditor/tern/tests/unit/xpcshell.ini => devtools/client/shared/sourceeditor/tern/tests/unit/xpcshell.ini rename : devtools/client/sourceeditor/test/.eslintrc.js => devtools/client/shared/sourceeditor/test/.eslintrc.js rename : devtools/client/sourceeditor/test/browser.ini => devtools/client/shared/sourceeditor/test/browser.ini rename : devtools/client/sourceeditor/test/browser_codemirror.js => devtools/client/shared/sourceeditor/test/browser_codemirror.js rename : devtools/client/sourceeditor/test/browser_css_autocompletion.js => devtools/client/shared/sourceeditor/test/browser_css_autocompletion.js rename : devtools/client/sourceeditor/test/browser_css_getInfo.js => devtools/client/shared/sourceeditor/test/browser_css_getInfo.js rename : devtools/client/sourceeditor/test/browser_css_statemachine.js => devtools/client/shared/sourceeditor/test/browser_css_statemachine.js rename : devtools/client/sourceeditor/test/browser_detectindent.js => devtools/client/shared/sourceeditor/test/browser_detectindent.js rename : devtools/client/sourceeditor/test/browser_editor_addons.js => devtools/client/shared/sourceeditor/test/browser_editor_addons.js rename : devtools/client/sourceeditor/test/browser_editor_alt_b_f.js => devtools/client/shared/sourceeditor/test/browser_editor_alt_b_f.js rename : devtools/client/sourceeditor/test/browser_editor_autocomplete_basic.js => devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_basic.js rename : devtools/client/sourceeditor/test/browser_editor_autocomplete_events.js => devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_events.js rename : devtools/client/sourceeditor/test/browser_editor_autocomplete_js.js => devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_js.js rename : devtools/client/sourceeditor/test/browser_editor_basic.js => devtools/client/shared/sourceeditor/test/browser_editor_basic.js rename : devtools/client/sourceeditor/test/browser_editor_cursor.js => devtools/client/shared/sourceeditor/test/browser_editor_cursor.js rename : devtools/client/sourceeditor/test/browser_editor_find_again.js => devtools/client/shared/sourceeditor/test/browser_editor_find_again.js rename : devtools/client/sourceeditor/test/browser_editor_goto_line.js => devtools/client/shared/sourceeditor/test/browser_editor_goto_line.js rename : devtools/client/sourceeditor/test/browser_editor_history.js => devtools/client/shared/sourceeditor/test/browser_editor_history.js rename : devtools/client/sourceeditor/test/browser_editor_markers.js => devtools/client/shared/sourceeditor/test/browser_editor_markers.js rename : devtools/client/sourceeditor/test/browser_editor_movelines.js => devtools/client/shared/sourceeditor/test/browser_editor_movelines.js rename : devtools/client/sourceeditor/test/browser_editor_prefs.js => devtools/client/shared/sourceeditor/test/browser_editor_prefs.js rename : devtools/client/sourceeditor/test/browser_editor_script_injection.js => devtools/client/shared/sourceeditor/test/browser_editor_script_injection.js rename : devtools/client/sourceeditor/test/browser_vimemacs.js => devtools/client/shared/sourceeditor/test/browser_vimemacs.js rename : devtools/client/sourceeditor/test/cm_mode_ruby.js => devtools/client/shared/sourceeditor/test/cm_mode_ruby.js rename : devtools/client/sourceeditor/test/cm_script_injection_test.js => devtools/client/shared/sourceeditor/test/cm_script_injection_test.js rename : devtools/client/sourceeditor/test/codemirror/codemirror.html => devtools/client/shared/sourceeditor/test/codemirror/codemirror.html rename : devtools/client/sourceeditor/test/codemirror/comment_test.js => devtools/client/shared/sourceeditor/test/codemirror/comment_test.js rename : devtools/client/sourceeditor/test/codemirror/doc_test.js => devtools/client/shared/sourceeditor/test/codemirror/doc_test.js rename : devtools/client/sourceeditor/test/codemirror/driver.js => devtools/client/shared/sourceeditor/test/codemirror/driver.js rename : devtools/client/sourceeditor/test/codemirror/emacs_test.js => devtools/client/shared/sourceeditor/test/codemirror/emacs_test.js rename : devtools/client/sourceeditor/test/codemirror/mode/javascript/test.js => devtools/client/shared/sourceeditor/test/codemirror/mode/javascript/test.js rename : devtools/client/sourceeditor/test/codemirror/mode_test.css => devtools/client/shared/sourceeditor/test/codemirror/mode_test.css rename : devtools/client/sourceeditor/test/codemirror/mode_test.js => devtools/client/shared/sourceeditor/test/codemirror/mode_test.js rename : devtools/client/sourceeditor/test/codemirror/multi_test.js => devtools/client/shared/sourceeditor/test/codemirror/multi_test.js rename : devtools/client/sourceeditor/test/codemirror/search_test.js => devtools/client/shared/sourceeditor/test/codemirror/search_test.js rename : devtools/client/sourceeditor/test/codemirror/sublime_test.js => devtools/client/shared/sourceeditor/test/codemirror/sublime_test.js rename : devtools/client/sourceeditor/test/codemirror/test.js => devtools/client/shared/sourceeditor/test/codemirror/test.js rename : devtools/client/sourceeditor/test/codemirror/vim_test.js => devtools/client/shared/sourceeditor/test/codemirror/vim_test.js rename : devtools/client/sourceeditor/test/codemirror/vimemacs.html => devtools/client/shared/sourceeditor/test/codemirror/vimemacs.html rename : devtools/client/sourceeditor/test/css_autocompletion_tests.json => devtools/client/shared/sourceeditor/test/css_autocompletion_tests.json rename : devtools/client/sourceeditor/test/css_statemachine_testcases.css => devtools/client/shared/sourceeditor/test/css_statemachine_testcases.css rename : devtools/client/sourceeditor/test/css_statemachine_tests.json => devtools/client/shared/sourceeditor/test/css_statemachine_tests.json rename : devtools/client/sourceeditor/test/head.js => devtools/client/shared/sourceeditor/test/head.js rename : devtools/client/sourceeditor/test/head.xul => devtools/client/shared/sourceeditor/test/head.xul rename : devtools/client/sourceeditor/test/helper_codemirror_runner.js => devtools/client/shared/sourceeditor/test/helper_codemirror_runner.js rename : devtools/client/sourceeditor/wasm.js => devtools/client/shared/sourceeditor/wasm.js rename : devtools/client/sourceeditor/webpack.config.js => devtools/client/shared/sourceeditor/webpack.config.js extra : moz-landing-system : lando
1545 lines
45 KiB
JavaScript
1545 lines
45 KiB
JavaScript
/* -*- indent-tabs-mode: nil; js-indent-level: 2; fill-column: 80 -*- */
|
|
/* vim:set ts=2 sw=2 sts=2 et tw=80:
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
const {
|
|
EXPAND_TAB,
|
|
TAB_SIZE,
|
|
DETECT_INDENT,
|
|
getIndentationFromIteration,
|
|
} = require("devtools/shared/indentation");
|
|
|
|
const ENABLE_CODE_FOLDING = "devtools.editor.enableCodeFolding";
|
|
const KEYMAP = "devtools.editor.keymap";
|
|
const AUTO_CLOSE = "devtools.editor.autoclosebrackets";
|
|
const AUTOCOMPLETE = "devtools.editor.autocomplete";
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
const VALID_KEYMAPS = new Set(["emacs", "vim", "sublime"]);
|
|
|
|
// Maximum allowed margin (in number of lines) from top or bottom of the editor
|
|
// while shifting to a line which was initially out of view.
|
|
const MAX_VERTICAL_OFFSET = 3;
|
|
|
|
// Match @Scratchpad/N:LINE[:COLUMN] or (LINE[:COLUMN]) anywhere at an end of
|
|
// line in text selection.
|
|
const RE_SCRATCHPAD_ERROR = /(?:@Scratchpad\/\d+:|\()(\d+):?(\d+)?(?:\)|\n)/;
|
|
const RE_JUMP_TO_LINE = /^(\d+):?(\d+)?/;
|
|
const AUTOCOMPLETE_MARK_CLASSNAME = "cm-auto-complete-shadow-text";
|
|
|
|
const Services = require("Services");
|
|
const EventEmitter = require("devtools/shared/event-emitter");
|
|
const { PrefObserver } = require("devtools/client/shared/prefs");
|
|
const { getClientCssProperties } = require("devtools/shared/fronts/css-properties");
|
|
const KeyShortcuts = require("devtools/client/shared/key-shortcuts");
|
|
|
|
const {LocalizationHelper} = require("devtools/shared/l10n");
|
|
const L10N = new LocalizationHelper("devtools/client/locales/sourceeditor.properties");
|
|
|
|
const {
|
|
getWasmText,
|
|
isWasm,
|
|
lineToWasmOffset,
|
|
wasmOffsetToLine,
|
|
} = require("./wasm");
|
|
|
|
const { OS } = Services.appinfo;
|
|
|
|
// CM_SCRIPTS and CM_IFRAME represent the HTML and JavaScript that is
|
|
// injected into an iframe in order to initialize a CodeMirror instance.
|
|
|
|
const CM_SCRIPTS = [
|
|
"chrome://devtools/content/shared/sourceeditor/codemirror/codemirror.bundle.js",
|
|
];
|
|
|
|
const CM_IFRAME = "chrome://devtools/content/shared/sourceeditor/codemirror/cmiframe.html";
|
|
|
|
const CM_MAPPING = [
|
|
"clearHistory",
|
|
"defaultCharWidth",
|
|
"extendSelection",
|
|
"focus",
|
|
"getCursor",
|
|
"getLine",
|
|
"getScrollInfo",
|
|
"getSelection",
|
|
"getViewport",
|
|
"hasFocus",
|
|
"lineCount",
|
|
"openDialog",
|
|
"redo",
|
|
"refresh",
|
|
"replaceSelection",
|
|
"setSelection",
|
|
"somethingSelected",
|
|
"undo",
|
|
];
|
|
|
|
const editors = new WeakMap();
|
|
|
|
Editor.modes = {
|
|
cljs: { name: "text/x-clojure" },
|
|
css: { name: "css" },
|
|
fs: { name: "x-shader/x-fragment" },
|
|
haxe: { name: "haxe" },
|
|
html: { name: "htmlmixed" },
|
|
js: { name: "javascript" },
|
|
text: { name: "text" },
|
|
vs: { name: "x-shader/x-vertex" },
|
|
wasm: { name: "wasm" },
|
|
};
|
|
|
|
/**
|
|
* A very thin wrapper around CodeMirror. Provides a number
|
|
* of helper methods to make our use of CodeMirror easier and
|
|
* another method, appendTo, to actually create and append
|
|
* the CodeMirror instance.
|
|
*
|
|
* Note that Editor doesn't expose CodeMirror instance to the
|
|
* outside world.
|
|
*
|
|
* Constructor accepts one argument, config. It is very
|
|
* similar to the CodeMirror configuration object so for most
|
|
* properties go to CodeMirror's documentation (see below).
|
|
*
|
|
* Other than that, it accepts one additional and optional
|
|
* property contextMenu. This property should be an element, or
|
|
* an ID of an element that we can use as a context menu.
|
|
*
|
|
* This object is also an event emitter.
|
|
*
|
|
* CodeMirror docs: http://codemirror.net/doc/manual.html
|
|
*/
|
|
function Editor(config) {
|
|
const tabSize = Services.prefs.getIntPref(TAB_SIZE);
|
|
const useTabs = !Services.prefs.getBoolPref(EXPAND_TAB);
|
|
const useAutoClose = Services.prefs.getBoolPref(AUTO_CLOSE);
|
|
|
|
this.version = null;
|
|
this.config = {
|
|
value: "",
|
|
mode: Editor.modes.text,
|
|
indentUnit: tabSize,
|
|
tabSize: tabSize,
|
|
contextMenu: null,
|
|
matchBrackets: true,
|
|
extraKeys: {},
|
|
indentWithTabs: useTabs,
|
|
inputStyle: "textarea",
|
|
styleActiveLine: true,
|
|
autoCloseBrackets: "()[]{}''\"\"``",
|
|
autoCloseEnabled: useAutoClose,
|
|
theme: "mozilla",
|
|
themeSwitching: true,
|
|
autocomplete: false,
|
|
autocompleteOpts: {},
|
|
// Set to true to prevent the search addon to be activated.
|
|
disableSearchAddon: false,
|
|
};
|
|
|
|
// Additional shortcuts.
|
|
this.config.extraKeys[Editor.keyFor("jumpToLine")] = () => this.jumpToLine();
|
|
this.config.extraKeys[Editor.keyFor("moveLineUp", { noaccel: true })] =
|
|
() => this.moveLineUp();
|
|
this.config.extraKeys[Editor.keyFor("moveLineDown", { noaccel: true })] =
|
|
() => this.moveLineDown();
|
|
this.config.extraKeys[Editor.keyFor("toggleComment")] = "toggleComment";
|
|
|
|
// Disable ctrl-[ and ctrl-] because toolbox uses those shortcuts.
|
|
this.config.extraKeys[Editor.keyFor("indentLess")] = false;
|
|
this.config.extraKeys[Editor.keyFor("indentMore")] = false;
|
|
|
|
// Disable Alt-B and Alt-F to navigate groups (respectively previous and next) since:
|
|
// - it's not standard in input fields
|
|
// - it also inserts a character which feels weird
|
|
this.config.extraKeys["Alt-B"] = false;
|
|
this.config.extraKeys["Alt-F"] = false;
|
|
|
|
// Overwrite default config with user-provided, if needed.
|
|
Object.keys(config).forEach(k => {
|
|
if (k != "extraKeys") {
|
|
this.config[k] = config[k];
|
|
return;
|
|
}
|
|
|
|
if (!config.extraKeys) {
|
|
return;
|
|
}
|
|
|
|
Object.keys(config.extraKeys).forEach(key => {
|
|
this.config.extraKeys[key] = config.extraKeys[key];
|
|
});
|
|
});
|
|
|
|
if (!this.config.gutters) {
|
|
this.config.gutters = [];
|
|
}
|
|
if (this.config.lineNumbers
|
|
&& !this.config.gutters.includes("CodeMirror-linenumbers")) {
|
|
this.config.gutters.push("CodeMirror-linenumbers");
|
|
}
|
|
|
|
// Remember the initial value of autoCloseBrackets.
|
|
this.config.autoCloseBracketsSaved = this.config.autoCloseBrackets;
|
|
|
|
// Overwrite default tab behavior. If something is selected,
|
|
// indent those lines. If nothing is selected and we're
|
|
// indenting with tabs, insert one tab. Otherwise insert N
|
|
// whitespaces where N == indentUnit option.
|
|
this.config.extraKeys.Tab = cm => {
|
|
if (config.extraKeys && config.extraKeys.Tab) {
|
|
// If a consumer registers its own extraKeys.Tab, we execute it before doing
|
|
// anything else. If it returns false, that mean that all the key handling work is
|
|
// done, so we can do an early return.
|
|
const res = config.extraKeys.Tab(cm);
|
|
if (res === false) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (cm.somethingSelected()) {
|
|
cm.indentSelection("add");
|
|
return;
|
|
}
|
|
|
|
if (this.config.indentWithTabs) {
|
|
cm.replaceSelection("\t", "end", "+input");
|
|
return;
|
|
}
|
|
|
|
let num = cm.getOption("indentUnit");
|
|
if (cm.getCursor().ch !== 0) {
|
|
num -= cm.getCursor().ch % num;
|
|
}
|
|
cm.replaceSelection(" ".repeat(num), "end", "+input");
|
|
};
|
|
|
|
// Allow add-ons to inject scripts for their editor instances
|
|
if (!this.config.externalScripts) {
|
|
this.config.externalScripts = [];
|
|
}
|
|
|
|
if (this.config.cssProperties) {
|
|
// Ensure that autocompletion has cssProperties if it's passed in via the options.
|
|
this.config.autocompleteOpts.cssProperties = this.config.cssProperties;
|
|
} else {
|
|
// Use a static client-side database of CSS values if none is provided.
|
|
this.config.cssProperties = getClientCssProperties();
|
|
}
|
|
|
|
EventEmitter.decorate(this);
|
|
}
|
|
|
|
Editor.prototype = {
|
|
container: null,
|
|
version: null,
|
|
config: null,
|
|
Doc: null,
|
|
|
|
/**
|
|
* Exposes the CodeMirror class. We want to be able to
|
|
* invoke static commands such as runMode for syntax highlighting.
|
|
*/
|
|
get CodeMirror() {
|
|
const codeMirror = editors.get(this);
|
|
return codeMirror && codeMirror.constructor;
|
|
},
|
|
|
|
/**
|
|
* Exposes the CodeMirror instance. We want to get away from trying to
|
|
* abstract away the API entirely, and this makes it easier to integrate in
|
|
* various environments and do complex things.
|
|
*/
|
|
get codeMirror() {
|
|
if (!editors.has(this)) {
|
|
throw new Error(
|
|
"CodeMirror instance does not exist. You must wait " +
|
|
"for it to be appended to the DOM."
|
|
);
|
|
}
|
|
return editors.get(this);
|
|
},
|
|
|
|
/**
|
|
* Appends the current Editor instance to the element specified by
|
|
* 'el'. You can also provide your own iframe to host the editor as
|
|
* an optional second parameter. This method actually creates and
|
|
* loads CodeMirror and all its dependencies.
|
|
*
|
|
* This method is asynchronous and returns a promise.
|
|
*/
|
|
appendTo: function(el, env) {
|
|
return new Promise(resolve => {
|
|
const cm = editors.get(this);
|
|
|
|
if (!env) {
|
|
env = el.ownerDocument.createElementNS(el.namespaceURI, "iframe");
|
|
|
|
if (el.namespaceURI === XUL_NS) {
|
|
env.flex = 1;
|
|
}
|
|
}
|
|
|
|
if (cm) {
|
|
throw new Error("You can append an editor only once.");
|
|
}
|
|
|
|
const onLoad = () => {
|
|
const win = env.contentWindow.wrappedJSObject;
|
|
|
|
if (!this.config.themeSwitching) {
|
|
win.document.documentElement.setAttribute("force-theme", "light");
|
|
}
|
|
|
|
Services.scriptloader.loadSubScript(
|
|
"chrome://devtools/content/shared/theme-switching.js",
|
|
win
|
|
);
|
|
this.container = env;
|
|
this._setup(win.document.body, el.ownerDocument);
|
|
env.removeEventListener("load", onLoad, true);
|
|
|
|
resolve();
|
|
};
|
|
|
|
env.addEventListener("load", onLoad, true);
|
|
env.setAttribute("src", CM_IFRAME);
|
|
el.appendChild(env);
|
|
|
|
this.once("destroy", () => el.removeChild(env));
|
|
});
|
|
},
|
|
|
|
appendToLocalElement: function(el) {
|
|
this._setup(el);
|
|
},
|
|
|
|
/**
|
|
* Do the actual appending and configuring of the CodeMirror instance. This is
|
|
* used by both append functions above, and does all the hard work to
|
|
* configure CodeMirror with all the right options/modes/etc.
|
|
*/
|
|
_setup: function(el, doc) {
|
|
doc = doc || el.ownerDocument;
|
|
const win = el.ownerDocument.defaultView;
|
|
|
|
const scriptsToInject = CM_SCRIPTS.concat(this.config.externalScripts);
|
|
scriptsToInject.forEach(url => {
|
|
if (url.startsWith("chrome://")) {
|
|
Services.scriptloader.loadSubScript(url, win);
|
|
}
|
|
});
|
|
|
|
// Replace the propertyKeywords, colorKeywords and valueKeywords
|
|
// properties of the CSS MIME type with the values provided by the CSS properties
|
|
// database.
|
|
const {
|
|
propertyKeywords,
|
|
colorKeywords,
|
|
valueKeywords,
|
|
} = getCSSKeywords(this.config.cssProperties);
|
|
|
|
const cssSpec = win.CodeMirror.resolveMode("text/css");
|
|
cssSpec.propertyKeywords = propertyKeywords;
|
|
cssSpec.colorKeywords = colorKeywords;
|
|
cssSpec.valueKeywords = valueKeywords;
|
|
win.CodeMirror.defineMIME("text/css", cssSpec);
|
|
|
|
const scssSpec = win.CodeMirror.resolveMode("text/x-scss");
|
|
scssSpec.propertyKeywords = propertyKeywords;
|
|
scssSpec.colorKeywords = colorKeywords;
|
|
scssSpec.valueKeywords = valueKeywords;
|
|
win.CodeMirror.defineMIME("text/x-scss", scssSpec);
|
|
|
|
win.CodeMirror.commands.save = () => this.emit("saveRequested");
|
|
|
|
// Create a CodeMirror instance add support for context menus,
|
|
// overwrite the default controller (otherwise items in the top and
|
|
// context menus won't work).
|
|
|
|
const cm = win.CodeMirror(el, this.config);
|
|
this.Doc = win.CodeMirror.Doc;
|
|
|
|
// Disable APZ for source editors. It currently causes the line numbers to
|
|
// "tear off" and swim around on top of the content. Bug 1160601 tracks
|
|
// finding a solution that allows APZ to work with CodeMirror.
|
|
cm.getScrollerElement().addEventListener("wheel", ev => {
|
|
// By handling the wheel events ourselves, we force the platform to
|
|
// scroll synchronously, like it did before APZ. However, we lose smooth
|
|
// scrolling for users with mouse wheels. This seems acceptible vs.
|
|
// doing nothing and letting the gutter slide around.
|
|
ev.preventDefault();
|
|
|
|
let { deltaX, deltaY } = ev;
|
|
|
|
if (ev.deltaMode == ev.DOM_DELTA_LINE) {
|
|
deltaX *= cm.defaultCharWidth();
|
|
deltaY *= cm.defaultTextHeight();
|
|
} else if (ev.deltaMode == ev.DOM_DELTA_PAGE) {
|
|
deltaX *= cm.getWrapperElement().clientWidth;
|
|
deltaY *= cm.getWrapperElement().clientHeight;
|
|
}
|
|
|
|
cm.getScrollerElement().scrollBy(deltaX, deltaY);
|
|
});
|
|
|
|
cm.getWrapperElement().addEventListener("contextmenu", ev => {
|
|
if (!this.config.contextMenu) {
|
|
return;
|
|
}
|
|
|
|
ev.stopPropagation();
|
|
ev.preventDefault();
|
|
|
|
let popup = this.config.contextMenu;
|
|
if (typeof popup == "string") {
|
|
popup = doc.getElementById(this.config.contextMenu);
|
|
}
|
|
|
|
this.emit("popupOpen", ev, popup);
|
|
popup.openPopupAtScreen(ev.screenX, ev.screenY, true);
|
|
});
|
|
|
|
const pipedEvents = [
|
|
"beforeChange",
|
|
"changes",
|
|
"cursorActivity",
|
|
"focus",
|
|
"scroll",
|
|
];
|
|
for (const eventName of pipedEvents) {
|
|
cm.on(eventName, () => this.emit(eventName));
|
|
}
|
|
|
|
cm.on("change", () => {
|
|
this.emit("change");
|
|
if (!this._lastDirty) {
|
|
this._lastDirty = true;
|
|
this.emit("dirty-change");
|
|
}
|
|
});
|
|
|
|
cm.on("gutterClick", (cmArg, line, gutter, ev) => {
|
|
const lineOrOffset = !this.isWasm ? line : this.lineToWasmOffset(line);
|
|
this.emit("gutterClick", lineOrOffset, ev.button);
|
|
});
|
|
|
|
win.CodeMirror.defineExtension("l10n", (name) => {
|
|
return L10N.getStr(name);
|
|
});
|
|
|
|
if (!this.config.disableSearchAddon) {
|
|
this._initSearchShortcuts(win);
|
|
}
|
|
|
|
editors.set(this, cm);
|
|
|
|
this.reloadPreferences = this.reloadPreferences.bind(this);
|
|
this._prefObserver = new PrefObserver("devtools.editor.");
|
|
this._prefObserver.on(TAB_SIZE, this.reloadPreferences);
|
|
this._prefObserver.on(EXPAND_TAB, this.reloadPreferences);
|
|
this._prefObserver.on(KEYMAP, this.reloadPreferences);
|
|
this._prefObserver.on(AUTO_CLOSE, this.reloadPreferences);
|
|
this._prefObserver.on(AUTOCOMPLETE, this.reloadPreferences);
|
|
this._prefObserver.on(DETECT_INDENT, this.reloadPreferences);
|
|
this._prefObserver.on(ENABLE_CODE_FOLDING, this.reloadPreferences);
|
|
|
|
this.reloadPreferences();
|
|
|
|
win.editor = this;
|
|
const editorReadyEvent = new win.CustomEvent("editorReady");
|
|
win.dispatchEvent(editorReadyEvent);
|
|
},
|
|
|
|
/**
|
|
* Returns a boolean indicating whether the editor is ready to
|
|
* use. Use appendTo(el).then(() => {}) for most cases
|
|
*/
|
|
isAppended: function() {
|
|
return editors.has(this);
|
|
},
|
|
|
|
/**
|
|
* Returns the currently active highlighting mode.
|
|
* See Editor.modes for the list of all suppoert modes.
|
|
*/
|
|
getMode: function() {
|
|
return this.getOption("mode");
|
|
},
|
|
|
|
/**
|
|
* Loads a script into editor's containing window.
|
|
*/
|
|
loadScript: function(url) {
|
|
if (!this.container) {
|
|
throw new Error("Can't load a script until the editor is loaded.");
|
|
}
|
|
const win = this.container.contentWindow.wrappedJSObject;
|
|
Services.scriptloader.loadSubScript(url, win);
|
|
},
|
|
|
|
/**
|
|
* Creates a CodeMirror Document
|
|
* @returns CodeMirror.Doc
|
|
*/
|
|
createDocument: function() {
|
|
return new this.Doc("");
|
|
},
|
|
|
|
/**
|
|
* Replaces the current document with a new source document
|
|
*/
|
|
replaceDocument: function(doc) {
|
|
const cm = editors.get(this);
|
|
cm.swapDoc(doc);
|
|
},
|
|
|
|
/**
|
|
* Changes the value of a currently used highlighting mode.
|
|
* See Editor.modes for the list of all supported modes.
|
|
*/
|
|
setMode: function(value) {
|
|
this.setOption("mode", value);
|
|
|
|
// If autocomplete was set up and the mode is changing, then
|
|
// turn it off and back on again so the proper mode can be used.
|
|
if (this.config.autocomplete) {
|
|
this.setOption("autocomplete", false);
|
|
this.setOption("autocomplete", true);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* The source editor can expose several commands linked from system and context menus.
|
|
* Kept for backward compatibility with scratchpad and styleeditor.
|
|
*/
|
|
insertCommandsController: function() {
|
|
const { insertCommandsController } =
|
|
require("devtools/client/shared/sourceeditor/editor-commands-controller");
|
|
insertCommandsController(this);
|
|
},
|
|
|
|
/**
|
|
* Returns text from the text area. If line argument is provided
|
|
* the method returns only that line.
|
|
*/
|
|
getText: function(line) {
|
|
const cm = editors.get(this);
|
|
|
|
if (line == null) {
|
|
return cm.getValue();
|
|
}
|
|
|
|
const info = this.lineInfo(line);
|
|
return info ? info.text : "";
|
|
},
|
|
|
|
getDoc: function() {
|
|
const cm = editors.get(this);
|
|
return cm.getDoc();
|
|
},
|
|
|
|
get isWasm() {
|
|
return isWasm(this.getDoc());
|
|
},
|
|
|
|
wasmOffsetToLine: function(offset) {
|
|
return wasmOffsetToLine(this.getDoc(), offset);
|
|
},
|
|
|
|
lineToWasmOffset: function(number) {
|
|
return lineToWasmOffset(this.getDoc(), number);
|
|
},
|
|
|
|
toLineIfWasmOffset: function(maybeOffset) {
|
|
if (typeof maybeOffset !== "number" || !this.isWasm) {
|
|
return maybeOffset;
|
|
}
|
|
return this.wasmOffsetToLine(maybeOffset);
|
|
},
|
|
|
|
lineInfo: function(lineOrOffset) {
|
|
const line = this.toLineIfWasmOffset(lineOrOffset);
|
|
if (line == undefined) {
|
|
return null;
|
|
}
|
|
const cm = editors.get(this);
|
|
return cm.lineInfo(line);
|
|
},
|
|
|
|
getLineOrOffset: function(line) {
|
|
return this.isWasm ? this.lineToWasmOffset(line) : line;
|
|
},
|
|
|
|
/**
|
|
* Replaces whatever is in the text area with the contents of
|
|
* the 'value' argument.
|
|
*/
|
|
setText: function(value) {
|
|
const cm = editors.get(this);
|
|
|
|
if (typeof value !== "string" && "binary" in value) { // wasm?
|
|
// binary does not survive as Uint8Array, converting from string
|
|
const binary = value.binary;
|
|
const data = new Uint8Array(binary.length);
|
|
for (let i = 0; i < data.length; i++) {
|
|
data[i] = binary.charCodeAt(i);
|
|
}
|
|
const { lines, done } = getWasmText(this.getDoc(), data);
|
|
const MAX_LINES = 10000000;
|
|
if (lines.length > MAX_LINES) {
|
|
lines.splice(MAX_LINES, lines.length - MAX_LINES);
|
|
lines.push(";; .... text is truncated due to the size");
|
|
}
|
|
if (!done) {
|
|
lines.push(";; .... possible error during wast conversion");
|
|
}
|
|
// cm will try to split into lines anyway, saving memory
|
|
value = { split: () => lines };
|
|
}
|
|
|
|
cm.setValue(value);
|
|
|
|
this.resetIndentUnit();
|
|
},
|
|
|
|
/**
|
|
* Reloads the state of the editor based on all current preferences.
|
|
* This is called automatically when any of the relevant preferences
|
|
* change.
|
|
*/
|
|
reloadPreferences: function() {
|
|
// Restore the saved autoCloseBrackets value if it is preffed on.
|
|
const useAutoClose = Services.prefs.getBoolPref(AUTO_CLOSE);
|
|
this.setOption("autoCloseBrackets",
|
|
useAutoClose ? this.config.autoCloseBracketsSaved : false);
|
|
|
|
// If alternative keymap is provided, use it.
|
|
const keyMap = Services.prefs.getCharPref(KEYMAP);
|
|
if (VALID_KEYMAPS.has(keyMap)) {
|
|
this.setOption("keyMap", keyMap);
|
|
} else {
|
|
this.setOption("keyMap", "default");
|
|
}
|
|
this.updateCodeFoldingGutter();
|
|
|
|
this.resetIndentUnit();
|
|
this.setupAutoCompletion();
|
|
},
|
|
|
|
/**
|
|
* Sets the editor's indentation based on the current prefs and
|
|
* re-detect indentation if we should.
|
|
*/
|
|
resetIndentUnit: function() {
|
|
const cm = editors.get(this);
|
|
|
|
const iterFn = function(start, end, callback) {
|
|
cm.eachLine(start, end, (line) => {
|
|
return callback(line.text);
|
|
});
|
|
};
|
|
|
|
const {indentUnit, indentWithTabs} = getIndentationFromIteration(iterFn);
|
|
|
|
cm.setOption("tabSize", indentUnit);
|
|
cm.setOption("indentUnit", indentUnit);
|
|
cm.setOption("indentWithTabs", indentWithTabs);
|
|
},
|
|
|
|
/**
|
|
* Replaces contents of a text area within the from/to {line, ch}
|
|
* range. If neither `from` nor `to` arguments are provided works
|
|
* exactly like setText. If only `from` object is provided, inserts
|
|
* text at that point, *overwriting* as many characters as needed.
|
|
*/
|
|
replaceText: function(value, from, to) {
|
|
const cm = editors.get(this);
|
|
|
|
if (!from) {
|
|
this.setText(value);
|
|
return;
|
|
}
|
|
|
|
if (!to) {
|
|
const text = cm.getRange({ line: 0, ch: 0 }, from);
|
|
this.setText(text + value);
|
|
return;
|
|
}
|
|
|
|
cm.replaceRange(value, from, to);
|
|
},
|
|
|
|
/**
|
|
* Inserts text at the specified {line, ch} position, shifting existing
|
|
* contents as necessary.
|
|
*/
|
|
insertText: function(value, at) {
|
|
const cm = editors.get(this);
|
|
cm.replaceRange(value, at, at);
|
|
},
|
|
|
|
/**
|
|
* Deselects contents of the text area.
|
|
*/
|
|
dropSelection: function() {
|
|
if (!this.somethingSelected()) {
|
|
return;
|
|
}
|
|
|
|
this.setCursor(this.getCursor());
|
|
},
|
|
|
|
/**
|
|
* Returns true if there is more than one selection in the editor.
|
|
*/
|
|
hasMultipleSelections: function() {
|
|
const cm = editors.get(this);
|
|
return cm.listSelections().length > 1;
|
|
},
|
|
|
|
/**
|
|
* Gets the first visible line number in the editor.
|
|
*/
|
|
getFirstVisibleLine: function() {
|
|
const cm = editors.get(this);
|
|
return cm.lineAtHeight(0, "local");
|
|
},
|
|
|
|
/**
|
|
* Scrolls the view such that the given line number is the first visible line.
|
|
*/
|
|
setFirstVisibleLine: function(line) {
|
|
const cm = editors.get(this);
|
|
const { top } = cm.charCoords({line: line, ch: 0}, "local");
|
|
cm.scrollTo(0, top);
|
|
},
|
|
|
|
/**
|
|
* Sets the cursor to the specified {line, ch} position with an additional
|
|
* option to align the line at the "top", "center" or "bottom" of the editor
|
|
* with "top" being default value.
|
|
*/
|
|
setCursor: function({line, ch}, align) {
|
|
const cm = editors.get(this);
|
|
this.alignLine(line, align);
|
|
cm.setCursor({line: line, ch: ch});
|
|
this.emit("cursorActivity");
|
|
},
|
|
|
|
/**
|
|
* Aligns the provided line to either "top", "center" or "bottom" of the
|
|
* editor view with a maximum margin of MAX_VERTICAL_OFFSET lines from top or
|
|
* bottom.
|
|
*/
|
|
alignLine: function(line, align) {
|
|
const cm = editors.get(this);
|
|
const from = cm.lineAtHeight(0, "page");
|
|
const to = cm.lineAtHeight(cm.getWrapperElement().clientHeight, "page");
|
|
const linesVisible = to - from;
|
|
const halfVisible = Math.round(linesVisible / 2);
|
|
|
|
// If the target line is in view, skip the vertical alignment part.
|
|
if (line <= to && line >= from) {
|
|
return;
|
|
}
|
|
|
|
// Setting the offset so that the line always falls in the upper half
|
|
// of visible lines (lower half for bottom aligned).
|
|
// MAX_VERTICAL_OFFSET is the maximum allowed value.
|
|
const offset = Math.min(halfVisible, MAX_VERTICAL_OFFSET);
|
|
|
|
let topLine = {
|
|
"center": Math.max(line - halfVisible, 0),
|
|
"bottom": Math.max(line - linesVisible + offset, 0),
|
|
"top": Math.max(line - offset, 0),
|
|
}[align || "top"] || offset;
|
|
|
|
// Bringing down the topLine to total lines in the editor if exceeding.
|
|
topLine = Math.min(topLine, this.lineCount());
|
|
this.setFirstVisibleLine(topLine);
|
|
},
|
|
|
|
/**
|
|
* Returns whether a marker of a specified class exists in a line's gutter.
|
|
*/
|
|
hasMarker: function(line, gutterName, markerClass) {
|
|
const marker = this.getMarker(line, gutterName);
|
|
if (!marker) {
|
|
return false;
|
|
}
|
|
|
|
return marker.classList.contains(markerClass);
|
|
},
|
|
|
|
/**
|
|
* Adds a marker with a specified class to a line's gutter. If another marker
|
|
* exists on that line, the new marker class is added to its class list.
|
|
*/
|
|
addMarker: function(line, gutterName, markerClass) {
|
|
const cm = editors.get(this);
|
|
const info = this.lineInfo(line);
|
|
if (!info) {
|
|
return;
|
|
}
|
|
|
|
const gutterMarkers = info.gutterMarkers;
|
|
let marker;
|
|
if (gutterMarkers) {
|
|
marker = gutterMarkers[gutterName];
|
|
if (marker) {
|
|
marker.classList.add(markerClass);
|
|
return;
|
|
}
|
|
}
|
|
|
|
marker = cm.getWrapperElement().ownerDocument.createElement("div");
|
|
marker.className = markerClass;
|
|
cm.setGutterMarker(info.line, gutterName, marker);
|
|
},
|
|
|
|
/**
|
|
* The reverse of addMarker. Removes a marker of a specified class from a
|
|
* line's gutter.
|
|
*/
|
|
removeMarker: function(line, gutterName, markerClass) {
|
|
if (!this.hasMarker(line, gutterName, markerClass)) {
|
|
return;
|
|
}
|
|
|
|
this.lineInfo(line).gutterMarkers[gutterName].classList.remove(markerClass);
|
|
},
|
|
|
|
/**
|
|
* Adds a marker with a specified class and an HTML content to a line's
|
|
* gutter. If another marker exists on that line, it is overwritten by a new
|
|
* marker.
|
|
*/
|
|
addContentMarker: function(line, gutterName, markerClass, content) {
|
|
const cm = editors.get(this);
|
|
const info = this.lineInfo(line);
|
|
if (!info) {
|
|
return;
|
|
}
|
|
|
|
const marker = cm.getWrapperElement().ownerDocument.createElement("div");
|
|
marker.className = markerClass;
|
|
// eslint-disable-next-line no-unsanitized/property
|
|
marker.innerHTML = content;
|
|
cm.setGutterMarker(info.line, gutterName, marker);
|
|
},
|
|
|
|
/**
|
|
* The reverse of addContentMarker. Removes any line's markers in the
|
|
* specified gutter.
|
|
*/
|
|
removeContentMarker: function(line, gutterName) {
|
|
const cm = editors.get(this);
|
|
const info = this.lineInfo(line);
|
|
if (!info) {
|
|
return;
|
|
}
|
|
|
|
cm.setGutterMarker(info.line, gutterName, null);
|
|
},
|
|
|
|
getMarker: function(line, gutterName) {
|
|
const info = this.lineInfo(line);
|
|
if (!info) {
|
|
return null;
|
|
}
|
|
|
|
const gutterMarkers = info.gutterMarkers;
|
|
if (!gutterMarkers) {
|
|
return null;
|
|
}
|
|
|
|
return gutterMarkers[gutterName];
|
|
},
|
|
|
|
/**
|
|
* Removes all gutter markers in the gutter with the given name.
|
|
*/
|
|
removeAllMarkers: function(gutterName) {
|
|
const cm = editors.get(this);
|
|
cm.clearGutter(gutterName);
|
|
},
|
|
|
|
/**
|
|
* Handles attaching a set of events listeners on a marker. They should
|
|
* be passed as an object literal with keys as event names and values as
|
|
* function listeners. The line number, marker node and optional data
|
|
* will be passed as arguments to the function listener.
|
|
*
|
|
* You don't need to worry about removing these event listeners.
|
|
* They're automatically orphaned when clearing markers.
|
|
*/
|
|
setMarkerListeners: function(line, gutterName, markerClass, eventsArg, data) {
|
|
if (!this.hasMarker(line, gutterName, markerClass)) {
|
|
return;
|
|
}
|
|
|
|
const cm = editors.get(this);
|
|
const marker = cm.lineInfo(line).gutterMarkers[gutterName];
|
|
|
|
for (const name in eventsArg) {
|
|
const listener = eventsArg[name].bind(this, line, marker, data);
|
|
marker.addEventListener(name, listener);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Returns whether a line is decorated using the specified class name.
|
|
*/
|
|
hasLineClass: function(line, className) {
|
|
const info = this.lineInfo(line);
|
|
|
|
if (!info || !info.wrapClass) {
|
|
return false;
|
|
}
|
|
|
|
return info.wrapClass.split(" ").includes(className);
|
|
},
|
|
|
|
/**
|
|
* Sets a CSS class name for the given line, including the text and gutter.
|
|
*/
|
|
addLineClass: function(lineOrOffset, className) {
|
|
const cm = editors.get(this);
|
|
const line = this.toLineIfWasmOffset(lineOrOffset);
|
|
cm.addLineClass(line, "wrap", className);
|
|
},
|
|
|
|
/**
|
|
* The reverse of addLineClass.
|
|
*/
|
|
removeLineClass: function(lineOrOffset, className) {
|
|
const cm = editors.get(this);
|
|
const line = this.toLineIfWasmOffset(lineOrOffset);
|
|
cm.removeLineClass(line, "wrap", className);
|
|
},
|
|
|
|
/**
|
|
* Mark a range of text inside the two {line, ch} bounds. Since the range may
|
|
* be modified, for example, when typing text, this method returns a function
|
|
* that can be used to remove the mark.
|
|
*/
|
|
markText: function(from, to, className = "marked-text") {
|
|
const cm = editors.get(this);
|
|
const text = cm.getRange(from, to);
|
|
const span = cm.getWrapperElement().ownerDocument.createElement("span");
|
|
span.className = className;
|
|
span.textContent = text;
|
|
|
|
const mark = cm.markText(from, to, { replacedWith: span });
|
|
return {
|
|
anchor: span,
|
|
clear: () => mark.clear(),
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Calculates and returns one or more {line, ch} objects for
|
|
* a zero-based index who's value is relative to the start of
|
|
* the editor's text.
|
|
*
|
|
* If only one argument is given, this method returns a single
|
|
* {line,ch} object. Otherwise it returns an array.
|
|
*/
|
|
getPosition: function(...args) {
|
|
const cm = editors.get(this);
|
|
const res = args.map((ind) => cm.posFromIndex(ind));
|
|
return args.length === 1 ? res[0] : res;
|
|
},
|
|
|
|
/**
|
|
* The reverse of getPosition. Similarly to getPosition this
|
|
* method returns a single value if only one argument was given
|
|
* and an array otherwise.
|
|
*/
|
|
getOffset: function(...args) {
|
|
const cm = editors.get(this);
|
|
const res = args.map((pos) => cm.indexFromPos(pos));
|
|
return args.length > 1 ? res : res[0];
|
|
},
|
|
|
|
/**
|
|
* Returns a {line, ch} object that corresponds to the
|
|
* left, top coordinates.
|
|
*/
|
|
getPositionFromCoords: function({left, top}) {
|
|
const cm = editors.get(this);
|
|
return cm.coordsChar({ left: left, top: top });
|
|
},
|
|
|
|
/**
|
|
* The reverse of getPositionFromCoords. Similarly, returns a {left, top}
|
|
* object that corresponds to the specified line and character number.
|
|
*/
|
|
getCoordsFromPosition: function({line, ch}) {
|
|
const cm = editors.get(this);
|
|
return cm.charCoords({ line: ~~line, ch: ~~ch });
|
|
},
|
|
|
|
/**
|
|
* Returns true if there's something to undo and false otherwise.
|
|
*/
|
|
canUndo: function() {
|
|
const cm = editors.get(this);
|
|
return cm.historySize().undo > 0;
|
|
},
|
|
|
|
/**
|
|
* Returns true if there's something to redo and false otherwise.
|
|
*/
|
|
canRedo: function() {
|
|
const cm = editors.get(this);
|
|
return cm.historySize().redo > 0;
|
|
},
|
|
|
|
/**
|
|
* Marks the contents as clean and returns the current
|
|
* version number.
|
|
*/
|
|
setClean: function() {
|
|
const cm = editors.get(this);
|
|
this.version = cm.changeGeneration();
|
|
this._lastDirty = false;
|
|
this.emit("dirty-change");
|
|
return this.version;
|
|
},
|
|
|
|
/**
|
|
* Returns true if contents of the text area are
|
|
* clean i.e. no changes were made since the last version.
|
|
*/
|
|
isClean: function() {
|
|
const cm = editors.get(this);
|
|
return cm.isClean(this.version);
|
|
},
|
|
|
|
/**
|
|
* This method opens an in-editor dialog asking for a line to
|
|
* jump to. Once given, it changes cursor to that line.
|
|
*/
|
|
jumpToLine: function() {
|
|
const doc = editors.get(this).getWrapperElement().ownerDocument;
|
|
const div = doc.createElement("div");
|
|
const inp = doc.createElement("input");
|
|
const txt = doc.createTextNode(L10N.getStr("gotoLineCmd.promptTitle"));
|
|
|
|
inp.type = "text";
|
|
inp.style.width = "10em";
|
|
inp.style.marginInlineStart = "1em";
|
|
|
|
div.appendChild(txt);
|
|
div.appendChild(inp);
|
|
|
|
if (!this.hasMultipleSelections()) {
|
|
const cm = editors.get(this);
|
|
const sel = cm.getSelection();
|
|
// Scratchpad inserts and selects a comment after an error happens:
|
|
// "@Scratchpad/1:10:2". Parse this to get the line and column.
|
|
// In the string above this is line 10, column 2.
|
|
const match = sel.match(RE_SCRATCHPAD_ERROR);
|
|
if (match) {
|
|
const [, line, column ] = match;
|
|
inp.value = column ? line + ":" + column : line;
|
|
inp.selectionStart = inp.selectionEnd = inp.value.length;
|
|
}
|
|
}
|
|
|
|
this.openDialog(div, (line) => {
|
|
// Handle LINE:COLUMN as well as LINE
|
|
const match = line.toString().match(RE_JUMP_TO_LINE);
|
|
if (match) {
|
|
const [, matchLine, column ] = match;
|
|
this.setCursor({line: matchLine - 1, ch: column ? column - 1 : 0 });
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Moves the content of the current line or the lines selected up a line.
|
|
*/
|
|
moveLineUp: function() {
|
|
const cm = editors.get(this);
|
|
const start = cm.getCursor("start");
|
|
const end = cm.getCursor("end");
|
|
|
|
if (start.line === 0) {
|
|
return;
|
|
}
|
|
|
|
// Get the text in the lines selected or the current line of the cursor
|
|
// and append the text of the previous line.
|
|
let value;
|
|
if (start.line !== end.line) {
|
|
value = cm.getRange({ line: start.line, ch: 0 },
|
|
{ line: end.line, ch: cm.getLine(end.line).length }) + "\n";
|
|
} else {
|
|
value = cm.getLine(start.line) + "\n";
|
|
}
|
|
value += cm.getLine(start.line - 1);
|
|
|
|
// Replace the previous line and the currently selected lines with the new
|
|
// value and maintain the selection of the text.
|
|
cm.replaceRange(value, { line: start.line - 1, ch: 0 },
|
|
{ line: end.line, ch: cm.getLine(end.line).length });
|
|
cm.setSelection({ line: start.line - 1, ch: start.ch },
|
|
{ line: end.line - 1, ch: end.ch });
|
|
},
|
|
|
|
/**
|
|
* Moves the content of the current line or the lines selected down a line.
|
|
*/
|
|
moveLineDown: function() {
|
|
const cm = editors.get(this);
|
|
const start = cm.getCursor("start");
|
|
const end = cm.getCursor("end");
|
|
|
|
if (end.line + 1 === cm.lineCount()) {
|
|
return;
|
|
}
|
|
|
|
// Get the text of next line and append the text in the lines selected
|
|
// or the current line of the cursor.
|
|
let value = cm.getLine(end.line + 1) + "\n";
|
|
if (start.line !== end.line) {
|
|
value += cm.getRange({ line: start.line, ch: 0 },
|
|
{ line: end.line, ch: cm.getLine(end.line).length });
|
|
} else {
|
|
value += cm.getLine(start.line);
|
|
}
|
|
|
|
// Replace the currently selected lines and the next line with the new
|
|
// value and maintain the selection of the text.
|
|
cm.replaceRange(value, { line: start.line, ch: 0 },
|
|
{ line: end.line + 1, ch: cm.getLine(end.line + 1).length});
|
|
cm.setSelection({ line: start.line + 1, ch: start.ch },
|
|
{ line: end.line + 1, ch: end.ch });
|
|
},
|
|
|
|
/**
|
|
* Intercept CodeMirror's Find and replace key shortcut to select the search input
|
|
*/
|
|
findOrReplace: function(node, isReplaceAll) {
|
|
const cm = editors.get(this);
|
|
const isInput = node.tagName === "INPUT";
|
|
const isSearchInput = isInput && node.type === "search";
|
|
// replace box is a different input instance than search, and it is
|
|
// located in a code mirror dialog
|
|
const isDialogInput = isInput &&
|
|
node.parentNode &&
|
|
node.parentNode.classList.contains("CodeMirror-dialog");
|
|
if (!(isSearchInput || isDialogInput)) {
|
|
return;
|
|
}
|
|
|
|
if (isSearchInput || isReplaceAll) {
|
|
// select the search input
|
|
// it's the precise reason why we reimplement these key shortcuts
|
|
node.select();
|
|
}
|
|
|
|
// need to call it since we prevent the propagation of the event and
|
|
// cancel codemirror's key handling
|
|
cm.execCommand("find");
|
|
},
|
|
|
|
/**
|
|
* Intercept CodeMirror's findNext and findPrev key shortcut to allow
|
|
* immediately search for next occurance after typing a word to search.
|
|
*/
|
|
findNextOrPrev: function(node, isFindPrev) {
|
|
const cm = editors.get(this);
|
|
const isInput = node.tagName === "INPUT";
|
|
const isSearchInput = isInput && node.type === "search";
|
|
if (!isSearchInput) {
|
|
return;
|
|
}
|
|
const query = node.value;
|
|
// cm.state.search allows to automatically start searching for the next occurance
|
|
// it's the precise reason why we reimplement these key shortcuts
|
|
if (!cm.state.search || cm.state.search.query !== query) {
|
|
cm.state.search = {
|
|
posFrom: null,
|
|
posTo: null,
|
|
overlay: null,
|
|
query,
|
|
};
|
|
}
|
|
|
|
// need to call it since we prevent the propagation of the event and
|
|
// cancel codemirror's key handling
|
|
if (isFindPrev) {
|
|
cm.execCommand("findPrev");
|
|
} else {
|
|
cm.execCommand("findNext");
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Returns current font size for the editor area, in pixels.
|
|
*/
|
|
getFontSize: function() {
|
|
const cm = editors.get(this);
|
|
const el = cm.getWrapperElement();
|
|
const win = el.ownerDocument.defaultView;
|
|
|
|
return parseInt(win.getComputedStyle(el).getPropertyValue("font-size"), 10);
|
|
},
|
|
|
|
/**
|
|
* Sets font size for the editor area.
|
|
*/
|
|
setFontSize: function(size) {
|
|
const cm = editors.get(this);
|
|
cm.getWrapperElement().style.fontSize = parseInt(size, 10) + "px";
|
|
cm.refresh();
|
|
},
|
|
|
|
/**
|
|
* Sets an option for the editor. For most options it just defers to
|
|
* CodeMirror.setOption, but certain ones are maintained within the editor
|
|
* instance.
|
|
*/
|
|
setOption: function(o, v) {
|
|
const cm = editors.get(this);
|
|
|
|
// Save the state of a valid autoCloseBrackets string, so we can reset
|
|
// it if it gets preffed off and back on.
|
|
if (o === "autoCloseBrackets" && v) {
|
|
this.config.autoCloseBracketsSaved = v;
|
|
}
|
|
|
|
if (o === "autocomplete") {
|
|
this.config.autocomplete = v;
|
|
this.setupAutoCompletion();
|
|
} else {
|
|
cm.setOption(o, v);
|
|
this.config[o] = v;
|
|
}
|
|
|
|
if (o === "enableCodeFolding") {
|
|
// The new value maybe explicitly force foldGUtter on or off, ignoring
|
|
// the prefs service.
|
|
this.updateCodeFoldingGutter();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Gets an option for the editor. For most options it just defers to
|
|
* CodeMirror.getOption, but certain ones are maintained within the editor
|
|
* instance.
|
|
*/
|
|
getOption: function(o) {
|
|
const cm = editors.get(this);
|
|
if (o === "autocomplete") {
|
|
return this.config.autocomplete;
|
|
}
|
|
|
|
return cm.getOption(o);
|
|
},
|
|
|
|
/**
|
|
* Sets up autocompletion for the editor. Lazily imports the required
|
|
* dependencies because they vary by editor mode.
|
|
*
|
|
* Autocompletion is special, because we don't want to automatically use
|
|
* it just because it is preffed on (it still needs to be requested by the
|
|
* editor), but we do want to always disable it if it is preffed off.
|
|
*/
|
|
setupAutoCompletion: function() {
|
|
if (!this.config.autocomplete && !this.initializeAutoCompletion) {
|
|
// Do nothing since there is no autocomplete config and no autocompletion have
|
|
// been initialized.
|
|
return;
|
|
}
|
|
// The autocomplete module will overwrite this.initializeAutoCompletion
|
|
// with a mode specific autocompletion handler.
|
|
if (!this.initializeAutoCompletion) {
|
|
this.extend(require("./autocomplete"));
|
|
}
|
|
|
|
if (this.config.autocomplete && Services.prefs.getBoolPref(AUTOCOMPLETE)) {
|
|
this.initializeAutoCompletion(this.config.autocompleteOpts);
|
|
} else {
|
|
this.destroyAutoCompletion();
|
|
}
|
|
},
|
|
|
|
getAutoCompletionText() {
|
|
const cm = editors.get(this);
|
|
const mark = cm.getAllMarks().find(m => m.className === AUTOCOMPLETE_MARK_CLASSNAME);
|
|
if (!mark) {
|
|
return "";
|
|
}
|
|
|
|
return mark.title || "";
|
|
},
|
|
|
|
setAutoCompletionText: function(text) {
|
|
const cursor = this.getCursor();
|
|
const cm = editors.get(this);
|
|
const className = AUTOCOMPLETE_MARK_CLASSNAME;
|
|
|
|
cm.operation(() => {
|
|
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) {
|
|
* let { cm, ed } = ctx;
|
|
* cm; // CodeMirror instance
|
|
* ed; // Editor instance
|
|
* name; // 'Mozilla'
|
|
* }
|
|
*
|
|
* editor.extend({ hello: hello });
|
|
* editor.hello('Mozilla');
|
|
*/
|
|
extend: function(funcs) {
|
|
Object.keys(funcs).forEach(name => {
|
|
const cm = editors.get(this);
|
|
const ctx = { ed: this, cm: cm, Editor: Editor};
|
|
|
|
if (name === "initialize") {
|
|
funcs[name](ctx);
|
|
return;
|
|
}
|
|
|
|
this[name] = funcs[name].bind(null, ctx);
|
|
});
|
|
},
|
|
|
|
isDestroyed: function() {
|
|
return !editors.get(this);
|
|
},
|
|
|
|
destroy: function() {
|
|
this.container = null;
|
|
this.config = null;
|
|
this.version = null;
|
|
|
|
if (this._prefObserver) {
|
|
this._prefObserver.off(TAB_SIZE, this.reloadPreferences);
|
|
this._prefObserver.off(EXPAND_TAB, this.reloadPreferences);
|
|
this._prefObserver.off(KEYMAP, this.reloadPreferences);
|
|
this._prefObserver.off(AUTO_CLOSE, this.reloadPreferences);
|
|
this._prefObserver.off(AUTOCOMPLETE, this.reloadPreferences);
|
|
this._prefObserver.off(DETECT_INDENT, this.reloadPreferences);
|
|
this._prefObserver.off(ENABLE_CODE_FOLDING, this.reloadPreferences);
|
|
this._prefObserver.destroy();
|
|
}
|
|
|
|
// Remove the link between the document and code-mirror.
|
|
const cm = editors.get(this);
|
|
if (cm && cm.doc) {
|
|
cm.doc.cm = null;
|
|
}
|
|
|
|
this.emit("destroy");
|
|
},
|
|
|
|
updateCodeFoldingGutter: function() {
|
|
let shouldFoldGutter = this.config.enableCodeFolding;
|
|
const foldGutterIndex = this.config.gutters.indexOf("CodeMirror-foldgutter");
|
|
const cm = editors.get(this);
|
|
|
|
if (shouldFoldGutter === undefined) {
|
|
shouldFoldGutter = Services.prefs.getBoolPref(ENABLE_CODE_FOLDING);
|
|
}
|
|
|
|
if (shouldFoldGutter) {
|
|
// Add the gutter before enabling foldGutter
|
|
if (foldGutterIndex === -1) {
|
|
const gutters = this.config.gutters.slice();
|
|
gutters.push("CodeMirror-foldgutter");
|
|
this.setOption("gutters", gutters);
|
|
}
|
|
|
|
this.setOption("foldGutter", true);
|
|
} else {
|
|
// No code should remain folded when folding is off.
|
|
if (cm) {
|
|
cm.execCommand("unfoldAll");
|
|
}
|
|
|
|
// Remove the gutter so it doesn't take up space
|
|
if (foldGutterIndex !== -1) {
|
|
const gutters = this.config.gutters.slice();
|
|
gutters.splice(foldGutterIndex, 1);
|
|
this.setOption("gutters", gutters);
|
|
}
|
|
|
|
this.setOption("foldGutter", false);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Register all key shortcuts.
|
|
*/
|
|
_initSearchShortcuts: function(win) {
|
|
const shortcuts = new KeyShortcuts({
|
|
window: win,
|
|
});
|
|
this._onSearchShortcut = this._onSearchShortcut.bind(this);
|
|
const keys = [
|
|
"find.key",
|
|
"findNext.key",
|
|
"findPrev.key",
|
|
];
|
|
|
|
if (OS === "Darwin") {
|
|
keys.push("replaceAllMac.key");
|
|
} else {
|
|
keys.push("replaceAll.key");
|
|
}
|
|
// Process generic keys:
|
|
keys.forEach(name => {
|
|
const key = L10N.getStr(name);
|
|
shortcuts.on(key, event => this._onSearchShortcut(name, event));
|
|
});
|
|
},
|
|
/**
|
|
* Key shortcut listener.
|
|
*/
|
|
_onSearchShortcut: function(name, event) {
|
|
if (!this._isInputOrTextarea(event.target)) {
|
|
return;
|
|
}
|
|
const node = event.originalTarget;
|
|
|
|
switch (name) {
|
|
// replaceAll.key is Alt + find.key
|
|
case "replaceAllMac.key":
|
|
this.findOrReplace(node, true);
|
|
break;
|
|
// replaceAll.key is Shift + find.key
|
|
case "replaceAll.key":
|
|
this.findOrReplace(node, true);
|
|
break;
|
|
case "find.key":
|
|
this.findOrReplace(node, false);
|
|
break;
|
|
// findPrev.key is Shift + findNext.key
|
|
case "findPrev.key":
|
|
this.findNextOrPrev(node, true);
|
|
break;
|
|
case "findNext.key":
|
|
this.findNextOrPrev(node, false);
|
|
break;
|
|
default:
|
|
console.error("Unexpected editor key shortcut", name);
|
|
return;
|
|
}
|
|
// Prevent default for this action
|
|
event.stopPropagation();
|
|
event.preventDefault();
|
|
},
|
|
|
|
/**
|
|
* Check if a node is an input or textarea
|
|
*/
|
|
_isInputOrTextarea: function(element) {
|
|
const name = element.tagName.toLowerCase();
|
|
return name === "input" || name === "textarea";
|
|
},
|
|
};
|
|
|
|
// Since Editor is a thin layer over CodeMirror some methods
|
|
// are mapped directly—without any changes.
|
|
|
|
CM_MAPPING.forEach(name => {
|
|
Editor.prototype[name] = function(...args) {
|
|
const cm = editors.get(this);
|
|
return cm[name].apply(cm, args);
|
|
};
|
|
});
|
|
|
|
// Static methods on the Editor object itself.
|
|
|
|
/**
|
|
* Returns a string representation of a shortcut 'key' with
|
|
* a OS specific modifier. Cmd- for Macs, Ctrl- for other
|
|
* platforms. Useful with extraKeys configuration option.
|
|
*
|
|
* CodeMirror defines all keys with modifiers in the following
|
|
* order: Shift - Ctrl/Cmd - Alt - Key
|
|
*/
|
|
Editor.accel = function(key, modifiers = {}) {
|
|
return (modifiers.shift ? "Shift-" : "") +
|
|
(Services.appinfo.OS == "Darwin" ? "Cmd-" : "Ctrl-") +
|
|
(modifiers.alt ? "Alt-" : "") + key;
|
|
};
|
|
|
|
/**
|
|
* Returns a string representation of a shortcut for a
|
|
* specified command 'cmd'. Append Cmd- for macs, Ctrl- for other
|
|
* platforms unless noaccel is specified in the options. Useful when overwriting
|
|
* or disabling default shortcuts.
|
|
*/
|
|
Editor.keyFor = function(cmd, opts = { noaccel: false }) {
|
|
const key = L10N.getStr(cmd + ".commandkey");
|
|
return opts.noaccel ? key : Editor.accel(key);
|
|
};
|
|
|
|
/**
|
|
* We compute the CSS property names, values, and color names to be used with
|
|
* CodeMirror to more closely reflect what is supported by the target platform.
|
|
* The database is used to replace the values used in CodeMirror while initiating
|
|
* an editor object. This is done here instead of the file codemirror/css.js so
|
|
* as to leave that file untouched and easily upgradable.
|
|
*/
|
|
function getCSSKeywords(cssProperties) {
|
|
function keySet(array) {
|
|
const keys = {};
|
|
for (let i = 0; i < array.length; ++i) {
|
|
keys[array[i]] = true;
|
|
}
|
|
return keys;
|
|
}
|
|
|
|
const propertyKeywords = cssProperties.getNames();
|
|
const colorKeywords = {};
|
|
const valueKeywords = {};
|
|
|
|
propertyKeywords.forEach(property => {
|
|
if (property.includes("color")) {
|
|
cssProperties.getValues(property).forEach(value => {
|
|
colorKeywords[value] = true;
|
|
});
|
|
} else {
|
|
cssProperties.getValues(property).forEach(value => {
|
|
valueKeywords[value] = true;
|
|
});
|
|
}
|
|
});
|
|
|
|
return {
|
|
propertyKeywords: keySet(propertyKeywords),
|
|
colorKeywords: colorKeywords,
|
|
valueKeywords: valueKeywords,
|
|
};
|
|
}
|
|
|
|
module.exports = Editor;
|