mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-13 14:09:06 -05:00
added tests for formatting in try blocks, add startEndOverlapsWithStartEnd
This commit is contained in:
@@ -151,9 +151,7 @@ module ts.formatting {
|
||||
return false;
|
||||
}
|
||||
|
||||
var s = Math.max(r.pos, error.start);
|
||||
var e = Math.min(r.end, error.start + error.length);
|
||||
if (s < e) {
|
||||
if (startEndOverlapsWithStartEnd(r.pos, r.end, error.start, error.start + error.length)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -177,15 +175,10 @@ module ts.formatting {
|
||||
// formatting context to be used by rules provider to get rules
|
||||
var formattingContext = new FormattingContext(sourceFile, requestKind);
|
||||
|
||||
var formattingScanner = getFormattingScanner(sourceFile, originalRange.pos, originalRange.end);
|
||||
|
||||
var enclosingNode = findEnclosingNode(originalRange, sourceFile);
|
||||
if (enclosingNode.kind === SyntaxKind.SourceFile) {
|
||||
var formattingScanner = getFormattingScanner(sourceFile, originalRange.pos, originalRange.end);
|
||||
var initialIndentation = 0;
|
||||
}
|
||||
else {
|
||||
var formattingScanner = getFormattingScanner(sourceFile, enclosingNode.pos, originalRange.end);
|
||||
var initialIndentation = SmartIndenter.getIndentationForNode(enclosingNode, originalRange, sourceFile, options);
|
||||
}
|
||||
var initialIndentation = SmartIndenter.getIndentationForNode(enclosingNode, originalRange, sourceFile, options);
|
||||
|
||||
var previousRangeHasError: boolean;
|
||||
var previousRange: TextRangeWithKind;
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
/// <reference path='outliningElementsCollector.ts' />
|
||||
/// <reference path='navigationBar.ts' />
|
||||
/// <reference path='breakpoints.ts' />
|
||||
/// <reference path='indentation.ts' />
|
||||
/// <reference path='signatureHelp.ts' />
|
||||
/// <reference path='utilities.ts' />
|
||||
/// <reference path='smartIndenter.ts' />
|
||||
|
||||
@@ -8,17 +8,18 @@ module ts {
|
||||
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
|
||||
Debug.assert(line >= 1);
|
||||
var lineStarts = sourceFile.getLineStarts();
|
||||
|
||||
line = line - 1;
|
||||
if (line === lineStarts.length - 1) {
|
||||
|
||||
// lines returned by SourceFile.getLineAndCharacterForPosition are 1-based
|
||||
var lineIndex = line - 1;
|
||||
if (lineIndex === lineStarts.length - 1) {
|
||||
// last line - return EOF
|
||||
return sourceFile.text.length - 1;
|
||||
}
|
||||
else {
|
||||
// current line start
|
||||
var start = lineStarts[line];
|
||||
var start = lineStarts[lineIndex];
|
||||
// take the start position of the next line -1 = it should be some line break
|
||||
var pos = lineStarts[line + 1] - 1;
|
||||
var pos = lineStarts[lineIndex + 1] - 1;
|
||||
Debug.assert(isLineBreak(sourceFile.text.charCodeAt(pos)));
|
||||
// walk backwards skipping line breaks, stop the the beginning of current line.
|
||||
// i.e:
|
||||
@@ -55,8 +56,12 @@ module ts {
|
||||
}
|
||||
|
||||
export function rangeOverlapsWithStartEnd(r1: TextRange, start: number, end: number) {
|
||||
var start = Math.max(r1.pos, start);
|
||||
var end = Math.min(r1.end, end);
|
||||
return startEndOverlapsWithStartEnd(r1.pos, r1.end, start, end);
|
||||
}
|
||||
|
||||
export function startEndOverlapsWithStartEnd(start1: number, end1: number, start2: number, end2: number) {
|
||||
var start = Math.max(start1, start2);
|
||||
var end = Math.min(end1, end2);
|
||||
return start < end;
|
||||
}
|
||||
|
||||
|
||||
13
tests/cases/fourslash/formatInTryCatchFinally.ts
Normal file
13
tests/cases/fourslash/formatInTryCatchFinally.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////try
|
||||
////{
|
||||
//// var x = 1/*1*/
|
||||
////}
|
||||
////catch (e)
|
||||
////{
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
edit.insert(";")
|
||||
verify.currentLineContentIs(" var x = 1;");
|
||||
12
tests/cases/fourslash/formattingBlockInCaseClauses.ts
Normal file
12
tests/cases/fourslash/formattingBlockInCaseClauses.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////switch (1) {
|
||||
//// case 1:
|
||||
//// {
|
||||
//// /*1*/
|
||||
//// break;
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
edit.insert("}");
|
||||
verify.currentLineContentIs(" }");
|
||||
12
tests/cases/fourslash/formattingIfInElseBlock.ts
Normal file
12
tests/cases/fourslash/formattingIfInElseBlock.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////if (true) {
|
||||
////}
|
||||
////else {
|
||||
//// if (true) {
|
||||
//// /*1*/
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
edit.insert("}")
|
||||
verify.currentLineContentIs(" }");
|
||||
Reference in New Issue
Block a user