Browse Source

Colorize autocompletion results according to token type.

cl-refactor
yann300 10 years ago
parent
commit
c86860df43
  1. 60
      mix/qml/html/cm/anyword-hint.js
  2. 24
      mix/qml/html/cm/show-hint.css
  3. 32
      mix/qml/html/cm/solidity.js
  4. 29
      mix/qml/html/cm/solidityToken.js
  5. 1
      mix/qml/html/codeeditor.html
  6. 1
      mix/web.qrc

60
mix/qml/html/cm/anyword-hint.js

@ -20,18 +20,21 @@
var text = editor.getLine(line), m;
while (m = re.exec(text)) {
if (line == cur.line && m[0] === curWord) continue;
if ((!curWord || m[0].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
if ((!curWord || m[0].lastIndexOf(curWord, 0) === 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
seen[m[0]] = true;
list.push(m[0]);
list.push({ text: m[0] });
}
}
}
}
for (var key in solidityKeywords())
if (editor.getMode().name === "solidity")
{
if (curWord === false || key.indexOf(curWord, 0) === 0)
list.push(key);
list = addSolToken(curWord, list, solCurrency(), solCurrency);
list = addSolToken(curWord, list, solKeywords(), solKeywords);
list = addSolToken(curWord, list, solStdContract(), solStdContract);
list = addSolToken(curWord, list, solTime(), solTime);
list = addSolToken(curWord, list, solTypes(), solTypes);
list = addSolToken(curWord, list, solMisc(), solMisc);
}
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
@ -39,35 +42,20 @@
})();
solidityKeywords = function(list)
function addSolToken(curWord, list, tokens, type)
{
var keywords = { "address":true, "indexed":true, "event":true, "delete":true, "break":true, "case":true, "constant":true, "continue":true, "contract":true, "default":true,
"do":true, "else":true, "is":true, "for":true, "function":true, "if":true, "import":true, "mapping":true, "new":true,
"public":true, "private":true, "return":true, "returns":true, "struct":true, "switch":true, "var":true, "while":true,
"int":true, "uint":true, "hash":true, "bool":true, "string":true, "string0":true, "text":true, "real":true,
"ureal":true,
"owned":true,
"onlyowner":true,
"named":true,
"mortal":true,
"coin":true
};
for (var i = 1; i <= 32; i++) {
keywords["int" + i * 8] = true;
keywords["uint" + i * 8] = true;
keywords["hash" + i * 8] = true;
keywords["string" + i] = true;
};
keywords["true"] = true;
keywords["false"] = true;
keywords["null"] = true;
keywords["Config"] = true;
keywords["NameReg"] = true;
keywords["CoinReg"] = true;
return keywords;
for (var key in tokens)
{
if (curWord === false || key.indexOf(curWord, 0) === 0)
{
var token = { text: key };
token.render = function(elt, data, cur)
{
elt.className = elt.className + " " + type.name.toLowerCase();
elt.appendChild(document.createTextNode(cur.displayText || cur.text));
}
list.push(token);
}
}
return list;
}

24
mix/qml/html/cm/show-hint.css

@ -37,3 +37,27 @@
background: #4a90e2;
color: white;
}
.solcurrency {
color: red;
}
.solkeywords {
color: brown;
}
.solstdcontract {
color: blue;
}
.soltime {
color: green;
}
.soltypes {
color: orange;
}
.solMisc {
color: grey;
}

32
mix/qml/html/cm/solidity.js

