Xcode插件 - 访问打开文件的主textView

时间:2014-04-22 20:47:18

标签: xcode macos

我想构建一个Xcode插件,在打开的文件中标记一些文本(主要是.h和.m文件)。

我正在尝试引用主要的textView,

任何方向?

1 个答案:

答案 0 :(得分:1)

我假设你确实有插件本身在运行,只需要主编辑器

+ (id)currentEditor {
    NSWindowController *currentWindowController = [[NSApp keyWindow] windowController];
    if ([currentWindowController isKindOfClass:NSClassFromString(@"IDEWorkspaceWindowController")]) {
        id workspaceController = (IDEWorkspaceWindowController *)currentWindowController;
        id editorArea = [workspaceController editorArea];
        id editorContext = [editorArea lastActiveEditorContext];
        return [editorContext editor];
    }
    return nil;
}

+ (NSTextView *)currentSourceCodeTextView {
    if ([[self currentEditor] isKindOfClass:NSClassFromString(@"IDESourceCodeEditor")]) {
        id editor = [XCFXcodeFormatter currentEditor];
        return [editor textView];
    }

    if ([[self currentEditor] isKindOfClass:NSClassFromString(@"IDESourceCodeComparisonEditor")]) {
        id editor = [XCFXcodeFormatter currentEditor];
        return [editor keyTextView];
    }

    return nil;
}