Skip to content

Adds some docs for how a formatter works #32072

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 4 commits into from
Jun 25, 2019
Merged

Conversation

orta
Copy link
Contributor

@orta orta commented Jun 24, 2019

I've been reading the code on how the formatter works, and given an overall summary in the README docs

@andrewbranch - you were the most recent person to touch these, so I figure you might be a reasonable reviewer

@orta orta requested a review from andrewbranch June 24, 2019 22:45

### Where is this used?

The formatter is used mainly from the language service for formatting with an editor, but [services/textChanges.ts](/src/services/textChanges.ts) also uses this formatter when emitting code for quick fixes.
Copy link
Member

Choose a reason for hiding this comment

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

AFAIK this is essentially “any language service operation that inserts or modifies code”

```ts
const nonFormattedText = `[js source code]`;
const sourceFile = createSourceFile("any file name", nonFormattedText, ScriptTarget.ESNext, /*setParentNodes*/ true, ScriptKind.JS);
const changes = formatting.formatDocument(sourceFile, formatContext);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is realistic since everything happens internally within the language service itself 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, might just kill the section

As it recurses, `processNode` is called on the children setting the indentation is decided and passed
through into each of that node's children.

The meat of formatting decisions is made via `processPair`, the pair here being the current node and the previous node. `processPair` which mutates the formatting context to represent the current place in the scanner and requests a set of rules which can be applied to the items via `createRulesMap`.
Copy link
Member

Choose a reason for hiding this comment

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

This paragraph and the next are probably the most important bits

@orta
Copy link
Contributor Author

orta commented Jun 25, 2019

Cool, thanks, will merge on green

@orta orta merged commit 105f763 into microsoft:master Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants