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

Language switcher: connect to prefs & clean up some code #8444

Merged
merged 5 commits into from
Aug 6, 2014

Conversation

peterflynn
Copy link
Member

Follow-on to PR #6409...

It may be easiest to review each commit individually, since they're fairly distinct:

Second commit - new feature:

  • Add "Set as Default" menu item to status bar language switcher -- adds a permanent file extension mapping to preferences, based on the current file's path-specific language override (if any) and its file extension.

First commit - cleanups:

  • 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 to users 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

Third commit:

  • Improve "Set as Default" menu item -- gray it out when the current file has no path-specific override (i.e., when the current language already is the default for the current filetype)
  • Extend DropdownButton / DropdownEventHandler to support disabled items.
  • In the process, clean up dropdown keyboard navigation substantially -- fixing Page Up/Down work oddly in dropdowns without scrollbars #8433.

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)

* 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) {
Copy link
Member Author

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
@redmunds
Copy link
Contributor

@peterflynn Is this something that should be merged for Release 0.42?

@peterflynn
Copy link
Member Author

@redmunds Originally I was hoping so, but it seems like we're out of runway at this point...

@peterflynn
Copy link
Member Author

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

@redmunds
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Member Author

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...

Copy link
Contributor

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

Copy link
Member Author

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.

@JeffryBooher
Copy link
Contributor

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.

@JeffryBooher
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Member Author

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.

@peterflynn
Copy link
Member Author

@JeffryBooher I'm not seeing either of the behavior issues you described above:

  1. Create a file named "test.foo"
  2. Change language switcher from Text to JavaScript
  3. Open language switcher again and choose "Set as Default for .foo Files"

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
@peterflynn
Copy link
Member Author

@JeffryBooher Changes pushed. Thanks for review!

@JeffryBooher
Copy link
Contributor

@peterflynn It's probable that these are introduced by CEF 1750 and we should investigate them.

@JeffryBooher
Copy link
Contributor

@peterflynn I went back to CEF 1547 and the problem still reproduces so it isn't 1750 specific.

Download and open the following project:
https://www.dropbox.com/s/2k1ak5lcktejnog/test1234.zip

  • open app.jst (language default should be text)
  • Select "Javascript" from the language drop list
  • Select "Set as default for .jst files"
    ==> Nothing happens


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");
Copy link
Contributor

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

Copy link
Member Author

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).

Copy link
Contributor

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.
@peterflynn
Copy link
Member Author

@JeffryBooher Changes pushed to fix that bug & add TODOs in ScopeManager. Lmk if you need anything more before merging.

@JeffryBooher
Copy link
Contributor

Looks good. All tests pass. The switcher behaves as it should for me now. Merging.

JeffryBooher added a commit that referenced this pull request Aug 6, 2014
…ents

Language switcher: connect to prefs & clean up some code
@JeffryBooher JeffryBooher merged commit 90398b4 into master Aug 6, 2014
@JeffryBooher JeffryBooher deleted the pflynn/language-switcher-improvements branch August 6, 2014 21:38
@peterflynn
Copy link
Member Author

Thanks, Jeff!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants