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 #3270 from WebsiteDeveloper/UsageOfLanguageId
Browse files Browse the repository at this point in the history
Use language id instead of CodeMirror mode in CodeHintManager
  • Loading branch information
DennisKehrig committed Apr 1, 2013
2 parents 38d7f05 + 7a112d7 commit 8364e2b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
81 changes: 40 additions & 41 deletions src/editor/CodeHintManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
*
* The CodeHintManager mediates the interaction between the editor and a
* collection of hint providers. If hints are requested explicitly by the
* user, then the providers registered for the current mode are queried
* user, then the providers registered for the current language are queried
* for their ability to provide hints in order of descending priority by
* way their hasHints methods. Character insertions may also constitute an
* implicit request for hints; consequently, providers for the current
* mode are also queried on character insertion for both their ability to
* language are also queried on character insertion for both their ability to
* provide hints and also for the suitability of providing implicit hints
* in the given editor context.
*
Expand Down Expand Up @@ -240,77 +240,76 @@ define(function (require, exports, module) {

/**
* The method by which a CodeHintProvider registers its willingness to
* providing hints for editors in a given mode.
* providing hints for editors in a given language.
*
* @param {CodeHintProvider} provider
* The hint provider to be registered, described below.
*
* @param {Array[(string|Object<name: string>)]} modes
* The set of mode names for which the provider is capable of
* providing hints. If the special mode name "all" is included then
* the provider may be called upon to provide hints for any mode.
* @param {Array[(string|Object<name: string>)]} languageIDs
* The set of language ids for which the provider is capable of
* providing hints. If the special language id name "all" is included then
* the provider may be called upon to provide hints for any language.
*
* @param {Integer} priority
* A non-negative number used to break ties among hint providers for a
* particular mode. Providers that register with a higher priority
* will have the opportunity to provide hints at a given mode before
* particular language. Providers that register with a higher priority
* will have the opportunity to provide hints at a given language before
* those with a lower priority. Brackets default providers have
* priority zero.
*/
function registerHintProvider(providerInfo, modes, priority) {
function registerHintProvider(providerInfo, languageIDs, priority) {
var providerObj = { provider: providerInfo,
priority: priority || 0 };

if (modes) {
var modeNames = [], registerInAllModes = false;
var i, currentModeName;
for (i = 0; i < modes.length; i++) {
currentModeName = (typeof modes[i] === "string") ? modes[i] : modes[i].name;
if (currentModeName) {
if (currentModeName === "all") {
registerInAllModes = true;
if (languageIDs) {
var languageIdNames = [], registerForAllLanguages = false;
var i, currentLanguageID;
for (i = 0; i < languageIDs.length; i++) {
currentLanguageID = languageIDs[i];
if (currentLanguageID) {
if (currentLanguageID === "all") {
registerForAllLanguages = true;
break;
} else {
modeNames.push(currentModeName);
languageIdNames.push(currentLanguageID);
}
}
}

if (registerInAllModes) {
// if we're registering in all, then we ignore the modeNames array
if (registerForAllLanguages) {
// if we're registering in all, then we ignore the languageIdNames array
// so that we avoid registering a provider twice
var modeName;
for (modeName in hintProviders) {
if (hintProviders.hasOwnProperty(modeName)) {
hintProviders[modeName].push(providerObj);
hintProviders[modeName].sort(_providerSort);
var languageId;
for (languageId in hintProviders) {
if (hintProviders.hasOwnProperty(languageId)) {
hintProviders[languageId].push(providerObj);
hintProviders[languageId].sort(_providerSort);
}
}
} else {
modeNames.forEach(function (modeName) {
if (modeName) {
if (!hintProviders[modeName]) {
// initialize a new mode with all providers
hintProviders[modeName] = Array.prototype.concat(hintProviders.all);
languageIdNames.forEach(function (languageId) {
if (languageId) {
if (!hintProviders[languageId]) {
// initialize a new language id with all providers
hintProviders[languageId] = Array.prototype.concat(hintProviders.all);
}
hintProviders[modeName].push(providerObj);
hintProviders[modeName].sort(_providerSort);
hintProviders[languageId].push(providerObj);
hintProviders[languageId].sort(_providerSort);
}
});
}
}
}

/**
* Return the array of hint providers for the given mode.
* Return the array of hint providers for the given language id.
* This gets called (potentially) on every keypress. So, it should be fast.
*
* @param {(string|Object<name: string>)} mode
* @return {Array.<{provider: Object, modes: Array.<string>, priority: number}>}
* @param {(string|Object<name: string>)} languageID
* @return {Array.<{provider: Object, languageIDs: Array.<string>, priority: number}>}
*/
function _getProvidersForMode(mode) {
var modeName = (typeof mode === "string") ? mode : mode.name;
return hintProviders[modeName] || hintProviders.all;
function _getProvidersForLanguageID(languageID) {
return hintProviders[languageID] || hintProviders.all;
}

/**
Expand Down Expand Up @@ -395,8 +394,8 @@ define(function (require, exports, module) {
*/
function _beginSession(editor) {
// Find a suitable provider, if any
var mode = editor.getModeForSelection(),
enabledProviders = _getProvidersForMode(mode);
var language = editor.getLanguageForSelection(),
enabledProviders = _getProvidersForLanguageID(language.getId());

$.each(enabledProviders, function (index, item) {
if (item.provider.hasHints(editor, lastChar)) {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptCodeHints/HintUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
define(function (require, exports, module) {
"use strict";

var MODE_NAME = "javascript",
var LANGUAGE_ID = "javascript",
SCOPE_MSG_TYPE = "outerScope",
SINGLE_QUOTE = "\'",
DOUBLE_QUOTE = "\"";
Expand Down Expand Up @@ -380,7 +380,7 @@ define(function (require, exports, module) {
exports.BUILTIN_GLOBALS = BUILTIN_GLOBALS;
exports.KEYWORDS = KEYWORDS;
exports.LITERALS = LITERALS;
exports.MODE_NAME = MODE_NAME;
exports.LANGUAGE_ID = LANGUAGE_ID;
exports.SCOPE_MSG_TYPE = SCOPE_MSG_TYPE;
exports.SINGLE_QUOTE = SINGLE_QUOTE;
exports.DOUBLE_QUOTE = DOUBLE_QUOTE;
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptCodeHints/ScopeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ define(function (require, exports, module) {
file = split.file;

if (file.indexOf(".") > 1) { // ignore /.dotfiles
var mode = LanguageManager.getLanguageForPath(entry.fullPath).getMode();
if (mode === HintUtils.MODE_NAME) {
var languageID = LanguageManager.getLanguageForPath(entry.fullPath).getId();
if (languageID === HintUtils.LANGUAGE_ID) {
DocumentManager.getDocumentForPath(path).done(function (document) {
refreshOuterScope(dir, file, document.getText());
});
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ define(function (require, exports, module) {
cachedHints = null;
cachedType = null;

if (editor && editor.getModeForDocument() === HintUtils.MODE_NAME) {
if (editor && editor.getLanguageForSelection().getId() === HintUtils.LANGUAGE_ID) {
initializeSession(editor);
$(editor)
.on(HintUtils.eventName("change"), function () {
Expand Down Expand Up @@ -447,7 +447,7 @@ define(function (require, exports, module) {
installEditorListeners(EditorManager.getActiveEditor());

var jsHints = new JSHints();
CodeHintManager.registerHintProvider(jsHints, [HintUtils.MODE_NAME], 0);
CodeHintManager.registerHintProvider(jsHints, [HintUtils.LANGUAGE_ID], 0);

// for unit testing
exports.jsHintProvider = jsHints;
Expand Down

0 comments on commit 8364e2b

Please sign in to comment.