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

Commit

Permalink
Merge branch 'dk/less-refactoring' into jasonsanjose/language-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsanjose committed Feb 22, 2013
2 parents e0ae992 + d7411eb commit 3c3fb17
Show file tree
Hide file tree
Showing 21 changed files with 838 additions and 375 deletions.
48 changes: 41 additions & 7 deletions src/document/DocumentManager.js
Expand Up @@ -23,7 +23,7 @@


/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $ */
/*global define, $, PathUtils */

/**
* DocumentManager maintains a list of currently 'open' Documents. It also owns the list of files in
Expand Down Expand Up @@ -92,7 +92,8 @@ define(function (require, exports, module) {
Async = require("utils/Async"),
CollectionUtils = require("utils/CollectionUtils"),
PerfUtils = require("utils/PerfUtils"),
Commands = require("command/Commands");
Commands = require("command/Commands"),
LanguageManager = require("language/LanguageManager");

/**
* Unique PreferencesManager clientID
Expand Down Expand Up @@ -598,6 +599,11 @@ define(function (require, exports, module) {
this.file = file;
this.refreshText(rawText, initialTimestamp);

this._updateLanguage();
// TODO: remove this listener when the document object is obsolete.
// But when is this the case? When _refCount === 0?
$(this.file).on("rename", this._updateLanguage.bind(this));

// This is a good point to clean up any old dangling Documents
_gcDocuments();
}
Expand All @@ -613,6 +619,12 @@ define(function (require, exports, module) {
* @type {!FileEntry}
*/
Document.prototype.file = null;

/**
* The Language for this document. Will be resolved by file extension in the constructor
* @type {!Language}
*/
Document.prototype.language = null;

/**
* Whether this document has unsaved changes or not.
Expand Down Expand Up @@ -929,6 +941,28 @@ define(function (require, exports, module) {
return "[Document " + this.file.fullPath + dirtyInfo + editorInfo + refInfo + "]";
};

/**
* Returns the language this document is written in.
* The language returned is based on the file extension.
* @return {Language} An object describing the language used in this document
*/
Document.prototype.getLanguage = function () {
return this.language;
};

/**
* Updates the language according to the file extension
*/
Document.prototype._updateLanguage = function () {
var oldLanguage = this.language;
var ext = PathUtils.filenameExtension(this.file.fullPath);
this.language = LanguageManager.getLanguageForFileExtension(ext);

if (oldLanguage && oldLanguage !== this.language) {
$(this).triggerHandler("languageChanged", [oldLanguage, this.language]);
}
};

