Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] introduce scopes #3837

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions demo/kitchen-sink/token_tooltip.js
Expand Up @@ -91,13 +91,13 @@ oop.inherits(TokenTooltip, Tooltip);
return;
}

var tokenText = token.type;
if (token.state)
tokenText += "|" + token.state;
if (token.merge)
tokenText += "\n merge";
if (token.stateTransitions)
tokenText += "\n " + token.stateTransitions.join("\n ");
var tokenText = "";
var scope = token.type;
do {
tokenText += scope.name + "\n"
if (!scope.parent)
tokenText += "\ntoken count:" + count(scope);
} while(scope = scope.parent)

if (this.tokenText != tokenText) {
this.setText(tokenText);
Expand Down Expand Up @@ -153,4 +153,10 @@ oop.inherits(TokenTooltip, Tooltip);

exports.TokenTooltip = TokenTooltip;

function count(root) {
return Object.keys(root.children).reduce(function(n, key) {
return n + count(root.children[key]);
}, 1);
};

});
5 changes: 3 additions & 2 deletions lib/ace/background_tokenizer.js
Expand Up @@ -232,9 +232,10 @@ var BackgroundTokenizer = function(tokenizer, editor) {
var state = this.states[row - 1];

var data = this.tokenizer.getLineTokens(line, state, row);
var newState=data.tokens[data.tokens.length - 1].type.parent;

if (this.states[row] + "" !== data.state + "") {
this.states[row] = data.state;
if (this.states[row] !== newState) {
this.states[row] = newState;
this.lines[row + 1] = null;
if (this.currentLine > row + 1)
this.currentLine = row + 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/ace/edit_session/bracket_match.js
Expand Up @@ -147,7 +147,7 @@ function BracketMatch() {
// whose type matches typeRe
do {
token = iterator.stepBackward();
} while (token && !typeRe.test(token.type));
} while (token && !typeRe.test(token.type.toString()));

if (token == null)
break;
Expand Down Expand Up @@ -205,7 +205,7 @@ function BracketMatch() {
// whose type matches typeRe
do {
token = iterator.stepForward();
} while (token && !typeRe.test(token.type));
} while (token && !typeRe.test(token.type.toString()));

if (token == null)
break;
Expand Down
4 changes: 2 additions & 2 deletions lib/ace/edit_session/folding.js
Expand Up @@ -606,9 +606,9 @@ function Folding() {
var token = iterator.getCurrentToken();
var type = token.type;
if (token && /^comment|string/.test(type)) {
type = type.match(/comment|string/)[0];
type = /comment|string/.exec(type)[0];
if (type == "comment")
type += "|doc-start";
type += "|doc-start|empty";
var re = new RegExp(type);
var range = new Range();
if (dir != 1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/editor.js
Expand Up @@ -566,7 +566,7 @@ Editor.$uid = 0;
var iterator = new TokenIterator(self.session, pos.row, pos.column);
var token = iterator.getCurrentToken();

if (!token || !/\b(?:tag-open|tag-name)/.test(token.type)) {
if (!token || !/\b(?:tag-open|tag-name)/.test(token.type.toString())) {
session.removeMarker(session.$tagHighlight);
session.$tagHighlight = null;
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/ace/ext/beautify.js
Expand Up @@ -153,13 +153,13 @@ exports.beautify = function(session) {
breakBefore = true;

// trim value if not in a comment or string
if (!is(token, "comment") && !token.type.match(/^(comment|string)$/))
if (!is(token, "comment") && !/^(comment|string)$/.test(token.type))
value = value.trimLeft();
}

if (value) {
// whitespace
if (token.type === "keyword" && value.match(/^(if|else|elseif|for|foreach|while|switch)$/)) {
if (token.type === "keyword" && /^(if|else|elseif|for|foreach|while|switch)$/.test(value)) {
parents[depth] = value;

trimNext();
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/layer/text.js
Expand Up @@ -397,7 +397,7 @@ var Text = function(parentEl) {

valueFragment.appendChild(this.dom.createTextNode(i ? value.slice(i) : value, this.element));

if (!this.$textToken[token.type]) {
if (!this.$textToken[token.type.toString()]) {
var classes = "ace_" + token.type.replace(/\./g, " ace_");
var span = this.dom.createElement("span");
if (token.type == "fold")
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/mode/behaviour/xml.js
Expand Up @@ -37,7 +37,7 @@ var TokenIterator = require("../../token_iterator").TokenIterator;
var lang = require("../../lib/lang");

function is(token, type) {
return token && token.type.lastIndexOf(type + ".xml") > -1;
return token && token.type.hasParent("xml") > -1 && token.type.indexOf(type) > -1;
}

var XmlBehaviour = function () {
Expand Down
16 changes: 7 additions & 9 deletions lib/ace/mode/coffee_highlight_rules.js
Expand Up @@ -133,19 +133,17 @@ define(function(require, exports, module) {
{defaultToken: "string"}
]
}, {
regex: "[{}]", onMatch: function(val, state, stack) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking change for all language modes. How do we communicate this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I do like how it simplifies the code

Copy link
Member Author

Choose a reason for hiding this comment

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

this is indeed a breaking change, we'll have to include this in v2 if we decide this is useful enough.

regex: "[{}]", onMatch: function(val, scope) {
this.next = "";
if (val == "{" && stack.length) {
stack.unshift("start", state);
return "paren";
if (val == "{") {
return scope.get("paren");
}
if (val == "}" && stack.length) {
stack.shift();
this.next = stack.shift() || "";
if (val == "}") {
this.next = scope.parent || "";
if (this.next.indexOf("string") != -1)
return "paren.string";
return this.next.get("paren.string");
}
return "paren";
return scope.get("paren");
}
}, {
token : "string.regex",
Expand Down
30 changes: 12 additions & 18 deletions lib/ace/mode/d_highlight_rules.js
Expand Up @@ -119,16 +119,16 @@ var DHighlightRules = function() {
regex : "\\/\\+",
next: "plus-comment"
}, { //-------------------------------------------------------- STRINGS
onMatch: function(value, currentState, state) {
state.unshift(this.next, value.substr(2));
return "string";
onMatch: function(value, scope) {
scope.meta = value.substr(2);
return scope.get(this.next).get("string");
},
regex: 'q"(?:[\\[\\(\\{\\<]+)',
next: 'operator-heredoc-string'
}, {
onMatch: function(value, currentState, state) {
state.unshift(this.next, value.substr(2));
return "string";
onMatch: function(value, scope) {
scope.meta = value.substr(2);
return scope.get(this.next).get("string");
},
regex: 'q"(?:[a-zA-Z_]+)$',
next: 'identifier-heredoc-string'
Expand Down Expand Up @@ -231,16 +231,13 @@ var DHighlightRules = function() {

"operator-heredoc-string": [
{
onMatch: function(value, currentState, state) {
onMatch: function(value, scope) {
value = value.substring(value.length-2, value.length-1);
var map = {'>':'<',']':'[',')':'(','}':'{'};
if(Object.keys(map).indexOf(value) != -1)
value = map[value];
if(value != state[1]) return "string";
state.shift();
state.shift();

return "string";
if(value != scope.parent.meta) return scope.get("string");
return scope.parent.get("string");
},
regex: '(?:[\\]\\)}>]+)"',
next: 'start'
Expand All @@ -252,13 +249,10 @@ var DHighlightRules = function() {

"identifier-heredoc-string": [
{
onMatch: function(value, currentState, state) {
onMatch: function(value, scope) {
value = value.substring(0, value.length-1);
if(value != state[1]) return "string";
state.shift();
state.shift();

return "string";
if(value != scope.parent.meta) return scope.parent.get("string");
return scope.get("string");
},
regex: '^(?:[A-Za-z_][a-zA-Z0-9]+)"',
next: 'start'
Expand Down
5 changes: 2 additions & 3 deletions lib/ace/mode/elm_highlight_rules.js
Expand Up @@ -90,9 +90,8 @@ var ElmHighlightRules = function() {
}, {
regex: "{-#?",
token: "comment.start",
onMatch: function(value, currentState, stack) {
this.next = value.length == 2 ? "blockComment" : "docComment";
return this.token;
onMatch: function(value, scope) {
return scope.get(value.length == 2 ? "blockComment" : "docComment").get(this.token);
}
}, {
token: "variable.language",
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/mode/folding/xml.js
Expand Up @@ -56,7 +56,7 @@ var Tag = function() {
};

function is(token, type) {
return token.type.lastIndexOf(type + ".xml") > -1;
return token.type.hasParent("xml") > -1 && token.type.indexOf(type) > -1;
}

(function() {
Expand Down
32 changes: 14 additions & 18 deletions lib/ace/mode/jade_highlight_rules.js
Expand Up @@ -81,9 +81,10 @@ var JadeHighlightRules = function() {
regex: "^!!!\\s*(?:[a-zA-Z0-9-_]+)?"
},
{
onMatch: function(value, currentState, stack) {
stack.unshift(this.next, value.length - 2, currentState);
return "comment";
onMatch: function(value, scope) {
var parent = scope.get("comment" + (value.length - 2));
parent.meta = value.length - 2;
return parent.get(this.next).get("comment");
},
regex: /^\s*\/\//,
next: "comment_block"
Expand Down Expand Up @@ -152,22 +153,17 @@ var JadeHighlightRules = function() {
}
],
"comment_block": [
{regex: /^\s*(?:\/\/)?/, onMatch: function(value, currentState, stack) {
if (value.length <= stack[1]) {
if (value.slice(-1) == "/") {
stack[1] = value.length - 2;
this.next = "";
return "comment";
{regex: /^\s*(?:\/\/)?/, onMatch: function(value, scope) {
if (value.length <= scope.parent.meta) {
if (value.slice(-1) == "/") {
scope.parent.meta = value.length - 2;
return scope.parent.parent.get("comment");
}
return scope.parent.parent.get("text");
} else {
return scope.get("comment");
}
stack.shift();
stack.shift();
this.next = stack.shift();
return "text";
} else {
this.next = "";
return "comment";
}
}, next: "start"},
}},
{defaultToken: "comment"}
],
/*
Expand Down
14 changes: 5 additions & 9 deletions lib/ace/mode/javascript_highlight_rules.js
Expand Up @@ -358,18 +358,14 @@ var JavaScriptHighlightRules = function(options) {

if (!options || !options.noES6) {
this.$rules.no_regex.unshift({
regex: "[{}]", onMatch: function(val, state, stack) {
regex: "[{}]", onMatch: function(val, scope) {
this.next = val == "{" ? this.nextState : "";
if (val == "{" && stack.length) {
stack.unshift("start", state);
}
else if (val == "}" && stack.length) {
stack.shift();
this.next = stack.shift();
if (val == "}" && scope.parent.parent) {
this.next = scope.parent;
if (this.next.indexOf("string") != -1 || this.next.indexOf("jsx") != -1)
return "paren.quasi.end";
return scope.parent.get("paren.quasi.end");
}
return val == "{" ? "paren.lparen" : "paren.rparen";
return val == "{" ? scope.get("paren.lparen") : scope.get("paren.rparen");
},
nextState: "start"
}, {
Expand Down
38 changes: 16 additions & 22 deletions lib/ace/mode/lua_highlight_rules.js
Expand Up @@ -95,25 +95,22 @@ var LuaHighlightRules = function() {
this.$rules = {
"start" : [{
stateName: "bracketedComment",
onMatch : function(value, currentState, stack){
stack.unshift(this.next, value.length - 2, currentState);
return "comment";
onMatch : function(value, scope){
var parent = scope.get("bracketedComment" + (value.length - 2))
parent.meta = (value.length - 2);
return parent.get(this.next).get("comment");
},
regex : /\-\-\[=*\[/,
next : [
{
onMatch : function(value, currentState, stack) {
if (value.length == stack[1]) {
stack.shift();
stack.shift();
this.next = stack.shift();
onMatch : function(value, scope) {
if (scope.parent && value.length == scope.parent.meta) {
return scope.parent.parent.get("comment");
} else {
this.next = "";
return scope.get("comment");
}
return "comment";
},
regex : /\]=*\]/,
next : "start"
}, {
defaultToken : "comment"
}
Expand All @@ -126,26 +123,23 @@ var LuaHighlightRules = function() {
},
{
stateName: "bracketedString",
onMatch : function(value, currentState, stack){
stack.unshift(this.next, value.length, currentState);
return "string.start";
onMatch : function(value, scope){
var parent = scope.get("bracketedString" + value.length);
parent.meta = value.length;
return parent.get(this.next).get("string.start");
},
regex : /\[=*\[/,
next : [
{
onMatch : function(value, currentState, stack) {
if (value.length == stack[1]) {
stack.shift();
stack.shift();
this.next = stack.shift();
onMatch : function(value, scope) {
if (scope.parent && value.length == scope.parent.meta) {
return scope.parent.parent.get("string.end");
} else {
this.next = "";
return scope.get("string.end");
}
return "string.end";
},

regex : /\]=*\]/,
next : "start"
}, {
defaultToken : "string"
}
Expand Down