Skip to content

Commit

Permalink
See #3. Add indent_width and use_tabs options support
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Sep 23, 2018
1 parent 0a5d935 commit 094d12f
Show file tree
Hide file tree
Showing 3 changed files with 3,558 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const beautifier: Beautifier = {
name: "yapf",
package: pkg,
options: {
Python: true,
Python: {
indent_width: "indent_size",
use_tabs: [["indent_style"], options => options.indent_style === "tab"],
},
},
dependencies: [
{
Expand All @@ -44,22 +47,24 @@ export const beautifier: Beautifier = {
beautify({
text,
dependencies,
filePath,
options,
beautifierConfig,
}: BeautifierBeautifyData) {
const yapf = dependencies.get<ExecutableDependency>("yapf");
const basePath: string = os.tmpdir();
const config =
const style =
beautifierConfig && beautifierConfig.filePath
? `--style=${beautifierConfig.filePath}`
: "";
? `${beautifierConfig.filePath}`
: stringifyOptions(options);
const config = style ? `--style=${style}` : "";
// tslint:disable-next-line no-console
console.log(`Using config: ${config}`);
return tmpFile({ postfix: ".py" }).then(filePath =>
writeFile(filePath, text).then(() =>
return tmpFile({ postfix: ".py" }).then(filePath => {
const basePath: string = os.tmpdir();
const args = relativizePaths([config, "--in-place", filePath], basePath);
return writeFile(filePath, text).then(() =>
yapf
.run({
args: relativizePaths(["--in-place", filePath, config], basePath),
args,
options: {
cwd: basePath,
},
Expand All @@ -70,8 +75,8 @@ export const beautifier: Beautifier = {
}
return readFile(filePath);
})
)
);
);
});
},
};

Expand Down Expand Up @@ -158,4 +163,22 @@ function relativizePaths(args: string[], basePath: string): string[] {
});
}

function stringifyOptions(
options: BeautifierBeautifyData["options"]
): string | undefined {
const ops: string[] = Object.keys(options)
.map(optionKey => {
const value = options[optionKey];
if (value === undefined) {
return;
}
return `${optionKey}: ${value}`;
})
.filter(Boolean) as any;
if (ops.length > 0) {
return `{${ops.join(", ")}}`;
}
return undefined;
}

export default beautifier;
46 changes: 46 additions & 0 deletions test/options/indentWidth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { newUnibeautify, Beautifier } from "unibeautify";
import beautifier from "@src";

// import * as fs from "fs";
// import * as path from "path";

// const text: string = fs
// .readFileSync(path.resolve(__dirname, `../fixtures/test1.py`))
// .toString();

// testWithTabWidth(0, true);
testWithTabWidth(1, true);
testWithTabWidth(2, true);

// testWithTabWidth(0, false);
testWithTabWidth(2, false);
testWithTabWidth(4, false);

function testWithTabWidth(indentWidth: number, useTabs: boolean = false) {
test(`should successfully beautify Python text with useTabs=${useTabs} and indentWidth=${indentWidth}`, () => {
const unibeautify = newUnibeautify();
unibeautify.loadBeautifier(beautifier);

const indentChar = useTabs ? "\t" : " ";
const indentation = useTabs ? "\t" : indentChar.repeat(indentWidth);
const indentArg = useTabs ? "tab" : "space";

const text = `def test(n):\n return n + 1\n`;
const beautifierResult = `def test(n):\n${indentation}return n + 1\n`;

return unibeautify
.beautify({
languageName: "Python",
options: {
Python: {
indent_style: indentArg,
indent_size: indentWidth,
},
},
text,
})
.then(results => {
expect(results).toBe(beautifierResult);
});
});
}
Loading

0 comments on commit 094d12f

Please sign in to comment.