mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Port over changes to the ScriptSnapshot API from the languageService-v2 branch.
This commit is contained in:
+31
-28
@@ -26,7 +26,6 @@
|
||||
/// <reference path='compiler\pathUtils.ts' />
|
||||
|
||||
module ts {
|
||||
|
||||
export interface Node {
|
||||
getSourceFile(): SourceFile;
|
||||
getChildCount(): number;
|
||||
@@ -72,7 +71,8 @@ module ts {
|
||||
getSourceUnit(): TypeScript.SourceUnitSyntax;
|
||||
getSyntaxTree(): TypeScript.SyntaxTree;
|
||||
getBloomFilter(): TypeScript.BloomFilter;
|
||||
update(scriptSnapshot: TypeScript.IScriptSnapshot, version: number, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile;
|
||||
getScriptSnapshot(): TypeScript.IScriptSnapshot;
|
||||
update(scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile;
|
||||
}
|
||||
|
||||
var scanner: Scanner = createScanner(ScriptTarget.ES5);
|
||||
@@ -320,7 +320,7 @@ module ts {
|
||||
public identifierCount: number;
|
||||
public symbolCount: number;
|
||||
public statements: NodeArray<Statement>;
|
||||
public version: number;
|
||||
public version: string;
|
||||
public isOpen: boolean;
|
||||
public languageVersion: ScriptTarget;
|
||||
|
||||
@@ -333,6 +333,10 @@ module ts {
|
||||
return this.getSyntaxTree().sourceUnit();
|
||||
}
|
||||
|
||||
public getScriptSnapshot(): TypeScript.IScriptSnapshot {
|
||||
return this.scriptSnapshot;
|
||||
}
|
||||
|
||||
public getLineMap(): TypeScript.LineMap {
|
||||
return this.getSyntaxTree().lineMap();
|
||||
}
|
||||
@@ -384,7 +388,7 @@ module ts {
|
||||
return this.bloomFilter;
|
||||
}
|
||||
|
||||
public update(scriptSnapshot: TypeScript.IScriptSnapshot, version: number, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile {
|
||||
public update(scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile {
|
||||
// See if we are currently holding onto a syntax tree. We may not be because we're
|
||||
// either a closed file, or we've just been lazy and haven't had to create the syntax
|
||||
// tree yet. Access the field instead of the method so we don't accidently realize
|
||||
@@ -419,7 +423,7 @@ module ts {
|
||||
return SourceFileObject.createSourceFileObject(this.languageVersion, this.filename, scriptSnapshot, version, isOpen, newSyntaxTree);
|
||||
}
|
||||
|
||||
public static createSourceFileObject(languageVersion: ScriptTarget, filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, version: number, isOpen: boolean, syntaxTree: TypeScript.SyntaxTree) {
|
||||
public static createSourceFileObject(languageVersion: ScriptTarget, filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, syntaxTree: TypeScript.SyntaxTree) {
|
||||
var newSourceFile = <SourceFileObject><any>createSourceFile(filename, scriptSnapshot.getText(0, scriptSnapshot.getLength()), languageVersion, version, isOpen);
|
||||
newSourceFile.scriptSnapshot = scriptSnapshot;
|
||||
newSourceFile.syntaxTree = syntaxTree;
|
||||
@@ -442,7 +446,7 @@ module ts {
|
||||
export interface LanguageServiceHost extends Logger {
|
||||
getCompilationSettings(): CompilerOptions;
|
||||
getScriptFileNames(): string[];
|
||||
getScriptVersion(fileName: string): number;
|
||||
getScriptVersion(fileName: string): string;
|
||||
getScriptIsOpen(fileName: string): boolean;
|
||||
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot;
|
||||
getLocalizedDiagnosticMessages(): any;
|
||||
@@ -692,7 +696,7 @@ module ts {
|
||||
filename: string,
|
||||
compilationSettings: CompilerOptions,
|
||||
scriptSnapshot: TypeScript.IScriptSnapshot,
|
||||
version: number,
|
||||
version: string,
|
||||
isOpen: boolean,
|
||||
referencedFiles: string[]): SourceFile;
|
||||
|
||||
@@ -701,7 +705,7 @@ module ts {
|
||||
filename: string,
|
||||
compilationSettings: CompilerOptions,
|
||||
scriptSnapshot: TypeScript.IScriptSnapshot,
|
||||
version: number,
|
||||
version: string,
|
||||
isOpen: boolean,
|
||||
textChangeRange: TypeScript.TextChangeRange
|
||||
): SourceFile;
|
||||
@@ -819,7 +823,7 @@ module ts {
|
||||
// Information about a specific host file.
|
||||
interface HostFileInformation {
|
||||
filename: string;
|
||||
version: number;
|
||||
version: string;
|
||||
isOpen: boolean;
|
||||
sourceText?: TypeScript.IScriptSnapshot;
|
||||
}
|
||||
@@ -928,7 +932,7 @@ module ts {
|
||||
return fileNames;
|
||||
}
|
||||
|
||||
public getVersion(filename: string): number {
|
||||
public getVersion(filename: string): string {
|
||||
return this.getEntry(filename).version;
|
||||
}
|
||||
|
||||
@@ -944,14 +948,14 @@ module ts {
|
||||
return file.sourceText;
|
||||
}
|
||||
|
||||
public getScriptTextChangeRangeSinceVersion(filename: string, lastKnownVersion: number): TypeScript.TextChangeRange {
|
||||
public getChangeRange(filename: string, lastKnownVersion: string, oldScriptSnapshot: TypeScript.IScriptSnapshot): TypeScript.TextChangeRange {
|
||||
var currentVersion = this.getVersion(filename);
|
||||
if (lastKnownVersion === currentVersion) {
|
||||
return TypeScript.TextChangeRange.unchanged; // "No changes"
|
||||
}
|
||||
|
||||
var scriptSnapshot = this.getScriptSnapshot(filename);
|
||||
return scriptSnapshot.getTextChangeRangeSinceVersion(lastKnownVersion);
|
||||
return scriptSnapshot.getChangeRange(oldScriptSnapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -960,11 +964,10 @@ module ts {
|
||||
|
||||
// For our syntactic only features, we also keep a cache of the syntax tree for the
|
||||
// currently edited file.
|
||||
private currentfilename: string = "";
|
||||
private currentFileVersion: number = -1;
|
||||
private currentFilename: string = "";
|
||||
private currentFileVersion: string = null;
|
||||
private currentSourceFile: SourceFile = null;
|
||||
private currentFileSyntaxTree: TypeScript.SyntaxTree = null;
|
||||
private currentFileScriptSnapshot: TypeScript.IScriptSnapshot = null;
|
||||
|
||||
constructor(private host: LanguageServiceHost) {
|
||||
this.hostCache = new HostCache(host);
|
||||
@@ -979,7 +982,7 @@ module ts {
|
||||
var syntaxTree: TypeScript.SyntaxTree = null;
|
||||
var sourceFile: SourceFile;
|
||||
|
||||
if (this.currentFileSyntaxTree === null || this.currentfilename !== filename) {
|
||||
if (this.currentFileSyntaxTree === null || this.currentFilename !== filename) {
|
||||
var scriptSnapshot = this.hostCache.getScriptSnapshot(filename);
|
||||
syntaxTree = this.createSyntaxTree(filename, scriptSnapshot);
|
||||
sourceFile = createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true);
|
||||
@@ -988,9 +991,10 @@ module ts {
|
||||
}
|
||||
else if (this.currentFileVersion !== version) {
|
||||
var scriptSnapshot = this.hostCache.getScriptSnapshot(filename);
|
||||
syntaxTree = this.updateSyntaxTree(filename, scriptSnapshot, this.currentFileSyntaxTree, this.currentFileVersion);
|
||||
syntaxTree = this.updateSyntaxTree(filename, scriptSnapshot,
|
||||
this.currentSourceFile.getScriptSnapshot(), this.currentFileSyntaxTree, this.currentFileVersion);
|
||||
|
||||
var editRange = this.hostCache.getScriptTextChangeRangeSinceVersion(filename, this.currentFileVersion);
|
||||
var editRange = this.hostCache.getChangeRange(filename, this.currentFileVersion, this.currentSourceFile.getScriptSnapshot());
|
||||
sourceFile = !editRange
|
||||
? createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true)
|
||||
: this.currentSourceFile.update(scriptSnapshot, version, /*isOpen*/ true, editRange);
|
||||
@@ -1001,9 +1005,8 @@ module ts {
|
||||
if (syntaxTree !== null) {
|
||||
Debug.assert(sourceFile);
|
||||
// All done, ensure state is up to date
|
||||
this.currentFileScriptSnapshot = scriptSnapshot;
|
||||
this.currentFileVersion = version;
|
||||
this.currentfilename = filename;
|
||||
this.currentFilename = filename;
|
||||
this.currentFileSyntaxTree = syntaxTree;
|
||||
this.currentSourceFile = sourceFile;
|
||||
}
|
||||
@@ -1038,7 +1041,7 @@ module ts {
|
||||
public getCurrentScriptSnapshot(filename: string): TypeScript.IScriptSnapshot {
|
||||
// update currentFileScriptSnapshot as a part of 'getCurrentFileSyntaxTree' call
|
||||
this.getCurrentFileSyntaxTree(filename);
|
||||
return this.currentFileScriptSnapshot;
|
||||
return this.getCurrentSourceFile(filename).getScriptSnapshot();
|
||||
}
|
||||
|
||||
private createSyntaxTree(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot): TypeScript.SyntaxTree {
|
||||
@@ -1052,8 +1055,8 @@ module ts {
|
||||
return syntaxTree;
|
||||
}
|
||||
|
||||
private updateSyntaxTree(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, previousSyntaxTree: TypeScript.SyntaxTree, previousFileVersion: number): TypeScript.SyntaxTree {
|
||||
var editRange = this.hostCache.getScriptTextChangeRangeSinceVersion(filename, previousFileVersion);
|
||||
private updateSyntaxTree(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, previousScriptSnapshot: TypeScript.IScriptSnapshot, previousSyntaxTree: TypeScript.SyntaxTree, previousFileVersion: string): TypeScript.SyntaxTree {
|
||||
var editRange = this.hostCache.getChangeRange(filename, previousFileVersion, previousScriptSnapshot);
|
||||
|
||||
// Debug.assert(newLength >= 0);
|
||||
|
||||
@@ -1065,7 +1068,7 @@ module ts {
|
||||
var nextSyntaxTree = TypeScript.IncrementalParser.parse(
|
||||
previousSyntaxTree, editRange, TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot));
|
||||
|
||||
this.ensureInvariants(filename, editRange, nextSyntaxTree, this.currentFileScriptSnapshot, scriptSnapshot);
|
||||
this.ensureInvariants(filename, editRange, nextSyntaxTree, this.getCurrentScriptSnapshot(filename), scriptSnapshot);
|
||||
|
||||
return nextSyntaxTree;
|
||||
}
|
||||
@@ -1133,7 +1136,7 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
function createSourceFileFromScriptSnapshot(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, settings: CompilerOptions, version: number, isOpen: boolean) {
|
||||
function createSourceFileFromScriptSnapshot(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, settings: CompilerOptions, version: string, isOpen: boolean) {
|
||||
return createSourceFile(filename, scriptSnapshot.getText(0, scriptSnapshot.getLength()), settings.target, version, isOpen);
|
||||
}
|
||||
|
||||
@@ -1178,7 +1181,7 @@ module ts {
|
||||
filename: string,
|
||||
compilationSettings: CompilerOptions,
|
||||
scriptSnapshot: TypeScript.IScriptSnapshot,
|
||||
version: number,
|
||||
version: string,
|
||||
isOpen: boolean): SourceFile {
|
||||
|
||||
var bucket = getBucketForCompilationSettings(compilationSettings, /*createIfMissing*/ true);
|
||||
@@ -1202,7 +1205,7 @@ module ts {
|
||||
filename: string,
|
||||
compilationSettings: CompilerOptions,
|
||||
scriptSnapshot: TypeScript.IScriptSnapshot,
|
||||
version: number,
|
||||
version: string,
|
||||
isOpen: boolean,
|
||||
textChangeRange: TypeScript.TextChangeRange
|
||||
): SourceFile {
|
||||
@@ -1388,7 +1391,7 @@ module ts {
|
||||
// new text buffer).
|
||||
var textChangeRange: TypeScript.TextChangeRange = null;
|
||||
if (sourceFile.isOpen && isOpen) {
|
||||
textChangeRange = hostCache.getScriptTextChangeRangeSinceVersion(filename, sourceFile.version);
|
||||
textChangeRange = hostCache.getChangeRange(filename, sourceFile.version, sourceFile.getScriptSnapshot());
|
||||
}
|
||||
|
||||
sourceFile = documentRegistry.updateDocument(sourceFile, filename, compilationSettings, scriptSnapshot, version, isOpen, textChangeRange);
|
||||
|
||||
@@ -36,7 +36,7 @@ module ts {
|
||||
// { span: { start: number; length: number }; newLength: number }
|
||||
//
|
||||
// Or null value if there was no change.
|
||||
getTextChangeRangeSinceVersion(scriptVersion: number): string;
|
||||
getChangeRange(oldSnapshot: ScriptSnapshotShim): string;
|
||||
}
|
||||
|
||||
//
|
||||
@@ -48,7 +48,7 @@ module ts {
|
||||
// Returns a JSON encoded value of the type:
|
||||
// string[]
|
||||
getScriptFileNames(): string;
|
||||
getScriptVersion(fileName: string): number;
|
||||
getScriptVersion(fileName: string): string;
|
||||
getScriptIsOpen(fileName: string): boolean;
|
||||
getScriptSnapshot(fileName: string): ScriptSnapshotShim;
|
||||
getLocalizedDiagnosticMessages(): string;
|
||||
@@ -283,8 +283,9 @@ module ts {
|
||||
return this.lineStartPositions;
|
||||
}
|
||||
|
||||
public getTextChangeRangeSinceVersion(scriptVersion: number): TypeScript.TextChangeRange {
|
||||
var encoded = this.scriptSnapshotShim.getTextChangeRangeSinceVersion(scriptVersion);
|
||||
public getChangeRange(oldSnapshot: TypeScript.IScriptSnapshot): TypeScript.TextChangeRange {
|
||||
var oldSnapshotShim = <ScriptSnapshotShimAdapter>oldSnapshot;
|
||||
var encoded = this.scriptSnapshotShim.getChangeRange(oldSnapshotShim.scriptSnapshotShim);
|
||||
if (encoded == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -295,7 +296,7 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
class LanguageServiceShimHostAdapter implements LanguageServiceHost {
|
||||
export class LanguageServiceShimHostAdapter implements LanguageServiceHost {
|
||||
constructor(private shimHost: LanguageServiceShimHost) {
|
||||
}
|
||||
|
||||
@@ -349,7 +350,7 @@ module ts {
|
||||
return new ScriptSnapshotShimAdapter(this.shimHost.getScriptSnapshot(fileName));
|
||||
}
|
||||
|
||||
public getScriptVersion(fileName: string): number {
|
||||
public getScriptVersion(fileName: string): string {
|
||||
return this.shimHost.getScriptVersion(fileName);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,12 @@ module TypeScript {
|
||||
// always determine this (albeit in a more expensive manner).
|
||||
getLineStartPositions(): number[];
|
||||
|
||||
// Returns a text change range representing what text has changed since the specified version.
|
||||
// If the change cannot be determined (say, because a file was opened/closed), then 'null'
|
||||
// should be returned.
|
||||
getTextChangeRangeSinceVersion(scriptVersion: number): TextChangeRange;
|
||||
// Gets the TextChangeRange that describe how the text changed between this text and
|
||||
// an older version. This informatoin is used by the incremental parser to determine
|
||||
// what sections of the script need to be reparsed. 'null' can be returned if the
|
||||
// change range cannot be determined. However, in that case, incremental parsing will
|
||||
// not happen and the entire document will be reparsed.
|
||||
getChangeRange(oldSnapshot: IScriptSnapshot): TextChangeRange;
|
||||
}
|
||||
|
||||
export module ScriptSnapshot {
|
||||
@@ -45,7 +47,7 @@ module TypeScript {
|
||||
return this._lineStartPositions;
|
||||
}
|
||||
|
||||
public getTextChangeRangeSinceVersion(scriptVersion: number): TypeScript.TextChangeRange {
|
||||
public getChangeRange(oldSnapshot: IScriptSnapshot): TextChangeRange {
|
||||
throw Errors.notYetImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user