intializingRangeProvider.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { sanitizeRanges } from './syntaxRangeProvider.js';
  6. export const ID_INIT_PROVIDER = 'init';
  7. export class InitializingRangeProvider {
  8. constructor(editorModel, initialRanges, onTimeout, timeoutTime) {
  9. this.editorModel = editorModel;
  10. this.id = ID_INIT_PROVIDER;
  11. if (initialRanges.length) {
  12. let toDecorationRange = (range) => {
  13. return {
  14. range: {
  15. startLineNumber: range.startLineNumber,
  16. startColumn: 0,
  17. endLineNumber: range.endLineNumber,
  18. endColumn: editorModel.getLineLength(range.endLineNumber)
  19. },
  20. options: {
  21. description: 'folding-initializing-range-provider',
  22. stickiness: 1 /* NeverGrowsWhenTypingAtEdges */
  23. }
  24. };
  25. };
  26. this.decorationIds = editorModel.deltaDecorations([], initialRanges.map(toDecorationRange));
  27. this.timeout = setTimeout(onTimeout, timeoutTime);
  28. }
  29. }
  30. dispose() {
  31. if (this.decorationIds) {
  32. this.editorModel.deltaDecorations(this.decorationIds, []);
  33. this.decorationIds = undefined;
  34. }
  35. if (typeof this.timeout === 'number') {
  36. clearTimeout(this.timeout);
  37. this.timeout = undefined;
  38. }
  39. }
  40. compute(cancelationToken) {
  41. let foldingRangeData = [];
  42. if (this.decorationIds) {
  43. for (let id of this.decorationIds) {
  44. let range = this.editorModel.getDecorationRange(id);
  45. if (range) {
  46. foldingRangeData.push({ start: range.startLineNumber, end: range.endLineNumber, rank: 1 });
  47. }
  48. }
  49. }
  50. return Promise.resolve(sanitizeRanges(foldingRangeData, Number.MAX_VALUE));
  51. }
  52. }