diff --git a/changelog_unreleased/misc/17254.md b/changelog_unreleased/misc/17254.md new file mode 100644 index 000000000..334277a8d --- /dev/null +++ b/changelog_unreleased/misc/17254.md @@ -0,0 +1,51 @@ +#### Fix embedded format with `cursorOffset` (#17254 by @fisker) + + +````md + +# Angular note + +```typescript + import {Component} from '@angular/core'; + + @Component({ + selector: 'app-root', + standalone: true, + imports: [], + template: ` +

+ + {{ title }}

+ `, + styleUrls: ['./app.component.css'], + }) + export class AppComponent { + title = 'default'; + } +``` + + +Error: There are too many 'cursor' in doc. + + +# Angular note + +```typescript +import { Component } from "@angular/core"; + +@Component({ + selector: "app-root", + standalone: true, + imports: [], + template: ` +

+ {{ title }} +

+ `, + styleUrls: ["./app.component.css"], +}) +export class AppComponent { + title = "default"; +} +``` +```` diff --git a/scripts/generate-changelog.js b/scripts/generate-changelog.js index 36a44f61b..9a699e66a 100755 --- a/scripts/generate-changelog.js +++ b/scripts/generate-changelog.js @@ -150,6 +150,7 @@ function getSyntaxFromCategory(category) { case "flow": case "javascript": case "api": + case "misc": return "jsx"; case "typescript": return "tsx"; diff --git a/src/main/ast-to-doc.js b/src/main/ast-to-doc.js index 7d2f23703..ba65c62bf 100644 --- a/src/main/ast-to-doc.js +++ b/src/main/ast-to-doc.js @@ -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; diff --git a/src/main/multiparser.js b/src/main/multiparser.js index 47fc24722..dc048345a 100644 --- a/src/main/multiparser.js +++ b/src/main/multiparser.js @@ -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 }, ); diff --git a/tests/format/markdown/cursor/17227.md b/tests/format/markdown/cursor/17227.md new file mode 100644 index 000000000..0318005cb --- /dev/null +++ b/tests/format/markdown/cursor/17227.md @@ -0,0 +1,20 @@ +#<|> Angular note + +```typescript + import {Component} from '@angular/core'; + + @Component({ + selector: 'app-root', + standalone: true, + imports: [], + template: ` +

+ + {{ title }}

+ `, + styleUrls: ['./app.component.css'], + }) + export class AppComponent { + title = 'default'; + } +``` diff --git a/tests/format/markdown/cursor/__snapshots__/format.test.js.snap b/tests/format/markdown/cursor/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..62269d89e --- /dev/null +++ b/tests/format/markdown/cursor/__snapshots__/format.test.js.snap @@ -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: \` +

+ + {{ title }}

+ \`, + 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: \` +

+ {{ title }} +

+ \`, + styleUrls: ["./app.component.css"], +}) +export class AppComponent { + title = "default"; +} +\`\`\` + +================================================================================ +`; diff --git a/tests/format/markdown/cursor/format.test.js b/tests/format/markdown/cursor/format.test.js new file mode 100644 index 000000000..7df6c9eaa --- /dev/null +++ b/tests/format/markdown/cursor/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["markdown"]);