Skip to content

Commit

Permalink
refactor due to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc authored and nightwing committed Nov 24, 2024
1 parent fb48def commit bf581ad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/mode/lua_highlight_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var LuaHighlightRules = function() {
},
regex : /\]=*\]/,
}, {
defaultToken : "comment"
defaultToken : "comment.body"
}
]
},
Expand Down
8 changes: 1 addition & 7 deletions src/mode/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ Mode = function() {
this.$highlightRules = this.$highlightRules || new this.HighlightRules(this.$highlightRuleConfig);
var modeName;
if (this.$id) {
var chunks = this.$id.split('/');
if (chunks.length > 1) {
modeName = chunks[chunks.length - 1];
}
else {
modeName = chunks;
}
modeName = this.$id.split('/').pop();
}
else {
modeName = "root";
Expand Down
28 changes: 28 additions & 0 deletions src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Scope {
stringObj.count = this.count;
stringObj.getAllScopeNames = this.getAllScopeNames;
stringObj.toStack = this.toStack;
stringObj.fromStack = this.fromStack;

return stringObj;
}
Expand Down Expand Up @@ -88,6 +89,33 @@ class Scope {
} while (self = self.parent);
return stack;
}

/**
* Retrieves the scope for the given stack and current state.
*
* @param {string[]} stack - The stack of scopes.
* @param {string | Scope} currentState - The current state.
* @returns {Scope} The scope for the given stack and current state.
*/
fromStack(stack, currentState) {
/**@type{Scope}*/
let scope = this;
while (scope.parent) {
scope = scope.parent;
}

if (stack.length === 0) {
return scope.get(currentState); //the start state
}
for (var i = stack.length - 1; i >= 0; i--) {
scope = scope.get(stack[i]);
}
if (stack[0] !== currentState) {
scope = scope.get(currentState, "#tmp");
}

return scope;
}
}


Expand Down
24 changes: 2 additions & 22 deletions src/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class Tokenizer {
else {
if (rule.next) {
currentState = rule.next(currentState.toString(), stack);
currentState = this.fromStack(stack, currentState);
currentState = this.rootScope.fromStack(stack, currentState);
}
else {
currentState = rule.next2(currentState, stack);
Expand Down Expand Up @@ -331,7 +331,7 @@ class Tokenizer {

if (value) {
if (type && !type["getAllScopeNames"]) {
currentState = this.fromStack(stack, currentState.toString());
currentState = this.rootScope.fromStack(stack, currentState);
}

if (type && !Array.isArray(type) && type != "") {
Expand Down Expand Up @@ -402,27 +402,7 @@ class Tokenizer {
};
}

/**
* Retrieves the scope for the given stack and current state.
*
* @param {string[]} stack - The stack of scopes.
* @param {string} currentState - The current state.
* @returns {Scope} The scope for the given stack and current state.
*/
fromStack(stack, currentState) {
let scope = this.rootScope;
if (stack.length === 0) {
return scope.get(currentState); //the start state
}
for (var i = stack.length - 1; i >= 0; i--) {
scope = scope.get(stack[i]);
}
if (stack[0] !== currentState) {
scope = scope.get(currentState, "#tmp");
}

return scope;
}
}

Tokenizer.prototype.reportError = reportError;
Expand Down
4 changes: 2 additions & 2 deletions src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class Tooltip {
* @param {Number} y
**/
setPosition(x, y) {
this.getElement().style.left = Math.round(x) + "px";
this.getElement().style.top = Math.round(y) + "px";
this.getElement().style.left = x + "px";
this.getElement().style.top = y + "px";
}

/**
Expand Down

0 comments on commit bf581ad

Please sign in to comment.