Skip to content

Commit

Permalink
Customize the CodeMirror Tern text to manage 'this' for Mongo Map case.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Jun 11, 2013
1 parent 1b5910a commit 508b202
Show file tree
Hide file tree
Showing 8 changed files with 5,710 additions and 12 deletions.
114 changes: 114 additions & 0 deletions codemirror-extension/addon/format/formatting.js
@@ -0,0 +1,114 @@
(function() {

CodeMirror.extendMode("css", {
commentStart: "/*",
commentEnd: "*/",
newlineAfterToken: function(_type, content) {
return /^[;{}]$/.test(content);
}
});

CodeMirror.extendMode("javascript", {
commentStart: "/*",
commentEnd: "*/",
// FIXME semicolons inside of for
newlineAfterToken: function(_type, content, textAfter, state) {
if (this.jsonMode) {
return /^[\[,{]$/.test(content) || /^}/.test(textAfter);
} else {
if (content == ";" && state.lexical && state.lexical.type == ")") return false;
return /^[;{}]$/.test(content) && !/^;/.test(textAfter);
}
}
});

var inlineElements = /^(a|abbr|acronym|area|base|bdo|big|br|button|caption|cite|code|col|colgroup|dd|del|dfn|em|frame|hr|iframe|img|input|ins|kbd|label|legend|link|map|object|optgroup|option|param|q|samp|script|select|small|span|strong|sub|sup|textarea|tt|var)$/;

CodeMirror.extendMode("xml", {
commentStart: "<!--",
commentEnd: "-->",
newlineAfterToken: function(type, content, textAfter, state) {
var inline = false;
if (this.configuration == "html")
inline = state.context ? inlineElements.test(state.context.tagName) : false;
return !inline && ((type == "tag" && />$/.test(content) && state.context) ||
/^</.test(textAfter));
}
});

// Comment/uncomment the specified range
CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
var cm = this, curMode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(from).state).mode;
cm.operation(function() {
if (isComment) { // Comment range
cm.replaceRange(curMode.commentEnd, to);
cm.replaceRange(curMode.commentStart, from);
if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside
cm.setCursor(from.line, from.ch + curMode.commentStart.length);
} else { // Uncomment range
var selText = cm.getRange(from, to);
var startIndex = selText.indexOf(curMode.commentStart);
var endIndex = selText.lastIndexOf(curMode.commentEnd);
if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
// Take string till comment start
selText = selText.substr(0, startIndex)
// From comment start till comment end
+ selText.substring(startIndex + curMode.commentStart.length, endIndex)
// From comment end till string end
+ selText.substr(endIndex + curMode.commentEnd.length);
}
cm.replaceRange(selText, from, to);
}
});
});

// Applies automatic mode-aware indentation to the specified range
CodeMirror.defineExtension("autoIndentRange", function (from, to) {
var cmInstance = this;
this.operation(function () {
for (var i = from.line; i <= to.line; i++) {
cmInstance.indentLine(i, "smart");
}
});
});

// Applies automatic formatting to the specified range
CodeMirror.defineExtension("autoFormatRange", function (from, to) {
var cm = this;
var outer = cm.getMode(), text = cm.getRange(from, to).split("\n");
var state = CodeMirror.copyState(outer, cm.getTokenAt(from).state);
var tabSize = cm.getOption("tabSize");

var out = "", lines = 0, atSol = from.ch == 0;
function newline() {
out += "\n";
atSol = true;
++lines;
}

for (var i = 0; i < text.length; ++i) {
var stream = new CodeMirror.StringStream(text[i], tabSize);
while (!stream.eol()) {
var inner = CodeMirror.innerMode(outer, state);
var style = outer.token(stream, state), cur = stream.current();
stream.start = stream.pos;
if (!atSol || /\S/.test(cur)) {
out += cur;
atSol = false;
}
if (!atSol && inner.mode.newlineAfterToken &&
inner.mode.newlineAfterToken(style, cur, stream.string.slice(stream.pos) || text[i+1] || "", inner.state))
newline();
}
if (!stream.pos && outer.blankLine) outer.blankLine(state);
if (!atSol && i < text.length - 1) newline();
}

cm.operation(function () {
cm.replaceRange(out, from, to);
for (var cur = from.line + 1, end = from.line + lines; cur <= end; ++cur)
cm.indentLine(cur, "smart");
cm.setSelection(from, cm.getCursor(false));
});
});
})();
29 changes: 22 additions & 7 deletions codemirror-javascript/addon/hint/tern/tern-hint.js
Expand Up @@ -2,6 +2,16 @@
var server = null, defs = [];
var Pos = CodeMirror.Pos;

function TernState(cm, options) {
this.options = options;
}

function parseOptions(options) {
if (options instanceof Function) return {getText: options};
else if (!options || !options.getText) throw new Error("Required option 'getText' missing (tern-hint addon)");
return options;
}

function load(file, c) {
var xhr = new XMLHttpRequest();
xhr.open("get", file, true);
Expand Down Expand Up @@ -181,8 +191,9 @@
if (url) {
content += '<li>See <a href="' + url + '" target="_blank" >'
+ url + '</a></li>';
/*content += '<li><iframe src="' + url
+ '" ></iframe></li>';*/
/*
* content += '<li><iframe src="' + url + '" ></iframe></li>';
*/
}
content += '</ul>'
}
Expand Down Expand Up @@ -212,12 +223,18 @@
}

function buildRequest(cm, query, allowFragments) {
var text = null;
if (cm.state && cm.state.tern) {
text = cm.state.tern.options.getText(cm);
} else {
text = cm.getValue();
}
// files
var files = [];
files.push({
type : "full",
name : "cm-tern",
text : cm.getValue()
text : text
});
// query
query.lineCharPositions = true;
Expand Down Expand Up @@ -272,13 +289,11 @@

CodeMirror.defineOption("ternWith", false, function(cm, val, old) {
if (old && old != CodeMirror.Init) {
/*
* clearMarks(cm); cm.off("change", onChange); delete cm._foldingState;
*/
delete cm.state.tern;
}

if (val) {

var state = cm.state.tern = new TernState(cm, parseOptions(val));
}
});

Expand Down
5 changes: 2 additions & 3 deletions codemirror-javascript/demo/javascript-all.html
Expand Up @@ -16,7 +16,7 @@
<script src="../../codemirror/addon/runmode/runmode.js"></script>

<script src="../../codemirror/mode/javascript/javascript.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jshint/r07/jshint.js"></script>
<script src="../../jshint/jshint-r12-80277ef.js"></script>
<script src="../../codemirror/addon/lint/lint.js"></script>
<link rel="stylesheet" href="../../codemirror/addon/lint/lint.css">
<script src="../../codemirror/addon/lint/javascript-lint.js"></script>
Expand Down Expand Up @@ -103,8 +103,7 @@ <h1>Javascript - Lint + Hint with Tern demo</h1>
"Ctrl-Space": "autocomplete"
},
gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers"],
lintWith: CodeMirror.javascriptValidator,
ternWith : true
lintWith: CodeMirror.javascriptValidator
});
</script>

Expand Down
3 changes: 1 addition & 2 deletions codemirror-javascript/demo/javascript-tern.html
Expand Up @@ -97,8 +97,7 @@ <h1>Javascript - Hint with Tern demo</h1>
"'.'": passAndHint,
"Ctrl-Space": "autocomplete"
},
gutters: ["CodeMirror-linenumbers"],
ternWith : true
gutters: ["CodeMirror-linenumbers"]
});
</script>
</body>
Expand Down

0 comments on commit 508b202

Please sign in to comment.