|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// tslint:disable-next-line:import-name |
| 5 | +import Char from 'typescript-char'; |
| 6 | +import { BraceCounter } from '../language/braceCounter'; |
| 7 | +import { TextBuilder } from '../language/textBuilder'; |
| 8 | +import { Tokenizer } from '../language/tokenizer'; |
| 9 | +import { ITextRangeCollection, IToken, TokenType } from '../language/types'; |
| 10 | + |
| 11 | +export class LineFormatter { |
| 12 | + private builder: TextBuilder; |
| 13 | + private tokens: ITextRangeCollection<IToken>; |
| 14 | + private braceCounter: BraceCounter; |
| 15 | + private text: string; |
| 16 | + |
| 17 | + // tslint:disable-next-line:cyclomatic-complexity |
| 18 | + public formatLine(text: string): string { |
| 19 | + this.tokens = new Tokenizer().tokenize(text); |
| 20 | + this.text = text; |
| 21 | + this.builder = new TextBuilder(); |
| 22 | + this.braceCounter = new BraceCounter(); |
| 23 | + |
| 24 | + if (this.tokens.count === 0) { |
| 25 | + return this.text; |
| 26 | + } |
| 27 | + |
| 28 | + const ws = this.text.substr(0, this.tokens.getItemAt(0).start); |
| 29 | + if (ws.length > 0) { |
| 30 | + this.builder.append(ws); // Preserve leading indentation |
| 31 | + } |
| 32 | + |
| 33 | + for (let i = 0; i < this.tokens.count; i += 1) { |
| 34 | + const t = this.tokens.getItemAt(i); |
| 35 | + const prev = i > 0 ? this.tokens.getItemAt(i - 1) : undefined; |
| 36 | + const next = i < this.tokens.count - 1 ? this.tokens.getItemAt(i + 1) : undefined; |
| 37 | + |
| 38 | + switch (t.type) { |
| 39 | + case TokenType.Operator: |
| 40 | + this.handleOperator(i); |
| 41 | + break; |
| 42 | + |
| 43 | + case TokenType.Comma: |
| 44 | + this.builder.append(','); |
| 45 | + if (next && !this.isCloseBraceType(next.type)) { |
| 46 | + this.builder.softAppendSpace(); |
| 47 | + } |
| 48 | + break; |
| 49 | + |
| 50 | + case TokenType.Identifier: |
| 51 | + if (!prev || (!this.isOpenBraceType(prev.type) && prev.type !== TokenType.Colon)) { |
| 52 | + this.builder.softAppendSpace(); |
| 53 | + } |
| 54 | + this.builder.append(this.text.substring(t.start, t.end)); |
| 55 | + break; |
| 56 | + |
| 57 | + case TokenType.Colon: |
| 58 | + // x: 1 if not in slice, x[1:y] if inside the slice |
| 59 | + this.builder.append(':'); |
| 60 | + if (!this.braceCounter.isOpened(TokenType.OpenBracket) && (next && next.type !== TokenType.Colon)) { |
| 61 | + // Not inside opened [[ ... ] sequence |
| 62 | + this.builder.softAppendSpace(); |
| 63 | + } |
| 64 | + break; |
| 65 | + |
| 66 | + case TokenType.Comment: |
| 67 | + // add space before in-line comment |
| 68 | + if (prev) { |
| 69 | + this.builder.softAppendSpace(); |
| 70 | + } |
| 71 | + this.builder.append(this.text.substring(t.start, t.end)); |
| 72 | + break; |
| 73 | + |
| 74 | + default: |
| 75 | + this.handleOther(t); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + return this.builder.getText(); |
| 80 | + } |
| 81 | + |
| 82 | + private handleOperator(index: number): void { |
| 83 | + const t = this.tokens.getItemAt(index); |
| 84 | + if (index >= 2 && t.length === 1 && this.text.charCodeAt(t.start) === Char.Equal) { |
| 85 | + if (this.braceCounter.isOpened(TokenType.OpenBrace)) { |
| 86 | + // Check if this is = in function arguments. If so, do not |
| 87 | + // add spaces around it. |
| 88 | + const prev = this.tokens.getItemAt(index - 1); |
| 89 | + const prevPrev = this.tokens.getItemAt(index - 2); |
| 90 | + if (prev.type === TokenType.Identifier && |
| 91 | + (prevPrev.type === TokenType.Comma || prevPrev.type === TokenType.OpenBrace)) { |
| 92 | + this.builder.append('='); |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + this.builder.softAppendSpace(); |
| 98 | + this.builder.append(this.text.substring(t.start, t.end)); |
| 99 | + this.builder.softAppendSpace(); |
| 100 | + } |
| 101 | + |
| 102 | + private handleOther(t: IToken): void { |
| 103 | + if (this.isBraceType(t.type)) { |
| 104 | + this.braceCounter.countBrace(t); |
| 105 | + } |
| 106 | + this.builder.append(this.text.substring(t.start, t.end)); |
| 107 | + } |
| 108 | + |
| 109 | + private isOpenBraceType(type: TokenType): boolean { |
| 110 | + return type === TokenType.OpenBrace || type === TokenType.OpenBracket || type === TokenType.OpenCurly; |
| 111 | + } |
| 112 | + private isCloseBraceType(type: TokenType): boolean { |
| 113 | + return type === TokenType.CloseBrace || type === TokenType.CloseBracket || type === TokenType.CloseCurly; |
| 114 | + } |
| 115 | + private isBraceType(type: TokenType): boolean { |
| 116 | + return this.isOpenBraceType(type) || this.isCloseBraceType(type); |
| 117 | + } |
| 118 | +} |
0 commit comments