Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/indentation/common/indentUtils.ts
3296 views
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
6
export function getSpaceCnt(str: string, tabSize: number) {
7
let spacesCnt = 0;
8
9
for (let i = 0; i < str.length; i++) {
10
if (str.charAt(i) === '\t') {
11
spacesCnt += tabSize;
12
} else {
13
spacesCnt++;
14
}
15
}
16
17
return spacesCnt;
18
}
19
20
export function generateIndent(spacesCnt: number, tabSize: number, insertSpaces: boolean) {
21
spacesCnt = spacesCnt < 0 ? 0 : spacesCnt;
22
23
let result = '';
24
if (!insertSpaces) {
25
const tabsCnt = Math.floor(spacesCnt / tabSize);
26
spacesCnt = spacesCnt % tabSize;
27
for (let i = 0; i < tabsCnt; i++) {
28
result += '\t';
29
}
30
}
31
32
for (let i = 0; i < spacesCnt; i++) {
33
result += ' ';
34
}
35
36
return result;
37
}
38