How to use the prettier/standalone.formatWithCursor function in prettier

To help you get started, we’ve selected a few prettier examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github seek-oss / playroom / src / utils / formatting.js View on Github external
export const runPrettier = ({ code, cursorOffset }) => {
  try {
    return prettier.formatWithCursor(code, {
      cursorOffset,
      parser: 'babel',
      plugins: [babylon]
    });
  } catch (e) {
    // Just a formatting error so we pass
    return null;
  }
};
github ifiokjr / remirror / @remirror / extension-code-block / src / __tests__ / code-block.spec.ts View on Github external
const formatter: CodeBlockFormatter = ({ cursorOffset, language, source }) => {
      if (getLanguage({ fallback: 'text', language, supportedLanguages }) === 'typescript') {
        return formatWithCursor(source, {
          cursorOffset,
          plugins: [typescriptPlugin],
          parser: 'typescript',
          singleQuote: true,
        });
      }
      return;
    };
github ifiokjr / remirror / @remirror / showcase / src / code-formatter.ts View on Github external
const formatCode = ({ parser, source, cursorOffset }: FormatCodeParams) => {
  return formatWithCursor(source, {
    ...options,
    cursorOffset,
    plugins,
    parser,
  });
};
github Gregoor / tofu / src / editor.tsx View on Github external
? spreadCursor(state.cursor)
        : prevState.cursor;
    let [start, end] = cursor;

    const newState = { ...prevState, ...{ ...state, cursor } };
    let { ast, code, cursorFromAST, lastValidAST, printWidth } = newState;

    if (!state.code && state.ast) {
      code = generate(state.ast, { retainLines: true }).code;
    }

    const newWidth = printWidth !== prevState.printWidth;
    if (!ast || code !== prevState.code || newWidth) {
      if (prettify) {
        try {
          const { formatted, cursorOffset } = prettier.formatWithCursor(code, {
            parser: 'babel',
            plugins: [babylon],
            cursorOffset: start,
            printWidth,
            trailingComma: 'all'
          });
          ast = parse(formatted);
          code = formatted;
          start = end = cursorOffset;
        } catch (e) {
          if (!(e instanceof SyntaxError)) {
            throw e;
          }
          if (ast) {
            ast = null;
          }