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

Commit

Permalink
Fixes microsoft/monaco-editor#1999: use colon to change previous toke…
Browse files Browse the repository at this point in the history
…n to a property name
  • Loading branch information
alexdima committed Sep 18, 2020
1 parent 49a5482 commit be0f95e
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions src/tokenization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function createTokenizationSupport(
supportComments: boolean
): languages.TokensProvider {
return {
getInitialState: () => new JSONState(null, null, false),
getInitialState: () => new JSONState(null, null),
tokenize: (line, state, offsetDelta?, stopAtOffset?) =>
tokenize(
supportComments,
Expand Down Expand Up @@ -38,20 +38,14 @@ class JSONState implements languages.IState {
private _state: languages.IState;

public scanError: json.ScanError;
public lastWasColon: boolean;

constructor(
state: languages.IState,
scanError: json.ScanError,
lastWasColon: boolean
) {
constructor(state: languages.IState, scanError: json.ScanError) {
this._state = state;
this.scanError = scanError;
this.lastWasColon = lastWasColon;
}

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 {
Expand All @@ -61,10 +55,7 @@ class JSONState implements languages.IState {
if (!other || !(other instanceof JSONState)) {
return false;
}
return (
this.scanError === (<JSONState>other).scanError &&
this.lastWasColon === (<JSONState>other).lastWasColon
);
return this.scanError === (<JSONState>other).scanError;
}

public getStateData(): languages.IState {
Expand Down Expand Up @@ -99,7 +90,6 @@ function tokenize(
}

const scanner = json.createScanner(line);
let lastWasColon = state.lastWasColon;

const ret: languages.ILineTokens = {
tokens: <languages.IToken[]>[],
Expand Down Expand Up @@ -134,44 +124,45 @@ function tokenize(
switch (kind) {
case json.SyntaxKind.OpenBraceToken:
type = TOKEN_DELIM_OBJECT;
lastWasColon = false;
break;
case json.SyntaxKind.CloseBraceToken:
type = TOKEN_DELIM_OBJECT;
lastWasColon = false;
break;
case json.SyntaxKind.OpenBracketToken:
type = TOKEN_DELIM_ARRAY;
lastWasColon = false;
break;
case json.SyntaxKind.CloseBracketToken:
type = TOKEN_DELIM_ARRAY;
lastWasColon = false;
break;
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;
lastWasColon = true;
break;
case json.SyntaxKind.CommaToken:
type = TOKEN_DELIM_COMMA;
lastWasColon = false;
break;
case json.SyntaxKind.TrueKeyword:
case json.SyntaxKind.FalseKeyword:
type = TOKEN_VALUE_BOOLEAN;
lastWasColon = false;
break;
case json.SyntaxKind.NullKeyword:
type = TOKEN_VALUE_NULL;
lastWasColon = false;
break;
case json.SyntaxKind.StringLiteral:
type = lastWasColon ? TOKEN_VALUE_STRING : TOKEN_PROPERTY_NAME;
lastWasColon = false;
type = TOKEN_VALUE_STRING;

This comment has been minimized.

Copy link
@pankajk07

pankajk07 Sep 18, 2020

@alexdima We are assuming the strings to always default as value string. A scenario like when you start typing the key part of the string after opening {, it displays as value string but changes to property name only after you have typed a colon. Is it a correct expectation, as it a deviation from the current experience.

This comment has been minimized.

Copy link
@alexdima

alexdima Sep 18, 2020

Author Member

You have a very good point. Do you have a better idea how to make this better? PR welcome.

This comment has been minimized.

Copy link
@pankajk07

pankajk07 Sep 19, 2020

@alexdima Let me know if this looks good to you. #12

break;
case json.SyntaxKind.NumericLiteral:
type = TOKEN_VALUE_NUMBER;
lastWasColon = false;
break;
}

Expand All @@ -187,11 +178,7 @@ function tokenize(
}
}

ret.endState = new JSONState(
state.getStateData(),
scanner.getTokenError(),
lastWasColon
);
ret.endState = new JSONState(state.getStateData(), scanner.getTokenError());
ret.tokens.push({
startIndex: offset,
scopes: type
Expand Down

0 comments on commit be0f95e

Please sign in to comment.