I’m using codeMirror for SQL editor and have a requirement of using double-quote for string variable, how can I achieve that? I used this code learning from this post:
CodeMirror.defineMode('allowDoubleQuote', () => ({
startState: () => {
return { inString: false };
},
token: (stream: StringStream, state) => {
//If a string starts here
if (!state.inString && stream.peek() === '"') {
stream.next(); // Skip quote
state.inString = true; // Update state
}
if (state.inString) {
if (stream.skipTo('"')) {
// Quote found on this line
stream.next(); // Skip quote
state.inString = false; // Clear flag
} else {
stream.skipToEnd(); // Rest of line is string
}
return 'string'; // Token style
} else {
stream.skipTo('"') || stream.skipToEnd();
return null; // Unstyled token
}
},
}));
const editor = CodeMirror.fromTextArea(xxxx, {
mode: 'allowDoubleQuote',
...
})
The state inString
is true for all the text inside the double quote, however, when I run editor.getLineTokens
, the texts inside the double quote are still cut into pieces and identified as keyword.
What’s wrong, or, what’s a better approach to achieve that?