Edit files, parse project folders

This commit is contained in:
Matthias Neeracher 2014-11-16 19:39:32 +01:00 committed by Matthias Neeracher
parent dd506616b3
commit 2c59b97dd9
6 changed files with 198 additions and 44 deletions

View File

@ -13,6 +13,7 @@
9501D8091A17025C0034C530 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9501D8081A17025C0034C530 /* Images.xcassets */; };
9501D80C1A17025C0034C530 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9501D80A1A17025C0034C530 /* MainMenu.xib */; };
9501D8181A17025C0034C530 /* AVRsackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9501D8171A17025C0034C530 /* AVRsackTests.swift */; };
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BF80EA1A185C9E0004A693 /* ASFileTree.swift */; };
95EA32621A17B90600F66EB0 /* ACEView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA325B1A17B8DA00F66EB0 /* ACEView.framework */; };
95EA32651A17BA8C00F66EB0 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA32631A17BA8C00F66EB0 /* Cocoa.framework */; };
/* End PBXBuildFile section */
@ -67,6 +68,7 @@
9501D8161A17025C0034C530 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9501D8171A17025C0034C530 /* AVRsackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVRsackTests.swift; sourceTree = "<group>"; };
958D76811A17DA1C00917D96 /* AVRsack-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AVRsack-Bridging-Header.h"; sourceTree = "<group>"; };
95BF80EA1A185C9E0004A693 /* ASFileTree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASFileTree.swift; sourceTree = "<group>"; };
95EA32541A17B8DA00F66EB0 /* ACEView.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ACEView.xcodeproj; path = ../ACEView/ACEView.xcodeproj; sourceTree = "<group>"; };
95EA32631A17BA8C00F66EB0 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
95EA32641A17BA8C00F66EB0 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
@ -117,9 +119,8 @@
958D76811A17DA1C00917D96 /* AVRsack-Bridging-Header.h */,
9501D8011A17025C0034C530 /* ASApplication.swift */,
9501D8031A17025C0034C530 /* ASProjDoc.swift */,
9501D8051A17025C0034C530 /* ASProjDoc.xib */,
9501D8081A17025C0034C530 /* Images.xcassets */,
9501D80A1A17025C0034C530 /* MainMenu.xib */,
95BF80EA1A185C9E0004A693 /* ASFileTree.swift */,
95BF80EC1A185CD90004A693 /* Resources */,
9501D7FF1A17025C0034C530 /* Supporting Files */,
95EA32671A17BAA600F66EB0 /* Frameworks */,
);
@ -151,6 +152,16 @@
name = "Supporting Files";
sourceTree = "<group>";
};
95BF80EC1A185CD90004A693 /* Resources */ = {
isa = PBXGroup;
children = (
9501D8081A17025C0034C530 /* Images.xcassets */,
9501D80A1A17025C0034C530 /* MainMenu.xib */,
9501D8051A17025C0034C530 /* ASProjDoc.xib */,
);
name = Resources;
sourceTree = "<group>";
};
95EA32551A17B8DA00F66EB0 /* Products */ = {
isa = PBXGroup;
children = (
@ -303,6 +314,7 @@
files = (
9501D8041A17025C0034C530 /* ASProjDoc.swift in Sources */,
9501D8021A17025C0034C530 /* ASApplication.swift in Sources */,
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -10,9 +10,6 @@ import Cocoa
@NSApplicationMain
class ASApplication: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}
@ -20,7 +17,5 @@ class ASApplication: NSObject, NSApplicationDelegate {
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}

115
AVRsack/ASFileTree.swift Normal file
View File

@ -0,0 +1,115 @@
//
// ASFileTree.swift
// AVRsack
//
// Created by Matthias Neeracher on 11/16/14.
// Copyright (c) 2014 Aere Perennius. All rights reserved.
//
import Foundation
enum ASFileType : String {
case Unknown = ""
case Header = "source.h"
case CFile = "source.c"
case Arduino = "source.ino"
case CppFile = "source.c++"
case AsmFile = "source.asm"
case Markdown = "doc.md"
static func guessForURL(url: NSURL) -> ASFileType {
switch url.pathExtension.lowercaseString {
case "hpp", "hh", "h":
return .Header
case "c":
return .CFile
case "ino":
return .Arduino
case "cpp", "c++", "cxx", "cc":
return .CppFile
case "s":
return .AsmFile
case "md":
return .Markdown
default:
return .Unknown
}
}
var aceMode : ACEMode {
switch self {
case .Header,.CFile,.CppFile,.Arduino:
return ACEModeCPP
case .Markdown:
return ACEModeMarkdown
default:
return ACEModeASCIIDoc
}
}
}
class ASFileNode {
func nodeName() -> String {
return ""
}
}
class ASFileGroup : ASFileNode {
var name : String
var children : [ASFileNode]
var expanded : Bool
init(name: String) {
self.name = name
self.children = []
self.expanded = true
}
convenience override init() {
self.init(name: "")
}
override func nodeName() -> String {
return (expanded ? "📂" : "📁")+" "+name
}
}
class ASFileItem : ASFileNode {
var url : NSURL
var type : ASFileType
init(url: NSURL, type: ASFileType) {
self.url = url
self.type = type
}
override func nodeName() -> String {
return "📄 "+url.lastPathComponent
}
}
class ASFileTree : NSObject, NSOutlineViewDataSource {
let root = ASFileGroup()
func addFileURL(url: NSURL, omitUnknown: Bool = true) {
let type = ASFileType.guessForURL(url)
if !omitUnknown || type != .Unknown {
root.children.append(ASFileItem(url: url, type: type))
}
}
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
if item == nil {
return root.children.count
} else {
return (item as ASFileGroup).children.count
}
}
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
let group = (item == nil) ? root : (item as ASFileGroup)
return group.children[index]
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return item is ASFileGroup
}
func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
return (item as ASFileItem).nodeName()
}
}

