Created
December 10, 2020 20:03
-
-
Save mao-test-h/3d131b6f0be03eb729fbc07a6d27c3bb to your computer and use it in GitHub Desktop.
Xcode Source Editor Extensionsを用いてprintメソッドの内容を全てもなふわすい〜とる〜むのURLに変更する為のEditor拡張
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import XcodeKit | |
// このGistでは実装のコア部分である`SourceEditorCommand.swift`のみアップ (実装は適当) | |
// Xcode Source Editor Extensionsの導入に関しては以下を参照 | |
// | |
// - Creating a Source Editor Extension | |
// https://developer.apple.com/documentation/xcodekit/creating_a_source_editor_extension | |
// | |
// - Xcode Source Editor Extension を使った Xcode プラグインの作り方 | |
// https://dev.classmethod.jp/articles/xcode-source-editor-extension-how-to-create/ | |
// | |
// - Xcode Source Editor Extensionの世界(完全版) / 20170916 #iosdc | |
// https://speakerdeck.com/takasek/20170916-number-iosdc | |
// NOTE: Xcode12.2だと`Frameworks and Libraries`から明示的に「XcodeKit.framework」を指定しないとエラーが発生した..? | |
/// printメソッドの内容を全てもなふわすい〜とる〜むのURLに変更 | |
class SourceEditorCommand: NSObject, XCSourceEditorCommand { | |
// NOTE: もなふわすい〜とる〜む | |
let url = "https://www.showroom-live.com/monaka-007" | |
func perform( | |
with invocation: XCSourceEditorCommandInvocation, | |
completionHandler: @escaping (Error?) -> Void) -> Void { | |
let buffer = invocation.buffer | |
// 行を頭から検索して置き換え処理を行なう | |
var index = 0 | |
for line in buffer.lines { | |
// 現在の行に対してメッチャ雑に判定 | |
let current = buffer.lines[index] as! String | |
guard current.contains("print(") else { | |
index += 1; | |
continue | |
} | |
// インデントの計算 (スマートな書き方が分からないので適当) | |
var count = 0 | |
for char in current { | |
if char == " " { | |
count += 1 | |
} else { | |
break | |
} | |
} | |
// 頭に数えたインデントを足して中身をもなふわすい〜と変更 | |
let indentSpace = String(repeating: " ", count: count) | |
buffer.lines[index] = "\(indentSpace)print(\"\(url)\")" | |
index += 1; | |
} | |
completionHandler(nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment