Skip to content

use character instead of column when formatting multiline comments with ... #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,16 @@ module FourSlash {
return runningOffset;
}

public copyFormatOptions(): ts.FormatCodeOptions {
return ts.clone(this.formatCodeOptions);
}

public setFormatOptions(formatCodeOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
var oldFormatCodeOptions = this.formatCodeOptions;
this.formatCodeOptions = formatCodeOptions;
return oldFormatCodeOptions;
}

public formatDocument() {
this.scenarioActions.push('<FormatDocument />');

Expand Down
16 changes: 8 additions & 8 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,9 @@ module ts.formatting {
var startLinePos = getStartPositionOfLine(startLine, sourceFile);

var nonWhitespaceColumnInFirstPart =
SmartIndenter.findFirstNonWhitespaceColumn(startLinePos, parts[0].pos, sourceFile, options);
SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(startLinePos, parts[0].pos, sourceFile, options);

if (indentation === nonWhitespaceColumnInFirstPart) {
if (indentation === nonWhitespaceColumnInFirstPart.column) {
return;
}

Expand All @@ -855,21 +855,21 @@ module ts.formatting {
}

// shift all parts on the delta size
var delta = indentation - nonWhitespaceColumnInFirstPart;
var delta = indentation - nonWhitespaceColumnInFirstPart.column;
for (var i = startIndex, len = parts.length; i < len; ++i, ++startLine) {
var startLinePos = getStartPositionOfLine(startLine, sourceFile);
var nonWhitespaceColumn =
var nonWhitespaceCharacterAndColumn =
i === 0
? nonWhitespaceColumnInFirstPart
: SmartIndenter.findFirstNonWhitespaceColumn(parts[i].pos, parts[i].end, sourceFile, options);
: SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(parts[i].pos, parts[i].end, sourceFile, options);

var newIndentation = nonWhitespaceColumn + delta;
var newIndentation = nonWhitespaceCharacterAndColumn.column + delta;
if (newIndentation > 0) {
var indentationString = getIndentationString(newIndentation, options);
recordReplace(startLinePos, nonWhitespaceColumn, indentationString);
recordReplace(startLinePos, nonWhitespaceCharacterAndColumn.character, indentationString);
}
else {
recordDelete(startLinePos, nonWhitespaceColumn);
recordDelete(startLinePos, nonWhitespaceCharacterAndColumn.character);
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,20 @@ module ts.formatting {
return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options);
}

export function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): number {
/*
Character is the actual index of the character since the beginning of the line.
Column - position of the character after expanding tabs to spaces
"0\t2$"
value of 'character' for '$' is 3
value of 'column' for '$' is 6 (assuming that tab size is 4)
*/
export function findFirstNonWhitespaceCharacterAndColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth explaining the difference between a character and a column

var character = 0;
var column = 0;
for (var pos = startPos; pos < endPos; ++pos) {
var ch = sourceFile.text.charCodeAt(pos);
if (!isWhiteSpace(ch)) {
return column;
break;
}

if (ch === CharacterCodes.tab) {
Expand All @@ -320,8 +328,14 @@ module ts.formatting {
else {
column++;
}

character++;
}
return column;
return { column, character };
}

export function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): number {
return findFirstNonWhitespaceCharacterAndColumn(startPos, endPos, sourceFile, options).column;
}

function nodeContentIsAlwaysIndented(kind: SyntaxKind): boolean {
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/fourslash/formattingMultilineCommentsWithTabs1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts'/>

////var f = function (j) {
////
//// switch (j) {
//// case 1:
/////*1*/ /* when current checkbox has focus, Firefox has changed check state already
/////*2*/ on SPACE bar press only
/////*3*/ IE does not have issue, use the CSS class
/////*4*/ input:focus[type=checkbox] (z-index = 31290)
/////*5*/ to determine whether checkbox has focus or not
//// */
//// break;
//// case 2:
//// break;
//// }
////}
format.document();
goTo.marker("1");
verify.currentLineContentIs(" /* when current checkbox has focus, Firefox has changed check state already");
goTo.marker("2");
verify.currentLineContentIs(" on SPACE bar press only");
goTo.marker("3");
verify.currentLineContentIs(" IE does not have issue, use the CSS class");
goTo.marker("4");
verify.currentLineContentIs(" input:focus[type=checkbox] (z-index = 31290)");
goTo.marker("5");
verify.currentLineContentIs(" to determine whether checkbox has focus or not");

36 changes: 36 additions & 0 deletions tests/cases/fourslash/formattingMultilineCommentsWithTabs2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference path='fourslash.ts'/>

////var f = function (j) {
////
//// switch (j) {
//// case 1:
/////*1*/ /* when current checkbox has focus, Firefox has changed check state already
/////*2*/ on SPACE bar press only
/////*3*/ IE does not have issue, use the CSS class
/////*4*/ input:focus[type=checkbox] (z-index = 31290)
/////*5*/ to determine whether checkbox has focus or not
//// */
//// break;
//// case 2:
//// break;
//// }
////}
var options = format.copyFormatOptions();
options.ConvertTabsToSpaces = false;
var oldOptions = format.setFormatOptions(options);
try {
format.document();
goTo.marker("1");
verify.currentLineContentIs(" /* when current checkbox has focus, Firefox has changed check state already");
goTo.marker("2");
verify.currentLineContentIs(" on SPACE bar press only");
goTo.marker("3");
verify.currentLineContentIs(" IE does not have issue, use the CSS class");
goTo.marker("4");
verify.currentLineContentIs(" input:focus[type=checkbox] (z-index = 31290)");
goTo.marker("5");
verify.currentLineContentIs(" to determine whether checkbox has focus or not");
}
finally {
format.setFormatOptions(oldOptions);
}
27 changes: 27 additions & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ module FourSlashInterface {
position: number;
data?: any;
}

export interface EditorOptions {
IndentSize: number;
TabSize: number;
NewLineCharacter: string;
ConvertTabsToSpaces: boolean;
}

export interface FormatCodeOptions extends EditorOptions {
InsertSpaceAfterCommaDelimiter: boolean;
InsertSpaceAfterSemicolonInForStatements: boolean;
InsertSpaceBeforeAndAfterBinaryOperators: boolean;
InsertSpaceAfterKeywordsInControlFlowStatements: boolean;
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean;
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
PlaceOpenBraceOnNewLineForFunctions: boolean;
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
[s: string]: boolean | number| string;
}

export interface Range {
fileName: string;
Expand Down Expand Up @@ -541,6 +560,14 @@ module FourSlashInterface {
FourSlash.currentTestState.formatDocument();
}

public copyFormatOptions(): FormatCodeOptions {
return FourSlash.currentTestState.copyFormatOptions();
}

public setFormatOptions(options: FormatCodeOptions) {
return FourSlash.currentTestState.setFormatOptions(options);
}

public selection(startMarker: string, endMarker: string) {
FourSlash.currentTestState.formatSelection(FourSlash.currentTestState.getMarkerByName(startMarker).position, FourSlash.currentTestState.getMarkerByName(endMarker).position);
}
Expand Down