如何从 DVTSourceTextView 确定文件 URL

How to determine file URL from DVTSourceTextView

我正在为 Xcode 7 写一个插件。我有 DVTSourceTextView 并且可以很好地操作它。我想找到的一件事是哪个文件与此相关。不幸的是,DVTSourceTextView 似乎没有提供该信息 - 或者如果提供了,它以我看不到的方式被掩埋了。

我确定这很琐碎,我只是遗漏了一些东西。

好的,这比我想象的要容易。我是从一种不同的(尽管几乎是正确的)方式来处理它的。

class func currentEditorView() -> (NSURL?, NSView?) {
    let currentWindowController = NSApp.keyWindow?.windowController
    guard currentWindowController!.className == "IDEWorkspaceWindowController" else { return (nil, nil) }

    let filename = currentWindowController!.valueForKey("editorArea")!.valueForKey("lastActiveEditorContext")!.valueForKey("originalRequestedDocumentURL")

    let editor = currentWindowController!.valueForKey("editorArea")!.valueForKey("lastActiveEditorContext")!.valueForKey("editor")!.valueForKey("textView")

    return (filename as? NSURL, editor as? NSView)
}

这为我提供了作为 NSURL 的文件名以及作为 NSView 的 DVTSourceTextView,而无需包含私有 headers。漂亮。

现在我不仅知道我正在编辑的文件的名称,而且我还可以确定它是 swift、objc、c 还是 c++ 文件!那就是酷!