Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ argument is optional, and all of the defaults are shown below:
const prettier = require("prettier");

prettier.format(source, {
// Indent lines with tabs
useTabs: false,

// Fit code within this line limit
printWidth: 80,

Expand Down
3 changes: 3 additions & 0 deletions bin/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const argv = minimist(process.argv.slice(2), {
boolean: [
"write",
"stdin",
"use-tabs",
"single-quote",
"bracket-spacing",
"jsx-bracket-same-line",
Expand Down Expand Up @@ -120,6 +121,7 @@ function getTrailingComma() {
}

const options = {
useTabs: argv["use-tabs"],
printWidth: getIntOption("print-width"),
tabWidth: getIntOption("tab-width"),
bracketSpacing: argv["bracket-spacing"],
Expand Down Expand Up @@ -183,6 +185,7 @@ if (argv["help"] || (!filepatterns.length && !stdin)) {
" --stdin Read input from stdin.\n" +
" --print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.\n" +
" --tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.\n" +
" --use-tabs Indent lines with tabs instead of spaces. Defaults to false.\n" +
" --single-quote Use single quotes instead of double.\n" +
" --bracket-spacing Put spaces between brackets. Defaults to true.\n" +
" --jsx-bracket-same-line Put > on the last line. Defaults to false.\n" +
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function format(text, opts) {
const ast = parse(text, opts);
const astComments = attachComments(text, ast, opts);
const doc = printAstToDoc(ast, opts);
const str = printDocToString(doc, opts.printWidth, guessLineEnding(text));
opts.newLine = guessLineEnding(text);
const str = printDocToString(doc, opts);
ensureAllCommentsPrinted(astComments);
return str;
}
Expand Down Expand Up @@ -100,7 +101,7 @@ module.exports = {
formatAST: function(ast, opts) {
opts = normalizeOptions(opts);
const doc = printAstToDoc(ast, opts);
const str = printDocToString(doc, opts.printWidth);
const str = printDocToString(doc, opts);
return str;
},
// Doesn't handle shebang for now
Expand All @@ -119,7 +120,7 @@ module.exports = {
},
printDocToString: function(doc, opts) {
opts = normalizeOptions(opts);
const str = printDocToString(doc, opts.printWidth);
const str = printDocToString(doc, opts);
return str;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var concat = docBuilders.concat;
var hardline = docBuilders.hardline;
var breakParent = docBuilders.breakParent;
var indent = docBuilders.indent;
var align = docBuilders.align;
var lineSuffix = docBuilders.lineSuffix;
var join = docBuilders.join;
var util = require("./util");
Expand Down Expand Up @@ -773,7 +774,7 @@ function printDanglingComments(path, options, sameIndent) {
if (sameIndent) {
return join(hardline, parts);
}
return indent(options.tabWidth, concat([hardline, join(hardline, parts)]));
return indent(concat([hardline, join(hardline, parts)]));
}

function printComments(path, print, options) {
Expand Down
13 changes: 10 additions & 3 deletions src/doc-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ function concat(parts) {
return { type: "concat", parts };
}

function indent(n, contents) {
function indent(contents) {
assertDoc(contents);

return { type: "indent", contents, n };
return { type: "indent", contents };
}

function align(n, contents) {
assertDoc(contents);

return { type: "align", contents, n };
}

function group(contents, opts) {
Expand Down Expand Up @@ -104,5 +110,6 @@ module.exports = {
lineSuffixBoundary,
breakParent,
ifBreak,
indent
indent,
align
};
57 changes: 49 additions & 8 deletions src/doc-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
const MODE_BREAK = 1;
const MODE_FLAT = 2;

function rootIndent() {
return {
indent: 0,
align: {
spaces: 0,
tabs: 0
}
};
}

function indent(ind) {
return {
indent: ind.indent + 1,
align: ind.align
};
}

function align(ind, n) {
return {
indent: ind.indent,
align: {
spaces: ind.align.spaces + n,
tabs: ind.align.tabs + (n ? 1 : 0)
}
};
}

function fits(next, restCommands, width) {
let restIdx = restCommands.length;
const cmds = [next];
Expand Down Expand Up @@ -35,7 +62,11 @@ function fits(next, restCommands, width) {

break;
case "indent":
cmds.push([ind + doc.n, mode, doc.contents]);
cmds.push([indent(ind), mode, doc.contents]);

break;
case "align":
cmds.push([align(ind, doc.n), mode, doc.contents]);

break;
case "group":
Expand Down Expand Up @@ -77,14 +108,17 @@ function fits(next, restCommands, width) {
return false;
}

function printDocToString(doc, width, newLine) {
newLine = newLine || "\n";

function printDocToString(doc, options) {
let width = options.printWidth;
let tabWidth = options.tabWidth;
let useTabs = options.useTabs;
let indentStr = useTabs ? "\t" : " ".repeat(tabWidth);
let newLine = options.newLine || "\n";
let pos = 0;
// cmds is basically a stack. We've turned a recursive call into a
// while loop which is much faster. The while loop below adds new
// cmds to the array instead of recursively calling `print`.
let cmds = [[0, MODE_BREAK, doc]];
let cmds = [[rootIndent(), MODE_BREAK, doc]];
let out = [];
let shouldRemeasure = false;
let lineSuffix = [];
Expand All @@ -108,7 +142,11 @@ function printDocToString(doc, width, newLine) {

break;
case "indent":
cmds.push([ind + doc.n, mode, doc.contents]);
cmds.push([indent(ind), mode, doc.contents]);

break;
case "align":
cmds.push([align(ind, doc.n), mode, doc.contents]);

break;
case "group":
Expand Down Expand Up @@ -239,8 +277,11 @@ function printDocToString(doc, width, newLine) {
);
}

out.push(newLine + " ".repeat(ind));
pos = ind;
let lineIndent = useTabs
? indentStr.repeat(ind.indent + ind.align.tabs)
: indentStr.repeat(ind.indent) + " ".repeat(ind.align.spaces);
out.push(newLine + lineIndent);
pos = ind.indent * tabWidth + ind.align.spaces;
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var validate = require("jest-validate").validate;
var deprecatedConfig = require("./deprecated");

var defaults = {
useTabs: false,
tabWidth: 2,
printWidth: 80,
singleQuote: false,
Expand Down
Loading