Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Changes from 2 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
52 changes: 48 additions & 4 deletions src/extensions/default/CSSCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ define(function (require, exports, module) {
HTMLUtils = brackets.getModule("language/HTMLUtils"),
LanguageManager = brackets.getModule("language/LanguageManager"),
TokenUtils = brackets.getModule("utils/TokenUtils"),
StringMatch = brackets.getModule("utils/StringMatch"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need that module any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Point!

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;
var lastContext,
matcher = null, // string matcher for hints
prefs = PreferencesManager.getExtensionPrefs("CSSCodeHints");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need a pref for this? As long as the behavior we offer is not a cause for complaint (or something people reasonably disagree on), then I don't think a knob is needed.


prefs.definePreference("fuzzy-matching", "boolean", true);

/**
* @constructor
*/
Expand All @@ -49,6 +55,33 @@ define(function (require, exports, module) {
this.exclusion = null;
}

/**
* Returns the preferences used to add/remove the menu items
* @return {{fuzzyMatching: boolean}}
*/
function getPreferences() {
// It's senseless to look prefs up for the current file, instead look them up for
// the current project (or globally)
return {
fuzzyMatching : prefs.get("fuzzy-matching", PreferencesManager.CURRENT_PROJECT)
};
}

/**
* Create a new StringMatcher instance, if needed.
*
* @return {StringMatcher} - a StringMatcher instance.
*/
function getStringMatcher() {
if (!matcher) {
matcher = new StringMatch.StringMatcher({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your intention to create a StringMatcher and hang onto it permanently? The StringMatcher instance will cache the results it has seen. I haven't tested this yet, but I could imagine there being some unexpected behavior from this. You may just want to call StringMatch.stringMatch.

preferPrefixMatches: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should actually be true. (That's how JS code hints uses the matcher.)

});
}

return matcher;
}

/**
* Get the CSS style text of the file open in the editor for this hinting session.
* For a CSS file, this is just the text of the file. For an HTML file,
Expand Down Expand Up @@ -206,7 +239,9 @@ define(function (require, exports, module) {
valueArray,
namedFlows,
result,
selectInitial = false;
selectInitial = false,
matcher = getStringMatcher(),
useFuzzyMatching = getPreferences().fuzzyMatching;


// Clear the exclusion if the user moves the cursor with left/right arrow key.
Expand Down Expand Up @@ -249,7 +284,11 @@ define(function (require, exports, module) {
}

result = $.map(valueArray, function (pvalue, pindex) {
if (pvalue.indexOf(valueNeedle) === 0) {
if (!useFuzzyMatching) {
if (pvalue.indexOf(valueNeedle) === 0) {
return pvalue;
}
} else if (matcher.match(pvalue, valueNeedle)) {
return pvalue;
}
}).sort();
Expand All @@ -269,9 +308,14 @@ define(function (require, exports, module) {
lastContext = CSSUtils.PROP_NAME;
needle = needle.substr(0, this.info.offset);
result = $.map(properties, function (pvalues, pname) {
if (pname.indexOf(needle) === 0) {
if (!useFuzzyMatching) {
if (pname.indexOf(needle) === 0) {
return pname;
}
} else if (matcher.match(pname, needle)) {
return pname;
}

}).sort();

return {
Expand Down