Skip to content

Commit

Permalink
- Ability to remove file extension/name mappings from existing Language
Browse files Browse the repository at this point in the history
(adobe#6873) - with unit tests.
- Remove inaccurate docs on Language add extension/name APIs
- Fix PHP mixed-mode handling so token-level detection (getLanguageForMode())
works - similar to our handling of "htmlmixed" mode. Allows us to remove
"clike" dummy language without breaking PHP Toggle Comment functionality
  • Loading branch information
peterflynn authored and turhothgor-dev committed Mar 29, 2014
1 parent cb540c4 commit 815994e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
48 changes: 45 additions & 3 deletions src/language/LanguageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ define(function (require, exports, module) {
/**
* Adds a file extension to this language.
* @param {!string} extension A file extension used by this language
* @return {boolean} Whether adding the file extension was successful or not
*/
Language.prototype.addFileExtension = function (extension) {
// Remove a leading dot if present
Expand All @@ -502,10 +501,32 @@ define(function (require, exports, module) {
}
};

/**
* Unregisters a file extension from this language.
* @param {!string} extension File extension to stop using for this language
*/
Language.prototype.removeFileExtension = function (extension) {
// Remove a leading dot if present
if (extension.charAt(0) === ".") {
extension = extension.substr(1);
}

// Make checks below case-INsensitive
extension = extension.toLowerCase();

var index = this._fileExtensions.indexOf(extension);
if (index !== -1) {
this._fileExtensions.splice(index, 1);

delete _fileExtensionToLanguageMap[extension];

this._wasModified();
}
};

/**
* Adds a file name to the language which is used to match files that don't have extensions like "Makefile" for example.
* @param {!string} extension An extensionless file name used by this language
* @return {boolean} Whether adding the file name was successful or not
*/
Language.prototype.addFileName = function (name) {
// Make checks below case-INsensitive
Expand All @@ -523,7 +544,24 @@ define(function (require, exports, module) {

this._wasModified();
}
return true;
};

/**
* Unregisters a file name from this language.
* @param {!string} extension An extensionless file name used by this language
*/
Language.prototype.removeFileName = function (name) {
// Make checks below case-INsensitive
name = name.toLowerCase();

var index = this._fileNames.indexOf(name);
if (index !== -1) {
this._fileNames.splice(index, 1);

delete _fileNameToLanguageMap[name];

this._wasModified();
}
};

/**
Expand Down Expand Up @@ -805,6 +843,10 @@ define(function (require, exports, module) {
// But for now, we need to associate this madeup "html" mode with our HTML language object.
_setLanguageForMode("html", html);

// Similarly, the php mode uses clike internally for the PHP parts
var php = getLanguage("php");
php._setLanguageForMode("clike", php);

// The fallback language for unknown modes and file extensions
_fallbackLanguage = getLanguage("unknown");
});
Expand Down
4 changes: 3 additions & 1 deletion src/language/languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
"php": {
"name": "PHP",
"mode": "php",
"fileExtensions": ["php", "php3", "php4", "php5", "phtm", "phtml", "ctp"]
"fileExtensions": ["php", "php3", "php4", "php5", "phtm", "phtml", "ctp"],
"blockComment": ["/*", "*/"],
"lineComment": ["//", "#"]
},

"c": {
Expand Down
28 changes: 28 additions & 0 deletions test/spec/LanguageManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,34 @@ define(function (require, exports, module) {
expect(LanguageManager.getLanguageForPath("cakefile.doesNotExist")).toBe(unknown);
expect(LanguageManager.getLanguageForPath("Something.cakefile")).toBe(unknown);
});

it("should remove file extensions and add to new languages", function () {
var html = LanguageManager.getLanguage("html"),
ruby = LanguageManager.getLanguage("ruby"),
unknown = LanguageManager.getLanguage("unknown");

expect(LanguageManager.getLanguageForPath("test.html")).toBe(html);

html.removeFileExtension("html");
expect(LanguageManager.getLanguageForPath("test.html")).toBe(unknown);

ruby.addFileExtension("html");
expect(LanguageManager.getLanguageForPath("test.html")).toBe(ruby);
});

it("should remove file names and add to new languages", function () {
var coffee = LanguageManager.getLanguage("coffeescript"),
html = LanguageManager.getLanguage("html"),
unknown = LanguageManager.getLanguage("unknown");

expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(coffee);

coffee.removeFileName("Cakefile");
expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(unknown);

html.addFileName("Cakefile");
expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(html);
});
});

describe("defineLanguage", function () {
Expand Down

0 comments on commit 815994e

Please sign in to comment.