forked from prettier/prettier-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrettierEditProvider.ts
156 lines (150 loc) · 4.86 KB
/
PrettierEditProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
workspace,
window,
DocumentRangeFormattingEditProvider,
DocumentFormattingEditProvider,
Range,
TextDocument,
FormattingOptions,
CancellationToken,
TextEdit,
Selection,
Position
} from 'vscode';
import { requireLocalPkg } from './requirePkg';
import {
PrettierVSCodeConfig,
PrettierConfig,
Prettier,
PrettierEslintFormat
} from './types.d';
type ShowAction = "Show";
/**
* Format the given text with user's configuration.
* @param text Text to format
* @param path formatting file's path
* @returns {string} formatted text
*/
function format(text: string, path: string): string {
const config: PrettierVSCodeConfig = 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 prettierOptions = {
printWidth: config.printWidth,
tabWidth: config.tabWidth,
singleQuote: config.singleQuote,
trailingComma,
bracketSpacing: config.bracketSpacing,
jsxBracketSameLine: config.jsxBracketSameLine,
parser: parser,
semi: config.semi,
useTabs: config.useTabs,
};
if (config.eslintIntegration) {
const prettierEslint = requireLocalPkg(path, 'prettier-eslint') as PrettierEslintFormat;
return prettierEslint({
text,
filePath: path,
fallbackPrettierOptions: prettierOptions
});
}
const prettier = requireLocalPkg(path, 'prettier') as Prettier;
return prettier.format(text, prettierOptions);
}
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[] {
try {
return [TextEdit.replace(
range,
format(document.getText(range), document.fileName)
)];
} catch (e) {
let errorPosition;
if (e.loc) {
let charPos = e.loc.column;
if (e.loc.line === 1) { // start selection range
charPos = range.start.character + e.loc.column;
}
errorPosition = new Position(e.loc.line - 1 + range.start.line, charPos);
}
handleError(document, e.message, errorPosition);
}
}
provideDocumentFormattingEdits(
document: TextDocument,
options: FormattingOptions,
token: CancellationToken
): TextEdit[] {
try {
return [TextEdit.replace(
fullDocumentRange(document),
format(document.getText(), document.fileName)
)];
} catch (e) {
let errorPosition;
if (e.loc) {
errorPosition = new Position(e.loc.line - 1, e.loc.column);
}
handleError(document, e.message, errorPosition);
}
}
}
/**
* Handle errors for a given text document.
* Steps:
* - Show the error message.
* - Scroll to the error position in given document if asked for it.
*
* @param document Document which raised the error
* @param message Error message
* @param errorPosition Position where the error occured. Relative to document.
*/
function handleError(document: TextDocument, message: string, errorPosition: Position) {
if (errorPosition) {
window.showErrorMessage(message, "Show")
.then(function onAction(action?: ShowAction) {
if (action === "Show") {
const rangeError = new Range(errorPosition, errorPosition);
/*
Show text document which has errored.
Format on save case. (save all)
*/
window.showTextDocument(document).then(
(editor) => {
// move cursor to error position and show it.
editor.selection = new Selection(rangeError.start, rangeError.end);
editor.revealRange(rangeError);
}
);
}
});
} else {
window.showErrorMessage(message);
}
}
export default PrettierEditProvider;