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

Commit 78ac6ea

Browse files
committed
- Ability to remove file extension/name mappings from existing Language
(#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
1 parent 247d711 commit 78ac6ea

3 files changed

Lines changed: 76 additions & 4 deletions

File tree

src/language/LanguageManager.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ define(function (require, exports, module) {
468468
/**
469469
* Adds a file extension to this language.
470470
* @param {!string} extension A file extension used by this language
471-
* @return {boolean} Whether adding the file extension was successful or not
472471
*/
473472
Language.prototype.addFileExtension = function (extension) {
474473
// Remove a leading dot if present
@@ -493,10 +492,32 @@ define(function (require, exports, module) {
493492
}
494493
};
495494

495+
/**
496+
* Unregisters a file extension from this language.
497+
* @param {!string} extension File extension to stop using for this language
498+
*/
499+
Language.prototype.removeFileExtension = function (extension) {
500+
// Remove a leading dot if present
501+
if (extension.charAt(0) === ".") {
502+
extension = extension.substr(1);
503+
}
504+
505+
// Make checks below case-INsensitive
506+
extension = extension.toLowerCase();
507+
508+
var index = this._fileExtensions.indexOf(extension);
509+
if (index !== -1) {
510+
this._fileExtensions.splice(index, 1);
511+
512+
delete _fileExtensionToLanguageMap[extension];
513+
514+
this._wasModified();
515+
}
516+
};
517+
496518
/**
497519
* Adds a file name to the language which is used to match files that don't have extensions like "Makefile" for example.
498520
* @param {!string} extension An extensionless file name used by this language
499-
* @return {boolean} Whether adding the file name was successful or not
500521
*/
501522
Language.prototype.addFileName = function (name) {
502523
// Make checks below case-INsensitive
@@ -514,7 +535,24 @@ define(function (require, exports, module) {
514535

515536
this._wasModified();
516537
}
517-
return true;
538+
};
539+
540+
/**
541+
* Unregisters a file name from this language.
542+
* @param {!string} extension An extensionless file name used by this language
543+
*/
544+
Language.prototype.removeFileName = function (name) {
545+
// Make checks below case-INsensitive
546+
name = name.toLowerCase();
547+
548+
var index = this._fileNames.indexOf(name);
549+
if (index !== -1) {
550+
this._fileNames.splice(index, 1);
551+
552+
delete _fileNameToLanguageMap[name];
553+
554+
this._wasModified();
555+
}
518556
};
519557

520558
/**
@@ -796,6 +834,10 @@ define(function (require, exports, module) {
796834
// But for now, we need to associate this madeup "html" mode with our HTML language object.
797835
_setLanguageForMode("html", html);
798836

837+
// Similarly, the php mode uses clike internally for the PHP parts
838+
var php = getLanguage("php");
839+
php._setLanguageForMode("clike", php);
840+
799841
// The fallback language for unknown modes and file extensions
800842
_fallbackLanguage = getLanguage("unknown");
801843
});

src/language/languages.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
"php": {
9494
"name": "PHP",
9595
"mode": "php",
96-
"fileExtensions": ["php", "php3", "php4", "php5", "phtm", "phtml", "ctp"]
96+
"fileExtensions": ["php", "php3", "php4", "php5", "phtm", "phtml", "ctp"],
97+
"blockComment": ["/*", "*/"],
98+
"lineComment": ["//", "#"]
9799
},
98100

99101
"c": {

test/spec/LanguageManager-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ define(function (require, exports, module) {
181181
expect(LanguageManager.getLanguageForPath("cakefile.doesNotExist")).toBe(unknown);
182182
expect(LanguageManager.getLanguageForPath("Something.cakefile")).toBe(unknown);
183183
});
184+
185+
it("should remove file extensions and add to new languages", function () {
186+
var html = LanguageManager.getLanguage("html"),
187+
ruby = LanguageManager.getLanguage("ruby"),
188+
unknown = LanguageManager.getLanguage("unknown");
189+
190+
expect(LanguageManager.getLanguageForPath("test.html")).toBe(html);
191+
192+
html.removeFileExtension("html");
193+
expect(LanguageManager.getLanguageForPath("test.html")).toBe(unknown);
194+
195+
ruby.addFileExtension("html");
196+
expect(LanguageManager.getLanguageForPath("test.html")).toBe(ruby);
197+
});
198+
199+
it("should remove file names and add to new languages", function () {
200+
var coffee = LanguageManager.getLanguage("coffeescript"),
201+
html = LanguageManager.getLanguage("html"),
202+
unknown = LanguageManager.getLanguage("unknown");
203+
204+
expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(coffee);
205+
206+
coffee.removeFileName("Cakefile");
207+
expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(unknown);
208+
209+
html.addFileName("Cakefile");
210+
expect(LanguageManager.getLanguageForPath("Cakefile")).toBe(html);
211+
});
184212
});
185213

186214
describe("defineLanguage", function () {

0 commit comments

Comments
 (0)