FormatterDebug.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. Formatter.prototype.debug = function(from, to)
  2. {
  3. var debug = "";
  4. var outerMode = this._codeMirror.getMode();
  5. var content = this._codeMirror.getRange(from, to);
  6. var state = CodeMirror.copyState(outerMode, this._codeMirror.getTokenAt(from).state);
  7. function pad(str, x, doNotQuote)
  8. {
  9. var result = doNotQuote ? str : "'" + str + "'";
  10. for (var toPad = x - result.length; toPad > 0; --toPad)
  11. result += " ";
  12. return result;
  13. }
  14. function debugToken(mode, token, state, stream, originalPosition, startOfNewLine)
  15. {
  16. // Token Type
  17. debug += "Token: " + pad(String(token), 14, !token);
  18. // Original Position
  19. debug += "Position: " + pad(String(originalPosition), 10);
  20. // Language Specific Info
  21. if (state.lexical) {
  22. debug += "Lexical: " + pad(String(state.lexical.type), 10); // JavaScript
  23. debug += "Prev: " + pad(String(state.lexical.prev ? state.lexical.prev.type : state.lexical.prev), 10, !state.lexical.prev);
  24. }
  25. else if (state.stack)
  26. debug += "Stack: " + pad(String(state.stack[state.stack.length-1]), 16); // CSS
  27. // String
  28. debug += "Current: '" + stream.current() + "'\n";
  29. }
  30. var lineOffset = 0;
  31. var lines = content.split("\n");
  32. for (var i = 0; i < lines.length; ++i) {
  33. var line = lines[i];
  34. var startOfNewLine = true;
  35. var stream = new CodeMirror.StringStream(line);
  36. while (!stream.eol()) {
  37. var innerMode = CodeMirror.innerMode(outerMode, state);
  38. var token = outerMode.token(stream, state);
  39. debugToken(innerMode.mode, token, state, stream, lineOffset + stream.start, startOfNewLine);
  40. stream.start = stream.pos;
  41. startOfNewLine = false;
  42. }
  43. }
  44. return debug;
  45. }