10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Xcode 関数定義からヘッダファイルの関数宣言を生成するプラグインを作ってみた

Last updated at Posted at 2014-11-01

作成したプラグインの紹介と、プラグイン作成にあたり調べた事についてです。

作成したプラグインについて

概要

ソースコード中の関数定義からヘッダーファイルに関数宣言を追加します。

rakufun.gif

インストール

githubからソース一式をダウンロードして、コンパイル->xcode再起動でインストール完了です。

ショートカットキーがCTRL+mに割りついているので、RakufunPlugin.mのDEFAULT_KEYを必要に応じて変更しください。

使い方

// A.m
-(void)foo { // <- ここでCTRL+mを実行
...          // <- ここでも実行してもOK
}

// A.h
@interface A

-(void)foo; // <- これを自動生成!
            // 既に存在する場合は生成しません。(パラメータ名が不一致する場合などは生成します。)
@end

※現状では関数外から実行すると、カーソル位置から探索して、一番最初にマッチしたシグネチャに対して自動生成してしまいます。

課題

  • 構文解析が貧弱なのでできれば強化したい。
  • 複数の関数を処理できるようにしたい。
  • ヘッダーファイルから定義を生成したい。
  • 関数外から実行した場合は処理をしないようにする。

実装編

プラグイン作成にあたって調べた事などです。

プラグインを作る準備

ざっとこのあたりには目を通します。

注意する点としては、
Targets->Build Settings->Deployment->Skip InstallをNoにする必要があります。(そうしないとDeploy先にプラグインがインストールされない)

正直なところ実際に作るには情報が足らず大変でしたが、この情報がなければスタートもできませんでした。記事を書いてくださった方々に感謝です。

調べ方

基本的にやりたい事に近いプラグインを見つけて、コードを調べる、という作業になりました。

今回の場合、参考となったプラグインは

です。

Xcode.app内のFrameworkについて

Xcode上のエディタの情報を参照するために、Xcode.app内のライブラリをリンクする必要があります。

ソースコードから情報を取得するにあたり

  • IDEKit
  • DVTKit
  • IDEFoundation
  • IDESourceEditor(これはリンクできないっぽいので不要かも)

をリンクしています。

Frameworkのヘッダーファイルについて

参考にしたプラグインを見ると、上記のFramework用に独自ヘッダーが定義されてたりして「なんだろうな〜」と思っていたのですが、class-dumpというツールでFrameworkからヘッダーファイルを生成しているようです。

※但し、このファイルはとんでもない行数があり、ファイルを開くとXcodeが非常に重くなるので、実際にはプラグインで利用するクラス/メソッド/プロパティのみを取り出して、独自にヘッダーファイルを定義しています。

// Rakufun/Xcode/XcodeComponents.hの抜粋
// Rakufunプラグインで必要な定義のみ行っている。
@interface IDEEditor : NSObject

@property(readonly) IDESourceCodeDocument *sourceCodeDocument;

@end

@class IDESourceCodeEditor;

@interface IDESourceCodeEditor : IDEEditor
@property (retain) NSTextView *textView;
- (IDESourceCodeDocument *)sourceCodeDocument;
@end

〜の仕方

ここからはHow toです。

プラグインの初期化の仕方

いくつかのプラグインを参考にすると、だいたい次のように初期化されているようです。(VVDocumenterを参考)

+(void)pluginDidLoad:(NSBundle*)plugin {
    DbgLog(@"load Rakufun!");
    [self sharedInstance];
}

+(instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidFinishLaunching:)
                                                     name:NSApplicationDidFinishLaunchingNotification
                                                   object:nil];
    }
    return self;
}

-(void)applicationDidFinishLaunching:(NSNotification*)noti {
    // ここで初期化!!
    [self initMenu];
}

現在のエディタの取得の仕方

BBUncrustifyPluginを参考

// 現在のソースコードエディターを返す
+(IDESourceCodeEditor*)currentEditor {
    NSWindowController *currentWindowController = [[NSApp keyWindow] windowController];
    if ([currentWindowController isKindOfClass:NSClassFromString(@"IDEWorkspaceWindowController")]) {
        IDEWorkspaceWindowController *workspaceController = (IDEWorkspaceWindowController *)currentWindowController;
        IDEEditorArea *editorArea = [workspaceController editorArea];
        IDEEditorContext *editorContext = [editorArea lastActiveEditorContext];
        IDEEditor* editor = (IDEEditor*)[editorContext editor];
        
        if( [editor isKindOfClass:NSClassFromString(@"IDESourceCodeEditor")] ) {
            DbgLog(@"(get source code editor");
            return (IDESourceCodeEditor*)editor;
        }
    }
    return nil;
}

+(IDESourceCodeDocument*)currentDocument {
    IDESourceCodeEditor* editor = [self currentEditor];
    if( editor ) {
        return editor.sourceCodeDocument;
    }
    return nil;
}

+ (NSTextView *)currentSourceCodeView {
    IDESourceCodeEditor* editor = [self currentEditor];
    if( editor ) {
        return editor.textView;
    }
    return nil;
}

当初、IDESourceCodeEditorのカテゴリで実現しようとしたのですが、リンクエラーになって実現できませんでした。カテゴリを作る場合は、”コンパイル時に”シンボルが解決できる必要があるようです。

現在のソースコード(文字列)の取得の仕方

NSTextView* headerView = [XcodeHelper currentSourceCodeView];
NSString* headerText = headerView.textStorage.string;

ヘッダーファイルとソースファイルを切り替えるには

xvimを参考

[NSApp sendAction:@selector(jumpToPreviousCounterpart:) to:nil from:self];

Xcodeへ送信できるactionは?

IDEKit.h内のIDEEditorContextクラスやIDESourceCodeEditor.h内のIDESourceCodeEditorクラスなどを参考にする。


以上です。プラグインを作る際の参考になれば幸いです。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?