Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Created December 10, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/3d131b6f0be03eb729fbc07a6d27c3bb to your computer and use it in GitHub Desktop.
Save mao-test-h/3d131b6f0be03eb729fbc07a6d27c3bb to your computer and use it in GitHub Desktop.
Xcode Source Editor Extensionsを用いてprintメソッドの内容を全てもなふわすい〜とる〜むのURLに変更する為のEditor拡張
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