a46f44f4571dc5ad7a3f131c38ddce0a0115584ed8beec84b543ace62da2aca38178c643925d31e21467e437bd0763c117ad095a7e859ba62e059bdad71b8f 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  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 * as strings from '../../../base/common/strings.js';
  6. import { CursorColumns } from './cursorColumns.js';
  7. function _normalizeIndentationFromWhitespace(str, indentSize, insertSpaces) {
  8. let spacesCnt = 0;
  9. for (let i = 0; i < str.length; i++) {
  10. if (str.charAt(i) === '\t') {
  11. spacesCnt = CursorColumns.nextIndentTabStop(spacesCnt, indentSize);
  12. }
  13. else {
  14. spacesCnt++;
  15. }
  16. }
  17. let result = '';
  18. if (!insertSpaces) {
  19. const tabsCnt = Math.floor(spacesCnt / indentSize);
  20. spacesCnt = spacesCnt % indentSize;
  21. for (let i = 0; i < tabsCnt; i++) {
  22. result += '\t';
  23. }
  24. }
  25. for (let i = 0; i < spacesCnt; i++) {
  26. result += ' ';
  27. }
  28. return result;
  29. }
  30. export function normalizeIndentation(str, indentSize, insertSpaces) {
  31. let firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(str);
  32. if (firstNonWhitespaceIndex === -1) {
  33. firstNonWhitespaceIndex = str.length;
  34. }
  35. return _normalizeIndentationFromWhitespace(str.substring(0, firstNonWhitespaceIndex), indentSize, insertSpaces) + str.substring(firstNonWhitespaceIndex);
  36. }