-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Language switcher: connect to prefs & clean up some code #8444
Conversation
* Make LanguageManager the official public API that is used to set per-file overrides, rather than proxying through Document objects - remove Document.setLanguageOverride() * Clear path overrides when switching projects, so it's more obvious that it's not something that gets persisted across sessions * Simplify how JS Code Hints listen for language updates: remove dependency on "currentDocumentLanguageChanged" event; remove unneeded extra 'if' tests
permanent file extension mapping to preferences, based on the current file's path-specific language override (if any) and its file extension.
@@ -624,18 +624,21 @@ define(function (require, exports, module) { | |||
* @param {Editor} previous - the previous editor context | |||
*/ | |||
function handleActiveEditorChange(event, current, previous) { | |||
// Uninstall "languageChanged" event listeners on the previous editor's document | |||
if (previous && previous !== current) { |
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.
previous !== current
is guaranteed to be true for all "activeEditorChange"
events, so I removed that check
…e doc's current language is already the default (i.e. there's no path-specific override). DropdownButton changes: * Support disabled items * Fix bug #8433 (Page Up/Down work oddly) - Only wrap around to other end of list when using arrow keys. Page Up/Down 'stick' at start/end of list instead (so they will act like Home/End when there's no scrollbar). * Add Home/End key support
@peterflynn Is this something that should be merged for Release 0.42? |
@redmunds Originally I was hoping so, but it seems like we're out of runway at this point... |
The feature will be a little more confusing if left as-is for a sprint, but we can message that in the release notes & update notification |
Yeah, there's more changes than I thought -- best to wait on this one. |
@@ -1037,8 +1037,8 @@ define(function (require, exports, module) { | |||
* Do the work to initialize a code hinting session. | |||
* | |||
* @param {Session} session - the active hinting session |
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.
Session can't be null here either
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.
Hmm, actually it looks entirely unused...
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.
we should make that note in the doc and type it as optional
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.
I'd rather not explicitly indicate it's ok to write code which passes null for that arg, unless we've verified it's correct for it to be ignoring the arg :-) And that seems outside the scope of this PR -- but I'll add a TODO to draw attention to it.
After changing the language of a custom file type (.jst) to "Javascript" then selecting "set as default for .jst files", the "Javascript" item in the droplist doesn't have (default) next to it. Seems like we want it to say "default" if we changed the default. |
When selecting the item "Set as Default for files" (when enabled), nothing happens. The drop list should either disappear or the item should become disabled and the currently selected language should get the (default) next to it. Or we could just dismiss the droplist. |
/** | ||
* Updates the language according to the file extension. If the current | ||
* language was forced (set manually by user), don't change it. | ||
* Updates the language to match the current mapping given by LanguageManager |
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 original Language Switcher pr went through and we didn't update the events in Document.js -- specifically the LanguageChanged
event (which is a past-tense event name and we normally use current-tense event names)
Actually it appears that there are NO events documented for document objects for some reason. is there a reason why we aren't documenting them? Are they documented somewhere else? I couldn't find them documented anywhere.
We also should notify lint extension authors that they should update their extensions to listen to the "languageChanged" event since most of them leave cruft behind after changing the language of a javascript file to "text". We might want to change it to "languageChange" to match the rest of the events if no one is using it yet as well.
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 "languageChanged"
event was added much earlier than the 0.42 language switcher PR -- it dates back to Sprint 21 (c00bfb1), so I'd rather not change it as part of this diff.
There are some events documented for Document (see docs on the constructor at the top of Document.js), but not this one. I'll add it.
@JeffryBooher I'm not seeing either of the behavior issues you described above:
The dropdown does close for me, and when I reopen it, it now shows "(default)" next to JavaScript instead of Text. Is there a different recipe I need to follow in order to repro the issues you saw? |
* Use .hasClass() instead of .is() where possible * More succinct math operators in DropdownEventHandler._tryToSelect() * Document language-change events * Improve some more JS code hints docs
@JeffryBooher Changes pushed. Thanks for review! |
@peterflynn It's probable that these are introduced by CEF 1750 and we should investigate them. |
@peterflynn I went back to CEF 1547 and the problem still reproduces so it isn't 1750 specific. Download and open the following project:
|
|
||
if (lang === LANGUAGE_SET_AS_DEFAULT) { | ||
// Set file's current language in preferences as a file extension override (only enabled if not default already) | ||
var fileExtensionMap = PreferencesManager.get("language.fileExtensions"); |
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.
This is why I can't get this to work for me. You need to handle undefined
as a return value from PreferenecesManager.get()
Uncaught TypeError: Cannot set property 'jst' of undefined EditorStatusBar.js:385
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.
Yikes, good catch indeed,
@dangoor The docs for LanguageManager.definePreference() make it appear as if the initial
value arg is required, so is the bug in this case that LanguageManager didn't set the default for "language.fileExtensions"
to {}
? (I found only a couple more cases throughout the codebase where a default isn't explicitly specified).
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.
You're right. What LanguageManager does is less than ideal because _updateFromPrefs
does:
var newMapping = PreferencesManager.get(pref) || {},
rather than just having a default set via definePreference
.
mappings are set yet - don't leave default value of pref undefined. Clarify two other cases of definePreference() calls where we really *do* want the default value to be undefined by passing it explicitly. Also: add TODOs for unused Session arg in JS code hints.
@JeffryBooher Changes pushed to fix that bug & add TODOs in ScopeManager. Lmk if you need anything more before merging. |
Looks good. All tests pass. The switcher behaves as it should for me now. Merging. |
…ents Language switcher: connect to prefs & clean up some code
Thanks, Jeff! |
Follow-on to PR #6409...
It may be easiest to review each commit individually, since they're fairly distinct:
Second commit - new feature:
First commit - cleanups:
Document.setLanguageOverride()
"currentDocumentLanguageChanged"
event; remove unneeded extra 'if' testsThird commit:
For clarity, I'd ideally like the "Set as Default" menu item to be grayed out when there is no path-specific override on the current file (i.e., when the current language already is the default for the current filetype)... but disabled items aren't a think in DropdownButton yet and I will need to do some cleanup there to get it working -- so I wanted to post this initial for review first.(this is now implemented)