Fix embedded format with cursorOffset (#17254)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-03-17 11:23:17 +08:00
1 parent 44d7298f2d
commit 2b3710d805
7 files changed
+138 -5

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
#### Fix embedded format with `cursorOffset` (#17254 by @fisker)
<!-- prettier-ignore -->
````md
<!-- Input (--cursor-offset=1) -->
# Angular note
```typescript
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [],
template: `
<h1>
{{ title }}</h1>
`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'default';
}
```
<!-- Prettier stable -->
Error: There are too many 'cursor' in doc.
<!-- Prettier main -->
# Angular note
```typescript
import { Component } from "@angular/core";
@Component({
selector: "app-root",
standalone: true,
imports: [],
template: `
<h1>
{{ title }}
</h1>
`,
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title = "default";
}
```
````
+1
View File
@@ -150,6 +150,7 @@ function getSyntaxFromCategory(category) {
case "flow":
case "javascript":
case "api":
case "misc":
return "jsx";
case "typescript":
return "tsx";
+7 -5
View File
@@ -51,11 +51,13 @@ async function printAstToDoc(ast, options) {
ensureAllCommentsPrinted(options);
if (options.nodeAfterCursor && !options.nodeBeforeCursor) {
return [cursor, doc];
}
if (options.nodeBeforeCursor && !options.nodeAfterCursor) {
return [doc, cursor];
if (options.cursorOffset >= 0) {
if (options.nodeAfterCursor && !options.nodeBeforeCursor) {
return [cursor, doc];
}
if (options.nodeBeforeCursor && !options.nodeAfterCursor) {
return [doc, cursor];
}
}
return doc;
+4
View File
@@ -116,6 +116,10 @@ async function textToDoc(
...partialNextOptions,
parentParser: parentOptions.parser,
originalText: text,
// Improve this if we calculate the relative index
cursorOffset: undefined,
rangeStart: undefined,
rangeEnd: undefined,
},
{ passThrough: true },
);
+20
View File
@@ -0,0 +1,20 @@
#<|> Angular note
```typescript
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [],
template: `
<h1>
{{ title }}</h1>
`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'default';
}
```
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`17227.md format 1`] = `
====================================options=====================================
cursorOffset: 1
parsers: ["markdown"]
printWidth: 80
| printWidth
=====================================input======================================
#<|> Angular note
\`\`\`typescript
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [],
template: \`
<h1>
{{ title }}</h1>
\`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'default';
}
\`\`\`
=====================================output=====================================
#<|> Angular note
\`\`\`typescript
import { Component } from "@angular/core";
@Component({
selector: "app-root",
standalone: true,
imports: [],
template: \`
<h1>
{{ title }}
</h1>
\`,
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title = "default";
}
\`\`\`
================================================================================
`;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["markdown"]);