Xcode 内置片段编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4963034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode built-in snippets edit
提问by Martin Cote
Is there a way to edit Xcode's built-in snippets? There is an edit button, but pressing it doesn't seem to allow changing the snippet's text.
有没有办法编辑 Xcode 的内置片段?有一个编辑按钮,但按下它似乎不允许更改代码段的文本。
Any insight is appreciated.
任何见解表示赞赏。
回答by Matt Wilding
You still can't edit the built-in system snippets. You can, however, edit "user" snippets.
您仍然无法编辑内置系统代码段。但是,您可以编辑“用户”片段。
The simplest solution in my mind was to create copies of all the default snippets, but modify them so that they are "user" snippets and override the default versions. I wrote a little Python script to do the job. It's very simple, and after running it all of Xcode's snippets will be magically editable via the Xcode GUI. No need to go mucking around in the plist by hand:
我认为最简单的解决方案是创建所有默认片段的副本,但修改它们,使它们成为“用户”片段并覆盖默认版本。我写了一个小的 Python 脚本来完成这项工作。它非常简单,在运行它之后,所有 Xcode 的片段都可以通过 Xcode GUI 神奇地进行编辑。无需手动在 plist 中乱搞:
import plistlib
import os.path
# Create user snippet directory if needed.
user_snippet_path = os.path.expanduser("~/Library/Developer/Xcode/UserData/CodeSnippets")
try:
os.makedirs(user_snippet_path)
except OSError, err:
if err.errno != errno.EEXIST or not os.path.isdir(user_snippet_path):
raise
# Important, you'll need to quit and restart Xcode to notice the effects.
# Important, change this if you're on a Developer Preview of Xcode.
system_snippet_path = "/Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets"
print("Reading snippets from " + system_snippet_path)
plist = plistlib.readPlist(system_snippet_path)
for entry in plist:
# Create a new user snippet file with modified
# contents of the original snippet. Ignore paths that
# already contain a user snippet to prevent overwriting
# previously generated snippets.
snippet_id = entry["IDECodeSnippetIdentifier"]
snippet_path = user_snippet_path + "/" + snippet_id + ".codesnippet"
if os.path.exists(snippet_path):
print(snippet_path + " already exitsts: Skipping.")
continue
print("Writing " + snippet_path)
# Marks the snippet as a user snippet. Xcode will
# crash if a user snippet and a system snippet share
# the same identifier.
entry["IDECodeSnippetUserSnippet"] = True
# Given two snippets with the same identifier,
# Xcode will only show the snippet with the higher
# "version number". This effectively hides the
# default version of the snippet.
entry["IDECodeSnippetVersion"] += 1
plistlib.writePlist(entry, snippet_path)
print("Done writing snippets.")
You'll notice that it doesn't actually change any of Xcode's internal files. It just adds files, and Xcode is smart enough to use the added files instead of the original snippets. You can roll back to the originals at any time by simply deleting the user version of the snippet. You can also run the script as many times as you want without worrying about overwriting any user snippets generated by previous runs of the script.
您会注意到它实际上并没有更改任何 Xcode 的内部文件。它只是添加文件,而 Xcode 足够聪明,可以使用添加的文件而不是原始片段。您可以随时通过简单地删除代码段的用户版本来回滚到原始版本。您还可以根据需要多次运行脚本,而不必担心覆盖之前运行脚本生成的任何用户片段。
回答by software evolved
There's a great little tool called "Snippet Edit". I just tried it, and highly recommend it. Apparently it used to be a for-pay app, but the author is now giving it away for free.
有一个很棒的小工具叫做“Snippet Edit”。我刚试过,强烈推荐。显然它曾经是一个付费应用程序,但作者现在免费赠送它。
回答by Aplextor
You can edit system code snippets manually:
您可以手动编辑系统代码片段:
- Go to this directory: "/Developer/Library/Xcode/PrivatePlugIns".
- Show package contents of "IDECodeSnippetLibrary.ideplugin"
- Open "Contents/Resources/SystemCodeSnippets.codesnippets" as text file
- Edit it
- 转到此目录:“/Developer/Library/Xcode/PrivatePlugIns”。
- 显示“IDECodeSnippetLibrary.ideplugin”的包内容
- 打开“Contents/Resources/SystemCodeSnippets.codesnippets”作为文本文件
- 编辑它
.codesnippets file is a .plist but some strings entered with CR/LF and cannot be edited by standard plist editor.
.codesnippets 文件是一个 .plist,但有些字符串是用 CR/LF 输入的,不能被标准 plist 编辑器编辑。
回答by The Dude
You can edit the Xcode system snippets using a text editor and knowing the location of the system code snippets file. In Xcode 5.1.1 the location of the system code snippets file has changed once again to:
您可以使用文本编辑器并知道系统代码片段文件的位置来编辑 Xcode 系统片段。在 Xcode 5.1.1 中,系统代码片段文件的位置再次更改为:
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
and you must have root
privileges to edit the plist file in place because its
owner and permissions are as follows:
并且您必须具有root
在适当位置编辑 plist 文件的权限,因为其所有者和权限如下:
$ ls -l /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
-rw-r--r-- 1 root wheel 190 May 16 18:23 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
The plist dictionary keys are pretty self-explanatory and for the
IDECodeSnippetIdentifier
key, you can generate UUIDs yourself using
for example the command:
plist 字典键是非常不言自明的,对于
IDECodeSnippetIdentifier
键,您可以使用例如以下命令自己生成 UUID:
$ uuidgen
42F6B133-5DA3-41DB-8874-4E10E447F723
Once you've edited the file using for instance sudo
and your editor of choice, you
have to restart Xcode in order to pick up your changes.
一旦您使用例如sudo
和您选择的编辑器编辑了文件,您必须重新启动 Xcode 以获取您的更改。
Happy hacking!
快乐黑客!
回答by sudo rm -rf
Either this is a bug, or it's a feature. I believe it's the latter. You can add your own snippets, but you can't edit the built-in ones. I'd just make a new snippet and customize it to how you want it.
这是一个错误,或者它是一个功能。我相信是后者。您可以添加自己的片段,但不能编辑内置片段。我只是制作一个新片段并根据您的需要对其进行自定义。
回答by Jonathan Saggau
I wrote a script today that uses python and uncrustify to pull the snippets out of those provided by Xcode, reformat them to my liking, and dump them into a directory where I can then import them into ~/Library/Developer/Xcode/UserData/CodeSnippets. It's on github here: Xcode4Customization
我今天写了一个脚本,它使用 python 和 uncrustify 从 Xcode 提供的片段中提取片段,根据我的喜好重新格式化它们,然后将它们转储到一个目录中,然后我可以将它们导入 ~/Library/Developer/Xcode/UserData/代码片段。它在 github 上:Xcode4Customization