prettier#CursorOptions TypeScript Examples

The following examples show how to use prettier#CursorOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: prettier-utils.ts    From utopia with MIT License 7 votes vote down vote up
export function applyPrettier(
  code: string,
  calculateCursorOffset: boolean,
  cursorOffset?: number,
): {
  formatted: string
  cursorOffset: number
} {
  const offset = cursorOffset || 0
  try {
    if (calculateCursorOffset) {
      return prettier.formatWithCursor(code, {
        ...PrettierConfig,
        cursorOffset: offset,
      } as CursorOptions)
    } else {
      const formattedCode = prettier.format(code, PrettierConfig)
      return {
        formatted: formattedCode,
        cursorOffset: 0,
      }
    }
  } catch (e) {
    // Prettier will attempt to parse the code and will throw on syntax errors
    return { formatted: code, cursorOffset: offset }
  }
}