-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Switch language / syntax mode of current document #6409
Changes from 32 commits
6c34d70
6e8f881
c047b0b
d37f69b
3d6e752
50ad04c
6e07bdb
4e6df78
f44a15a
f8a2b96
7dca978
0801076
29c9caa
3333370
6544c97
ecdf141
cbd04e6
e0a7c9f
a959fb3
fe057c6
b0a982e
aff5ed6
193bfa8
331ccd3
bdde023
8ebe1c2
33a9a47
0ed7853
d54da79
3df7680
5701d5c
6f4db4b
dce3176
c118a97
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 |
---|---|---|
|
@@ -474,9 +474,13 @@ define(function (require, exports, module) { | |
if (_currentDocument === doc) { | ||
return; | ||
} | ||
|
||
var perfTimerName = PerfUtils.markStart("setCurrentDocument:\t" + doc.file.fullPath); | ||
|
||
if (_currentDocument) { | ||
$(_currentDocument).off("languageChanged.DocumentManager"); | ||
} | ||
|
||
// If file is untitled or otherwise not within project tree, add it to | ||
// working set right now (don't wait for it to become dirty) | ||
if (doc.isUntitled() || !ProjectManager.isWithinProject(doc.file.fullPath)) { | ||
|
@@ -491,6 +495,12 @@ define(function (require, exports, module) { | |
// Make it the current document | ||
var previousDocument = _currentDocument; | ||
_currentDocument = doc; | ||
|
||
// Proxy this doc's languageChange events as long as it's current | ||
$(_currentDocument).on("languageChanged.DocumentManager", function (data) { | ||
$(exports).trigger("currentDocumentLanguageChanged", data); | ||
}); | ||
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. Was moving this slightly lower in the last commit (d54da79) important for fixing something? If so, could you explain how/why? I'm having trouble seeing where it would make a difference... If it's just because it seemed cleaner to move it closer to the other event stuff, np though. 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. I believe that was because the handlers may actual handlers may consult the current document for its language. I am not sure, though, whether the case was real or my thought experiment. If it's ok, let's leave it like this. 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. I don't think that calling .on() could ever synchronously trigger the handler that was just attached, so it seems like the old location of that block (where |
||
|
||
$(exports).triggerHandler("currentDocumentChange", [_currentDocument, previousDocument]); | ||
// (this event triggers EditorManager to actually switch editors in the UI) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,12 +34,12 @@ define(function (require, exports, module) { | |
DocumentManager = brackets.getModule("document/DocumentManager"), | ||
Commands = brackets.getModule("command/Commands"), | ||
CommandManager = brackets.getModule("command/CommandManager"), | ||
LanguageManager = brackets.getModule("language/LanguageManager"), | ||
Menus = brackets.getModule("command/Menus"), | ||
AppInit = brackets.getModule("utils/AppInit"), | ||
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"), | ||
PerfUtils = brackets.getModule("utils/PerfUtils"), | ||
StringMatch = brackets.getModule("utils/StringMatch"), | ||
LanguageManager = brackets.getModule("language/LanguageManager"), | ||
ProjectManager = brackets.getModule("project/ProjectManager"), | ||
PreferencesManager = brackets.getModule("preferences/PreferencesManager"), | ||
ParameterHintManager = require("ParameterHintManager"), | ||
|
@@ -305,8 +305,7 @@ define(function (require, exports, module) { | |
* @return {boolean} - true if the document is a html file | ||
*/ | ||
function isHTMLFile(document) { | ||
var languageID = LanguageManager.getLanguageForPath(document.file.fullPath).getId(); | ||
return languageID === "html"; | ||
return LanguageManager.getLanguageForPath(document.file.fullPath).getId() === "html"; | ||
} | ||
|
||
function isInlineScript(editor) { | ||
|
@@ -593,7 +592,6 @@ define(function (require, exports, module) { | |
} | ||
ignoreChange = false; | ||
}); | ||
|
||
ParameterHintManager.installListeners(editor); | ||
} else { | ||
session = null; | ||
|
@@ -623,6 +621,18 @@ 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 commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, were you seeing cases where the event was sent with 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. I've verified this is impossible, and removed that check in cc47043 |
||
$(previous.document) | ||
.off(HintUtils.eventName("languageChanged")); | ||
} | ||
if (current && current.document !== DocumentManager.getCurrentDocument()) { | ||
$(current.document) | ||
.on(HintUtils.eventName("languageChanged"), function () { | ||
uninstallEditorListeners(current); | ||
installEditorListeners(current); | ||
}); | ||
} | ||
uninstallEditorListeners(previous); | ||
installEditorListeners(current, previous); | ||
} | ||
|
@@ -790,6 +800,13 @@ define(function (require, exports, module) { | |
.on(HintUtils.eventName("activeEditorChange"), | ||
handleActiveEditorChange); | ||
|
||
$(DocumentManager) | ||
.on("currentDocumentLanguageChanged", function (e) { | ||
var activeEditor = EditorManager.getActiveEditor(); | ||
uninstallEditorListeners(activeEditor); | ||
installEditorListeners(activeEditor); | ||
}); | ||
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. There's a potential edge case where this does unneeded work, because it's possible that current document != active editor. If focus is in an inline editor but something causes the outer editor's language to change, this code would refresh listeners for the inline editor (even though it hasn't changed language) when in fact nothing needs to be done (because JS code hints only care about the language of the active editor, so the change to the host editor didn't matter). However, you can't hit that with the status bar UI -- you'd have to edit prefs.json or something else in order to change the non-active editor's language. I think we could clean this up by removing this code and taking away the current-document check in the handleActiveEditorChange() code above. Not a big deal, though... 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. I've included this cleanup in |
||
|
||
$(ProjectManager).on("beforeProjectClose", function () { | ||
ScopeManager.handleProjectClose(); | ||
}); | ||
|
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 feels a little confusing. A select box isn't a text field so maybe change to something like:
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 select isn't used anymore, so we can remove the new code too.
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.
Wouldn't hurt to have them work properly if an extension decided to use one, though... might as well leave it in, with var name corrected?