/**
* Gets an existing open Document for the given file, or creates a new one if the Document is
* not currently open ('open' means referenced by the UI somewhere). Always use this method to
Expand Down Expand Up @@ -1136,16 +1170,16 @@ define(function (require, exports, module) {
var keysToDelete = [];
for (path in _openDocuments) {
if (_openDocuments.hasOwnProperty(path)) {
if (path.indexOf(oldName) === 0) {
if (FileUtils.isAffectedWhenRenaming(path, oldName, newName, isFolder)) {
// Copy value to new key
var newKey = path.replace(oldName, newName);

_openDocuments[newKey] = _openDocuments[path];
keysToDelete.push(path);

// Update document file
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName);
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName, isFolder);

if (!isFolder) {
// If the path name is a file, there can only be one matched entry in the open document
// list, which we just updated. Break out of the for .. in loop.
Expand All @@ -1161,13 +1195,13 @@ define(function (require, exports, module) {

// Update working set
for (i = 0; i < _workingSet.length; i++) {
FileUtils.updateFileEntryPath(_workingSet[i], oldName, newName);
FileUtils.updateFileEntryPath(_workingSet[i], oldName, newName, isFolder);
}

// Send a "fileNameChanged" event. This will trigger the views to update.
$(exports).triggerHandler("fileNameChange", [oldName, newName]);
}

// Define public API
exports.Document = Document;
exports.getCurrentDocument = getCurrentDocument;
Expand Down
56 changes: 32 additions & 24 deletions src/editor/Editor.js
Expand Up @@ -284,15 +284,11 @@ define(function (require, exports, module) {
* @param {!boolean} makeMasterEditor If true, this Editor will set itself as the (secret) "master"
* Editor for the Document. If false, this Editor will attach to the Document as a "slave"/
* secondary editor.
* @param {!(string|Object)} mode Syntax-highlighting language mode; "" means plain-text mode.
* May either be a string naming the mode, or an object containing a "name" property
* naming the mode along with configuration options required by the mode.
* See {@link EditorUtils#getModeFromFileExtension()}.
* @param {!jQueryObject} container Container to add the editor to.
* @param {{startLine: number, endLine: number}=} range If specified, range of lines within the document
* to display in this editor. Inclusive.
*/
function Editor(document, makeMasterEditor, mode, container, range) {
function Editor(document, makeMasterEditor, container, range) {
var self = this;

_instances.push(this);
Expand All @@ -308,8 +304,12 @@ define(function (require, exports, module) {
// store this-bound version of listeners so we can remove them later
this._handleDocumentChange = this._handleDocumentChange.bind(this);
this._handleDocumentDeleted = this._handleDocumentDeleted.bind(this);
this._handleDocumentLanguageChanged = this._handleDocumentLanguageChanged.bind(this);
$(document).on("change", this._handleDocumentChange);
$(document).on("deleted", this._handleDocumentDeleted);
$(document).on("languageChanged", this._handleDocumentLanguageChanged);

var mode = this._getModeFromDocument();

// (if makeMasterEditor, we attach the Doc back to ourselves below once we're fully initialized)

Expand Down Expand Up @@ -346,13 +346,6 @@ define(function (require, exports, module) {
"Cmd-Left": "goLineStartSmart"
};

// We'd like null/"" to mean plain text mode. CodeMirror defaults to plaintext for any
// unrecognized mode, but it complains on the console in that fallback case: so, convert
// here so we're always explicit, avoiding console noise.
if (!mode) {
mode = "text/plain";
}

// Create the CodeMirror instance
// (note: CodeMirror doesn't actually require using 'new', but jslint complains without it)
this._codeMirror = new CodeMirror(container, {
Expand Down Expand Up @@ -436,6 +429,7 @@ define(function (require, exports, module) {
this.document.releaseRef();
$(this.document).off("change", this._handleDocumentChange);
$(this.document).off("deleted", this._handleDocumentDeleted);
$(this.document).off("languageChanged", this._handleDocumentLanguageChanged);

if (this._visibleRange) { // TextRange also refs the Document
this._visibleRange.dispose();
Expand All @@ -453,6 +447,18 @@ define(function (require, exports, module) {
});
};

/**
* Determine the mode to use from the document's language
* Uses "text/plain" if the language does not define a mode
* @return string The mode to use
*/
Editor.prototype._getModeFromDocument = function () {
// We'd like undefined/null/"" to mean plain text mode. CodeMirror defaults to plaintext for any
// unrecognized mode, but it complains on the console in that fallback case: so, convert
// here so we're always explicit, avoiding console noise.
return this.document.getLanguage().mode || "text/plain";
};


/**
* Selects all text and maintains the current scroll position.
Expand Down Expand Up @@ -598,6 +604,14 @@ define(function (require, exports, module) {
$(this).triggerHandler("lostContent", [event]);
};

/**
* Responds to language changes, for instance when the file extension is changed.
*/
Editor.prototype._handleDocumentLanguageChanged = function (event) {
var mode = this._getModeFromDocument();
this._codeMirror.setOption("mode", this._getModeFromDocument());
};


/**
* Install event handlers on the CodeMirror instance, translating them into
Expand Down Expand Up @@ -1195,7 +1209,7 @@ define(function (require, exports, module) {
*
* @return {?(Object|string)} Name of syntax-highlighting mode, or object containing a "name" property
* naming the mode along with configuration options required by the mode.
* See {@link EditorUtils#getModeFromFileExtension()}.
* See {@link Languages#getLanguageFromFileExtension()} and {@link Language#mode}.
*/
Editor.prototype.getModeForSelection = function () {
// Check for mixed mode info
Expand All @@ -1221,25 +1235,19 @@ define(function (require, exports, module) {
}
};

Editor.prototype.getLanguageForSelection = function () {
return this.document.getLanguage().getLanguageForMode(this.getModeForSelection());
};

/**
* Gets the syntax-highlighting mode for the document.
*
* @return {Object|String} Object or Name of syntax-highlighting mode; see {@link EditorUtils#getModeFromFileExtension()}.
* @return {Object|String} Object or Name of syntax-highlighting mode; see {@link Languages#getLanguageFromFileExtension()} and {@link Language#mode}.
*/
Editor.prototype.getModeForDocument = function () {
return this._codeMirror.getOption("mode");
};

/**
* Sets the syntax-highlighting mode for the document.
*
* @param {(string|Object)} mode Name of syntax highlighting mode, or object containing a "name"
* property naming the mode along with configuration options required by the mode.
*/
Editor.prototype.setModeForDocument = function (mode) {
this._codeMirror.setOption("mode", mode);
};

/**
* The Document we're bound to
* @type {!Document}
Expand Down

0 comments on commit 3c3fb17

Please sign in to comment.