1. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
朝木卓見
Qt Creatorを拡張する
" C++ Editor: リファクタリング機能編
2. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
Qt Creatorのアーキテクチャ
• プラグインアーキテクチャ"
– 固定機能は最小限"
– プラグインの追加で拡張可能
2
3. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
リファクタリング機能
• エディタの文脈を考慮した簡易編集機能"
– http://qt-project.org/doc/qtcreator-3.2/creator-
editor-refactoring.html"
– 使いこなすことでより楽にコーディング可能"
– ex."
• メソッドの宣言に対する定義の作成"
• 選択範囲のコードから関数を作成"
• 基底クラスの仮想関数のオーバーライドの作成"
• etc.
3
4. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
エディタに機能を追加する
• TextEditorのアシスト用クラス"
– TextEditor::IAssistProcessor"
• http://doc.qt.digia.com/qtcreator-extending/texteditor-
iassistprocessor.html"
• Acts as an interface that actually computes an assist proposal"
• アシストの実行用クラス"
– TextEditor::IAssistProvider"
• http://doc.qt.digia.com/qtcreator-extending/texteditor-
iassistprovider.html"
• Acts as an interface for providing code assist"
• IAssistProcessor生成用クラス"
• 今回はC++特化のため上記を直接扱わない
4
5. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
C++エディタのリファクタリング機能
• cppeditorプラグイン"
– src/plugins/cppeditor/"
• C++エディタ専用の以下のクラスを利用"
– CppQuickFixOperation"
• リファクタリングを実行する"
– CppQuickFixFactory"
• コードモデルからリファクタリング用に
CppQuickFixOperationのインスタンスを作成する
5
6. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
実装する機能
• 文字列リテラルのエスケープ/アンエスケープ"
– ex."
• “あい” “xe3x81x82xe3x81x84”"
"
• 詳細は"
– https://codereview.qt-project.org/90103"
– Qt Creator 3.3 から利用可能"
– スライドは一部省略
6
7. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
CppQuickFixFactory(1)
• cppquickfixes.hに宣言を追加"
"
"
"
– interface: コードモデルへのインターフェース"
• コードの構造やカーソル位置の情報など"
– result: リファクタリング用インスタンスのリスト"
• 実行用クラスのインスタンスをここに登録する
7
class EscapeStringLiteral : public CppQuickFixFactory"
{"
public:"
void match(const CppQuickFixInterface &interface,"
TextEditor::QuickFixOperations &result); // virtual"
}
14. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
CppQuickFixOperation(4)
• cppquickfixes.cppに定義を追加
14
" // エスケープ処理"
" QByteArray newContents;"
" for (int i = 0; i < oldContents.length(); ++i) {"
" " quint8 c = oldContents.at(i);"
" " if (isascii(c) && isprint(c))"
" " " newContents += c;"
" " else {"
" " " newContents += QByteArray("x");"
" " " QByteArray buf;"
" " " buf.setNum(c, 16);"
" " " newContents += buf;"
" " }"
" }
15. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
CppQuickFixOperation(5)
• cppquickfixes.cppに定義を追加
15
" // エスケープの変更を反映"
" if (oldContents != newContents) {"
" " ChangeSet changes;"
" " changes.replace(startPos + 1, endPos - 1,"
" " " " QString::fromUtf8(newContents));"
" " currentFile->setChangeSet(changes);"
" " currentFile->apply();"
" }"
}
16. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
CppEditor::Internal::registerQuickFixes()
• EscapeStringLiteralを登録
16
void CppEditor::Internal::registerQuickFixes(ExtensionSystem::IPlugin *plugin)"
{"
" :"
" :"
"
" plugIn->addAutoReleasedObject(new EscapeStringLiteral);"
}
17. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
プラグイン化
• Qt Creator自体を変更せずに機能を拡張"
– Qt Creator プラグインプロジェクト"
• Qt Creatorの「ライブラリ」プロジェクト内"
– Doc: Creating Plugins"
• http://doc.qt.digia.com/qtcreator-extending/creating-
plugins.html"
– 依存関係の設定"
– Core::EditorManager"
• http://doc.qt.digia.com/qtcreator-extending/core-
editormanager.html"
• 今回は行わず
17
18. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
注意点
• ASTからのコード取得: char *"
– UTF-8(ファイルのエンコーディングとは無関係)"
• ChangeSetに設定するのはQString
18
19. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
まとめ
• Qt Creatorの拡張は難しくない"
– 多数の機能・仕組みが用意済み"
– (十分ではないが)開発者向けドキュメント"
• コードモデル"
– 比較的簡単にコードの構造を活用可能
19
20. Copyright (c)2014 Software Research Associates, Inc. All Right Reserved
参考文献
• Extending Qt Creator Manual!
– http://doc.qt.digia.com/qtcreator-extending/!
• Extending Qt Creator - Qt Developer Days 2014
Europe!
– http://devdays.kdab.com/wp-content/uploads/2014/11/
tobiashunger-extending-qtc.pdf!
• Qt Creator Plugin Development - Qt Developer Days
2014 Europe!
– http://devdays.kdab.com/wp-content/uploads/2014/11/
tobiashunger-qtc-plugins.pdf
20