Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Update to latest CodeMirror:
Browse files Browse the repository at this point in the history
* Explicitly call markClean() after clearHistory() since the two are no longer tied
* Pass new "precise" parameter to getTokenAt() to ensure tokens are properly updated after edits (addresses #2335)
  • Loading branch information
njx committed Jun 14, 2013
1 parent 75721fe commit 157775b
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,21 +675,23 @@ define(function (require, exports, module) {
};

/**
* Sets the contents of the editor and clears the undo/redo history. Dispatches a change event.
* Sets the contents of the editor, clears the undo/redo history and marks the document clean. Dispatches a change event.
* Semi-private: only Document should call this.
* @param {!string} text
*/
Editor.prototype._resetText = function (text) {
var perfTimerName = PerfUtils.markStart("Edtitor._resetText()\t" + (!this.document || this.document.file.fullPath));
var perfTimerName = PerfUtils.markStart("Editor._resetText()\t" + (!this.document || this.document.file.fullPath));

var cursorPos = this.getCursorPos(),
scrollPos = this.getScrollPos();

// This *will* fire a change event, but we clear the undo immediately afterward
this._codeMirror.setValue(text);

// Make sure we can't undo back to the empty state before setValue()
// Make sure we can't undo back to the empty state before setValue(), and mark
// the document clean.
this._codeMirror.clearHistory();
this._codeMirror.markClean();

// restore cursor and scroll positions
this.setCursorPos(cursorPos);
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptCodeHints/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ define(function (require, exports, module) {
var cm = this.editor._codeMirror;

if (cursor) {
return cm.getTokenAt(cursor);
return cm.getTokenAt(cursor, true);
} else {
return cm.getTokenAt(this.getCursor());
return cm.getTokenAt(this.getCursor(), true);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptQuickEdit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ define(function (require, exports, module) {
* @return {String} token string at the specified position
*/
function _getFunctionName(hostEditor, pos) {
var token = hostEditor._codeMirror.getTokenAt(pos);
var token = hostEditor._codeMirror.getTokenAt(pos, true);

// If the pos is at the beginning of a name, token will be the
// preceding whitespace or dot. In that case, try the next pos.
if (token.string.trim().length === 0 || token.string === ".") {
token = hostEditor._codeMirror.getTokenAt({line: pos.line, ch: pos.ch + 1});
token = hostEditor._codeMirror.getTokenAt({line: pos.line, ch: pos.ch + 1}, true);
}

// Return valid function expressions only (function call or reference)
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/default/QuickView/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ define(function (require, exports, module) {
}

// Query providers for a new popoverState
var token = cm.getTokenAt(pos);
var token = cm.getTokenAt(pos, true);
popoverState = queryPreviewProviders(editor, pos, token);

if (popoverState) {
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/default/QuickView/unittests.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ define(function (require, exports, module) {
token;

editor.setCursorPos(pos);
token = cm.getTokenAt(pos);
token = cm.getTokenAt(pos, true);

return QuickView._queryPreviewProviders(editor, pos, token);
}
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/samples/InlineImageViewer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ define(function (require, exports, module) {
* @return {String} token string at the specified position
*/
function _getStringAtPos(hostEditor, pos) {
var token = hostEditor._codeMirror.getTokenAt(pos);
var token = hostEditor._codeMirror.getTokenAt(pos, true);

// If the pos is at the beginning of a name, token will be the
// preceding whitespace or dot. In that case, try the next pos.
if (token.string.trim().length === 0 || token.string === ".") {
token = hostEditor._codeMirror.getTokenAt({line: pos.line, ch: pos.ch + 1});
token = hostEditor._codeMirror.getTokenAt({line: pos.line, ch: pos.ch + 1}, true);
}

if (token.type === "string") {
Expand Down
6 changes: 3 additions & 3 deletions src/language/CSSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ define(function (require, exports, module) {
offset = TokenUtils.offsetInToken(ctx),
canAddNewOne = false,
testPos = {ch: ctx.pos.ch + 1, line: ctx.pos.line},
testToken = editor._codeMirror.getTokenAt(testPos),
testToken = editor._codeMirror.getTokenAt(testPos, true),
propName;

// Get property name first. If we don't have a valid property name, then
Expand Down Expand Up @@ -376,7 +376,7 @@ define(function (require, exports, module) {
propValues = [],
offset = TokenUtils.offsetInToken(ctx),
testPos = {ch: ctx.pos.ch + 1, line: ctx.pos.line},
testToken = editor._codeMirror.getTokenAt(testPos);
testToken = editor._codeMirror.getTokenAt(testPos, true);

// Currently only support url. May be null if starting to type
if (ctx.token.className && ctx.token.className !== "string") {
Expand Down Expand Up @@ -454,7 +454,7 @@ define(function (require, exports, module) {
propName = ctx.token.string;
} else {
var testPos = {ch: ctx.pos.ch + 1, line: ctx.pos.line},
testToken = editor._codeMirror.getTokenAt(testPos);
testToken = editor._codeMirror.getTokenAt(testPos, true);

if (testToken.type === "property" || testToken.type === "property error" || testToken.type === "tag") {
propName = testToken.string;
Expand Down
2 changes: 1 addition & 1 deletion src/language/HTMLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ define(function (require, exports, module) {
// EOL, it uses EOL, same token is returned, and the following condition fails,
// so we don't need to worry about testPos being valid.
var testPos = {ch: ctx.pos.ch + 1, line: ctx.pos.line},
testToken = editor._codeMirror.getTokenAt(testPos);
testToken = editor._codeMirror.getTokenAt(testPos, true);

if (testToken.string.length > 0 && testToken.string.trim().length > 0 &&
testToken.string.charAt(0) !== ">") {
Expand Down
2 changes: 1 addition & 1 deletion src/thirdparty/CodeMirror2
8 changes: 4 additions & 4 deletions src/utils/TokenUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define(function (require, exports, module) {
return {
"editor": editor,
"pos": pos,
"token": editor.getTokenAt(pos)
"token": editor.getTokenAt(pos, true)
};
}

Expand All @@ -64,7 +64,7 @@ define(function (require, exports, module) {
} else {
ctx.pos.ch = ctx.token.start;
}
ctx.token = ctx.editor.getTokenAt(ctx.pos);
ctx.token = ctx.editor.getTokenAt(ctx.pos, true);
return true;
}

Expand All @@ -85,7 +85,7 @@ define(function (require, exports, module) {
} else {
ctx.pos.ch = ctx.token.end + 1;
}
ctx.token = ctx.editor.getTokenAt(ctx.pos);
ctx.token = ctx.editor.getTokenAt(ctx.pos, true);
return true;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ define(function (require, exports, module) {
*/
function getModeAt(cm, pos) {
var outerMode = cm.getMode(),
modeData = CodeMirror.innerMode(outerMode, cm.getTokenAt(pos).state),
modeData = CodeMirror.innerMode(outerMode, cm.getTokenAt(pos, true).state),
name;

name = (modeData.mode.name === "xml") ?
Expand Down
7 changes: 0 additions & 7 deletions test/spec/EditorCommandHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,6 @@ define(function (require, exports, module) {
expect(myDocument.getText()).toEqual(expectedCommentedText);
expectSel(expectedCommentedSel);

// Toggle comment off
// Can't immediately call BLOCK_COMMENT again to uncomment because CodeMirror might not
// be done re-tokenizing in response to the first toggle, and BLOCK_COMMENT depends on
// getting correct tokens. See #2335. Ideally we'd listen for onHighlightComplete() but
// it's not clear that will always get called (if CM decides no async work was needed).
// So we just wait until after the async tokenization must have been run.
waits(200);
runs(function () {
CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(startingContent);
Expand Down

0 comments on commit 157775b

Please sign in to comment.