forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshims.ts
More file actions
145 lines (116 loc) · 5.12 KB
/
Copy pathshims.ts
File metadata and controls
145 lines (116 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/// <reference path="services.ts"/>
module ts {
export interface LanguageServiceShimHost {
log(s: string): void;
getCompilationSettings(): string;
getScriptFileNames(): string;
getScriptVersion(fileName: string): string;
getScriptIsOpen(fileName: string): boolean;
getScriptByteOrderMark(fileName: string): number;
getScriptSnapshot(fileName: string): ScriptSnapshotShim;
getLocalizedDiagnosticMessages(): string;
// getCancellationToken(): CancellationToken
}
export interface ScriptSnapshotShim {
// Get's a portion of the script snapshot specified by [start, end).
getText(start: number, end: number): string;
// Get's the length of this script snapshot.
getLength(): number;
// This call returns the JSON encoded array of the type:
// number[]
getLineStartPositions(): string;
// Returns a JSON encoded value of the type:
// { span: { start: number; length: number }; newLength: number }
//
// Or null value if there was no change.
getChangeRange(oldSnapshot: ScriptSnapshotShim): string;
}
class ScriptSnapshotShimAdapter implements IScriptSnapshot {
private lineStartPositions: number[] = null;
constructor(private scriptSnapshotShim: ScriptSnapshotShim) {
}
public getText(start: number, end: number): string {
return this.scriptSnapshotShim.getText(start, end);
}
public getLength(): number {
return this.scriptSnapshotShim.getLength();
}
public getLineStartPositions(): number[] {
if (this.lineStartPositions == null) {
this.lineStartPositions = JSON.parse(this.scriptSnapshotShim.getLineStartPositions());
}
return this.lineStartPositions;
}
public getChangeRange(scriptSnapshot: IScriptSnapshot): TextChangeRange {
function createTextRange(start: number, length: number) {
function createSpan(start: number, length: number) {
return {
start: () => start,
end: () => start + length,
lenth: () => length,
isEmpty: () => length === 0
};
}
return {
span: () => createSpan(start, length),
newLength: () => length,
newSpan: () => createSpan(start, length),
isUnchanged: () => length === 0
};
}
var encoded = this.scriptSnapshotShim.getChangeRange((<ScriptSnapshotShimAdapter>scriptSnapshot).scriptSnapshotShim);
if (encoded == null) {
return null;
}
var decoded: { span: { start: number; length: number; }; newLength: number; } = JSON.parse(encoded);
return createTextRange(decoded.span.start, decoded.span.length);
}
}
export class LanguageServiceShimHostAdapter implements LanguageServiceHost {
constructor(private shimHost: LanguageServiceShimHost) {
}
public log(s: string): void {
this.shimHost.log(s);
}
public getCompilationSettings(): CompilerOptions {
var settingsJson = this.shimHost.getCompilationSettings();
if (settingsJson == null || settingsJson == "") {
return {};
}
var settings: CompilerOptions = JSON.parse(<any>settingsJson);
return settings;
}
public getScriptFileNames(): string[] {
var encoded = this.shimHost.getScriptFileNames();
return JSON.parse(encoded);
}
public getScriptSnapshot(fileName: string): IScriptSnapshot {
return new ScriptSnapshotShimAdapter(this.shimHost.getScriptSnapshot(fileName));
}
public getScriptVersion(fileName: string): string {
return this.shimHost.getScriptVersion(fileName);
}
public getScriptIsOpen(fileName: string): boolean {
return this.shimHost.getScriptIsOpen(fileName);
}
public getScriptByteOrderMark(fileName: string): ByteOrderMark {
return this.shimHost.getScriptByteOrderMark(fileName);
}
public getLocalizedDiagnosticMessages(): any {
var diagnosticMessagesJson = this.shimHost.getLocalizedDiagnosticMessages();
if (diagnosticMessagesJson == null || diagnosticMessagesJson == "") {
return null;
}
try {
return JSON.parse(diagnosticMessagesJson);
}
catch (e) {
this.log(e.description || "diagnosticMessages.generated.json has invalid JSON format");
return null;
}
}
//public getCancellationToken(): CancellationToken {
// return this.shimHost.getCancellationToken();
//}
}
}