-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSTDocument.m
More file actions
240 lines (194 loc) · 6.9 KB
/
Copy pathSTDocument.m
File metadata and controls
240 lines (194 loc) · 6.9 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// Document.m
// Smalltalk
//
// Created by Marcel Weiher on 31.03.19.
//
#import "STDocument.h"
#import "STProgramTextView.h"
#import <ObjectiveSmalltalk/ObjectiveSmalltalk.h>
@interface STDocument(st)
-(IBAction)showWorkspace:(id)sender;
@end
@interface STDocument ()
@property (nonatomic, strong) NSMutableSet *workspaces;
@end
@implementation STDocument
- (nullable instancetype)initWithType:(NSString *)typeName error:(NSError **)outError;
{
self=[super initWithType:typeName error:outError];
[self showWorkspace:nil];
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
self.workspaces = [NSMutableSet set];
}
return self;
}
+ (BOOL)autosavesInPlace {
return NO;
}
-(void)windowWillClose:(NSNotification*)closeNotification
{
NSWindow *windowToClose=closeNotification.object;
for ( NSView *workspace in [self.workspaces allObjects] ) {
if ( workspace.window == windowToClose) {
[self.workspaces removeObject:workspace];
}
}
}
- (NSString *)windowNibName {
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}
-(STProgramTextView*)programTextView
{
return (STProgramTextView*)[[self workspaces] anyObject]; // FIXME: allow only a single text view
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [[[self programTextView] text] asData];
}
-(id <MPWStorage>)workspacesStore
{
return [self.bundle storeForSubDir:@"Workspaces"];
}
-(BOOL)autosavesDrafts
{
return NO;
}
// FIXME: this is hack to work around the fact that STBundles currently only support saving in-place
// but NSDocument expects to safely write somewhere else and the move/copy
// It just writes to the original URL
-(BOOL)writeSafelyToURL:(NSURL *)url ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)op error:(NSError **)outError
{
if ( self.bundle) {
[self.bundle save];
id <MPWStorage> workspaces = [self workspacesStore];
workspaces[@"main.st"] = [[[self programTextView] string] asData];
NSLog(@"did save to %@",url);
return YES;
} else {
return [[[[self programTextView] text] asData] writeToURL:url atomically:YES];
}
}
-(BOOL)validateToolbarItem:(NSToolbarItem *)item
{
// NSLog(@"validate: %@ %@",item.itemIdentifier,item);
if ( [item.itemIdentifier isEqual:LinkDependencies] ) {
return !self.bundle.frameworksLoaded;
} else if ( [item.itemIdentifier isEqual:Compile] ) {
return !self.bundle.sourcesCompiled;
}
return YES;
}
static NSString *LinkDependencies = @"LinkDependencies";
static NSString *Compile = @"Compile";
static NSString *Browse = @"Browse";
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar {
// NSLog(@"toolbarAllowedItemIdentifiers: %@",toolbar);
return @[
NSToolbarToggleInspectorItemIdentifier,
NSToolbarShowColorsItemIdentifier,
Compile, LinkDependencies, Browse,
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarSpaceItemIdentifier,
NSToolbarSeparatorItemIdentifier,
];
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
{
// NSLog(@"toolbarDefaultItemIdentifiers %@",toolbar);
NSArray *items = @[
Compile, LinkDependencies, Browse,
];
// NSLog(@"toolbarDefaultItemIdentifiers items: %@",items);
return items;
}
- (void) toolbarWillAddItem:(NSNotification *) notification{
// NSLog(@"will add item: %@",notification.object);
}
- (BOOL) toolbar:(NSToolbar *) toolbar
itemIdentifier:(NSToolbarItemIdentifier) itemIdentifier
canBeInsertedAtIndex:(NSInteger) index
{
// NSLog(@"canBeInserted: %@",itemIdentifier);
return YES;
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if ([itemIdentifier isEqualTo:Compile]) {
[toolbarItem setLabel:@"Compile"];
// [toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setToolTip:@"Compile all code"];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(compileAllSourceFiles)];
} else if ([itemIdentifier isEqualTo:LinkDependencies]) {
[toolbarItem setLabel:@"Link"];
// [toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setToolTip:@"Link all dependenies"];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(linkDependencies)];
} else if ([itemIdentifier isEqualTo:Browse]) {
[toolbarItem setLabel:@"Browse"];
// [toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setToolTip:@"Browse source code"];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(openClassBrowser:)];
}
NSLog(@"toolbar item: %@",toolbarItem);
return [toolbarItem autorelease];
}
-(void)compileAllSourceFiles
{
[self.bundle compileAllSourceFiles];
}
-(void)linkDependencies
{
[self.bundle loadFrameworks];
}
-(void)configureToolbar
{
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"main"] autorelease];
toolbar.delegate = self;
[[[[self windowControllers] firstObject] window] setToolbar:toolbar];
NSLog(@"toolbar: %@",toolbar);
}
- (BOOL)readBundle:(NSString *)path ofType:(NSString *)typeName error:(NSError **)outError
{
self.bundle = [STBundle bundleWithPath:path];
STCompiler* compiler = [[[NSApplication sharedApplication] delegate] compiler];
[compiler evaluateScriptString:@"scheme:builder setPrefixes: [ 'MPW' , 'ST']. "];
[self.bundle setInterpreter:compiler];
[compiler bindValue:self.bundle toVariableNamed:@"bundle"];
NSLog(@"path: %@",path);
NSLog(@"bundle: %@",self.bundle);
[self showWorkspace:nil];
[self configureToolbar];
id <MPWStorage> workspaces = [self workspacesStore];
[[self programTextView] setString:[workspaces[@"main.st"] stringValue]];
return YES;
}
-(IBAction)openClassBrowser:sender
{
[[self.bundle classBrowser] openInWindow:@"Class Browser"];
}
-(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable *)outError
{
if ( [typeName isEqualToString:@"Software IC"]) {
return [self readBundle:[url path] ofType:typeName error:outError];
} else {
return [self readFromData:[NSData dataWithContentsOfURL:url] ofType:typeName error:outError];
}
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
[self showWorkspace:nil];
[[self programTextView] setString:[data stringValue]];
return YES;
}
@end