-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #231 - Node.formatText() - Format individual nodes.
- Loading branch information
Showing
11 changed files
with
151 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: Formatting | ||
--- | ||
|
||
## Formatting | ||
|
||
Sometimes you might encounter code that looks terrible. For example: | ||
|
||
```typescript | ||
// BadlyFormattedFile.ts | ||
var myVariable : string | number; | ||
function myFunction(param : MyClass){ | ||
return ""; | ||
} | ||
``` | ||
|
||
Automatically format the text of this file by calling format text on it: | ||
|
||
```typescript | ||
sourceFile.formatText(); | ||
// or provide optional formatting settings | ||
sourceFile.formatText({ | ||
placeOpenBraceOnNewLineForFunctions: true | ||
}); | ||
``` | ||
|
||
This will run the source file's text through the TypeScript compiler's formatting API, which will change the source file to contain the following text: | ||
|
||
```typescript | ||
// BadlyFormattedFile.ts (not anymore!) | ||
var myVariable: string | number; | ||
function myFunction(param: MyClass) { | ||
return ""; | ||
} | ||
``` | ||
|
||
### Individual Nodes | ||
|
||
Individual nodes can also be formatted. For example, say you have a file that looks like this: | ||
|
||
```ts | ||
// file.ts | ||
export class MyClass { | ||
prop : string ; | ||
|
||
myMethod( example: string ) { | ||
console.log( example ); | ||
} | ||
} | ||
``` | ||
|
||
You can select down to the specific node you want to format: | ||
|
||
```ts | ||
ast.getSourceFileOrThrow("file.ts") | ||
.getClassOrThrow("MyClass") | ||
.getInstanceMethodOrThrow("myMethod") | ||
.getStatements()[0] | ||
.formatText(); | ||
``` | ||
|
||
Which would selectively only format the first statement in the method: | ||
|
||
```ts | ||
// file.ts | ||
export class MyClass { | ||
prop : string ; | ||
|
||
myMethod( example: string ) { | ||
console.log(example); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export * from "./base"; | ||
export * from "./ClassDeclaration"; | ||
export * from "./ConstructorDeclaration"; | ||
export * from "./GetAccessorDeclaration"; | ||
export * from "./MethodDeclaration"; | ||
export * from "./PropertyDeclaration"; | ||
export * from "./SetAccessorDeclaration"; | ||
export * from "./base"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {TextChange, SourceFile} from "./../../compiler"; | ||
|
||
export function getTextFromFormattingEdits(sourceFile: SourceFile, formattingEdits: TextChange[]) { | ||
// reverse the order | ||
formattingEdits = [...formattingEdits].sort((a, b) => b.getSpan().getStart() - a.getSpan().getStart()); | ||
let text = sourceFile.getFullText(); | ||
|
||
for (const textChange of formattingEdits) { | ||
const span = textChange.getSpan(); | ||
text = text.slice(0, span.getStart()) + textChange.getNewText() + text.slice(span.getEnd()); | ||
} | ||
|
||
return text; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters