How to use the codemirror.defineExtension function in codemirror

To help you get started, we’ve selected a few codemirror examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mjmlio / mjml-app / src / helpers / codemirror-util-autoformat.js View on Github external
if (content == ';' && state.lexical && state.lexical.type == ')') return false
        return /^[;{}]$/.test(content) && !/^;/.test(textAfter)
      }
    },
  })

  CodeMirror.extendMode('xml', {
    commentStart: '',
    newlineAfterToken: function(type, content, textAfter) {
      return (type == 'tag' && />$/.test(content)) || /^
github probmods / webchurch / cm-comments.js View on Github external
if (options.fullLines != false) {
                var lastLineHasText = nonWS.test(self.getLine(end));
                self.replaceRange(pad + endString, Pos(end));
                self.replaceRange(startString + pad, Pos(from.line, 0));
                var lead = options.blockCommentLead || mode.blockCommentLead;
                if (lead != null) for (var i = from.line + 1; i <= end; ++i)
                    if (i != end || lastLineHasText)
                        self.replaceRange(lead + pad, Pos(i, 0));
            } else {
                self.replaceRange(endString, to);
                self.replaceRange(startString, from);
            }
        });
    });

    CodeMirror.defineExtension("uncomment", function(from, to, options) {
        if (!options) options = noOptions;
        var self = this, mode = self.getModeAt(from);
        var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end);

        // Try finding line comments
        var lineString = options.lineComment || mode.lineComment, lines = [];
        var pad = options.padding == null ? " " : options.padding, didSomething;
        lineComment: {
            if (!lineString) break lineComment;
            for (var i = start; i <= end; ++i) {
                var line = self.getLine(i);
                var found = line.indexOf(lineString);
                if (found == -1 && (i != end || i == start) && nonWS.test(line)) break lineComment;
                if (i != start && found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
                lines.push(line);
            }
github dagster-io / dagster / js_modules / dagit / src / configeditor / codemirror-yaml / lint.js View on Github external
var state = (cm.state.lint = new LintState(
      cm,
      parseOptions(cm, val),
      hasLintGutter
    ));
    if (state.options.lintOnChange !== false) cm.on("change", onChange);
    if (state.options.tooltips != false && state.options.tooltips != "gutter")
      CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
  }

  // XXX(freiksenet): This is commented out because currently codemirror
  // reloads plugins on every change, causing infinite linting
  // startLinting(this);
});

CodeMirror.defineExtension("performLint", function() {
  if (this.state.lint) startLinting(this);
});
github probmods / webchurch / cm-folding.js View on Github external
if (typeof widget == "string") {
            var text = document.createTextNode(widget);
            widget = document.createElement("span");
            widget.appendChild(text);
            widget.className = "CodeMirror-foldmarker";
        }
        return widget;
    }

    // Clumsy backwards-compatible interface
    CodeMirror.newFoldFunction = function(rangeFinder, widget) {
        return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
    };

    // New-style interface
    CodeMirror.defineExtension("foldCode", function(pos, options) { doFold(this, pos, options); });

    CodeMirror.registerHelper("fold", "combine", function() {
        var funcs = Array.prototype.slice.call(arguments, 0);
        return function(cm, start) {
            for (var i = 0; i < funcs.length; ++i) {
                var found = funcs[i](cm, start);
                if (found) return found;
            }
        };
    });
})();
github Kong / insomnia / packages / insomnia-app / app / ui / components / codemirror / extensions / autocomplete.js View on Github external
const COMPLETION_CLOSE_KEYS = /[}|-]/;
const MAX_HINT_LOOK_BACK = 100;
const TYPE_VARIABLE = 'variable';
const TYPE_TAG = 'tag';
const TYPE_CONSTANT = 'constant';
const MAX_CONSTANTS = -1;
const MAX_VARIABLES = -1;
const MAX_TAGS = -1;

