forked from microsoft/vscode-editorconfig
-
Notifications
You must be signed in to change notification settings - Fork 117
/
api.ts
205 lines (190 loc) · 4.66 KB
/
api.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as editorconfig from 'editorconfig'
import { TextDocument, TextEditorOptions, Uri, window, workspace } from 'vscode'
/**
* Resolves `TextEditorOptions` for a `TextDocument`, combining the editor's
* default configuration with that of EditorConfig's configuration.
*/
export async function resolveTextEditorOptions(
doc: TextDocument,
{
onBeforeResolve,
onEmptyConfig,
}: {
onBeforeResolve?: (relativePath: string) => void
onEmptyConfig?: (relativePath: string) => void
} = {},
) {
const editorconfigSettings = await resolveCoreConfig(doc, {
onBeforeResolve,
})
if (editorconfigSettings) {
return fromEditorConfig(editorconfigSettings, pickWorkspaceDefaults(doc))
}
if (onEmptyConfig) {
const rp = resolveFile(doc).relativePath
if (rp) {
onEmptyConfig(rp)
}
}
return {}
}
/**
* Applies new `TextEditorOptions` to the active text editor.
*/
export async function applyTextEditorOptions(
newOptions: TextEditorOptions,
{
onNoActiveTextEditor,
onSuccess,
}: {
onNoActiveTextEditor?: () => void
onSuccess?: (newOptions: TextEditorOptions) => void
} = {},
) {
const editor = window.activeTextEditor
if (!editor) {
if (onNoActiveTextEditor) {
onNoActiveTextEditor()
}
return
}
editor.options = newOptions
if (onSuccess) {
onSuccess(newOptions)
}
}
/**
* Picks EditorConfig-relevant props from the editor's default configuration.
*/
export function pickWorkspaceDefaults(
doc?: TextDocument,
): {
/**
* The number of spaces a tab is equal to. When `editor.detectIndentation`
* is on, this property value will be `undefined`.
*/
tabSize?: number
/**
* Insert spaces when pressing `Tab`. When `editor.detectIndentation` is on,
* this property value will be `undefined`.
*/
insertSpaces?: boolean
} {
const workspaceConfig = workspace.getConfiguration('editor', doc)
const detectIndentation = workspaceConfig.get<boolean>('detectIndentation')
return detectIndentation
? {}
: {
tabSize: workspaceConfig.get<number>('tabSize'),
insertSpaces: workspaceConfig.get<boolean>('insertSpaces'),
}
}
export type ResolvedCoreConfig = editorconfig.KnownProps &
Record<string, string | number | boolean>
/**
* Resolves an EditorConfig configuration for the file related to a
* `TextDocument`.
*/
export async function resolveCoreConfig(
doc: TextDocument,
{
onBeforeResolve,
}: {
onBeforeResolve?: (relativePath: string) => void
} = {},
): Promise<ResolvedCoreConfig> {
const { fileName, relativePath } = resolveFile(doc)
if (!fileName) {
return {}
}
if (relativePath) {
onBeforeResolve?.(relativePath)
}
const config = await editorconfig.parse(fileName)
if (config.indent_size === 'tab') {
config.indent_size = config.tab_width
}
return config as ResolvedCoreConfig
}
export function resolveFile(
doc: TextDocument,
): {
fileName?: string
relativePath?: string
} {
if (doc.languageId === 'Log') {
return {}
}
const file = getFile()
return {
fileName: file?.fsPath,
relativePath: file && workspace.asRelativePath(file, true),
}
function getFile(): Uri | undefined {
if (!doc.isUntitled) {
return doc.uri
}
if (workspace.workspaceFolders?.[0]) {
return Uri.joinPath(workspace.workspaceFolders[0].uri, doc.fileName)
}
return undefined
}
}
/**
* Convert .editorconfig values to vscode editor options
*/
export function fromEditorConfig(
config: editorconfig.KnownProps = {},
defaults: TextEditorOptions = pickWorkspaceDefaults(),
): TextEditorOptions {
const resolved: TextEditorOptions = {
tabSize:
config.indent_style === 'tab'
? config.tab_width ?? config.indent_size
: config.indent_size ?? config.tab_width,
}
if (resolved.tabSize === 'tab') {
resolved.tabSize = config.tab_width
}
return {
...(config.indent_style === 'tab' ||
config.indent_size === 'tab' ||
config.indent_style === 'space'
? {
insertSpaces: config.indent_style === 'space',
}
: {}),
tabSize:
resolved.tabSize && resolved.tabSize >= 0
? resolved.tabSize
: defaults.tabSize,
}
}
/**
* Convert vscode editor options to .editorconfig values
*/
export function toEditorConfig(options: TextEditorOptions) {
const result: editorconfig.KnownProps = {}
switch (options.insertSpaces) {
case true:
result.indent_style = 'space'
if (options.tabSize) {
result.indent_size = resolveTabSize(options.tabSize)
}
break
case false:
case 'auto':
result.indent_style = 'tab'
if (options.tabSize) {
result.tab_width = resolveTabSize(options.tabSize)
}
break
}
return result
/**
* Convert vscode tabSize option into numeric value
*/
function resolveTabSize(tabSize: number | string) {
return tabSize === 'auto' ? 4 : parseInt(String(tabSize), 10)
}
}