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 #3848 from adobe/iwehrman/font-family-css-code-hints
Browse files Browse the repository at this point in the history
[CRAZY] CSS font-family hints
  • Loading branch information
RaymondLim committed May 18, 2013
2 parents 0d87fb1 + d337737 commit 8653151
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
49 changes: 30 additions & 19 deletions src/editor/CodeHintManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@
* The method by which a provider provides hints for the editor context
* associated with the current session. The getHints method is called only
* if the provider asserted its willingness to provide hints in an earlier
* call to hasHints. The provider may return null, which indicates that
* the manager should end the current hinting session and close the hint
* list window. Otherwise, the provider should return a response object
* that contains three properties:
* call to hasHints. The provider may return null or false, which indicates
* that the manager should end the current hinting session and close the hint
* list window; or true, which indicates that the manager should end the
* current hinting session but immediately attempt to begin a new hinting
* session by querying registered providers. Otherwise, the provider should
* return a response object that contains three properties:
*
* 1. hints, a sorted array hints that the provider could later insert
* into the editor;
Expand Down Expand Up @@ -295,6 +297,8 @@ define(function (require, exports, module) {
function _getProvidersForLanguageId(languageId) {
return hintProviders[languageId] || hintProviders.all;
}

var _beginSession;

/**
* End the current hinting session
Expand Down Expand Up @@ -355,31 +359,38 @@ define(function (require, exports, module) {
if (!response) {
// the provider wishes to close the session
_endSession();
} else if (response.hasOwnProperty("hints")) { // a synchronous response
if (hintList.isOpen()) {
// the session is open
hintList.update(response);
} else {
hintList.open(response);
}
} else { // response is a deferred
deferredHints = response;
response.done(function (hints) {
} else {
// if the response is true, end the session and begin another
if (response === true) {
var previousEditor = sessionEditor;
_endSession();
_beginSession(previousEditor);
} else if (response.hasOwnProperty("hints")) { // a synchronous response
if (hintList.isOpen()) {
// the session is open
hintList.update(hints);
hintList.update(response);
} else {
hintList.open(hints);
hintList.open(response);
}
});
} else { // response is a deferred
deferredHints = response;
response.done(function (hints) {
if (hintList.isOpen()) {
// the session is open
hintList.update(hints);
} else {
hintList.open(hints);
}
});
}
}
}

/**
* Try to begin a new hinting session.
* @param {Editor} editor
*/
function _beginSession(editor) {
_beginSession = function (editor) {
// Find a suitable provider, if any
var language = editor.getLanguageForSelection(),
enabledProviders = _getProvidersForLanguageId(language.getId());
Expand Down Expand Up @@ -410,7 +421,7 @@ define(function (require, exports, module) {
} else {
lastChar = null;
}
}
};

/**
* Explicitly start a new session. If we have an existing session,
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/default/CSSCodeHints/CSSProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"flex-wrap": {"values": ["nowrap", "wrap", "wrap-reverse"]},
"float": {"values": ["left", "right", "none", "inherit"]},
"font": {"values": []},
"font-family": {"values": []},
"font-family": {"values": ["cursive", "fantasy", "inherit", "monospace", "sans-serif", "serif"]},
"font-feature-settings": {"values": ["normal"]},
"font-kerning": {"values": ["auto", "none", "normal"]},
"font-language-override": {"values": ["normal"]},
Expand Down
14 changes: 14 additions & 0 deletions src/extensions/default/CSSCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ define(function (require, exports, module) {
CSSProperties = require("text!CSSProperties.json"),
properties = JSON.parse(CSSProperties);

// Context of the last request for hints: either CSSUtils.PROP_NAME,
// CSSUtils.PROP_VALUE or null.
var lastContext;

/**
* @constructor
*/
Expand Down Expand Up @@ -63,6 +67,7 @@ define(function (require, exports, module) {
this.editor = editor;
var cursor = this.editor.getCursorPos();

lastContext = null;
this.info = CSSUtils.getInfoAtPos(editor, cursor);

if (this.info.context !== CSSUtils.PROP_NAME && this.info.context !== CSSUtils.PROP_VALUE) {
Expand Down Expand Up @@ -111,6 +116,14 @@ define(function (require, exports, module) {
}

if (context === CSSUtils.PROP_VALUE) {
// When switching from a NAME to a VALUE context, restart the session
// to give other more specialized providers a chance to intervene.
if (lastContext === CSSUtils.PROP_NAME) {
return true;
} else {
lastContext = CSSUtils.PROP_VALUE;
}

if (!properties[needle]) {
return null;
}
Expand All @@ -133,6 +146,7 @@ define(function (require, exports, module) {
selectInitial: selectInitial
};
} else if (context === CSSUtils.PROP_NAME) {
lastContext = CSSUtils.PROP_NAME;
needle = needle.substr(0, this.info.offset);
result = $.map(properties, function (pvalues, pname) {
if (pname.indexOf(needle) === 0) {
Expand Down

0 comments on commit 8653151

Please sign in to comment.