const ICONS = {
  [TYPE_CONSTANT]: { char: '𝒄', title: 'Constant' },
  [TYPE_VARIABLE]: { char: '𝑥', title: 'Environment Variable' },
  [TYPE_TAG]: { char: 'ƒ', title: 'Generator Tag' },
};

CodeMirror.defineExtension('isHintDropdownActive', function() {
  return (
    this.state.completionActive &&
    this.state.completionActive.data &&
    this.state.completionActive.data.list &&
    this.state.completionActive.data.list.length
  );
});

CodeMirror.defineOption('environmentAutocomplete', null, (cm, options) => {
  if (!options) {
    return;
  }

  async function completeAfter(cm, fn, showAllOnNoMatch = false) {
    // Bail early if didn't match the callback test
    if (fn && !fn()) {
github tangrams / tangram-play / src / js / editor / codemirror / comment-tangram.js View on Github external
if (lead !== null) {
                for (let i = from.line + 1; i <= end; ++i) {
                    if (i !== end || lastLineHasText) {
                        self.replaceRange(lead + pad, cmPos(i, 0));
                    }
                }
            }
        }
        else {
            self.replaceRange(endString, to);
            self.replaceRange(startString, from);
        }
    });
});

CodeMirror.defineExtension('uncomment', function (from, to, options = DEFAULT_OPTIONS) {
    let self = this;
    let mode = self.getModeAt(from);
    let end = Math.min(to.ch !== 0 || to.line === from.line ? to.line : to.line - 1, self.lastLine());
    let start = Math.min(from.line, end);

    // Try finding line comments
    let lineString = options.lineComment || mode.lineComment;
    let lines = [];
    let pad = options.padding === undefined ? ' ' : options.padding;
    let didSomething;

    /* eslint-disable no-labels */
    lineComment: {
        if (!lineString) {
            // break lineComment;
            return false;
github probmods / webchurch / cm-brackets.js View on Github external
if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
            currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
        });
    }

    CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
        if (old && old != CodeMirror.Init)
            cm.off("cursorActivity", doMatchBrackets);
        if (val) {
            cm.state.matchBrackets = typeof val == "object" ? val : {};
            cm.on("cursorActivity", doMatchBrackets);
        }
    });

    CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
    CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config){
        return findMatchingBracket(this, pos, strict, config);
    });
    CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
        return scanForBracket(this, pos, dir, style, config);
    });

})()
github mathieudutour / kayero / src / hypermd / addon / hover.js View on Github external
this.tooltipIndicator.style.marginLeft = xOffset + 'px'
  },
  hideInfo: function () {
    if (this.tooltipDiv.parentElement === this.lineDiv) {
      this.lineDiv.removeChild(this.tooltipDiv)
    }
  }
}

function init () {
  var cm = this
  if (!cm.hmd) cm.hmd = {}
  cm.hmd.hover = new HMDHover(cm)
}

CodeMirror.defineExtension('hmdHoverInit', init)
github circlecell / jsoncompare.com / frontend / js / editor-init / highlight-line-extension.js View on Github external
import CodeMirror from 'codemirror';

CodeMirror.defineExtension('highlightErrorLine', function highlightErrorLine(line) {
    if (typeof line === 'number') {
        this.errorLine = this.addLineClass(line, 'background', 'lint-line-error');
    } else if (this.errorLine) {
        this.removeLineClass(this.errorLine, 'background', 'lint-line-error');
        this.errorLine = null;
    }
});
github probmods / webchurch / cm-brackets.js View on Github external
cm.operation(function() {
            if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
            currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
        });
    }

    CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
        if (old && old != CodeMirror.Init)
            cm.off("cursorActivity", doMatchBrackets);
        if (val) {
            cm.state.matchBrackets = typeof val == "object" ? val : {};
            cm.on("cursorActivity", doMatchBrackets);
        }
    });

    CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
    CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config){
        return findMatchingBracket(this, pos, strict, config);
    });
    CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
        return scanForBracket(this, pos, dir, style, config);
    });

})()