How to use the codemirror.countColumn 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 joeltg / ozymandias / client / src / editor / sublime.js View on Github external
cm.operation(function() {
    var cursors = cm.listSelections();
    var indentUnit = cm.getOption("indentUnit");

    for (var i = cursors.length - 1; i >= 0; i--) {
      var cursor = cursors[i].head;
      var toStartOfLine = cm.getRange({line: cursor.line, ch: 0}, cursor);
      var column = CodeMirror.countColumn(toStartOfLine, null, cm.getOption("tabSize"));

      // Delete by one character by default
      var deletePos = cm.findPosH(cursor, -1, "char", false);

      if (toStartOfLine && !/\S/.test(toStartOfLine) && column % indentUnit == 0) {
        var prevIndent = new Pos(cursor.line,
          CodeMirror.findColumn(toStartOfLine, column - indentUnit, indentUnit));

        // Smart delete only if we found a valid prevIndent location
        if (prevIndent.ch != cursor.ch) deletePos = prevIndent;
      }

      cm.replaceRange("", deletePos, cursor, "+delete");
    }
  });
};
github codemirror / google-modes / src / c_indent.js View on Github external
function baseIndent(cx, config) {
  for (let startLine = cx.startLine;; cx = cx.parent) {
    if (cx.name == "CondExpr")
      return CodeMirror.countColumn(cx.startLine, cx.startPos + 1, config.tabSize)
    if (statementish.indexOf(cx.name) > -1 && /(^\s*|[\(\{\[])$/.test(cx.startLine.slice(0, cx.startPos)))
      return CodeMirror.countColumn(cx.startLine, cx.startPos, config.tabSize)
    if (!cx.parent || cx.parent.startLine != startLine)
      return CodeMirror.countColumn(cx.startLine, null, config.tabSize)
  }
}
github codemirror / google-modes / src / c_indent.js View on Github external
export function indent(state, textAfter, line, config) {
  let top = state.context && state.context.name
  if (top == "DeclType" || top == "BeforeStatement" || top == "AnnotationHead" || top == "Template" || top == "str")
    return statementIndent(state.context, config)

  if ((top == "doccomment.braced" || top == "doccomment.tagGroup") && !/^[@*]/.test(textAfter))
    return CodeMirror.countColumn(state.context.startLine, null, config.tabSize) + 2 * config.indentUnit

  let passLine = config.forceContent && /^\s*(\/\/.*)?$/.test(line) ? "x" : line
  return findIndent(state.contextAt(passLine, line.length - textAfter.length), textAfter, config)
}
github codemirror / google-modes / src / c_indent.js View on Github external
function baseIndent(cx, config) {
  for (let startLine = cx.startLine;; cx = cx.parent) {
    if (cx.name == "CondExpr")
      return CodeMirror.countColumn(cx.startLine, cx.startPos + 1, config.tabSize)
    if (statementish.indexOf(cx.name) > -1 && /(^\s*|[\(\{\[])$/.test(cx.startLine.slice(0, cx.startPos)))
      return CodeMirror.countColumn(cx.startLine, cx.startPos, config.tabSize)
    if (!cx.parent || cx.parent.startLine != startLine)
      return CodeMirror.countColumn(cx.startLine, null, config.tabSize)
  }
}
github mb21 / panwriter / src / React / Basic / CodeMirror.js View on Github external
editor.on("renderLine", function(cm, line, elt) {
        var txt  = line.text
          , matches = txt.trim().match(listRe)
          ;
        if (matches && matches[1]) {
          var extraIndent = matches[1].length
            , columnCount = CodeMirror.countColumn(txt, null, cm.getOption("tabSize"))
            , off = (columnCount + extraIndent) * charWidth
            ;
          elt.style.textIndent = "-" + off + "px";
          elt.style.paddingLeft = (basePadding + off) + "px";
        }
      });
      editor.refresh();
github codemirror / google-modes / src / python.js View on Github external
function findIndent(cx, textAfter, curLine, config) {
  if (!cx) return 0
  if (cx.name == "string") return CodeMirror.Pass

  let brack = bracketed[cx.name]
  if (brack) {
    if (curLine != cx.startLine && aligned(cx))
      return CodeMirror.countColumn(cx.startLine, cx.startPos, config.tabSize) + 1

    let closed = textAfter && textAfter.charAt(0) == brack
    let flat = closed && brack != ")" || curLine == cx.startLine
    return findIndent(cx.parent, closed ? null : textAfter, cx.startLine, config) + (flat ? 0 : 2 * config.indentUnit)
  } else if (cx.name == "indentedBody") {
    for (;; cx = cx.parent) {
      if (!cx) return config.indentUnit
      if (cx.name == "Statement") return CodeMirror.countColumn(cx.startLine, null, config.tabSize) + config.indentUnit
    }
  } else {
    return findIndent(cx.parent, textAfter, curLine, config) +
      ((cx.name == "DictProp" || cx.name == "Statement") && cx.startLine != curLine ? 2 * config.indentUnit : 0)
  }
}
github codemirror / google-modes / src / python.js View on Github external
function findIndent(cx, textAfter, curLine, config) {
  if (!cx) return 0
  if (cx.name == "string") return CodeMirror.Pass

  let brack = bracketed[cx.name]
  if (brack) {
    if (curLine != cx.startLine && aligned(cx))
      return CodeMirror.countColumn(cx.startLine, cx.startPos, config.tabSize) + 1

    let closed = textAfter && textAfter.charAt(0) == brack
    let flat = closed && brack != ")" || curLine == cx.startLine
    return findIndent(cx.parent, closed ? null : textAfter, cx.startLine, config) + (flat ? 0 : 2 * config.indentUnit)
  } else if (cx.name == "indentedBody") {
    for (;; cx = cx.parent) {
      if (!cx) return config.indentUnit
      if (cx.name == "Statement") return CodeMirror.countColumn(cx.startLine, null, config.tabSize) + config.indentUnit
    }
  } else {
    return findIndent(cx.parent, textAfter, curLine, config) +
      ((cx.name == "DictProp" || cx.name == "Statement") && cx.startLine != curLine ? 2 * config.indentUnit : 0)
  }
}