Skip to content

Instantly share code, notes, and snippets.

@rooks
Last active March 12, 2024 12:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rooks/6a13affb544ef8bc338b49af7d018318 to your computer and use it in GitHub Desktop.
Save rooks/6a13affb544ef8bc338b49af7d018318 to your computer and use it in GitHub Desktop.
CodeMirror rainbow CSV mode
const DELIM = ','
const QUOTE = '"'
const MAX_LENGTH = 9
const tokenRainbow = state => `string num${state.num}`
const tokenPlain = () => 'string'
CodeMirror.defineMode('csv', function modeCsv(opts, modeOpts) {
let { delimiter = DELIM, rainbow = false } = modeOpts
let token = rainbow ? tokenRainbow : tokenPlain
function tokenMain(stream, state) {
if (stream.sol()) {
state.num = 0
}
let ch = stream.next()
if (ch === QUOTE) {
state.tokenize = tokenQuotes
return token(state)
}
if (ch === delimiter) {
state.num = (state.num + 1) % MAX_LENGTH
return 'delim'
}
return token(state)
}
function tokenQuotes(stream, state) {
let ch = stream.next()
if (ch === QUOTE) {
let next = stream.peek()
// escaped quotaion mark
if (next === QUOTE) {
stream.next()
}
// end of the record
else {
state.tokenize = tokenMain
}
}
return token(state)
}
return {
startState() {
return {
num: 0,
tokenize: tokenMain
}
},
token(stream, state) {
let style = state.tokenize(stream, state)
return style
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment