-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Switch language / syntax mode of current document #4276
Changes from all commits
6c34d70
6e8f881
c047b0b
d37f69b
3d6e752
50ad04c
6e07bdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,16 +32,20 @@ define(function (require, exports, module) { | |
| "use strict"; | ||
|
|
||
| // Load dependent modules | ||
| var AppInit = require("utils/AppInit"), | ||
| EditorManager = require("editor/EditorManager"), | ||
| Editor = require("editor/Editor").Editor, | ||
| KeyEvent = require("utils/KeyEvent"), | ||
| StatusBar = require("widgets/StatusBar"), | ||
| Strings = require("strings"), | ||
| StringUtils = require("utils/StringUtils"); | ||
| var AppInit = require("utils/AppInit"), | ||
| CollectionUtils = require("utils/CollectionUtils"), | ||
| DefaultDialogs = require("widgets/DefaultDialogs"), | ||
| Dialogs = require("widgets/Dialogs"), | ||
| EditorManager = require("editor/EditorManager"), | ||
| Editor = require("editor/Editor").Editor, | ||
| KeyEvent = require("utils/KeyEvent"), | ||
| LanguageManager = require("language/LanguageManager"), | ||
| StatusBar = require("widgets/StatusBar"), | ||
| Strings = require("strings"), | ||
| StringUtils = require("utils/StringUtils"); | ||
|
|
||
| /* StatusBar indicators */ | ||
| var $languageInfo, | ||
| var $languageSelect, | ||
| $cursorInfo, | ||
| $fileInfo, | ||
| $indentType, | ||
|
|
@@ -50,7 +54,11 @@ define(function (require, exports, module) { | |
|
|
||
|
|
||
| function _updateLanguageInfo(editor) { | ||
| $languageInfo.text(editor.document.getLanguage().getName()); | ||
| var lang = editor.document.getLanguage(); | ||
| $languageSelect.empty(); | ||
| $("<option />").val(lang.getId()).text(lang.getName()) | ||
| .appendTo($languageSelect); | ||
| $languageSelect.val(lang.getId()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The second val() call seems unneeded
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops sorry -- it's not |
||
| } | ||
|
|
||
| function _updateFileInfo(editor) { | ||
|
|
@@ -147,8 +155,52 @@ define(function (require, exports, module) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Setup and populate a custom <select> dropdown for switching the language | ||
| * mode for the given document. | ||
| * @param {!Document} document The document for which to switch the language | ||
| */ | ||
| function _setupLanguageSelect(document) { | ||
| // Lazy load the languages in the dropdown to avoid having to receive | ||
| // updates from LanguageManager (not to mention unnecessary processing | ||
| // since most users will not need to manually set the language). | ||
| var languages = LanguageManager.getLanguages(); | ||
|
|
||
| // lock width of <select>, else it changes when it's populated | ||
| $languageSelect.css("width", $languageSelect.css("width")); | ||
|
|
||
| // fill the dropbown using the languages list | ||
| $languageSelect.empty(); | ||
| CollectionUtils.forEach(languages, function (lang) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've switched to |
||
| $("<option />").val(lang.getId()).text(lang.getName()) | ||
| .appendTo($languageSelect); | ||
| }); | ||
| $languageSelect.val(document.getLanguage().getId()); | ||
|
|
||
| // sort dropdown alphabetically | ||
| $languageSelect.html($languageSelect.find("option").sort( | ||
| function (a, b) { | ||
| return a.text.toLowerCase().localeCompare(b.text.toLowerCase()); | ||
| } | ||
| )); | ||
|
|
||
| // set change handler for select box | ||
| $languageSelect.on("change", function () { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds a new handler each time the dropdown is opened, so forceLanguage() is called many times for each dropdown click after you've been using it for a while. Better to move this down into |
||
| var selectedLang = LanguageManager.getLanguage($(this).val()), | ||
| defaultLang = LanguageManager.getLanguageForPath( | ||
| document.file.fullPath | ||
| ); | ||
| // if default language selected, don't "force"" it | ||
| // (passing in null will reset the force flag) | ||
| document.forceLanguage( | ||
| selectedLang === defaultLang ? null : selectedLang | ||
| ); | ||
| $languageSelect.css("width", "auto"); // unlock width | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also needs to happen whenever |
||
| }); | ||
| } | ||
|
|
||
| function _init() { | ||
| $languageInfo = $("#status-language"); | ||
| $languageSelect = $("#language-select"); | ||
| $cursorInfo = $("#status-cursor"); | ||
| $fileInfo = $("#status-file"); | ||
| $indentType = $("#indent-type"); | ||
|
|
@@ -180,7 +232,13 @@ define(function (require, exports, module) { | |
| }); | ||
|
|
||
| $indentWidthInput.focus(function () { $indentWidthInput.select(); }); | ||
|
|
||
|
|
||
| // When language select clicked, set up dropdown. | ||
| // Using mousedown because the dropdown opens before mouseup. | ||
| $languageSelect.on("mousedown", function () { | ||
| _setupLanguageSelect(EditorManager.getActiveEditor().document); | ||
| }); | ||
|
|
||
| _onActiveEditorChange(null, EditorManager.getActiveEditor(), null); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,6 +166,33 @@ a, img { | |
| #status-language { | ||
| border-right: 1px solid rgba(0, 0, 0, 0.1); | ||
| } | ||
|
|
||
| /** | ||
| * Custom <select> styling currently only being used for language selection | ||
| * but could be reused elsewhere. | ||
| * (Here's a good intro to custom form fields: http://goo.gl/T4EPg) | ||
| */ | ||
| select.status-bar-select { | ||
| -webkit-appearance: none; | ||
| -webkit-font-smoothing: inherit; | ||
| background: url("images/select-triangles.svg") no-repeat right center; | ||
| border: 0; | ||
| // have to explicitly inherit some styles to override <select> defaults | ||
| color: inherit; | ||
| font-family: inherit; | ||
| font-size: inherit; | ||
| height: inherit; | ||
| line-height: inherit; | ||
| margin: 0; | ||
| padding: 0 12px 0 0; | ||
| width: auto; | ||
| &:focus { | ||
| outline: 0; | ||
| } | ||
| &:hover { | ||
| text-decoration: underline; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dropdown appears weirdly off-center: I'm not sure if this is an artifact of how the actual select is smaller than the area between the status bar dividers, or an unavoidable side effect of using custom styling, or what... it almost makes me wonder if we'd be better off with a custom dropdown like the ones we use for code hints, context menus, the Recent Projects dropdown, etc. |
||
| } | ||
|
|
||
| @-webkit-keyframes spinner-sprites-12 { | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dialogs imports are unused now