1
1
import * as vscode from "vscode" ;
2
2
import {
3
- TreeDataProvider ,
4
- TreeDragAndDropController ,
5
- ExtensionContext ,
3
+ type TreeDataProvider ,
4
+ type TreeDragAndDropController ,
5
+ type ExtensionContext ,
6
6
EventEmitter ,
7
- Event ,
8
- TreeView ,
9
- ProviderResult ,
10
- TreeItem ,
7
+ type Event ,
8
+ type TreeView ,
9
+ type ProviderResult ,
10
+ type TreeItem ,
11
11
TreeItemCollapsibleState ,
12
12
window ,
13
13
languages ,
14
- Uri ,
15
- CancellationToken ,
16
- FileDecoration ,
17
- DocumentDropEditProvider ,
14
+ type Uri ,
15
+ type CancellationToken ,
16
+ type FileDecoration ,
17
+ type DocumentDropEditProvider ,
18
18
workspace ,
19
19
} from "vscode" ;
20
- import * as fs from "fs" ;
20
+ import * as fs from "node: fs" ;
21
21
import {
22
22
get_configuration ,
23
23
find_file ,
@@ -28,15 +28,17 @@ import {
28
28
make_docs_uri ,
29
29
} from "../utils" ;
30
30
import { SceneParser } from "./parser" ;
31
- import { SceneNode , Scene } from "./types" ;
31
+ import type { SceneNode , Scene } from "./types" ;
32
32
33
33
const log = createLogger ( "scenes.preview" ) ;
34
34
35
- export class ScenePreviewProvider implements TreeDataProvider < SceneNode > , TreeDragAndDropController < SceneNode > , DocumentDropEditProvider {
35
+ export class ScenePreviewProvider
36
+ implements TreeDataProvider < SceneNode > , TreeDragAndDropController < SceneNode > , DocumentDropEditProvider
37
+ {
36
38
public dropMimeTypes = [ ] ;
37
39
public dragMimeTypes = [ ] ;
38
40
private tree : TreeView < SceneNode > ;
39
- private scenePreviewPinned = false ;
41
+ private scenePreviewLocked = false ;
40
42
private currentScene = "" ;
41
43
public parser = new SceneParser ( ) ;
42
44
public scene : Scene ;
@@ -52,20 +54,22 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
52
54
constructor ( private context : ExtensionContext ) {
53
55
this . tree = vscode . window . createTreeView ( "scenePreview" , {
54
56
treeDataProvider : this ,
55
- dragAndDropController : this
57
+ dragAndDropController : this ,
56
58
} ) ;
57
59
58
60
const selector = [
59
61
{ language : "csharp" , scheme : "file" } ,
60
62
{ language : "gdscript" , scheme : "file" } ,
61
63
] ;
62
64
context . subscriptions . push (
63
- register_command ( "scenePreview.pin " , this . pin_preview . bind ( this ) ) ,
64
- register_command ( "scenePreview.unpin " , this . unpin_preview . bind ( this ) ) ,
65
+ register_command ( "scenePreview.lock " , this . lock_preview . bind ( this ) ) ,
66
+ register_command ( "scenePreview.unlock " , this . unlock_preview . bind ( this ) ) ,
65
67
register_command ( "scenePreview.copyNodePath" , this . copy_node_path . bind ( this ) ) ,
66
68
register_command ( "scenePreview.copyResourcePath" , this . copy_resource_path . bind ( this ) ) ,
67
69
register_command ( "scenePreview.openScene" , this . open_scene . bind ( this ) ) ,
68
70
register_command ( "scenePreview.openScript" , this . open_script . bind ( this ) ) ,
71
+ register_command ( "scenePreview.openCurrentScene" , this . open_current_scene . bind ( this ) ) ,
72
+ register_command ( "scenePreview.openCurrentScript" , this . open_main_script . bind ( this ) ) ,
69
73
register_command ( "scenePreview.goToDefinition" , this . go_to_definition . bind ( this ) ) ,
70
74
register_command ( "scenePreview.openDocumentation" , this . open_documentation . bind ( this ) ) ,
71
75
register_command ( "scenePreview.refresh" , this . refresh . bind ( this ) ) ,
@@ -82,12 +86,21 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
82
86
this . refresh ( ) ;
83
87
}
84
88
85
- public handleDrag ( source : readonly SceneNode [ ] , data : vscode . DataTransfer , token : vscode . CancellationToken ) : void | Thenable < void > {
89
+ public handleDrag (
90
+ source : readonly SceneNode [ ] ,
91
+ data : vscode . DataTransfer ,
92
+ token : vscode . CancellationToken ,
93
+ ) : void | Thenable < void > {
86
94
data . set ( "godot/path" , new vscode . DataTransferItem ( source [ 0 ] . relativePath ) ) ;
87
95
data . set ( "godot/class" , new vscode . DataTransferItem ( source [ 0 ] . className ) ) ;
88
96
}
89
97
90
- public provideDocumentDropEdits ( document : vscode . TextDocument , position : vscode . Position , dataTransfer : vscode . DataTransfer , token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . DocumentDropEdit > {
98
+ public provideDocumentDropEdits (
99
+ document : vscode . TextDocument ,
100
+ position : vscode . Position ,
101
+ dataTransfer : vscode . DataTransfer ,
102
+ token : vscode . CancellationToken ,
103
+ ) : vscode . ProviderResult < vscode . DocumentDropEdit > {
91
104
const path = dataTransfer . get ( "godot/path" ) . value ;
92
105
const className = dataTransfer . get ( "godot/class" ) . value ;
93
106
@@ -106,7 +119,7 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
106
119
return ;
107
120
}
108
121
setTimeout ( async ( ) => {
109
- if ( uri . fsPath == this . currentScene ) {
122
+ if ( uri . fsPath === this . currentScene ) {
110
123
this . refresh ( ) ;
111
124
} else {
112
125
const document = await vscode . workspace . openTextDocument ( uri ) ;
@@ -116,7 +129,7 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
116
129
}
117
130
118
131
public async refresh ( ) {
119
- if ( this . scenePreviewPinned ) {
132
+ if ( this . scenePreviewLocked ) {
120
133
return ;
121
134
}
122
135
@@ -128,22 +141,22 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
128
141
if ( ! fileName . endsWith ( ".tscn" ) ) {
129
142
const searchName = fileName . replace ( ".gd" , ".tscn" ) . replace ( ".cs" , ".tscn" ) ;
130
143
131
- if ( mode == "anyFolder" ) {
144
+ if ( mode === "anyFolder" ) {
132
145
const relatedScene = await find_file ( searchName ) ;
133
146
if ( ! relatedScene ) {
134
147
return ;
135
148
}
136
149
fileName = relatedScene . fsPath ;
137
150
}
138
151
139
- if ( mode == "sameFolder" ) {
152
+ if ( mode === "sameFolder" ) {
140
153
if ( fs . existsSync ( searchName ) ) {
141
154
fileName = searchName ;
142
155
} else {
143
156
return ;
144
157
}
145
158
}
146
- if ( mode == "off" ) {
159
+ if ( mode === "off" ) {
147
160
return ;
148
161
}
149
162
}
@@ -162,20 +175,20 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
162
175
}
163
176
}
164
177
165
- private pin_preview ( ) {
166
- this . scenePreviewPinned = true ;
167
- set_context ( "scenePreview.pinned " , true ) ;
178
+ private lock_preview ( ) {
179
+ this . scenePreviewLocked = true ;
180
+ set_context ( "scenePreview.locked " , true ) ;
168
181
}
169
182
170
- private unpin_preview ( ) {
171
- this . scenePreviewPinned = false ;
172
- set_context ( "scenePreview.pinned " , false ) ;
183
+ private unlock_preview ( ) {
184
+ this . scenePreviewLocked = false ;
185
+ set_context ( "scenePreview.locked " , false ) ;
173
186
this . refresh ( ) ;
174
187
}
175
188
176
189
private copy_node_path ( item : SceneNode ) {
177
190
if ( item . unique ) {
178
- vscode . env . clipboard . writeText ( "%" + item . label ) ;
191
+ vscode . env . clipboard . writeText ( `% ${ item . label } ` ) ;
179
192
return ;
180
193
}
181
194
vscode . env . clipboard . writeText ( item . relativePath ) ;
@@ -201,6 +214,26 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
201
214
}
202
215
}
203
216
217
+ private async open_current_scene ( ) {
218
+ if ( this . currentScene ) {
219
+ const document = await vscode . workspace . openTextDocument ( this . currentScene ) ;
220
+ vscode . window . showTextDocument ( document ) ;
221
+ }
222
+ }
223
+
224
+ private async open_main_script ( ) {
225
+ if ( this . currentScene ) {
226
+ const root = this . scene . root ;
227
+ if ( root ?. hasScript ) {
228
+ const path = this . scene . externalResources [ root . scriptId ] . path ;
229
+ const uri = await convert_resource_path_to_uri ( path ) ;
230
+ if ( uri ) {
231
+ vscode . window . showTextDocument ( uri , { preview : true } ) ;
232
+ }
233
+ }
234
+ }
235
+ }
236
+
204
237
private async go_to_definition ( item : SceneNode ) {
205
238
const document = await vscode . workspace . openTextDocument ( this . currentScene ) ;
206
239
const start = document . positionAt ( item . position ) ;
@@ -216,7 +249,6 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
216
249
private tree_selection_changed ( event : vscode . TreeViewSelectionChangeEvent < SceneNode > ) {
217
250
// const item = event.selection[0];
218
251
// log(item.body);
219
-
220
252
// const editor = vscode.window.activeTextEditor;
221
253
// const range = editor.document.getText()
222
254
// editor.revealRange(range)
@@ -226,12 +258,10 @@ export class ScenePreviewProvider implements TreeDataProvider<SceneNode>, TreeDr
226
258
if ( ! element ) {
227
259
if ( ! this . scene ?. root ) {
228
260
return Promise . resolve ( [ ] ) ;
229
- } else {
230
- return Promise . resolve ( [ this . scene ?. root ] ) ;
231
261
}
232
- } else {
233
- return Promise . resolve ( element . children ) ;
262
+ return Promise . resolve ( [ this . scene ?. root ] ) ;
234
263
}
264
+ return Promise . resolve ( element . children ) ;
235
265
}
236
266
237
267
public getTreeItem ( element : SceneNode ) : TreeItem | Thenable < TreeItem > {
@@ -254,13 +284,13 @@ class UniqueDecorationProvider implements vscode.FileDecorationProvider {
254
284
return this . changeDecorationsEvent . event ;
255
285
}
256
286
257
- constructor ( private previewer : ScenePreviewProvider ) { }
287
+ constructor ( private previewer : ScenePreviewProvider ) { }
258
288
259
289
provideFileDecoration ( uri : Uri , token : CancellationToken ) : FileDecoration | undefined {
260
290
if ( uri . scheme !== "godot" ) return undefined ;
261
291
262
292
const node = this . previewer . scene ?. nodes . get ( uri . path ) ;
263
- if ( node && node . unique ) {
293
+ if ( node ? .unique ) {
264
294
return {
265
295
badge : "%" ,
266
296
} ;
@@ -274,13 +304,13 @@ class ScriptDecorationProvider implements vscode.FileDecorationProvider {
274
304
return this . changeDecorationsEvent . event ;
275
305
}
276
306
277
- constructor ( private previewer : ScenePreviewProvider ) { }
307
+ constructor ( private previewer : ScenePreviewProvider ) { }
278
308
279
309
provideFileDecoration ( uri : Uri , token : CancellationToken ) : FileDecoration | undefined {
280
310
if ( uri . scheme !== "godot" ) return undefined ;
281
311
282
312
const node = this . previewer . scene ?. nodes . get ( uri . path ) ;
283
- if ( node && node . hasScript ) {
313
+ if ( node ? .hasScript ) {
284
314
return {
285
315
badge : "S" ,
286
316
} ;
0 commit comments