How to use the prettier.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 flow-typed / flow-typed / definitions / npm / prettier_v1.x.x / flow_v0.56.x-v0.83.x / test_prettier_v1.x.x.js View on Github external
// $ExpectError (Must pass in some source code)
prettier.check();
prettier.check(code);

// $ExpectError (Can't use unsupported options)
prettier.check(code, { sem: true });
prettier.check(code, { semi: true });

// $ExpectError (Options should have proper types)
prettier.check(code, { singleQuote: "true" });
prettier.check(code, { singleQuote: true });

// $ExpectError (Must include CursorOptions)
prettier.formatWithCursor(code);
// $ExpectError (CursorOptions must have cursorOffset)
prettier.formatWithCursor(code, {});
// $ExpectError (CursorOptions cannot have rangeStart or rangeEnd)
prettier.formatWithCursor(code, { cursorOffset: 10, rangeStart: 1 });
prettier.formatWithCursor(code, { cursorOffset: 10 });

// $ExpectError (Must include filePath)
prettier.resolveConfig();
prettier.resolveConfig("/path");

const asyncConfig = prettier.resolveConfig("/path");
if (asyncConfig != null) {
  // $ExpectError (Returns promise)
  (asyncConfig.printWidth: number);
}
(prettier.resolveConfig("/path"): Promise);

// $ExpectError (Options should have proper types)
github prettier-solidity / prettier-plugin-solidity / tests_config / run_spec.js View on Github external
function prettyprint(src, options) {
  const result = prettier.formatWithCursor(src, options);
  if (options.cursorOffset >= 0) {
    result.formatted =
      result.formatted.slice(0, result.cursorOffset) +
      '<|>' +
      result.formatted.slice(result.cursorOffset);
  }
  return result.formatted;
}
github flow-typed / flow-typed / definitions / npm / prettier_v1.x.x / flow_v0.56.x-v0.83.x / test_prettier_v1.x.x.js View on Github external
}
});

/**
 * Test return types
 */

let result;

result = prettier.format(code);
(result: string);

result = prettier.check(code);
(result: boolean);

result = prettier.formatWithCursor(code, { cursorOffset: 1 });
(result.formatted: string);
(result.cursorOffset: number);

result = prettier.resolveConfig("/");
(result.then: Function);

result = prettier.clearConfigCache();
(result: void);

result = prettier.getSupportInfo("1.0");
(result.languages: Array<*>);
(result.options: Array<*>);
github rekit / rekit / packages / rekit-studio / middleware / api / format.js View on Github external
.then(options => {
      try {
        const formatted = prettier.formatWithCursor(
          content,
          Object.assign({ filepath: file, cursorOffset: req.body.cursorOffset }, options || DEFAULT_PRETTIER_OPTIONS)
        );
        res.write(JSON.stringify({ content: formatted }));
      } catch (err) {
        console.log('Failed to format code: ', err);
        res.write(JSON.stringify({ content, error: 'Failed to format code.', err }));
      }
      res.end();
    })
    .catch(err => {