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

发布于 2024-12-29 10:45:57 字数 81 浏览 1 评论 0原文

我想让 Xcode 4 将自定义文件扩展名(例如 *.lx)识别为 Objective-C,以实现语法突出显示和缩进目的。如何让工具自动执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2025-01-05 10:45:57

Xcode 根据文件的统一类型标识符。据我所知,不可能向现有 UTI 添加额外的文件扩展名标记,但您可以声明一个符合您要映射到的类型的新 UTI。然后,系统会将指定的文件扩展名与您的新 UTI 相关联,并通过一致性 Xcode 和每个其他 UTI 感知应用程序将这些文件识别为映射类型的源代码。

您可能需要考虑一下在哪里向新的 UTI 进行申报。例如,如果这种类型的文件是由工具创建的,则该工具的捆绑包将是最合适的位置。如果没有更好的替代方案,您可以创建一个存根应用程序包并在那里声明新的 UTI:

  1. 在 Xcode 中创建一个新的 Cocoa 应用程序项目。
  2. 在项目设置中,选择应用程序目标,然后选择“信息”选项卡。
  3. 创建一个新的导出 UTI。
    UTI 定义示例
  4. 使用反向 DNS 表示法将标识符字段设置为唯一名称您控制的域。例如,com.yourdomain.objective-c-source
  5. 将“符合”字段设置为要映射到的 UTI,例如 public.objective-c-source。您可以通过浏览系统声明的 UTI< /a> 或在 Xcode 的 Info.plist 中导出的内容。
  6. 将“扩展”字段设置为要与新 UTI 关联的扩展的逗号分隔列表。
  7. 通过按 Return 或将焦点移至其他字段,将更改提交到最后一个字段。
  8. 构建并运行应用程序以将其注册到启动服务。
  9. 重新启动 Xcode。

Xcode 现在应该对具有指定扩展名的文件使用适当的语法突出显示。

如果这不起作用,请检查构建的应用程序的 Info.plist,以确保所有预期信息都在那里,没有任何尾随空格。您还可以使用 lsregister 检查 UTI 是否已注册:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump

在输出中搜索 UTI 标识符并验证它是否存在且处于活动状态。

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.

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:

  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 UTIs or 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.

Xcode should now use appropriate syntax highlighting for files with the specified extension(s).

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:

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

永不分离 2025-01-05 10:45:57

Xcode 在“属性检查器”(“实用程序”右侧边栏中最左侧的选项卡)中为项目中的文件提供了一个“类型”字段:

< img src="https://i.sstatic.net/93PpP.png" alt="身份和类型设置">

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

在此处输入图像描述

注意:仅当文件添加到内的项目(而不是文件夹内)时,此选项才有效参考):

fastlane group

这可能并不理想,但 Xcode 无法保留项目文件中未明确跟踪的文件的属性。根据设计,文件夹在 Xcode 项目中没有其内容条目,因为它是动态的。

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

identity and type settings

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

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.

污味仙女 2025-01-05 10:45:57

在 Xcode 中,当您的文件在编辑器中处于活动状态时,您可以转到编辑器菜单 ->语法着色 -> Objective-C

这会将 Objective-C 语法着色应用于文件。为我使用纯文本文件,无论其扩展名如何。

这似乎也适用于自动缩进

In Xcode when your file is active in the editor, you go to the 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.

This seemed to work for automatic indentation also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文