- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathdebug-configuration-manager.ts
156 lines (150 loc) · 5.98 KB
/
debug-configuration-manager.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{FrontendApplicationStateService}from'@theia/core/lib/browser/frontend-application-state';
import{Disposable}from'@theia/core/lib/common/disposable';
import{Emitter,Event}from'@theia/core/lib/common/event';
importURIfrom'@theia/core/lib/common/uri';
import{inject,injectable}from'@theia/core/shared/inversify';
import{DebugConfigurationManagerasTheiaDebugConfigurationManager}from'@theia/debug/lib/browser/debug-configuration-manager';
import{DebugConfigurationModelasTheiaDebugConfigurationModel}from'@theia/debug/lib/browser/debug-configuration-model';
import{DebugConfiguration}from'@theia/debug/lib/common/debug-common';
import{FileService}from'@theia/filesystem/lib/browser/file-service';
import{
FileOperationError,
FileOperationResult,
}from'@theia/filesystem/lib/common/files';
importdebouncefrom'p-debounce';
import{SketchesService}from'../../../common/protocol';
import{
CurrentSketch,
SketchesServiceClientImpl,
}from'../../sketches-service-client-impl';
import{maybeUpdateReadOnlyState}from'../monaco/monaco-editor-provider';
import{DebugConfigurationModel}from'./debug-configuration-model';
@injectable()
exportclassDebugConfigurationManagerextendsTheiaDebugConfigurationManager{
@inject(SketchesService)
privatereadonlysketchesService: SketchesService;
@inject(SketchesServiceClientImpl)
privatereadonlysketchesServiceClient: SketchesServiceClientImpl;
@inject(FrontendApplicationStateService)
privatereadonlyappStateService: FrontendApplicationStateService;
@inject(FileService)
privatereadonlyfileService: FileService;
privateonTempContentDidChangeEmitter=
newEmitter<TheiaDebugConfigurationModel.JsonContent>();
getonTempContentDidChange(): Event<TheiaDebugConfigurationModel.JsonContent>{
returnthis.onTempContentDidChangeEmitter.event;
}
protectedoverrideasyncdoInit(): Promise<void>{
this.watchLaunchConfigEditor();
this.appStateService.reachedState('ready').then(async()=>{
consttempContent=awaitthis.getTempLaunchJsonContent();
if(!tempContent){
// No active sketch.
return;
}
// Watch the file of the container folder.
this.fileService.watch(
tempContentinstanceofURI ? tempContent : tempContent.uri
);
// Use the normalized temp folder name. We cannot compare Theia URIs here.
// /var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/arduino-ide2-a0337d47f86b24a51df3dbcf2cc17925/launch.json
// /private/var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/arduino-ide2-A0337D47F86B24A51DF3DBCF2CC17925/launch.json
consttempFolderName=(
tempContentinstanceofURI ? tempContent : tempContent.uri.parent
).path.base.toLowerCase();
this.fileService.onDidFilesChange((event)=>{
for(const{ resource }ofevent.changes){
if(
resource.path.base==='launch.json'&&
resource.parent.path.base.toLowerCase()===tempFolderName
){
this.getTempLaunchJsonContent().then((config)=>{
if(config&&!(configinstanceofURI)){
this.onTempContentDidChangeEmitter.fire(config);
}
});
break;
}
}
});
this.updateModels();
});
returnsuper.doInit();
}
/**
* Sets a listener on current sketch change, and maybe updates the readonly state of the editor showing the debug configuration. aka the `launch.json`.
*/
privatewatchLaunchConfigEditor(): Disposable{
returnthis.sketchesServiceClient.onCurrentSketchDidChange(()=>{
for(constwidgetofthis.editorManager.all){
maybeUpdateReadOnlyState(widget,(uri)=>
this.sketchesServiceClient.isReadOnly(uri)
);
}
});
}
protectedoverrideupdateModels=debounce(async()=>{
awaitthis.appStateService.reachedState('ready');
constroots=awaitthis.workspaceService.roots;
consttoDelete=newSet(this.models.keys());
for(constrootStatofroots){
constkey=rootStat.resource.toString();
toDelete.delete(key);
if(!this.models.has(key)){
consttempContent=awaitthis.getTempLaunchJsonContent();
if(!tempContent){
continue;
}
constconfigurations: DebugConfiguration[]=
tempContentinstanceofURI ? [] : tempContent.configurations;
consturi=tempContentinstanceofURI ? undefined : tempContent.uri;
constmodel=newDebugConfigurationModel(
key,
this.preferences,
configurations,
uri,
this.onTempContentDidChange
);
model.onDidChange(()=>this.updateCurrent());
model.onDispose(()=>this.models.delete(key));
this.models.set(key,model);
}
}
for(consturioftoDelete){
constmodel=this.models.get(uri);
if(model){
model.dispose();
}
}
this.updateCurrent();
},500);
privateasyncgetTempLaunchJsonContent(): Promise<
(TheiaDebugConfigurationModel.JsonContent&{uri: URI})|URI|undefined
>{
constsketch=awaitthis.sketchesServiceClient.currentSketch();
if(!CurrentSketch.isValid(sketch)){
returnundefined;
}
consturi=awaitthis.sketchesService.getIdeTempFolderUri(sketch);
consttempFolderUri=newURI(uri);
awaitthis.fileService.createFolder(tempFolderUri);
try{
consturi=tempFolderUri.resolve('launch.json');
const{ value }=awaitthis.fileService.read(uri);
constconfigurations=DebugConfigurationModel.parse(JSON.parse(value));
return{ uri, configurations,compounds: []};
}catch(err){
if(
errinstanceofFileOperationError&&
err.fileOperationResult===FileOperationResult.FILE_NOT_FOUND
){
returntempFolderUri;
}
console.error(
'Could not load debug configuration from IDE2 temp folder.',
err
);
throwerr;
}
}
}