diff --git a/AVRsack/ASFileTree.swift b/AVRsack/ASFileTree.swift index 74cf000..c22ea56 100644 --- a/AVRsack/ASFileTree.swift +++ b/AVRsack/ASFileTree.swift @@ -163,6 +163,13 @@ class ASFileGroup : ASFileNode { class ASProject : ASFileGroup { override private var kNodeType : String { return kNodeTypeProject } + override init(name: String = "") { + super.init(name: name) + } + override init(_ prop: NSDictionary, withRootURL rootURL: NSURL) { + super.init(prop, withRootURL:rootURL) + name = rootURL.lastPathComponent! + } override func nodeName() -> String { return "📘 "+name } @@ -187,6 +194,18 @@ class ASFileItem : ASFileNode { } else { url = NSURL(fileURLWithPath:(prop[kPathKey] as! String))!.URLByStandardizingPath! } + if !url.checkResourceIsReachableAndReturnError(nil) { + // + // When projects get moved, .ino files get renamed but that fact is not + // yet reflected in the project file. + // + let urlDir = url.URLByDeletingLastPathComponent + let newName = rootURL.lastPathComponent!.stringByAppendingPathExtension(url.pathExtension!)! + let altURL = urlDir?.URLByAppendingPathComponent(newName) + if altURL != nil && altURL!.checkResourceIsReachableAndReturnError(nil) { + url = altURL! + } + } super.init(name:url.lastPathComponent!) } override func nodeName() -> String { diff --git a/AVRsack/ASProjDoc.swift b/AVRsack/ASProjDoc.swift index a526c26..795018b 100644 --- a/AVRsack/ASProjDoc.swift +++ b/AVRsack/ASProjDoc.swift @@ -189,6 +189,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa success = writeToURL(projectURL, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil, error: outError) } } else { + fileURL = url success = super.readFromURL(url, ofType: typeName, error: outError) } return success @@ -220,7 +221,12 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa return true } - + + override func duplicateDocument(sender: AnyObject?) { + let app = NSApplication.sharedApplication().delegate as! ASApplication + app.openTemplate(fileURL!.URLByDeletingLastPathComponent!) + } + func updateLog(AnyObject?) { if let logNode = mainEditor as? ASLogNode { let url = fileURL?.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) @@ -238,8 +244,8 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa if (modified as! NSDate).compare(logModified) == .OrderedDescending || (size as! Int) != logSize { var enc : UInt = 0 - let newText = NSString(contentsOfURL:url!, usedEncoding:&enc, error:nil) as! String - editor.setString(newText) + let newText = NSString(contentsOfURL:url!, usedEncoding:&enc, error:nil) + editor.setString((newText as? String) ?? "") editor.gotoLine(1000000000, column: 0, animated: true) logModified = modified as! NSDate logSize = size as! Int