1
- import {
2
- workspace as Workspace ,
3
- window as Window ,
4
- commands as Commands ,
5
- languages as Languages ,
6
- Position ,
7
- Range ,
8
- TextEdit ,
9
- ConfigurationTarget ,
10
- type Disposable ,
11
- type DocumentSelector ,
12
- type QuickPickItem ,
13
- type WorkspaceConfiguration
14
- } from 'vscode' ;
1
+ import * as vscode from 'vscode' ;
15
2
import type { PHPFmt } from './PHPFmt' ;
16
3
import { type Widget , PHPFmtStatus } from './Widget' ;
17
4
import type { Transformation } from './Transformation' ;
18
5
import { PHPFmtSkipError } from './PHPFmtError' ;
19
6
import * as meta from './meta' ;
20
7
21
8
export class PHPFmtProvider {
22
- private readonly documentSelector : DocumentSelector ;
9
+ private readonly documentSelector : vscode . DocumentSelector ;
23
10
private transformation : Transformation ;
24
- private config : WorkspaceConfiguration ;
11
+ private config : vscode . WorkspaceConfiguration ;
25
12
26
13
public constructor (
27
14
private readonly widget : Widget ,
28
15
private readonly phpfmt : PHPFmt
29
16
) {
30
- this . config = Workspace . getConfiguration ( 'phpfmt' ) ;
17
+ this . config = vscode . workspace . getConfiguration ( 'phpfmt' ) ;
31
18
this . documentSelector = [
32
19
{ language : 'php' , scheme : 'file' } ,
33
20
{ language : 'php' , scheme : 'untitled' }
34
21
] ;
35
22
this . transformation = this . phpfmt . getTransformation ( ) ;
36
23
}
37
24
38
- public registerOnDidChangeConfiguration ( ) : Disposable {
39
- return Workspace . onDidChangeConfiguration ( ( ) => {
40
- this . config = Workspace . getConfiguration ( 'phpfmt' ) ;
25
+ public registerOnDidChangeConfiguration ( ) : vscode . Disposable {
26
+ return vscode . workspace . onDidChangeConfiguration ( ( ) => {
27
+ this . config = vscode . workspace . getConfiguration ( 'phpfmt' ) ;
41
28
this . phpfmt . loadSettings ( ) ;
42
29
this . transformation = this . phpfmt . getTransformation ( ) ;
43
30
this . widget . logInfo ( `settings reloaded` ) ;
44
31
} ) ;
45
32
}
46
33
47
- public registerFormatCommand ( ) : Disposable {
48
- return Commands . registerTextEditorCommand (
34
+ public registerFormatCommand ( ) : vscode . Disposable {
35
+ return vscode . commands . registerTextEditorCommand (
49
36
meta . commands . format ,
50
37
textEditor => {
51
38
if ( textEditor . document . languageId === 'php' ) {
52
- void Commands . executeCommand ( 'editor.action.formatDocument' ) ;
39
+ void vscode . commands . executeCommand ( 'editor.action.formatDocument' ) ;
53
40
}
54
41
}
55
42
) ;
56
43
}
57
44
58
- private async getTransformationItems ( ) : Promise < QuickPickItem [ ] > {
45
+ private async getTransformationItems ( ) : Promise < vscode . QuickPickItem [ ] > {
59
46
const transformationItems = await this . transformation . getTransformations ( ) ;
60
47
const items = transformationItems . map ( o => ( {
61
48
label : o . key ,
@@ -64,12 +51,12 @@ export class PHPFmtProvider {
64
51
return items ;
65
52
}
66
53
67
- public registerListTransformationsCommand ( ) : Disposable {
68
- return Commands . registerCommand (
54
+ public registerListTransformationsCommand ( ) : vscode . Disposable {
55
+ return vscode . commands . registerCommand (
69
56
meta . commands . listTransformations ,
70
57
async ( ) => {
71
58
const items = await this . getTransformationItems ( ) ;
72
- const result = await Window . showQuickPick ( items ) ;
59
+ const result = await vscode . window . showQuickPick ( items ) ;
73
60
74
61
if ( typeof result !== 'undefined' ) {
75
62
this . widget . logInfo ( 'Getting transformation output' ) ;
@@ -89,28 +76,31 @@ export class PHPFmtProvider {
89
76
section : string ,
90
77
value : T
91
78
) : Promise < void > {
92
- const targetResult = await Window . showQuickPick ( [ 'Global' , 'Workspace' ] , {
93
- placeHolder : 'Where to update settings.json?'
94
- } ) ;
95
- let target : ConfigurationTarget ;
79
+ const targetResult = await vscode . window . showQuickPick (
80
+ [ 'Global' , 'Workspace' ] ,
81
+ {
82
+ placeHolder : 'Where to update settings.json?'
83
+ }
84
+ ) ;
85
+ let target : vscode . ConfigurationTarget ;
96
86
97
87
if ( targetResult === 'Global' ) {
98
- target = ConfigurationTarget . Global ;
88
+ target = vscode . ConfigurationTarget . Global ;
99
89
} else {
100
- target = ConfigurationTarget . Workspace ;
90
+ target = vscode . ConfigurationTarget . Workspace ;
101
91
}
102
92
103
93
try {
104
94
await this . config . update ( section , value , target ) ;
105
- await Window . showInformationMessage (
95
+ await vscode . window . showInformationMessage (
106
96
'Configuration updated successfully!'
107
97
) ;
108
98
} catch ( err ) {
109
- await Window . showErrorMessage ( 'Configuration updated failed!' ) ;
99
+ await vscode . window . showErrorMessage ( 'Configuration updated failed!' ) ;
110
100
}
111
101
}
112
102
113
- public registerToggleTransformationsCommand ( ) : Disposable [ ] {
103
+ public registerToggleTransformationsCommand ( ) : vscode . Disposable [ ] {
114
104
const commands = [
115
105
{
116
106
command : meta . commands . toggleAdditionalTransformations ,
@@ -123,22 +113,22 @@ export class PHPFmtProvider {
123
113
] ;
124
114
125
115
return commands . map ( command =>
126
- Commands . registerCommand ( command . command , async ( ) => {
116
+ vscode . commands . registerCommand ( command . command , async ( ) => {
127
117
const items = await this . getTransformationItems ( ) ;
128
118
items . unshift ( {
129
119
label : 'All' ,
130
120
description : 'Choose all of following'
131
121
} ) ;
132
122
133
- const result = await Window . showQuickPick ( items ) ;
123
+ const result = await vscode . window . showQuickPick ( items ) ;
134
124
135
125
if ( typeof result !== 'undefined' ) {
136
126
let value = this . config . get < string [ ] > ( command . key ) ;
137
127
if ( result . label === 'All' ) {
138
128
value = items . filter ( o => o . label !== 'All' ) . map ( o => o . label ) ;
139
129
} else {
140
130
const enabled = value ?. includes ( result . label ) ;
141
- const enableResult = await Window . showQuickPick ( [
131
+ const enableResult = await vscode . window . showQuickPick ( [
142
132
{
143
133
label : 'Enable' ,
144
134
description : enabled ? 'Current' : ''
@@ -163,7 +153,7 @@ export class PHPFmtProvider {
163
153
) ;
164
154
}
165
155
166
- public registerToggleBooleanCommand ( ) : Disposable [ ] {
156
+ public registerToggleBooleanCommand ( ) : vscode . Disposable [ ] {
167
157
const commands = [
168
158
{
169
159
command : meta . commands . togglePSR1Naming ,
@@ -184,9 +174,9 @@ export class PHPFmtProvider {
184
174
] ;
185
175
186
176
return commands . map ( command =>
187
- Commands . registerCommand ( command . command , async ( ) => {
177
+ vscode . commands . registerCommand ( command . command , async ( ) => {
188
178
const value = this . config . get < boolean > ( command . key ) ;
189
- const result = await Window . showQuickPick ( [
179
+ const result = await vscode . window . showQuickPick ( [
190
180
{
191
181
label : 'Enable' ,
192
182
description : value ? 'Current' : ''
@@ -207,11 +197,17 @@ export class PHPFmtProvider {
207
197
) ;
208
198
}
209
199
210
- public registerToggleIndentWithSpaceCommand ( ) : Disposable {
211
- return Commands . registerCommand (
200
+ public registerToggleIndentWithSpaceCommand ( ) : vscode . Disposable {
201
+ return vscode . commands . registerCommand (
212
202
meta . commands . toggleIndentWithSpace ,
213
203
async ( ) => {
214
- const result = await Window . showQuickPick ( [ 'tabs' , '2' , '4' , '6' , '8' ] ) ;
204
+ const result = await vscode . window . showQuickPick ( [
205
+ 'tabs' ,
206
+ '2' ,
207
+ '4' ,
208
+ '6' ,
209
+ '8'
210
+ ] ) ;
215
211
216
212
if ( typeof result !== 'undefined' ) {
217
213
const value = result === 'tabs' ? false : Number ( result ) ;
@@ -222,25 +218,28 @@ export class PHPFmtProvider {
222
218
) ;
223
219
}
224
220
225
- public registerDocumentFormattingEditProvider ( ) : Disposable {
226
- return Languages . registerDocumentFormattingEditProvider (
221
+ public registerDocumentFormattingEditProvider ( ) : vscode . Disposable {
222
+ return vscode . languages . registerDocumentFormattingEditProvider (
227
223
this . documentSelector ,
228
224
{
229
225
provideDocumentFormattingEdits : async document => {
230
226
try {
231
227
const originalText = document . getText ( ) ;
232
228
const lastLine = document . lineAt ( document . lineCount - 1 ) ;
233
- const range = new Range ( new Position ( 0 , 0 ) , lastLine . range . end ) ;
229
+ const range = new vscode . Range (
230
+ new vscode . Position ( 0 , 0 ) ,
231
+ lastLine . range . end
232
+ ) ;
234
233
235
234
const text = await this . phpfmt . format ( originalText ) ;
236
235
this . widget . updateStatusBarItem ( PHPFmtStatus . Success ) ;
237
236
if ( text && text !== originalText ) {
238
- return [ new TextEdit ( range , text ) ] ;
237
+ return [ new vscode . TextEdit ( range , text ) ] ;
239
238
}
240
239
} catch ( err ) {
241
240
this . widget . updateStatusBarItem ( PHPFmtStatus . Error ) ;
242
241
if ( ! ( err instanceof PHPFmtSkipError ) && err instanceof Error ) {
243
- void Window . showErrorMessage ( err . message ) ;
242
+ void vscode . window . showErrorMessage ( err . message ) ;
244
243
this . widget . logError ( 'Format failed' , err ) ;
245
244
}
246
245
}
@@ -250,8 +249,8 @@ export class PHPFmtProvider {
250
249
) ;
251
250
}
252
251
253
- public registerDocumentRangeFormattingEditProvider ( ) : Disposable {
254
- return Languages . registerDocumentRangeFormattingEditProvider (
252
+ public registerDocumentRangeFormattingEditProvider ( ) : vscode . Disposable {
253
+ return vscode . languages . registerDocumentRangeFormattingEditProvider (
255
254
this . documentSelector ,
256
255
{
257
256
provideDocumentRangeFormattingEdits : async ( document , range ) => {
@@ -273,12 +272,12 @@ export class PHPFmtProvider {
273
272
}
274
273
this . widget . updateStatusBarItem ( PHPFmtStatus . Success ) ;
275
274
if ( text && text !== originalText ) {
276
- return [ new TextEdit ( range , text ) ] ;
275
+ return [ new vscode . TextEdit ( range , text ) ] ;
277
276
}
278
277
} catch ( err ) {
279
278
this . widget . updateStatusBarItem ( PHPFmtStatus . Error ) ;
280
279
if ( ! ( err instanceof PHPFmtSkipError ) && err instanceof Error ) {
281
- void Window . showErrorMessage ( err . message ) ;
280
+ void vscode . window . showErrorMessage ( err . message ) ;
282
281
this . widget . logError ( 'Format failed' , err ) ;
283
282
}
284
283
}
@@ -288,12 +287,12 @@ export class PHPFmtProvider {
288
287
) ;
289
288
}
290
289
291
- public registerStatusBarItem ( ) : Disposable [ ] {
290
+ public registerStatusBarItem ( ) : vscode . Disposable [ ] {
292
291
return [
293
- Window . onDidChangeActiveTextEditor ( editor => {
292
+ vscode . window . onDidChangeActiveTextEditor ( editor => {
294
293
this . widget . toggleStatusBarItem ( editor ) ;
295
294
} ) ,
296
- Commands . registerCommand ( meta . commands . openOutput , ( ) => {
295
+ vscode . commands . registerCommand ( meta . commands . openOutput , ( ) => {
297
296
this . widget . getOutputChannel ( ) . show ( ) ;
298
297
} )
299
298
] ;
0 commit comments