I'd like to get XCode 4 to recognize a custom file extension (e.g. *.lx) as Objective-C for syntax highlighting and indentation purposes. How do I get the tool to automatically do this?
我想让XCode 4将自定义文件扩展名(例如* .lx)识别为Objective-C,用于语法高亮和缩进。如何让工具自动执行此操作?
3 个解决方案
#1
25
Xcode determines how to represent a file in its user interface based on the file's Uniform Type Identifier. As far as I know it's not possible to add additional file extension tags to an existing UTI, but you can declare a new UTI that conforms to the type you want to map to. The system will then associate the specified file extension(s) with your new UTI and through conformance Xcode and every other UTI-aware application will recognize the files as source code of the mapped type.
Xcode根据文件的统一类型标识符确定如何在其用户界面中表示文件。据我所知,无法向现有UTI添加其他文件扩展名标签,但您可以声明符合要映射到的类型的新UTI。然后,系统将指定的文件扩展名与您的新UTI相关联,并通过一致性Xcode与每个其他UTI感知应用程序将这些文件识别为映射类型的源代码。
You may want to give some thought to where to declare to the new UTI. For example, if files of this type are being created by a tool the bundle for that tool would be the most appropriate location. In the absence of a better alternative you can create a stub application bundle and declare the new UTI there:
您可能想要考虑向新UTI声明的位置。例如,如果工具正在创建此类文件,则该工具的包将是最合适的位置。如果没有更好的替代方案,您可以创建一个存根应用程序包并在那里声明新的UTI:
- Create a new Cocoa Application project in Xcode.
- 在Xcode中创建一个新的Cocoa Application项目。
- In project settings, select the application target, then the Info tab.
- 在项目设置中,选择应用程序目标,然后选择“信息”选项卡。
- Create a new Exported UTI.
- 创建一个新的导出UTI。
- Set the Identifier field to a unique name using reverse DNS notation for a domain that you control. For example,
com.yourdomain.objective-c-source
. - 对于您控制的域,使用反向DNS表示法将标识符字段设置为唯一名称。例如,com.yourdomain.objective-c-source。
- Set the Conforms To field to the UTI that you want to map to, such as
public.objective-c-source
. You can find this by browsing the list of system-declared UTIs or those exported in Xcode's Info.plist. - 将Conforms To字段设置为要映射到的UTI,例如public.objective-c-source。您可以通过浏览系统声明的UTI列表或在Xcode的Info.plist中导出的UTI列表来找到它。
- Set the Extensions field to the comma-separated list of extensions that you want to associate with the new UTI.
- 将Extensions字段设置为要与新UTI关联的以逗号分隔的扩展列表。
- Commit the change to the last field by pressing return or moving the focus to a different field.
- 按返回或将焦点移动到其他字段,将更改提交到最后一个字段。
- Build and run the application to register it with Launch Services.
- 构建并运行应用程序以使用Launch Services注册它。
- Restart Xcode.
- 重启Xcode。
Xcode should now use appropriate syntax highlighting for files with the specified extension(s).
Xcode现在应该对具有指定扩展名的文件使用适当的语法突出显示。
If this doesn't work, check the Info.plist of the built application to ensure that all of the expected information is there without any trailing whitespace. You can also check that the UTI has been registered using lsregister
:
如果这不起作用,请检查构建的应用程序的Info.plist,以确保所有预期信息都没有任何尾随空格。您还可以使用lsregister检查UTI是否已注册:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump
Search the output for your UTI's identifier and verify that it is present and active.
在输出中搜索UTI的标识符,并验证它是否存在且处于活动状态。
#2
10
Xcode has a "Type" field in the "Attributes Inspector" (leftmost tab in "Utilities" right sidebar) for files in your project:
对于项目中的文件,Xcode在“属性检查器”(“实用程序”右侧边栏中最左侧的选项卡)中有一个“类型”字段:
As you can see above, I set our project's Fastfile
to be recognized as a Ruby Script—which it is, despite lacking the expected .rb
extension. Yielding the expected syntax highlighting (with my custom color scheme):
正如您在上面所看到的,我设置我们项目的Fastfile被识别为Ruby脚本 - 尽管缺少预期的.rb扩展名。产生预期的语法高亮(使用我的自定义颜色方案):
Note: this only works if the file is added to your project within a group (not inside a folder reference):
注意:这仅在文件添加到组内的项目时(不在文件夹引用中)才有效:
This might not be ideal, but Xcode can't persist attributes of files which aren't explicitly tracked in the project file. Folders, by design, don't have entries for their contents in the Xcode project, since it's meant to be dynamic.
这可能不太理想,但Xcode不能保留未在项目文件中显式跟踪的文件属性。根据设计,文件夹在Xcode项目中没有其内容条目,因为它意味着是动态的。
#3
8
In Xcode when your file is active in the editor, you go to the Editor Menu -> Syntax Coloring -> Objective-C
在Xcode中,当您的文件在编辑器中处于活动状态时,您将转到编辑器菜单 - >语法着色 - >目标-C
This will apply objective-c syntax coloring to a file. Worked for me with a plain text file regardless of its extension.
这将对文件应用objective-c语法着色。无论扩展名如何,我都使用纯文本文件。
This seemed to work for automatic indentation also
这似乎也适用于自动缩进
#1
25
Xcode determines how to represent a file in its user interface based on the file's Uniform Type Identifier. As far as I know it's not possible to add additional file extension tags to an existing UTI, but you can declare a new UTI that conforms to the type you want to map to. The system will then associate the specified file extension(s) with your new UTI and through conformance Xcode and every other UTI-aware application will recognize the files as source code of the mapped type.
Xcode根据文件的统一类型标识符确定如何在其用户界面中表示文件。据我所知,无法向现有UTI添加其他文件扩展名标签,但您可以声明符合要映射到的类型的新UTI。然后,系统将指定的文件扩展名与您的新UTI相关联,并通过一致性Xcode与每个其他UTI感知应用程序将这些文件识别为映射类型的源代码。
You may want to give some thought to where to declare to the new UTI. For example, if files of this type are being created by a tool the bundle for that tool would be the most appropriate location. In the absence of a better alternative you can create a stub application bundle and declare the new UTI there:
您可能想要考虑向新UTI声明的位置。例如,如果工具正在创建此类文件,则该工具的包将是最合适的位置。如果没有更好的替代方案,您可以创建一个存根应用程序包并在那里声明新的UTI:
- Create a new Cocoa Application project in Xcode.
- 在Xcode中创建一个新的Cocoa Application项目。
- In project settings, select the application target, then the Info tab.
- 在项目设置中,选择应用程序目标,然后选择“信息”选项卡。
- Create a new Exported UTI.
- 创建一个新的导出UTI。
- Set the Identifier field to a unique name using reverse DNS notation for a domain that you control. For example,
com.yourdomain.objective-c-source
. - 对于您控制的域,使用反向DNS表示法将标识符字段设置为唯一名称。例如,com.yourdomain.objective-c-source。
- Set the Conforms To field to the UTI that you want to map to, such as
public.objective-c-source
. You can find this by browsing the list of system-declared UTIs or those exported in Xcode's Info.plist. - 将Conforms To字段设置为要映射到的UTI,例如public.objective-c-source。您可以通过浏览系统声明的UTI列表或在Xcode的Info.plist中导出的UTI列表来找到它。
- Set the Extensions field to the comma-separated list of extensions that you want to associate with the new UTI.
- 将Extensions字段设置为要与新UTI关联的以逗号分隔的扩展列表。
- Commit the change to the last field by pressing return or moving the focus to a different field.
- 按返回或将焦点移动到其他字段,将更改提交到最后一个字段。
- Build and run the application to register it with Launch Services.
- 构建并运行应用程序以使用Launch Services注册它。
- Restart Xcode.
- 重启Xcode。
Xcode should now use appropriate syntax highlighting for files with the specified extension(s).
Xcode现在应该对具有指定扩展名的文件使用适当的语法突出显示。
If this doesn't work, check the Info.plist of the built application to ensure that all of the expected information is there without any trailing whitespace. You can also check that the UTI has been registered using lsregister
:
如果这不起作用,请检查构建的应用程序的Info.plist,以确保所有预期信息都没有任何尾随空格。您还可以使用lsregister检查UTI是否已注册:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump
Search the output for your UTI's identifier and verify that it is present and active.
在输出中搜索UTI的标识符,并验证它是否存在且处于活动状态。
#2
10
Xcode has a "Type" field in the "Attributes Inspector" (leftmost tab in "Utilities" right sidebar) for files in your project:
对于项目中的文件,Xcode在“属性检查器”(“实用程序”右侧边栏中最左侧的选项卡)中有一个“类型”字段:
As you can see above, I set our project's Fastfile
to be recognized as a Ruby Script—which it is, despite lacking the expected .rb
extension. Yielding the expected syntax highlighting (with my custom color scheme):
正如您在上面所看到的,我设置我们项目的Fastfile被识别为Ruby脚本 - 尽管缺少预期的.rb扩展名。产生预期的语法高亮(使用我的自定义颜色方案):
Note: this only works if the file is added to your project within a group (not inside a folder reference):
注意:这仅在文件添加到组内的项目时(不在文件夹引用中)才有效:
This might not be ideal, but Xcode can't persist attributes of files which aren't explicitly tracked in the project file. Folders, by design, don't have entries for their contents in the Xcode project, since it's meant to be dynamic.
这可能不太理想,但Xcode不能保留未在项目文件中显式跟踪的文件属性。根据设计,文件夹在Xcode项目中没有其内容条目,因为它意味着是动态的。
#3
8
In Xcode when your file is active in the editor, you go to the Editor Menu -> Syntax Coloring -> Objective-C
在Xcode中,当您的文件在编辑器中处于活动状态时,您将转到编辑器菜单 - >语法着色 - >目标-C
This will apply objective-c syntax coloring to a file. Worked for me with a plain text file regardless of its extension.
这将对文件应用objective-c语法着色。无论扩展名如何,我都使用纯文本文件。
This seemed to work for automatic indentation also
这似乎也适用于自动缩进