Xcode内置代码片段编辑

23

是否有办法编辑Xcode内置的代码片段?虽然有一个编辑按钮,但按下它似乎不能更改片段的文本。

非常感谢任何帮助和建议。


Xcode 4 不再受 NDA 的限制。 - Kristopher Johnson
6个回答

18

您仍然无法编辑内置的系统代码片段。但是,您可以编辑“用户”片段。

我的最简单的解决方案是创建所有默认片段的副本,但修改它们以使它们成为“用户”片段并覆盖默认版本。我编写了一个小的Python脚本来完成这项工作。它非常简单,在运行后,所有Xcode的片段都可以通过Xcode GUI神奇地进行编辑。无需手动在属性列表中进行操作:

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.")

你会发现它实际上并没有改变Xcode的任何内部文件,它只是添加了文件,而Xcode足够聪明来使用添加的文件而不是原始片段。你可以随时通过简单地删除用户版本的片段来回滚到原始版本。你也可以运行脚本多次,而不必担心覆盖之前运行脚本生成的任何用户片段。


+1,但是你的脚本有一个错误。我不是Python大师,但是在终端下运行你的脚本时收到了一个错误。Python报告说找不到文件或目录(输出plist的路径)。我认为你忘记在尝试写入plistlib.writePlist(entry, snippet_path)之前创建和打开可写的plist了。顺便说一句,感谢你的代码。 - anonim
2
@anonim.developer,感谢你的提示。我在一台干净的机器上尝试了一下,确实出现了问题。如果用户的片段目录不存在,它实际上会发生。如果需要,plist库将自动创建文件,但不会创建中间目录。我已经更新了脚本。 - Matt Wilding
注意:如果您已经创建了自定义代码片段,os.makedirs(user_snippet_path)将引发异常,因为该目录已经存在。 - memmons
此外,这个脚本似乎会影响Xcode的自动缩进。运行后,Xcode将不再自动缩进嵌套项,例如if语句下面的行或{}括号中的行。如果我删除脚本创建的自定义片段,则缩进功能将恢复正常。 - memmons
脚本会捕获并吞噬异常,如果片段路径已经存在。你真的看到了这种行为吗?至于自动缩进,这对我来说是新的。我是为Xcode 4编写的,自那以后我们已经进行了一次重大更新,所以有可能事情已经改变了... - Matt Wilding

12

有一个很棒的小工具叫做“Snippet Edit”。我刚试用了一下,强烈推荐。显然它曾经是一款收费应用程序,但作者现在正在免费提供。

http://cocoaholic.com/snippet_edit/


6

您可以手动编辑系统代码片段:

  1. 进入以下目录:"/Developer/Library/Xcode/PrivatePlugIns"。
  2. 显示 "IDECodeSnippetLibrary.ideplugin" 的包内容。
  3. 将 "Contents/Resources/SystemCodeSnippets.codesnippets" 以文本文件形式打开。
  4. 编辑它。

.codesnippets 文件是一个 .plist 文件,但有些字符串使用 CR/LF 输入,无法通过标准的 plist 编辑器进行编辑。


3
在 Xcode 4.3.2 中,该文件位于"/Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources"。 - Mundi
1
对于Xcode的自包含应用程序。这是默认代码片段的确切文件路径:"/Applications/Xcode4.3.3.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets" - LightMan

4
您可以使用文本编辑器编辑Xcode系统代码片段,并知道系统代码片段文件的位置。在Xcode 5.1.1中,系统代码片段文件的位置再次更改为:
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
您需要拥有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

plist字典键相当自说明,对于IDECodeSnippetIdentifier键,您可以使用例如以下命令自行生成UUID:

$ uuidgen
42F6B133-5DA3-41DB-8874-4E10E447F723

如果您使用sudo和您选择的编辑器编辑了文件,则必须重新启动Xcode以应用更改。

祝您编程愉快!


2

这可能是一个bug,也可能是一个特性。我认为这是后者。你可以添加自己的片段,但不能编辑内置的片段。我建议创建一个新片段并根据你的需求进行自定义。


1
今天我写了一个脚本,使用Python和Uncrustify从Xcode提供的代码片段中提取代码片段,将它们重新格式化为我喜欢的样式,并将它们导出到一个目录中,然后可以将它们导入到~/Library/Developer/Xcode/UserData/CodeSnippets中。这个脚本在Github上,链接是:Xcode4Customization

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接