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

Added HtmlSpecialCharsCodeHints as default extension #3237

Merged
merged 10 commits into from Mar 27, 2013
104 changes: 104 additions & 0 deletions src/extensions/default/HtmlSpecialCharsCodeHints/SpecialChars.json
@@ -0,0 +1,104 @@
[
"&lsquo", "&rsquo", "&sbquo", "&ldquo", "&rdquo", "&bdquo",
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 it will be easier to manage this list if it's a single column sorted alphabetically

"&dagger", "&Dagger",
"&permil",
"&lsaquo", "&rsaquo",
"&spades", "&clubs", "&hearts", "&diams",
"&oline",
"&larr", "&uarr", "&rarr", "&darr",
"&trade",
"&quot",
"&amp",
"&frasl",
"&lt",
"&gt",
"&hellip",
"&ndash", "&mdash",
"&nbsp",
"&iexcl",
"&cent", "&pound", "&curren", "&yen",
"&brvbar",
"&sect",
"&uml",
"&die",
"&copy",
"&ordf",
"&laquo",
"&not",
"&shy",
"&reg",
"&macr",
"&deg",
"&plusmn",
"&sup1", "&sup2", "&sup3",
"&acute",
"&micro",
"&para",
"&middot",
"&cedil",
"&ordm",
"&raquo",
"&frac14", "&frac12", "&frac34",
"&iquest",
"&Agrave", "&Aacute", "&Acirc", "&Atilde", "&Auml", "&Aring", "&AElig",
"&Ccedil",
"&Egrave", "&Eacute", "&Ecirc", "&Euml",
"&Igrave", "&Iacute", "&Icirc", "&Iuml",
"&ETH",
"&Ntilde",
"&Ograve", "&Oacute", "&Ocirc", "&Otilde", "&Ouml",
"&times",
"&Oslash",
"&Ugrave", "&Uacute", "&Ucirc", "&Uuml",
"&Yacute",
"&THORN",
"&szlig",
"&agrave", "&aacute", "&acirc", "&atilde", "&auml", "&aring", "&aelig",
"&ccedil",
"&egrave", "&eacute", "&ecirc", "&euml",
"&igrave", "&iacute", "&icirc", "&iuml",
"&eth",
"&ntilde",
"&ograve", "&oacute", "&ocirc", "&otilde", "&ouml",
"&divide",
"&oslash",
"&ugrave", "&uacute", "&ucirc", "&uuml",
"&yacute",
"&thorn",
"&yuml",
"&Alpha", "&alpha",
"&Beta", "&beta",
"&Gamma", "&gamma",
"&Delta", "&delta",
"&Epsilon", "&epsilon",
"&Zeta", "&zeta",
"&Eta", "&eta",
"&Theta", "&theta",
"&Iota", "&iota",
"&Kappa", "&kappa",
"&Lambda", "&lambda",
"&Mu", "&mu",
"&Nu", "&nu",
"&Xi", "&xi",
"&Omicron", "&omicron",
"&Pi", "&pi",
"&Rho", "&rho",
"&Sigma", "&sigma",
"&Tau", "&tau",
"&Upsilon", "&upsilon",
"&Phi", "&phi",
"&Chi", "&chi",
"&Psi", "&psi",
"&Omega", "&omega",

"&#33",
"&#35", "&#36", "&#37",
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a problem filtering numeric entities:

  1. Type: &#
  2. List of numeric entities is shown as expected
  3. Type: 3

Results:
Left column of list now shows only: 3, 5, 6, 7, 9

Expected:
Complete numeric entities to be displayed: &#33, &#35, &#36, &#37, &#39

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

"&#39", "&#40", "&#41", "&#42", "&#43", "&#44", "&#45", "&#46",
"&#58", "&#59",
"&#61",
"&#63", "&#64",
"&#91", "&#92", "&#93", "&#94", "&#95", "&#96",
"&#123", "&#124", "&#125", "&#126",
"&#9679",
"&#8226"
]
184 changes: 184 additions & 0 deletions src/extensions/default/HtmlSpecialCharsCodeHints/main.js
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/


/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $ */

