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

[CRAZY] CSS font-family hints #3848

Merged
merged 3 commits into from
May 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -291,6 +293,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 @@ -348,31 +352,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 @@ -403,7 +414,7 @@ define(function (require, exports, module) {
} else {
lastChar = null;
}
}
};

/**
* Handles keys related to displaying, searching, and navigating the hint list.
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