Skip to content
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

Fix an issue with applying edits #75

Merged
merged 1 commit into from
Feb 10, 2017
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
9 changes: 9 additions & 0 deletions lib/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export default function format(fileName: string, text: string, options = createD
function applyEdits(text: string, edits: ts.TextChange[]): string {
// Apply edits in reverse on the existing text
let result = text;

// An issue with `ts.formatting.formatDocument` is that it does
// not always give the edits array in ascending order of change start
// point. This can result that we add or remove some character in
// the begining of the document, making the all the other edits
// offsets invalid.

// We resolve this by sorting edits by ascending start point
edits.sort((a, b) => a.span.start - b.span.start);
for (let i = edits.length - 1; i >= 0; i--) {
let change = edits[i];
let head = result.slice(0, change.span.start);
Expand Down
17 changes: 17 additions & 0 deletions test/expected/schemats/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indentSize": 4,
"tabSize": 4,
"indentStyle": 2,
"newLineCharacter": "\r\n",
"convertTabsToSpaces": true,
"insertSpaceAfterCommaDelimiter": true,
"insertSpaceAfterSemicolonInForStatements": true,
"insertSpaceBeforeAndAfterBinaryOperators": true,
"insertSpaceAfterKeywordsInControlFlowStatements": true,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false
}
19 changes: 19 additions & 0 deletions test/expected/schemats/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* AUTO-GENERATED FILE @ 2016-12-07 13:17:46 - DO NOT EDIT!
*
* This file was generated with schemats node package:
* $ schemats generate -c postgres://username:password@localhost/test -t users -o ./test/osm.ts
*
* Re-run the command above.
*/


export namespace cta_situationcompte_scoFields {
export type sco_id = number;
export type sco_total_debit = number;
export type sco_total_credit = number;
export type sco_nb_ecritures = number;
export type pst_id = number;
export type cpt_id = number;

}
19 changes: 19 additions & 0 deletions test/fixture/schemats/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* AUTO-GENERATED FILE @ 2016-12-07 13:17:46 - DO NOT EDIT!
*
* This file was generated with schemats node package:
* $ schemats generate -c postgres://username:password@localhost/test -t users -o ./test/osm.ts
*
* Re-run the command above.
*/


export namespace cta_situationcompte_scoFields {
export type sco_id = number;
export type sco_total_debit = number;
export type sco_total_credit = number;
export type sco_nb_ecritures = number;
export type pst_id = number;
export type cpt_id = number;

}