define(function (require, exports, module) {
"use strict";

// Load dependent modules
var AppInit = brackets.getModule("utils/AppInit"),
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
HtmlSpecialChars = require("text!SpecialChars.json"),
specialChars;

/**
* @constructor
*/
function SpecialCharHints() {
this.primaryTriggerKeys = "&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#0123456789";
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 "&" is the only trigger key that you need here, since that's the first char of every entity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

those other keys have to be in there because otherwise the hinting sesssion would stop after typing another character after the ampersand.

this.currentQuery = "";
}

/**
* Determines whether HtmlSpecialChar hints are available in the current editor
* context.
*
* @param {Editor} editor
* A non-null editor object for the active window.
*
* @param {String} implicitChar
* Either null, if the hinting request was explicit, or a single character
* that represents the last insertion and that indicates an implicit
* hinting request.
*
* @return {Boolean}
* Determines whether the current provider is able to provide hints for
* the given editor context and, in case implicitChar is non- null,
* whether it is appropriate to do so.
*/
SpecialCharHints.prototype.hasHints = function (editor, implicitChar) {
var tagInfo,
query,
range;

this.editor = editor;

if (implicitChar === null) {
return false;
} else {
return implicitChar === "&";
}
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 else, and removing it makes it easier to see that something is returned in all cases:

        if (implicitChar === null) {
            return query !== null;
        }

        return implicitChar === "&" || query !== null;

};

/**
* Returns a list of avaliable HtmlSpecialChar hints if possible for the current
* editor context.
*
* @param {String} implicitChar
* Either null, if the hinting request was explicit, or a single character
* that represents the last insertion and that indicates an implicit
* hinting request.
*
* @return {Object<hints: Array<(String + jQuery.Obj)>, match: String, selectInitial: Boolean>}
* Null if the provider wishes to end the hinting session. Otherwise, a
* response object that provides 1. a sorted array hints that consists
* of strings; 2. a string match that is used by the manager to emphasize
* matching substrings when rendering the hint list; and 3. a boolean that
* indicates whether the first result, if one exists, should be selected
* by default in the hint list window.
*/
SpecialCharHints.prototype.getHints = function (implicitChar) {
var query,
result;

if (this.primaryTriggerKeys.indexOf(implicitChar) !== -1) {
this.currentQuery = query = this._getQuery();
result = $.map(specialChars, function (value, index) {
if (value.indexOf(query) === 0) {
return value + " <span style=\"float: right;\">" + value + ";</span>";
Copy link
Contributor

Choose a reason for hiding this comment

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

Create a class and apply class to span tag, instead of inline css.

Also, the code looks cleaner if you nest single-quotes inside of double-quotes:

    return value + " <span class='entity-display-char'>" + value + ";</span>";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.
should i add the style to the brackets.less file?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, you should create a CSS file in your extension directory and load it with ExtensionUtils.loadStyleSheet(module, "styles.css"); on AppInit.htmlReady.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added that.

}
}).sort();

return {
hints: result,
match: query,
selectInitial: true
};
}

return null;
};

/**
* Returns a query for the Hints
*
* @return {String}
* The Query for which to search
*/
SpecialCharHints.prototype._getQuery = function () {
var query,
lineContent,
startChar,
endChar;

query = "&";

lineContent = this.editor.document.getRange({
line: this.editor.getCursorPos().line,
ch: 0
}, this.editor.getCursorPos());

startChar = lineContent.lastIndexOf("&");
endChar = lineContent.lastIndexOf(";");

if (endChar < startChar) {
query = this.editor.document.getRange({
line: this.editor.getCursorPos().line,
ch: startChar
}, this.editor.getCursorPos());
}

return query;
};

/**
* Inserts a given HtmlSpecialChar hint into the current editor context.
*
* @param {String} hint
* The hint to be inserted into the editor context.
Copy link
Contributor

Choose a reason for hiding this comment

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

Parameter name is completion.

*
* @return {Boolean}
* Indicates whether the manager should follow hint insertion with an
* additional explicit hint request.
*/
SpecialCharHints.prototype.insertHint = function (completion) {
var start = {line: -1, ch: -1},
end = {line: -1, ch: -1},
cursor = this.editor.getCursorPos();

end.line = start.line = cursor.line;
start.ch = cursor.ch - this.currentQuery.length;
end.ch = start.ch + this.currentQuery.length;
completion = completion.slice(0, completion.indexOf(" ")) + ";";
if (start.ch !== end.ch) {
this.editor.document.replaceRange(completion, start, end);
} else {
this.editor.document.replaceRange(completion, start);
}

return false;
};

AppInit.appReady(function () {
// Parse JSON files
specialChars = JSON.parse(HtmlSpecialChars);

// Register code hint providers
var specialCharHints = new SpecialCharHints();

CodeHintManager.registerHintProvider(specialCharHints, ["html"], 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm seeing a weird problem in this case:

  1. Start with a paragraph: <p></p>
  2. Place cursor between the open and closing tags and type "&"
  3. The entity hint list is displayed as expected
  4. Press Esc to dismiss list
  5. Press Ctrl-space to re-invoke the hints list

Results:
Hints list is displayed, but it's a list of HTML Tags!

It might have something to do with using a priority of 0 to register these hints, which is the default priority. On the other hand, there should be no conflicts with the HTML hints, so not really sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually it the problem is, that the HTML tags hint provider can also provide hints for those and gets called before the specialCharHints, but when i changed the priority to one plus a few other tweaks in the has hints function it worked.
So fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

That problem is fixed, but I'm seeing another problem I think it related. Code hints work correctly inside of a tag, but if I type a "raw" & I don't get the hints. For example:

<body>
    &
</body>

});
});