adobe/brackets — pull request #3023 by peterflynn (2013-03-02 23:03:50+00:00)
Title: Fix #2951 (Many commands [including save] apply to wrong file after using Quick Open)

Fix bug #2951 (Many commands [including save] apply to wrong file after using Quick Open) - Block re-entrant focus attempts. This bug occurs with any blur handlers that call EditorManager.focusEditor(), since that can cause such re-entrant focus attempts (with two different editors).

Blur handlers run synchronously while an editor is in the middle of getting focus, so the state is a little funny - activeEditor hasn't been updated yet (blur fires before focus), so focusEditor() tries to focus the old Editor that's in the process of losing focus. Webkit seems to ignore tht attempt; but CodeMirror assumes that programmatic focus requests always
succeed, so CodeMirror's state.focused flag becomes wrong. This has a delayed effect the NEXT time that editor gains focus: it doesn't dispatch a focus event since CM thought it already had focus; as a result, activeEditor never updates and any code that uses activeEditor gets the wrong value.

I think we could fix this by triggering stuff on focusIn instead of blur (which is how ModalBar works and why the Find bar doesn't have a similar bug). But it seems safer just to cut it off at the root: re-entrant attempts to focus Editors.