Skip to content

Commit

Permalink
Fix an issue with applying edits
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamx committed Feb 8, 2017
1 parent f41d569 commit 7d08bad
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
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;

}

0 comments on commit 7d08bad

Please sign in to comment.