Xcode built-in snippets edit

2019-01-22 02:42发布

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.

Any insight is appreciated.

6条回答
Viruses.
2楼-- · 2019-01-22 03:26

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

查看更多
地球回转人心会变
3楼-- · 2019-01-22 03:27

You can edit system code snippets manually:

  1. Go to this directory: "/Developer/Library/Xcode/PrivatePlugIns".
  2. Show package contents of "IDECodeSnippetLibrary.ideplugin"
  3. Open "Contents/Resources/SystemCodeSnippets.codesnippets" as text file
  4. Edit it

.codesnippets file is a .plist but some strings entered with CR/LF and cannot be edited by standard plist editor.

查看更多
贼婆χ
4楼-- · 2019-01-22 03:38

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:

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.

查看更多
仙女界的扛把子
5楼-- · 2019-01-22 03:40

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:

/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:

$ 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:

$ 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.

Happy hacking!

查看更多
干净又极端
6楼-- · 2019-01-22 03:41

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.

http://cocoaholic.com/snippet_edit/

查看更多
我只想做你的唯一
7楼-- · 2019-01-22 03:43

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.

查看更多
登录 后发表回答