Topic on Extension talk:CodeMirror

Stability of cm.getTokenAt(pos)

Summary by Thiemo Kreuz (WMDE)

Please report at

MarMi wiki (talkcontribs)

Does the value returned by cm.getTokenAt(pos) (specifically string) shouldn't be stable (the string value shouldn't change its value on every call)?

Example from

  • Article Francisco Echevarría (link to edit in old code ditor [if not logged in] and in safemode), enable CodeMirror (pen[?] button between puzzle piece and Zaawansowane [Advanced]).
  • In dev console (F12) paste and execute var editor = $(".CodeMirror")
  • Paste and execute a few (3 or more) times: editor[0].CodeMirror.getTokenAt({line:26,ch:5}), notice that the string property adds a new token every time
  • Add new line before the 26 line (before the quotes of the black name Francisco Echevarria), notice that the color of highlighting changes.
  • Again execute a few times getTokenAt (with updated line to 27!), notice that the string property is stable (doesn't add new tokens)
  • To restore the previous state, delete the new line (also probably backspace would be needed, to place the name line after the closing brackets of template above it)
Thiemo Kreuz (WMDE) (talkcontribs)
MarMi wiki (talkcontribs)

It can cause problems (), since calling getTokenAt also influences what getLineTokens returns: normally it returns an array for entire line, but if you run getLineTokens after getTokenAt, on its first call the returned array will have less elements than normal.

getTokenAt cycles through all the tokens in the line - first it's the token at pos, then what's before it, and then adds the tokens after it (one by one), until the end of line, where it resets and starts the cycle again.

I don't know if it matters in wiki context, but it could cause problems with some CodeMirror addons.

MarMi wiki (talkcontribs)

I'm familiarizing myself with CodeMirror on, to eventually add user script with ref folding (using the CodeMirror fold addons) in old code editor. Text/mediawiki mode doesn't set the html/xml (sub)mode for tags, so the xml-fold helper doesn't work automatically.