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

Commit

Permalink
Made Language.addFileExtension public, added a languageModified event…
Browse files Browse the repository at this point in the history
…, made DocumentManager update document's languages as necessary
  • Loading branch information
DennisKehrig committed Mar 11, 2013
1 parent 8bbe82d commit ba472a3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
22 changes: 21 additions & 1 deletion src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,26 @@ define(function (require, exports, module) {
*/
function _handleLanguageAdded(event, language) {
CollectionUtils.forEach(_openDocuments, function (doc, key) {
doc._updateLanguage();
// No need to look at the new language if this document has one already
if (doc.getLanguage().isFallbackLanguage()) {
doc._updateLanguage();
}
});
}

/**
* @private
* Update document
*/
function _handleLanguageModified(event, language) {
CollectionUtils.forEach(_openDocuments, function (doc, key) {
var docLanguage = doc.getLanguage();
// A modified language can affect a document
// - if its language was modified
// - if the document doesn't have a language yet and its file extension was added to the modified language
if (docLanguage === language || docLanguage.isFallbackLanguage()) {
doc._updateLanguage();
}
});
}

Expand Down Expand Up @@ -1255,4 +1274,5 @@ define(function (require, exports, module) {

// Handle Language change events
$(LanguageManager).on("languageAdded", _handleLanguageAdded);
$(LanguageManager).on("languageModified", _handleLanguageModified);
});
50 changes: 40 additions & 10 deletions src/language/LanguageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ define(function (require, exports, module) {
return _fallbackLanguage;
}

/**
* @private
* Notify listeners when a language is added
* @param {!Language} language The new language
*/
function _triggerLanguageAdded(language) {
// finally, store language to _language map
_languages[language.getId()] = language;
$(exports).triggerHandler("languageAdded", [language]);
}

/**
* @private
* Notify listeners when a language is modified
* @param {!Language} language The modified language
*/
function _triggerLanguageModified(language) {
$(exports).triggerHandler("languageModified", [language]);
}


/**
* @constructor
* Model for a language.
Expand Down Expand Up @@ -357,9 +378,8 @@ define(function (require, exports, module) {
* Private for now since dependent code would need to by kept in sync with such changes.
* See https://github.com/adobe/brackets/issues/2966 for plans to make this public.
* @param {!string} extension A file extension used by this language
* @private
*/
Language.prototype._addFileExtension = function (extension) {
Language.prototype.addFileExtension = function (extension) {
extension = _normalizeFileExtension(extension);

if (this._fileExtensions.indexOf(extension) === -1) {
Expand All @@ -376,6 +396,8 @@ define(function (require, exports, module) {
// $(this).triggerHandler("fileExtensionAdded", [extension]);
// $(exports).triggerHandler("fileExtensionAdded", [extension, this]);
}

this._wasModified();
}
};

Expand Down Expand Up @@ -468,17 +490,25 @@ define(function (require, exports, module) {
}
this._modeToLanguageMap[mode] = language;
};

/**
* Determines whether this is the fallback language or not
* @return {boolean} True if this is the fallback language, false otherwise
*/
Language.prototype.isFallbackLanguage = function () {
return this === _fallbackLanguage;
};

/**
* Trigger the "languageModified" event if this language is registered already
* @see _triggerLanguageModified
* @private
* Notify listeners when a language is added
* @param {!Language} language The new language
*/
function _triggerLanguageAdded(language) {
// finally, store language to _language map
_languages[language.getId()] = language;
$(exports).triggerHandler("languageAdded", [language]);
}
Language.prototype._wasModified = function () {
if (_languages[this._id]) {
_triggerLanguageModified(this);
}
};

/**
* Defines a language.
Expand Down Expand Up @@ -523,7 +553,7 @@ define(function (require, exports, module) {
// register language file extensions after mode has loaded
if (fileExtensions) {
for (i = 0; i < fileExtensions.length; i++) {
language._addFileExtension(fileExtensions[i]);
language.addFileExtension(fileExtensions[i]);
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/spec/LanguageManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,46 @@ define(function (require, exports, module) {
});
});

it("should update the document's language when a language is modified", function () {
var unknown,
doc,
spy,
modifiedLanguage,
promise;

runs(function () {
// Create a shell script file
doc = SpecRunnerUtils.createMockActiveDocument({ filename: "test.foo" });

// Initial language will be unknown (shell is not a default language)
unknown = LanguageManager.getLanguage("unknown");

// listen for event
spy = jasmine.createSpy("languageChanged event handler");
$(doc).on("languageChanged", spy);

// sanity check language
expect(doc.getLanguage()).toBe(unknown);

// make active
doc.addRef();

modifiedLanguage = LanguageManager.getLanguage("html");
modifiedLanguage.addFileExtension("foo");

// language should change
expect(doc.getLanguage()).toBe(modifiedLanguage);
expect(spy).toHaveBeenCalled();
expect(spy.callCount).toEqual(1);

// check callback args (arg 0 is a jQuery event)
expect(spy.mostRecentCall.args[1]).toBe(unknown);
expect(spy.mostRecentCall.args[2]).toBe(modifiedLanguage);

// cleanup
doc.releaseRef();
});
});
});
});
});

0 comments on commit ba472a3

Please sign in to comment.