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

Commit

Permalink
Merge pull request #6409 from adobe/pflynn/JakeStoeffler-languagesele…
Browse files Browse the repository at this point in the history
…ctor-updated

Switch language / syntax mode of current document
  • Loading branch information
peterflynn committed Jul 10, 2014
2 parents 373c4f0 + c118a97 commit b409212
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 48 deletions.
7 changes: 4 additions & 3 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,15 @@ define(function (require, exports, module) {
$("html").on("mousedown", ".no-focus", function (e) {
// Text fields should always be focusable.
var $target = $(e.target),
isTextField =
isFormElement =
$target.is("input[type=text]") ||
$target.is("input[type=number]") ||
$target.is("input[type=password]") ||
$target.is("input:not([type])") || // input with no type attribute defaults to text
$target.is("textarea");
$target.is("textarea") ||
$target.is("select");

if (!isTextField) {
if (!isFormElement) {
e.preventDefault();
}
});
Expand Down
18 changes: 14 additions & 4 deletions src/document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ define(function (require, exports, module) {
* @type {FileUtils.LINE_ENDINGS_CRLF|FileUtils.LINE_ENDINGS_LF}
*/
Document.prototype._lineEndings = null;

/** Add a ref to keep this Document alive */
Document.prototype.addRef = function () {
//console.log("+++REF+++ "+this);
Expand Down Expand Up @@ -670,14 +670,25 @@ define(function (require, exports, module) {
Document.prototype.getLanguage = function () {
return this.language;
};

/**
* Overrides the default language of this document and sets it to the given
* language. This change is not persisted if the document is closed.
* @param {?Language} language The language to be set for this document; if
* null, the language will be set back to the default.
*/
Document.prototype.setLanguageOverride = function (language) {
LanguageManager._setLanguageOverrideForPath(this.file.fullPath, language);
this._updateLanguage();
};

/**
* Updates the language according to the file extension
* Updates the language according to the file extension. If the current
* language was forced (set manually by user), don't change it.
*/
Document.prototype._updateLanguage = function () {
var oldLanguage = this.language;
this.language = LanguageManager.getLanguageForPath(this.file.fullPath);

if (oldLanguage && oldLanguage !== this.language) {
$(this).triggerHandler("languageChanged", [oldLanguage, this.language]);
}
Expand All @@ -698,7 +709,6 @@ define(function (require, exports, module) {
return this.file instanceof InMemoryFile;
};


// Define public API
exports.Document = Document;
});
12 changes: 11 additions & 1 deletion src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
});

$(exports).triggerHandler("currentDocumentChange", [_currentDocument, previousDocument]);
// (this event triggers EditorManager to actually switch editors in the UI)

Expand Down
85 changes: 73 additions & 12 deletions src/editor/EditorStatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ define(function (require, exports, module) {
"use strict";

// Load dependent modules
var AppInit = require("utils/AppInit"),
AnimationUtils = require("utils/AnimationUtils"),
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 _ = require("thirdparty/lodash"),
AnimationUtils = require("utils/AnimationUtils"),
AppInit = require("utils/AppInit"),
DropdownButton = require("widgets/DropdownButton").DropdownButton,
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, // this is a DropdownButton instance
$cursorInfo,
$fileInfo,
$indentType,
Expand All @@ -67,7 +70,15 @@ define(function (require, exports, module) {
* @param {Editor} editor Current editor
*/
function _updateLanguageInfo(editor) {
$languageInfo.text(editor.document.getLanguage().getName());
var doc = editor.document,
lang = doc.getLanguage();

// Ensure width isn't left locked by a previous click of the dropdown (which may not have resulted in a "change" event at the time)
languageSelect.$button.css("width", "auto");
// Setting Untitled documents to non-text mode isn't supported yet, so disable the switcher in that case for now
languageSelect.$button.prop("disabled", doc.isUntitled());
// Show the current language as button title
languageSelect.$button.text(lang.getName());
}

/**
Expand Down Expand Up @@ -260,7 +271,9 @@ define(function (require, exports, module) {
$(current).on("overwriteToggle.statusbar", _updateOverwriteLabel);

current.document.addRef();
$(current.document).on("languageChanged.statusbar", function () { _updateLanguageInfo(current); });
$(current.document).on("languageChanged.statusbar", function () {
_updateLanguageInfo(current);
});

_updateCursorInfo(null, current);
_updateLanguageInfo(current);
Expand All @@ -271,18 +284,55 @@ define(function (require, exports, module) {
}
}

/**
* Populate the languageSelect DropdownButton's menu with all registered Languages
*/
function _populateLanguageDropdown() {
// Get all non-binary languages
var languages = _.values(LanguageManager.getLanguages()).filter(function (language) {
return !language.isBinary();
});

// sort dropdown alphabetically
languages.sort(function (a, b) {
return a.getName().toLowerCase().localeCompare(b.getName().toLowerCase());
});

languageSelect.items = languages;

}

/**
* Initialize
*/
function _init() {
$languageInfo = $("#status-language");

$cursorInfo = $("#status-cursor");
$fileInfo = $("#status-file");
$indentType = $("#indent-type");
$indentWidthLabel = $("#indent-width-label");
$indentWidthInput = $("#indent-width-input");
$statusOverwrite = $("#status-overwrite");

languageSelect = new DropdownButton("", [], function (item, index) {
var document = EditorManager.getActiveEditor().document,
defaultLang = LanguageManager.getLanguageForPath(document.file.fullPath, true),
html = _.escape(item.getName());

// Show indicators for currently selected & default languages for the current file
if (item === defaultLang) {
html += " <span class='default-language'>" + Strings.STATUSBAR_DEFAULT_LANG + "</span>";
}
if (item === document.getLanguage()) {
html = "<span class='checked-language'></span>" + html;
}
return html;
});

languageSelect.dropdownExtraClasses = "dropdown-status-bar";
languageSelect.$button.addClass("btn-status-bar");
$("#status-language").append(languageSelect.$button);

// indentation event handlers
$indentType.on("click", _toggleIndentType);
$indentWidthLabel
Expand Down Expand Up @@ -310,6 +360,16 @@ define(function (require, exports, module) {

$indentWidthInput.focus(function () { $indentWidthInput.select(); });

// Language select change handler
$(languageSelect).on("select", function (e, lang, index) {
var document = EditorManager.getActiveEditor().document,
fullPath = document.file.fullPath,
defaultLang = LanguageManager.getLanguageForPath(fullPath, true);
// if default language selected, don't "force" it
// (passing in null will reset the force flag)
document.setLanguageOverride(lang === defaultLang ? null : lang);
});

$statusOverwrite.on("click", _updateEditorOverwriteMode);

_onActiveEditorChange(null, EditorManager.getActiveEditor(), null);
Expand All @@ -319,4 +379,5 @@ define(function (require, exports, module) {
$(EditorManager).on("activeEditorChange", _onActiveEditorChange);

AppInit.htmlReady(_init);
AppInit.appReady(_populateLanguageDropdown);
});
25 changes: 21 additions & 4 deletions src/extensions/default/JavaScriptCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -308,8 +308,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) {
Expand Down Expand Up @@ -596,7 +595,6 @@ define(function (require, exports, module) {
}
ignoreChange = false;
});

ParameterHintManager.installListeners(editor);
} else {
session = null;
Expand Down Expand Up @@ -626,6 +624,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) {
$(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);
}
Expand Down Expand Up @@ -793,6 +803,13 @@ define(function (require, exports, module) {
.on(HintUtils.eventName("activeEditorChange"),
handleActiveEditorChange);

$(DocumentManager)
.on("currentDocumentLanguageChanged", function (e) {
var activeEditor = EditorManager.getActiveEditor();
uninstallEditorListeners(activeEditor);
installEditorListeners(activeEditor);
});

$(ProjectManager).on("beforeProjectClose", function () {
ScopeManager.handleProjectClose();
});
Expand Down
4 changes: 2 additions & 2 deletions src/language/CodeInspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ define(function (require, exports, module) {
function _unregisterAll() {
_providers = {};
}

/**
* Returns a list of provider for given file path, if available.
* Decision is made depending on the file extension.
Expand Down Expand Up @@ -474,7 +474,7 @@ define(function (require, exports, module) {
if (_enabled) {
// register our event listeners
$(DocumentManager)
.on("currentDocumentChange.codeInspection", function () {
.on("currentDocumentChange.codeInspection currentDocumentLanguageChanged.codeInspection", function () {
run();
})
.on("documentSaved.codeInspection documentRefreshed.codeInspection", function (event, document) {
Expand Down
Loading

0 comments on commit b409212

Please sign in to comment.