mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 20:52:56 +01:00
Fixes microsoft/monaco-editor#1999: use colon to change previous token to a property name
This commit is contained in:
parent
49a548240c
commit
be0f95eaff
1 changed files with 17 additions and 30 deletions
|
|
@ -10,7 +10,7 @@ export function createTokenizationSupport(
|
||||||
supportComments: boolean
|
supportComments: boolean
|
||||||
): languages.TokensProvider {
|
): languages.TokensProvider {
|
||||||
return {
|
return {
|
||||||
getInitialState: () => new JSONState(null, null, false),
|
getInitialState: () => new JSONState(null, null),
|
||||||
tokenize: (line, state, offsetDelta?, stopAtOffset?) =>
|
tokenize: (line, state, offsetDelta?, stopAtOffset?) =>
|
||||||
tokenize(
|
tokenize(
|
||||||
supportComments,
|
supportComments,
|
||||||
|
|
@ -38,20 +38,14 @@ class JSONState implements languages.IState {
|
||||||
private _state: languages.IState;
|
private _state: languages.IState;
|
||||||
|
|
||||||
public scanError: json.ScanError;
|
public scanError: json.ScanError;
|
||||||
public lastWasColon: boolean;
|
|
||||||
|
|
||||||
constructor(
|
constructor(state: languages.IState, scanError: json.ScanError) {
|
||||||
state: languages.IState,
|
|
||||||
scanError: json.ScanError,
|
|
||||||
lastWasColon: boolean
|
|
||||||
) {
|
|
||||||
this._state = state;
|
this._state = state;
|
||||||
this.scanError = scanError;
|
this.scanError = scanError;
|
||||||
this.lastWasColon = lastWasColon;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public clone(): JSONState {
|
public clone(): JSONState {
|
||||||
return new JSONState(this._state, this.scanError, this.lastWasColon);
|
return new JSONState(this._state, this.scanError);
|
||||||
}
|
}
|
||||||
|
|
||||||
public equals(other: languages.IState): boolean {
|
public equals(other: languages.IState): boolean {
|
||||||
|
|
@ -61,10 +55,7 @@ class JSONState implements languages.IState {
|
||||||
if (!other || !(other instanceof JSONState)) {
|
if (!other || !(other instanceof JSONState)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return (
|
return this.scanError === (<JSONState>other).scanError;
|
||||||
this.scanError === (<JSONState>other).scanError &&
|
|
||||||
this.lastWasColon === (<JSONState>other).lastWasColon
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getStateData(): languages.IState {
|
public getStateData(): languages.IState {
|
||||||
|
|
@ -99,7 +90,6 @@ function tokenize(
|
||||||
}
|
}
|
||||||
|
|
||||||
const scanner = json.createScanner(line);
|
const scanner = json.createScanner(line);
|
||||||
let lastWasColon = state.lastWasColon;
|
|
||||||
|
|
||||||
const ret: languages.ILineTokens = {
|
const ret: languages.ILineTokens = {
|
||||||
tokens: <languages.IToken[]>[],
|
tokens: <languages.IToken[]>[],
|
||||||
|
|
@ -134,44 +124,45 @@ function tokenize(
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case json.SyntaxKind.OpenBraceToken:
|
case json.SyntaxKind.OpenBraceToken:
|
||||||
type = TOKEN_DELIM_OBJECT;
|
type = TOKEN_DELIM_OBJECT;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.CloseBraceToken:
|
case json.SyntaxKind.CloseBraceToken:
|
||||||
type = TOKEN_DELIM_OBJECT;
|
type = TOKEN_DELIM_OBJECT;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.OpenBracketToken:
|
case json.SyntaxKind.OpenBracketToken:
|
||||||
type = TOKEN_DELIM_ARRAY;
|
type = TOKEN_DELIM_ARRAY;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.CloseBracketToken:
|
case json.SyntaxKind.CloseBracketToken:
|
||||||
type = TOKEN_DELIM_ARRAY;
|
type = TOKEN_DELIM_ARRAY;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.ColonToken:
|
case json.SyntaxKind.ColonToken:
|
||||||
|
for (let i = ret.tokens.length - 1; i >= 0; i--) {
|
||||||
|
const token = ret.tokens[i];
|
||||||
|
if (token.scopes === '' || token.scopes === TOKEN_COMMENT_BLOCK) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (token.scopes === TOKEN_VALUE_STRING) {
|
||||||
|
// !change previous token to property name!
|
||||||
|
token.scopes = TOKEN_PROPERTY_NAME;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
type = TOKEN_DELIM_COLON;
|
type = TOKEN_DELIM_COLON;
|
||||||
lastWasColon = true;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.CommaToken:
|
case json.SyntaxKind.CommaToken:
|
||||||
type = TOKEN_DELIM_COMMA;
|
type = TOKEN_DELIM_COMMA;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.TrueKeyword:
|
case json.SyntaxKind.TrueKeyword:
|
||||||
case json.SyntaxKind.FalseKeyword:
|
case json.SyntaxKind.FalseKeyword:
|
||||||
type = TOKEN_VALUE_BOOLEAN;
|
type = TOKEN_VALUE_BOOLEAN;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.NullKeyword:
|
case json.SyntaxKind.NullKeyword:
|
||||||
type = TOKEN_VALUE_NULL;
|
type = TOKEN_VALUE_NULL;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.StringLiteral:
|
case json.SyntaxKind.StringLiteral:
|
||||||
type = lastWasColon ? TOKEN_VALUE_STRING : TOKEN_PROPERTY_NAME;
|
type = TOKEN_VALUE_STRING;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
case json.SyntaxKind.NumericLiteral:
|
case json.SyntaxKind.NumericLiteral:
|
||||||
type = TOKEN_VALUE_NUMBER;
|
type = TOKEN_VALUE_NUMBER;
|
||||||
lastWasColon = false;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -187,11 +178,7 @@ function tokenize(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.endState = new JSONState(
|
ret.endState = new JSONState(state.getStateData(), scanner.getTokenError());
|
||||||
state.getStateData(),
|
|
||||||
scanner.getTokenError(),
|
|
||||||
lastWasColon
|
|
||||||
);
|
|
||||||
ret.tokens.push({
|
ret.tokens.push({
|
||||||
startIndex: offset,
|
startIndex: offset,
|
||||||
scopes: type
|
scopes: type
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue