-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve prettier against formatted file
- Loading branch information
Showing
1 changed file
with
112 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,112 @@ | ||
import { | ||
workspace, | ||
DocumentRangeFormattingEditProvider, | ||
DocumentFormattingEditProvider, | ||
Range, | ||
TextDocument, | ||
FormattingOptions, | ||
CancellationToken, | ||
TextEdit | ||
} from 'vscode'; | ||
|
||
const prettier = require('prettier'); | ||
|
||
type ParserOption = 'babylon' | 'flow' | ||
type TrailingCommaOption = 'none' | 'es5' | 'all' | boolean /* deprecated boolean*/ | ||
|
||
interface PrettierConfig { | ||
printWidth: number, | ||
tabWidth: number, | ||
useFlowParser: boolean, // deprecated | ||
singleQuote: boolean, | ||
trailingComma: TrailingCommaOption, | ||
bracketSpacing: boolean, | ||
jsxBracketSameLine: boolean, | ||
parser: ParserOption | ||
} | ||
|
||
function format(text: string): string { | ||
const config: PrettierConfig = workspace.getConfiguration('prettier') as any; | ||
/* | ||
handle deprecated parser option | ||
*/ | ||
let parser = config.parser; | ||
if (!parser) { // unset config | ||
parser = config.useFlowParser ? 'flow' : 'babylon'; | ||
} | ||
/* | ||
handle trailingComma changes boolean -> string | ||
*/ | ||
let trailingComma = config.trailingComma; | ||
if (trailingComma === true) { | ||
trailingComma = 'es5'; | ||
} else if (trailingComma === false) { | ||
trailingComma = 'none'; | ||
} | ||
let transformed: string; | ||
try { | ||
return prettier.format(text, { | ||
printWidth: config.printWidth, | ||
tabWidth: config.tabWidth, | ||
singleQuote: config.singleQuote, | ||
trailingComma, | ||
bracketSpacing: config.bracketSpacing, | ||
jsxBracketSameLine: config.jsxBracketSameLine, | ||
parser: parser | ||
}); | ||
} catch (e) { | ||
console.log("Error transforming using prettier:", e.message); | ||
return text; | ||
} | ||
} | ||
|
||
function fullDocumentRange(document: TextDocument): Range { | ||
const lastLineId = document.lineCount - 1; | ||
return new Range(0, 0, lastLineId, document.lineAt(lastLineId).text.length); | ||
} | ||
|
||
class PrettierEditProvider implements | ||
DocumentRangeFormattingEditProvider, | ||
DocumentFormattingEditProvider { | ||
provideDocumentRangeFormattingEdits( | ||
document: TextDocument, | ||
range: Range, | ||
options: FormattingOptions, | ||
token: CancellationToken | ||
): TextEdit[] { | ||
return [TextEdit.replace(range, format(document.getText(range)))]; | ||
} | ||
provideDocumentFormattingEdits( | ||
document: TextDocument, | ||
options: FormattingOptions, | ||
token: CancellationToken | ||
): TextEdit[] { | ||
return [TextEdit.replace(fullDocumentRange(document), format(document.getText()))]; | ||
} | ||
} | ||
|
||
export default PrettierEditProvider; | ||
export { PrettierConfig } | ||
import { | ||
workspace, | ||
DocumentRangeFormattingEditProvider, | ||
DocumentFormattingEditProvider, | ||
Range, | ||
TextDocument, | ||
FormattingOptions, | ||
CancellationToken, | ||
TextEdit | ||
} from 'vscode'; | ||
|
||
const path = require('path'); | ||
const Module = require('module'); | ||
|
||
type ParserOption = 'babylon' | 'flow' | ||
type TrailingCommaOption = 'none' | 'es5' | 'all' | boolean /* deprecated boolean*/ | ||
|
||
interface PrettierConfig { | ||
printWidth: number, | ||
tabWidth: number, | ||
useFlowParser: boolean, // deprecated | ||
singleQuote: boolean, | ||
trailingComma: TrailingCommaOption, | ||
bracketSpacing: boolean, | ||
jsxBracketSameLine: boolean, | ||
parser: ParserOption | ||
} | ||
interface Prettier { | ||
format: (string, PrettierConfig?) => string | ||
readonly version: string | ||
} | ||
|
||
function requirePrettier(fspath: string): Prettier { | ||
const fileModule = new Module(fspath); | ||
fileModule.paths = Module._nodeModulePaths(path.join(fspath, '..')); | ||
try { | ||
return fileModule.require("prettier"); | ||
} catch (e) { | ||
// No local prettier found | ||
} | ||
return require("prettier"); | ||
} | ||
|
||
function format(text: string, path: string): string { | ||
const config: PrettierConfig = workspace.getConfiguration('prettier') as any; | ||
/* | ||
handle deprecated parser option | ||
*/ | ||
let parser = config.parser; | ||
if (!parser) { // unset config | ||
parser = config.useFlowParser ? 'flow' : 'babylon'; | ||
} | ||
/* | ||
handle trailingComma changes boolean -> string | ||
*/ | ||
let trailingComma = config.trailingComma; | ||
if (trailingComma === true) { | ||
trailingComma = 'es5'; | ||
} else if (trailingComma === false) { | ||
trailingComma = 'none'; | ||
} | ||
const prettier = requirePrettier(path); | ||
let transformed: string; | ||
try { | ||
return prettier.format(text, { | ||
printWidth: config.printWidth, | ||
tabWidth: config.tabWidth, | ||
singleQuote: config.singleQuote, | ||
trailingComma, | ||
bracketSpacing: config.bracketSpacing, | ||
jsxBracketSameLine: config.jsxBracketSameLine, | ||
parser: parser | ||
}); | ||
} catch (e) { | ||
console.log("Error transforming using prettier:", e.message); | ||
return text; | ||
} | ||
} | ||
|
||
function fullDocumentRange(document: TextDocument): Range { | ||
const lastLineId = document.lineCount - 1; | ||
return new Range(0, 0, lastLineId, document.lineAt(lastLineId).text.length); | ||
} | ||
|
||
class PrettierEditProvider implements | ||
DocumentRangeFormattingEditProvider, | ||
DocumentFormattingEditProvider { | ||
provideDocumentRangeFormattingEdits( | ||
document: TextDocument, | ||
range: Range, | ||
options: FormattingOptions, | ||
token: CancellationToken | ||
): TextEdit[] { | ||
return [TextEdit.replace( | ||
range, | ||
format(document.getText(range), document.fileName) | ||
)]; | ||
} | ||
provideDocumentFormattingEdits( | ||
document: TextDocument, | ||
options: FormattingOptions, | ||
token: CancellationToken | ||
): TextEdit[] { | ||
return [TextEdit.replace( | ||
fullDocumentRange(document), | ||
format(document.getText(), document.fileName) | ||
)]; | ||
} | ||
} | ||
|
||
export default PrettierEditProvider; | ||
export { PrettierConfig } |