Skip to content

Commit

Permalink
Adds config params (prettier#3)
Browse files Browse the repository at this point in the history
* add configs

* added contrib, white space

* Clean Up
- Added vscode to gitignore

* issues with the vscode dir, settings files dont match up.  Added to git ignore

* add settings.json back in

* Update .gitignore
  • Loading branch information
colek42 authored and esbenp committed Jan 11, 2017
1 parent 92e1535 commit f6cd79b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
out
node_modules
node_modules
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"preLaunchTask": "npm"
}
]
}
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"out": true // set this to false to include "out" folder in search results
},
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
}
}
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Cole Kennedy](https://github.com/colek42)
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ Automatically format your Javascript file on save by enabling the *Format On Sav

Format Javascript files when saving.

#### printWidth (default: 80)

Fit code within this line limit

#### tabWidth (default: 2)

Number of spaces it should use per tab

#### useFlowParser (default: false)
Use the flow parser instead of babylon

#### singleQuote (default: false)
If true, will use single instead of double quotes

#### trailingComma (default: false)
Controls the printing of trailing commas wherever possible

#### bracketSpacing (default: true)
Controls the printing of spaces inside array and objects


### Contribute

This is my first Visual Studio Extension so I probably made some terrible choices. Feel free to open issue or PRs!
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@
"type": "boolean",
"default": false,
"description": "Format on save"
},
"prettier.printWidth": {
"type": "integer",
"default": 80,
"description": "Fit code within this line limit"
},
"prettier.tabWidth": {
"type": "integer",
"default": 2,
"description": "Number of spaces it should use per tab"
},
"prettier.useFlowParser": {
"type": "boolean",
"default": false,
"description": "Use the flow parser instead of babylon"
},
"prettier.singleQuote": {
"type": "boolean",
"default": false,
"description": "If true, will use single instead of double quotes"
},
"prettier.trailingComma": {
"type": "boolean",
"default": false,
"description": "Controls the printing of trailing commas wherever possible"
},
"prettier.bracketSpacing": {
"type": "boolean",
"default": false,
"description": "Controls the printing of spaces inside array and objects"
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { commands, ExtensionContext, Range, Position, TextEdit, window, workspac
const prettier = require('prettier')

export function activate(context: ExtensionContext) {

const eventDisposable = (workspace as any).onWillSaveTextDocument(e => {
const document = e.document;

Expand All @@ -20,7 +19,6 @@ export function activate(context: ExtensionContext) {

e.waitUntil(new Promise(resolve => {
const prettified = format(document, null);

const rangeObj = new Range(0, 0, document.lineCount, 0);
const edit = TextEdit.replace(rangeObj, prettified);

Expand All @@ -35,7 +33,6 @@ export function activate(context: ExtensionContext) {
}

const selection = editor.selection;

const prettified = format(editor.document, selection);

editor.edit((editBuilder) => {
Expand All @@ -45,26 +42,37 @@ export function activate(context: ExtensionContext) {
selection.end.line,
selection.end.character
);

editBuilder.replace(rangeObj, prettified);
})


});

context.subscriptions.push(eventDisposable);
context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {
}
const config = workspace.getConfiguration('prettier');

const printWidth = (config as any).printWidth;
const tabWidth = (config as any).tabWidth;
const useFlowParser = (config as any).useFlowParser;
const singleQuote = (config as any).singleQuote;
const trailingComma = (config as any).trailingComma;
const bracketSpacing = (config as any).bracketSpacing;

const format = (document, selection = null) => {
const text = document.getText(selection)

try {
var transformed = prettier.format(text)
var transformed = prettier.format(text, {
printWidth: printWidth,
tabWidth: tabWidth,
useFlowParser: useFlowParser,
singleQuote: singleQuote,
trailingComma: trailingComma,
bracketSpacing: bracketSpacing
});
} catch (e) {
console.log("Error transforming using prettier:", e);
transformed = text;
Expand Down

0 comments on commit f6cd79b

Please sign in to comment.