如何让 Xcode 将自定义文件扩展名识别为 Objective-C 以进行语法高亮显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9050035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:08:32  来源:igfitidea点击:

How to make Xcode recognize a custom file extension as Objective-C for syntax highlighting?

objective-cxcode

提问by user1175914

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,用于语法突出显示和缩进。如何让工具自动执行此操作?

回答by Matt Stevens

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 根据文件的Uniform Type Identifier确定如何在其用户界面中表示文件。据我所知,不可能向现有 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:

  1. Create a new Cocoa Application project in Xcode.
  2. In project settings, select the application target, then the Info tab.
  3. Create a new Exported UTI. UTI definition example
  4. 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.
  5. 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 UTIsor those exported in Xcode's Info.plist.
  6. Set the Extensions field to the comma-separated list of extensions that you want to associate with the new UTI.
  7. Commit the change to the last field by pressing return or moving the focus to a different field.
  8. Build and run the application to register it with Launch Services.
  9. Restart Xcode.
  1. 在 Xcode 中创建一个新的 Cocoa Application 项目。
  2. 在项目设置中,选择应用程序目标,然后选择信息选项卡。
  3. 创建一个新的导出 UTI。 UTI 定义示例
  4. 使用您控制的域的反向 DNS 表示法将标识符字段设置为唯一名称。例如,com.yourdomain.objective-c-source
  5. 将 Conforms To 字段设置为要映射到的 UTI,例如public.objective-c-source. 您可以通过浏览系统声明的 UTI列表或在 Xcode 的 Info.plist 中导出的列表来找到它。
  6. 将扩展字段设置为要与新 UTI 关联的以逗号分隔的扩展列表。
  7. 通过按回车键或将焦点移动到其他字段来提交对最后一个字段的更改。
  8. 构建并运行应用程序以将其注册到 Launch Services。
  9. 重新启动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 以确保所有预期信息都在那里,没有任何尾随空格。您还可以使用以下方法检查 UTI 是否已注册lsregister

/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 标识符并验证它是否存在并处于活动状态。

回答by Brian Gerstle

Xcode has a "Type" field in the "Attributes Inspector" (leftmost tab in "Utilities" right sidebar) for files in your project:

Xcode 在“属性检查器”(“实用工具”右侧栏中的最左侧选项卡)中有一个“类型”字段,用于您项目中的文件:

identity and type settings

身份和类型设置

As you can see above, I set our project's Fastfileto be recognized as a Ruby Script—which it is, despite lacking the expected .rbextension. Yielding the expected syntax highlighting (with my custom color scheme):

正如您在上面看到的,我将我们的项目设置Fastfile为被识别为 Ruby 脚本——尽管缺少预期的.rb扩展名,但确实如此。产生预期的语法突出显示(使用我的自定义配色方案):

enter image description here

在此处输入图片说明

Note:this only works if the file is added to your project within a group(not inside a folder reference):

注意:这仅适用于将文件添加到内的项目(不在文件夹引用内):

fastlane group

快速通道组

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 项目中没有其内容的条目,因为它是动态的。

回答by pontidm

In Xcode when your file is active in the editor, you go to the Editor Menu -> Syntax Coloring -> Objective-C

在 Xcode 中,当您的文件在编辑器中处于活动状态时,您可以转到 Editor Menu -> Syntax Coloring -> Objective-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

这似乎也适用于自动缩进