original.txt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /// <reference path="../../references.js" />
  2. (function () {
  3. "use strict";
  4. // Some useless comment
  5. var deltaDecorations = function (oldDecorations, newDecorations) {
  6. /// <summary>
  7. /// Update oldDecorations to match newDecorations.
  8. /// It will remove old decorations which are not found in new decorations
  9. /// and add only the really new decorations.
  10. /// </summary>
  11. /// <param name="oldDecorations" type="Array">
  12. /// An array containing ids of existing decorations
  13. /// </param>
  14. /// <param name="newDecorations" type="Array">
  15. /// An array containing literal objects describing new decorations. A
  16. /// literal contains the following two fields:
  17. /// range
  18. /// options
  19. /// </param>
  20. /// <returns type="Array">
  21. /// Returns an array of decorations ids
  22. /// </returns>
  23. var hashFunc = function (range, options) {
  24. return range.startLineNumber + "," + range.startColumn + "-" + range.endLineNumber + "," + range.endColumn +
  25. "-" + options.hoverMessage + "-" + options.className + "-" + options.isOverlay + "-" + options.showInOverviewRuler;
  26. };
  27. return this.changeDecorations(function (changeAccessor) {
  28. var i, len, oldDecorationsMap = {}, hash;
  29. // Record old decorations in a map
  30. for (i = 0, len = oldDecorations.length; i < len; i++) {
  31. hash = hashFunc(this.getDecorationRange(oldDecorations[i]), this.getDecorationOptions(oldDecorations[i]));
  32. oldDecorationsMap[hash] = i;
  33. }
  34. // Add only new decorations & mark reused ones
  35. var result = [], usedOldDecorationsMap = {};
  36. for (i = 0, len = newDecorations.length; i < len; i++) {
  37. hash = hashFunc(newDecorations[i].range, newDecorations[i].options);
  38. if (oldDecorationsMap.hasOwnProperty(hash)) {
  39. usedOldDecorationsMap[oldDecorationsMap[hash]] = true;
  40. result.push(oldDecorations[oldDecorationsMap[hash]]);
  41. } else {
  42. result.push(changeAccessor.addDecoration(newDecorations[i].range, newDecorations[i].options));
  43. }
  44. }
  45. // Remove unused old decorations
  46. for (i = 0, len = oldDecorations.length; i < len; i++) {
  47. if (!usedOldDecorationsMap.hasOwnProperty(i)) {
  48. changeAccessor.removeDecoration(oldDecorations[i]);
  49. }
  50. }
  51. return result;
  52. }.bind(this));
  53. };
  54. })();