modified.txt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /// <reference path="../../references.js" />
  2. (function () {
  3. "use strict";
  4. var deltaDecorations = function (oldDecorations, newDecorations) {
  5. /// <summary>
  6. /// Update oldDecorations to match newDecorations.
  7. /// It will remove old decorations which are not found in new decorations
  8. /// and add only the really new decorations.
  9. /// </summary>
  10. /// <param name="oldDecorations" type="Array">
  11. /// An array containing ids of existing decorations
  12. /// </param>
  13. /// <param name="newDecorations" type="Array">
  14. /// An array containing literal objects describing new decorations. A
  15. /// literal contains the following two fields:
  16. /// range
  17. /// options
  18. /// </param>
  19. /// <returns type="Array">
  20. /// Returns an array of decorations ids
  21. /// </returns>
  22. var hashFunc = function (range, options) {
  23. return range.startLineNumber + "," + range.startColumn + "-" + range.endLineNumber + "," + range.endColumn +
  24. "-" + options.hoverMessage + "-" + options.className + "-" + options.isOverlay + "-" + options.showInOverviewRuler;
  25. };
  26. return this.changeDecorations(function (changeAccessor) {
  27. var i, len, oldDecorationsMap = {}, hash;
  28. // Record old decorations in a map
  29. // Two decorations can have the same hash
  30. for (i = 0, len = oldDecorations.length; i < len; i++) {
  31. hash = hashFunc(this.getDecorationRange(oldDecorations[i]), this.getDecorationOptions(oldDecorations[i]));
  32. oldDecorationsMap[hash] = oldDecorationsMap[hash] || [];
  33. oldDecorationsMap[hash].push(oldDecorations[i]);
  34. }
  35. // Add only new decorations & mark reused ones
  36. var j, lenJ, result = [], usedOldDecorations = {}, oldDecorationsCandidates, reusedOldDecoration;
  37. for (i = 0, len = newDecorations.length; i < len; i++) {
  38. hash = hashFunc(newDecorations[i].range, newDecorations[i].options);
  39. reusedOldDecoration = false;
  40. if (oldDecorationsMap.hasOwnProperty(hash)) {
  41. oldDecorationsCandidates = oldDecorationsMap[hash];
  42. // We can try reusing an old decoration (if it hasn't been reused before)
  43. for (j = 0, lenJ = oldDecorationsCandidates.length; j < lenJ; j++) {
  44. if (!usedOldDecorations.hasOwnProperty(oldDecorationsCandidates[j])) {
  45. // Found an old decoration which can be reused & it hasn't been reused before
  46. reusedOldDecoration = true;
  47. usedOldDecorations[oldDecorationsCandidates[j]] = true;
  48. result.push(oldDecorationsCandidates[j]);
  49. break;
  50. }
  51. }
  52. }
  53. if (!reusedOldDecoration) {
  54. result.push(changeAccessor.addDecoration(newDecorations[i].range, newDecorations[i].options));
  55. }
  56. }
  57. // Remove unused old decorations
  58. for (i = 0, len = oldDecorations.length; i < len; i++) {
  59. if (!usedOldDecorations.hasOwnProperty(oldDecorations[i])) {
  60. changeAccessor.removeDecoration(oldDecorations[i]);
  61. }
  62. }
  63. return result;
  64. }.bind(this));
  65. };
  66. })();