-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
src/services/formatting/README.md
Outdated
|
||
### 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. |
There was a problem hiding this comment.
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”
src/services/formatting/README.md
Outdated
```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); |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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
Co-Authored-By: Andrew Branch <[email protected]>
Co-Authored-By: Andrew Branch <[email protected]>
Cool, thanks, will merge on green |
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