mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
distinguish parse errors so non-parse errors can be ignored during formatting
This commit is contained in:
@@ -907,7 +907,9 @@ module ts {
|
||||
? file.syntacticErrors[file.syntacticErrors.length - 1].start
|
||||
: -1;
|
||||
if (start !== lastErrorPos) {
|
||||
file.syntacticErrors.push(createFileDiagnostic(file, start, length, message, arg0, arg1, arg2));
|
||||
var diagnostic = createFileDiagnostic(file, start, length, message, arg0, arg1, arg2);
|
||||
diagnostic.isParseError = true;
|
||||
file.syntacticErrors.push(diagnostic);
|
||||
}
|
||||
|
||||
if (lookAheadMode === LookAheadMode.NoErrorYet) {
|
||||
|
||||
@@ -1067,6 +1067,7 @@ module ts {
|
||||
category: DiagnosticCategory;
|
||||
code: number;
|
||||
isEarly?: boolean;
|
||||
isParseError?: boolean;
|
||||
}
|
||||
|
||||
export enum DiagnosticCategory {
|
||||
|
||||
@@ -117,16 +117,50 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
function prepareRangeContainsErrorFunction(errors: Diagnostic[], originalRange: TextRange): (r: TextRange) => boolean {
|
||||
if (!errors.length) {
|
||||
// no errors - always return false
|
||||
return r => false;
|
||||
}
|
||||
else {
|
||||
var sorted = errors.slice(0).filter(d => d.isParseError).sort((e1, e2) => e1.start - e2.start);
|
||||
var index = 0; // TODO: set based on the range
|
||||
var endIndex = sorted.length; // TODO: set based on the range
|
||||
if (endIndex === 0 || index === sorted.length) {
|
||||
// errors are outside the interesting span - always return false
|
||||
return r => false;
|
||||
}
|
||||
return r => {
|
||||
while (true) {
|
||||
if (index >= endIndex) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
var curr = sorted[index];
|
||||
if (r.end <= curr.start) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
var s = Math.max(r.pos, curr.start);
|
||||
var e = Math.min(r.end, curr.start + curr.length);
|
||||
if (s < e) {
|
||||
return true;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function formatSpan(originalRange: TextRange,
|
||||
sourceFile: SourceFile,
|
||||
options: FormatCodeOptions,
|
||||
rulesProvider: RulesProvider,
|
||||
requestKind: FormattingRequestKind): TextChange[]{
|
||||
|
||||
var syntacticErrors = sourceFile.syntacticErrors.length !== 0 && sourceFile.syntacticErrors.slice(0);
|
||||
if (syntacticErrors) {
|
||||
syntacticErrors.sort((d1, d2) => d1.start - d2.start);
|
||||
}
|
||||
var rangeContainsError = prepareRangeContainsErrorFunction(sourceFile.syntacticErrors, originalRange);
|
||||
|
||||
// formatting context to be used by rules provider to get rules
|
||||
var formattingContext = new FormattingContext(sourceFile, requestKind);
|
||||
@@ -262,14 +296,6 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
function rangeContainsError(range: TextRange): boolean {
|
||||
if (!syntacticErrors.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
binarySearch
|
||||
}
|
||||
|
||||
function consumeTokenAndAdvanceScanner(currentTokenInfo: TokenInfo, parent: Node, contextNode: Node, indentation: DynamicIndentation): void {
|
||||
Debug.assert(rangeContainsRange(parent, currentTokenInfo.token));
|
||||
|
||||
@@ -424,6 +450,7 @@ module ts.formatting {
|
||||
|
||||
return lineAdded;
|
||||
}
|
||||
|
||||
function insertIndentation(pos: number, indentation: number, lineAdded: boolean): void {
|
||||
var indentationString = getIndentationString(indentation, options);
|
||||
if (lineAdded) {
|
||||
|
||||
@@ -13,10 +13,10 @@ format.document();
|
||||
goTo.marker('1');
|
||||
verify.currentLineContentIs('foo(): Bar { }');
|
||||
goTo.marker('2');
|
||||
verify.currentLineContentIs('function Foo () # { }');
|
||||
verify.currentLineContentIs('function Foo() # { }');
|
||||
goTo.marker('3');
|
||||
verify.currentLineContentIs('4+:5');
|
||||
verify.currentLineContentIs('4 +:5');
|
||||
goTo.marker('4');
|
||||
verify.currentLineContentIs(' : T) { }');
|
||||
goTo.marker('5');
|
||||
verify.currentLineContentIs('var x =');
|
||||
verify.currentLineContentIs('var x =');
|
||||
@@ -4,4 +4,4 @@
|
||||
|
||||
goTo.eof();
|
||||
edit.insert(';');
|
||||
verify.currentLineContentIs('function of1 (b:{ r:{ c: number;');
|
||||
verify.currentLineContentIs('function of1(b: { r: { c: number;');
|
||||
Reference in New Issue
Block a user