diff --git a/src/services/formatting.ts b/src/services/formatting.ts
index e455183eccc..f7fbb82491f 100644
--- a/src/services/formatting.ts
+++ b/src/services/formatting.ts
@@ -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;
diff --git a/src/services/services.ts b/src/services/services.ts
index defbdd9138c..e9d8de76679 100644
--- a/src/services/services.ts
+++ b/src/services/services.ts
@@ -8,7 +8,6 @@
///
///
///
-///
///
///
///
diff --git a/src/services/utilities.ts b/src/services/utilities.ts
index a4146c52e5e..b1a18dcbd30 100644
--- a/src/services/utilities.ts
+++ b/src/services/utilities.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;
}
diff --git a/tests/cases/fourslash/formatInTryCatchFinally.ts b/tests/cases/fourslash/formatInTryCatchFinally.ts
new file mode 100644
index 00000000000..dbbf28fb878
--- /dev/null
+++ b/tests/cases/fourslash/formatInTryCatchFinally.ts
@@ -0,0 +1,13 @@
+///
+
+////try
+////{
+//// var x = 1/*1*/
+////}
+////catch (e)
+////{
+////}
+
+goTo.marker("1");
+edit.insert(";")
+verify.currentLineContentIs(" var x = 1;");
diff --git a/tests/cases/fourslash/formattingBlockInCaseClauses.ts b/tests/cases/fourslash/formattingBlockInCaseClauses.ts
new file mode 100644
index 00000000000..64cd74ca858
--- /dev/null
+++ b/tests/cases/fourslash/formattingBlockInCaseClauses.ts
@@ -0,0 +1,12 @@
+///
+
+////switch (1) {
+//// case 1:
+//// {
+//// /*1*/
+//// break;
+////}
+
+goTo.marker("1");
+edit.insert("}");
+verify.currentLineContentIs(" }");
diff --git a/tests/cases/fourslash/formattingIfInElseBlock.ts b/tests/cases/fourslash/formattingIfInElseBlock.ts
new file mode 100644
index 00000000000..b4f5246fb35
--- /dev/null
+++ b/tests/cases/fourslash/formattingIfInElseBlock.ts
@@ -0,0 +1,12 @@
+///
+
+////if (true) {
+////}
+////else {
+//// if (true) {
+//// /*1*/
+////}
+
+goTo.marker("1");
+edit.insert("}")
+verify.currentLineContentIs(" }");