View File

@ -8,9 +8,11 @@
import Cocoa
class ASProjDoc: NSDocument {
class ASProjDoc: NSDocument, NSOutlineViewDelegate {
@IBOutlet weak var editor : ACEView!
@IBOutlet weak var outline: NSOutlineView!
@IBOutlet weak var outline : NSOutlineView!
let files : ASFileTree = ASFileTree()
var mainEditor : ASFileNode?
override init() {
super.init()
@ -19,9 +21,7 @@ class ASProjDoc: NSDocument {
override func windowControllerDidLoadNib(aController: NSWindowController) {
super.windowControllerDidLoadNib(aController)
editor.setString("Here we go!")
editor.setMode(UInt(ACEModeASCIIDoc))
editor.setTheme(UInt(ACEThemeXcode))
outline.setDataSource(files)
}
override class func autosavesInPlace() -> Bool {
@ -35,20 +35,53 @@ class ASProjDoc: NSDocument {
}
override func dataOfType(typeName: String, error outError: NSErrorPointer) -> NSData? {
// Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
outError.memory = NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
return nil
}
override func readFromData(data: NSData, ofType typeName: String, error outError: NSErrorPointer) -> Bool {
// Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false.
// You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
outError.memory = NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
return false
func importProject(url: NSURL, error outError: NSErrorPointer) -> Bool {
let existingProject = url.URLByAppendingPathComponent(url.lastPathComponent+".avrsackproj")
if existingProject.checkResourceIsReachableAndReturnError(nil) {
fileURL = existingProject
return readFromURL(existingProject, ofType:"Project", error:outError)
}
let filesInProject =
NSFileManager.defaultManager().contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil,
options: .SkipsHiddenFiles, error: nil)!
for file in filesInProject {
files.addFileURL(file as NSURL)
}
return true
}
override func readFromURL(url: NSURL, ofType typeName: String, error outError: NSErrorPointer) -> Bool {
var success : Bool = false
if typeName == "Arduino Source File" {
let projectURL = url.URLByDeletingPathExtension!.URLByAppendingPathExtension("avrsackproj")
fileURL = projectURL
success = importProject(url.URLByDeletingLastPathComponent!, error: outError)
} else {
success = true
}
return success
}
func outlineViewSelectionDidChange(notification: NSNotification) {
let selection = outline.itemAtRow(outline.selectedRow) as ASFileNode?
if selection !== mainEditor {
saveCurEditor()
}
if let file = (selection as? ASFileItem) {
var enc : UInt = 0
editor.setString(NSString(contentsOfURL:file.url, usedEncoding:&enc, error:nil))
editor.setMode(UInt(file.type.aceMode))
}
}
func saveCurEditor() {
if let file = (mainEditor as? ASFileItem) {
editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
}
}
}

View File

@ -34,7 +34,6 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<outlineView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" rowSizeStyle="automatic" viewBased="YES" indentationPerLevel="16" outlineTableColumn="89u-w6-f1G" id="nij-C2-Fna">
<rect key="frame" x="0.0" y="0.0" width="238" height="134"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@ -52,27 +51,11 @@
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="WhD-Fb-1Zu">
<rect key="frame" x="1" y="1" width="179" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3l3-7H-YtI">
<rect key="frame" x="0.0" y="0.0" width="100" height="17"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="VmI-Rd-18R">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<connections>
<outlet property="textField" destination="3l3-7H-YtI" id="Lvp-5L-4Ug"/>
</connections>
</tableCellView>
</prototypeCellViews>
</tableColumn>
</tableColumns>
<connections>
<outlet property="delegate" destination="-2" id="FXh-tV-xFH"/>
</connections>
</outlineView>
</subviews>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>

View File

@ -24,6 +24,22 @@
<key>NSDocumentClass</key>
<string>AVRsack.ASProjDoc</string>
</dict>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>ino</string>
</array>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>text/plain</string>
</array>
<key>CFBundleTypeName</key>
<string>Arduino Source File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>NSDocumentClass</key>
<string>AVRsack.ASProjDoc</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>