Skip to content

Commit 6609574

Browse files
author
tomsdev
committed
Add TypeScript mode to *.ts documents at runtime so there is no more need to modify the source of Brackets to add it now!
1 parent 9985c88 commit 6609574

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

PathUtils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,25 @@ define(function (require, exports, module) {
4949
return path.substr(0, path.lastIndexOf('/'));
5050
}
5151

52+
/**
53+
* Returns true if the path contains one of the given extension. Otherwise, returns
54+
* false.
55+
* @param {string} path
56+
* @param {Array.<string>} extensions
57+
* @returns {boolean}
58+
*/
59+
function hasExtensions(path, extensions) {
60+
if (!path || !extensions || !extensions.length) {
61+
return false;
62+
}
63+
var i = path.lastIndexOf("."),
64+
ext = (i === -1 || i >= path.length - 1) ? path : path.substr(i + 1);
65+
66+
return (extensions.indexOf(ext.toLowerCase()) !== -1);
67+
}
68+
5269
// Define public API
5370
exports.convertRelativePathToFullPath = convertRelativePathToFullPath;
5471
exports.getParentPath = getParentPath;
72+
exports.hasExtensions = hasExtensions;
5573
});

TypeScript/TypeScriptService.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,24 @@ define(function (require, exports, module) {
113113
current.document.file.fullPath === previous.document.file.fullPath)) {
114114
return;
115115
}
116+
117+
var fullPath = current.document.file.fullPath;
118+
// Change the current mode to typescript if the file has the extension .ts
119+
// We have to do this because typescript mode is not defined in Brackets
120+
// for the moment (in EditorUtils.js)
121+
if (PathUtils.hasExtensions(fullPath, ["ts"])) {
122+
current.setModeForDocument("text/typescript");
123+
}
124+
116125
// Here we could detach the session from the previous editor
117126
//if (_currentSession && previous) { }
118127

119128
_currentSession = null;
120129
if (current.getModeForSelection() === TypeScriptUtils.MODE_NAME) {
121-
console.log("Current session changing to: ", current.document.file.fullPath);
130+
console.log("Current session changing to: ", fullPath);
122131

123132
getSession(current.document).done(function (session) {
124-
console.log("Current session changed to: ", current.document.file.fullPath);
133+
console.log("Current session changed to: ", fullPath);
125134
_currentSession = session;
126135
});
127136
}

0 commit comments

Comments
 (0)