|
6 | 6 |
|
7 | 7 | import { IStringStream } from 'vs/platform/files/common/files'; |
8 | 8 | import * as crypto from 'crypto'; |
9 | | -import { DefaultEndOfLine, ITextModelCreationOptions, TextModelResolvedOptions, IRawText } from 'vs/editor/common/editorCommon'; |
| 9 | +import { ITextSource } from 'vs/editor/common/editorCommon'; |
10 | 10 | import * as strings from 'vs/base/common/strings'; |
11 | | -import { guessIndentation } from 'vs/editor/common/model/indentationGuesser'; |
12 | 11 | import { TPromise } from 'vs/base/common/winjs.base'; |
13 | 12 | import { CharCode } from 'vs/base/common/charCode'; |
14 | | -import { IRawTextProvider } from 'vs/editor/common/services/modelService'; |
| 13 | + |
| 14 | +export interface ModelBuilderResult { |
| 15 | + readonly hash: string; |
| 16 | + readonly value: ITextSource; |
| 17 | +} |
15 | 18 |
|
16 | 19 | class ModelLineBasedBuilder { |
17 | 20 |
|
@@ -42,119 +45,31 @@ class ModelLineBasedBuilder { |
42 | 45 | this.hash.update(lines.join('\n') + '\n'); |
43 | 46 | } |
44 | 47 |
|
45 | | - public finish(totalLength: number, carriageReturnCnt: number, containsRTL: boolean, isBasicASCII: boolean): ModelBuilderResult { |
46 | | - return new ModelBuilderResult(this.BOM, this.lines, totalLength, carriageReturnCnt, containsRTL, isBasicASCII, this.hash.digest('hex')); |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -export class ModelBuilderResult implements IRawTextProvider { |
51 | | - /** |
52 | | - * The BOM (leading character sequence of the file). |
53 | | - */ |
54 | | - private readonly BOM: string; |
55 | | - /** |
56 | | - * The text split into lines. |
57 | | - */ |
58 | | - private readonly lines: string[]; |
59 | | - /** |
60 | | - * The entire text length. |
61 | | - */ |
62 | | - private readonly length: number; |
63 | | - /** |
64 | | - * Number of lines with EOL \r\n |
65 | | - */ |
66 | | - private readonly carriageReturnCnt: number; |
67 | | - /** |
68 | | - * The text contains Unicode characters classified as "R" or "AL". |
69 | | - */ |
70 | | - private readonly containsRTL: boolean; |
71 | | - /** |
72 | | - * The text contains only characters inside the ASCII range 32-126 or \t \r \n |
73 | | - */ |
74 | | - private readonly isBasicASCII: boolean; |
75 | | - /** |
76 | | - * The content hash. |
77 | | - */ |
78 | | - public readonly hash: string; |
79 | | - |
80 | | - constructor(BOM: string, lines: string[], length: number, carriageReturnCnt: number, containsRTL: boolean, isBasicASCII: boolean, hash: string) { |
81 | | - this.BOM = BOM; |
82 | | - this.lines = lines; |
83 | | - this.length = length; |
84 | | - this.carriageReturnCnt = carriageReturnCnt; |
85 | | - this.containsRTL = containsRTL; |
86 | | - this.isBasicASCII = isBasicASCII; |
87 | | - this.hash = hash; |
88 | | - } |
89 | | - |
90 | | - public getEntireContent(): string { |
91 | | - let lineFeedCnt = this.lines.length - 1; |
92 | | - if (lineFeedCnt === 0) { |
93 | | - // Just one line, EOL does not matter |
94 | | - return this.lines[0]; |
95 | | - } |
96 | | - |
97 | | - let EOL = ''; |
98 | | - if (this.carriageReturnCnt > lineFeedCnt / 2) { |
99 | | - // More than half of the file contains \r\n ending lines |
100 | | - EOL = '\r\n'; |
101 | | - } else { |
102 | | - // At least one line more ends in \n |
103 | | - EOL = '\n'; |
104 | | - } |
105 | | - return this.lines.join(EOL); |
106 | | - } |
107 | | - |
108 | | - public getFirstLine(): string { |
109 | | - return this.lines[0]; |
110 | | - } |
111 | | - |
112 | | - public toRawText(opts: ITextModelCreationOptions): IRawText { |
113 | | - |
| 48 | + public finish(length: number, carriageReturnCnt: number, containsRTL: boolean, isBasicASCII: boolean): ModelBuilderResult { |
114 | 49 | let lineFeedCnt = this.lines.length - 1; |
115 | 50 | let EOL = ''; |
116 | | - if (lineFeedCnt === 0) { |
117 | | - // This is an empty file or a file with precisely one line |
118 | | - EOL = (opts.defaultEOL === DefaultEndOfLine.LF ? '\n' : '\r\n'); |
119 | | - } else if (this.carriageReturnCnt > lineFeedCnt / 2) { |
| 51 | + if (carriageReturnCnt > lineFeedCnt / 2) { |
120 | 52 | // More than half of the file contains \r\n ending lines |
121 | 53 | EOL = '\r\n'; |
122 | 54 | } else { |
123 | 55 | // At least one line more ends in \n |
124 | 56 | EOL = '\n'; |
125 | 57 | } |
126 | | - |
127 | | - let resolvedOpts: TextModelResolvedOptions; |
128 | | - if (opts.detectIndentation) { |
129 | | - let guessedIndentation = guessIndentation(this.lines, opts.tabSize, opts.insertSpaces); |
130 | | - resolvedOpts = new TextModelResolvedOptions({ |
131 | | - tabSize: guessedIndentation.tabSize, |
132 | | - insertSpaces: guessedIndentation.insertSpaces, |
133 | | - trimAutoWhitespace: opts.trimAutoWhitespace, |
134 | | - defaultEOL: opts.defaultEOL |
135 | | - }); |
136 | | - } else { |
137 | | - resolvedOpts = new TextModelResolvedOptions({ |
138 | | - tabSize: opts.tabSize, |
139 | | - insertSpaces: opts.insertSpaces, |
140 | | - trimAutoWhitespace: opts.trimAutoWhitespace, |
141 | | - defaultEOL: opts.defaultEOL |
142 | | - }); |
143 | | - } |
144 | | - |
145 | 58 | return { |
146 | | - BOM: this.BOM, |
147 | | - EOL: EOL, |
148 | | - lines: this.lines, |
149 | | - length: this.length, |
150 | | - containsRTL: this.containsRTL, |
151 | | - isBasicASCII: this.isBasicASCII, |
152 | | - options: resolvedOpts |
| 59 | + hash: this.hash.digest('hex'), |
| 60 | + value: { |
| 61 | + BOM: this.BOM, |
| 62 | + lines: this.lines, |
| 63 | + length, |
| 64 | + containsRTL: containsRTL, |
| 65 | + EOL, |
| 66 | + isBasicASCII, |
| 67 | + } |
153 | 68 | }; |
154 | 69 | } |
155 | 70 | } |
156 | 71 |
|
157 | | -export function computeHash(rawText: IRawText): string { |
| 72 | +export function computeHash(rawText: ITextSource): string { |
158 | 73 | let hash = crypto.createHash('sha1'); |
159 | 74 | for (let i = 0, len = rawText.lines.length; i < len; i++) { |
160 | 75 | hash.update(rawText.lines[i] + '\n'); |
|
0 commit comments