Xcode plugin is a tripartite plugin for Xcode. Having worked on iOS development for a few years, I’m sure I have some memories of plug-ins, although they are history. Before Xcode8, we could use a variety of plug-ins, since apple no longer supports tripartite plug-ins, but has introduced a new solution: —-Xcode – Source Editor Extension, which we will discuss in this article.

As you can see, extending this technology has been proposed for a long time, and this article explores what it can do to extend Xcode. I also found time to study and write a small demo recently, it is still relatively simple, if you are reading the article you have any new ideas, welcome to discuss in the comments section.

Introduction to the

Apple introduced Source Editor Extension in WWDC session. Now LET me briefly summarize its functions. In fact, it provides few interfaces, we can do few things, and there are many limitations.

A plugin can do several things:

  1. Gets the text being edited (or selected) in Xcode
  2. Replace the text being edited (or selected) in Xcode
  3. Generate a plug-in submenu in xcode-Editor
  4. You can assign a shortcut key to a plug-in

Create an Extension

If you want to develop your own Extension, the following article will be a good record to step on the hole. I hope it can help you.

Create a Mac OS project

File — New — Project — macOs — APP

Add a Source Editor Extension

File — New — Target — macOs — Xcode Source Editor Extension

Alternatively, you can add an Extension by doing the following

At the end, there will be a pop-up prompt, click on Active.

Finally, the structure of the project is as shown in the figure above. Notice that there are two schemes, and then the corresponding schemes are run and debug with extension.

Start your show

Now that the project is created, you’re ready to show off. There aren’t many things plug-ins can do, so it’s a good time to brainstorm what tools you can use to make your development more efficient

The following will take a small tool I developed as an example, mainly to show the development process and considerations of plug-ins, this tool may not be applicable to your project, if it can be used, please refer to it.

Project configuration

The main configuration is in info.plist, and as emphasized above, all subsequent development is for Extension, so don’t change the location.

<key>NSExtension</key>
	<dict>
		<key>NSExtensionAttributes</key>
		<dict>
			<key>XCSourceEditorCommandDefinitions</key>
			<array>
				<dict>
					<key>XCSourceEditorCommandClassName</key>
					<string>SourceEditorCommand</string>
					<key>XCSourceEditorCommandIdentifier</key>
					<string>$(PRODUCT_BUNDLE_IDENTIFIER).SourceEditorCommand</string>
					<key>XCSourceEditorCommandName</key>
					<string>LocalTool</string>
				</dict>
			</array>
			<key>XCSourceEditorExtensionPrincipalClass</key>
			<string>SourceEditorExtension</string>
		</dict>
		<key>NSExtensionPointIdentifier</key>
		<string>com.apple.dt.Xcode.extension.source-editor</string>
	</dict>
Copy the code

XCSourceEditorCommandDefinitions is to set up basic information for each of the Extension, XCSourceEditorCommandClassName is dealing with the Extension of the name of the class, XCSourceEditorCommandIdentifier each extension is a unique identifier for and other extensions do distinguish, XCSourceEditorCommandName is finally displayed in the Editor in the name of the menu.

The project if we only need to develop an extension, that you can, as long as change XCSourceEditorCommandName this other USES the default configuration.

Write extension code

In the project, two classes, SourceEditorExtension and SourceEditorCommand, are automatically generated. These two classes are also present in the plist configuration file, and the core code is written in these two classes.

The SourceEditorExtension class, AS I understand it, is mainly about extending life cycle control.

In the SourceEditorCommand, you can get the text that Xcode is editing, and you can replace it. Only performCommandWithInvocation such a core, the method of the following is the code for being edited text.

- (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError *  _Nullable nilOrError))completionHandler { XCSourceTextRange *selection = invocation.buffer.selections.firstObject; NSInteger lineNumber = selection.start.line; NSString *text = Invocation.buffer. Lines [lineNumber]; NSLog(@" select line %@",text); NSString *result =... [invocation.buffer.lines replaceObjectAtIndex:lineNumber withObject:result]; }Copy the code

debugging

Select the scheme corresponding to the extension to run, select Xcode, a gray Xcode interface will pop up, where you can open the existing project, or create a new test project

Then in the Editor menu bar, you can see the extension name that we just changed, and here the entire development process of the plug-in is complete.

Practice and stomp pits

I have made a multi-language annotation tool, as a plug-in practice, the following to share his functions and implementation ideas.

Believes that many junior partner of the company’s projects, are useful to multiple language development, here but more introduced, I made this little extension, is used for language code to add more comments, such as multiple languages used in conditional statements, readability is poor, then we are going to global search, find the string file corresponding to the content of adding comments, process is more troublesome.

"KNOW" = "KNOW"; "CANCEL" = "CANCEL"; "DONE" = "DONE"; "OK" = "OK";Copy the code

Here’s an extension to add comments to multilingual code in one click:

Train of thought

  1. You are editing in current Xcode extracted from XCSourceEditorCommandInvocation lines
  2. And from that, we’re going to extract the part that’s used for multilingual coding, which is “KNOW.”
  3. Gets the path to the multilanguage file, that is, the path to the xxx.strings file
  4. Read the contents of XXX. Strings
  5. Use “KNOW” to make a match and get the corresponding Chinese annotation inside, that is, “KNOW”
  6. Concatenate “known” as a comment after the content of the original edit line
  7. Substitution also has textual content
  8. The end of the

Technical points used

  1. String interception or regular matching
  2. The use of NSFileManager

Hit the pit

  1. The deepest pit is: certificate !!!!
IDEExtensionManager: Xcode Extension does not meet code signing requirement: com.sal.SourceEditorExtensionTest.LocalTool (file:///Users/xxxxx/Library/Developer/Xcode/DerivedData/SourceEditorExtensionTest-bltqsxwfdzxeirceowbwjuzaxpjd/Build/Pr oducts/Debug/SourceEditorExtensionTest.app/Contents/PlugIns/LocalTool.appex/), Error Domain=DVTSecErrorDomain Code=-67050 "code failed to satisfy specified code requirement(s)" UserInfo={NSLocalizedDescription=code failed to satisfy specified code requirement(s)}Copy the code

The issue is because of the certificate signature. It is necessary to understand the provisioning profile section of the certificate for the two targets.

  1. XcodeKit

If this error is reported when running, do the following:

The other is no big problem, it is a business problem to do this project, get the current xcode path:

  1. Extensions in system Settings
  2. Info.plist states that Apple Events need to be sent: Privacy-appleevents Sending Usage Description, similar to obtaining camera or location permission. This step declares to the system that Apple Script needs to be executed. When the plug-in is executed for the first time, the system will pop up a window for the user to obtain permission
  3. In the sandbox. Entitlements file, configure com. Apple. Security. Temporary – exception. Apple – events, Contents is com. Apple. Dt. Xcode and com. Apple. Dt. The document. The workspace, statement we need exception, to control the xcode
  4. If you accidentally clicked “No” in step 2, or if you clicked “No” by default because the first three options were not configured, then you will need to reset the AppleEvents prompt by typing tccutil reset AppleEvents on the command line

If you configure these four items and successfully execute Apple Script, Xcode Extension’s capabilities will be greatly improved.

portal

The practice code shared with this article is at ——> here

The literature

Medium.freecodecamp.org/how-to-conv…