@ -12,32 +12,10 @@
CodeMirror.defineMode("solidity", function(config) {
var indentUnit = config.indentUnit;
var keywords = { "address":true, "indexed":true, "event":true, "delete":true, "break":true, "case":true, "constant":true, "continue":true, "contract":true, "default":true,
"do":true, "else":true, "is":true, "for":true, "function":true, "if":true, "import":true, "mapping":true, "new":true,
"public":true, "private":true, "return":true, "returns":true, "struct":true, "switch":true, "var":true, "while":true,
"int":true, "uint":true, "hash":true, "bool":true, "string":true, "string0":true, "text":true, "real":true,
"ureal":true,
"owned":true,
"onlyowner":true,
"named":true,
"mortal":true,
"coin":true,
};
for (var i = 1; i <= 32; i++) {
keywords["int" + i * 8] = true;
keywords["uint" + i * 8] = true;
keywords["hash" + i * 8] = true;
keywords["string" + i] = true;
};
var atoms = {
"true":true, "false":true, "null":true,
"Config":true,
"NameReg":true,
"CoinReg":true,
};
var types = solTypes();
var stdContract = solStdContract();
var keywords = solKeywords();
var atoms = solMisc();
var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
@ -84,6 +62,8 @@ CodeMirror.defineMode("solidity", function(config) {
return "keyword";
}
if (atoms.propertyIsEnumerable(cur)) return "atom";
if (types.propertyIsEnumerable(cur)) return "variable-2";
if (stdContract.propertyIsEnumerable(cur)) return "variable-3";
return "variable";
}

29
mix/qml/html/cm/solidityToken.js

@ -0,0 +1,29 @@
function solCurrency()
{
return { "wei": true, "szabo": true, "finney": true, "ether": true };
}
function solKeywords()
{
return { "break": true, "case": true, "constant": true, "continue": true, "contract": true, "default": true, "do": true, "else": true, "event": true, "external": true, "is": true, "indexed": true, "for": true, "function": true, "if": true, "import": true, "mapping": true, "modifier": true, "new": true, "public": true, "private": true, "internal": true, "return": true, "returns": true, "struct": true, "switch": true, "var": true, "while": true, "enum": true };
}
function solStdContract()
{
return { "Config": true, "NameReg": true, "CoinReg": true, "owned": true, "onlyowner": true, "named": true, "mortal": true, "coin": true };
}
function solTime()
{
return { "seconds": true, "minutes": true, "hours": true, "days": true, "weeks": true, "years": true, "after": true };
}
function solTypes()
{
return { "int": true, "int8": true, "int16": true, "int24": true, "int32": true, "int40": true, "int48": true, "int56": true, "int64": true, "int72": true, "int80": true, "int88": true, "int96": true, "int104": true, "int112": true, "int120": true, "int128": true, "int136": true, "int144": true, "int152": true, "int160": true, "int168": true, "int178": true, "int184": true, "int192": true, "int200": true, "int208": true, "int216": true, "int224": true, "int232": true, "int240": true, "int248": true, "int256": true, "uint": true, "uint8": true, "uint16": true, "uint24": true, "uint32": true, "uint40": true, "uint48": true, "uint56": true, "uint64": true, "uint72": true, "uint80": true, "uint88": true, "uint96": true, "uint104": true, "uint112": true, "uint120": true, "uint128": true, "uint136": true, "uint144": true, "uint152": true, "uint160": true, "uint168": true, "uint178": true, "uint184": true, "uint192": true, "uint200": true, "uint208": true, "uint216": true, "uint224": true, "uint232": true, "uint240": true, "uint248": true, "uint256": true, "bytes0": true, "bytes1": true, "bytes2": true, "bytes3": true, "bytes4": true, "bytes5": true, "bytes6": true, "bytes7": true, "bytes8": true, "bytes9": true, "bytes10": true, "bytes11": true, "bytes12": true, "bytes13": true, "bytes14": true, "bytes15": true, "bytes16": true, "bytes17": true, "bytes18": true, "bytes19": true, "bytes20": true, "bytes21": true, "bytes22": true, "bytes23": true, "bytes24": true, "bytes25": true, "bytes26": true, "bytes27": true, "bytes28": true, "bytes29": true, "bytes30": true, "bytes31": true, "bytes32": true, "bytes": true, "byte": true, "address": true, "bool": true, "string": true, "real": true, "ureal": true };
}
function solMisc()
{
return { "true": true, "false": true, "null": true, "funtion": true, "contract": true };
}

1
mix/qml/html/codeeditor.html

@ -10,6 +10,7 @@
<script src="cm/css.js"></script>
<script src="cm/xml.js"></script>
<script src="cm/htmlmixed.js"></script>
<script src="cm/solidityToken.js"></script>
<script src="cm/solidity.js"></script>
<script src="cm/fullscreen.js"></script>
<script src="cm/active-line.js"></script>

1
mix/web.qrc

@ -25,5 +25,6 @@
<file>qml/html/cm/show-hint.js</file>
<file>qml/html/cm/show-hint.css</file>
<file>qml/html/cm/closebrackets.js</file>
<file>qml/html/cm/solidityToken.js</file>
</qresource>
</RCC>

Loading…
Cancel
Save