As an iOS developer I suddenly wanted to try Mac development and this is my first Mac APP
The software currently contains the following features
- ConvertFromCase changes the selected underlined content to a hump
- DeleteEmptyLines Deletes the selected empty line
- SortLines sorts the selected rows
- JsonToModel converts the Json data into the Swift model, naming the underline as a hump
How to use
- downloadLatest version
- After decompression, you can move the application to the application for next use!
- Open the app!
- In the security and privacy Settings should still be open
- The screen opens to convert the Json data to the Swift model, changing the underline name to hump. Put Json on the left and click Convert.
- Select extensions in Settings and check these features.
- Open Xcode and you can see the plug-ins in the Editor in the menu bar
Create the Cocoa APP
Select macOS->Cocoa App when you create the project
Xcode plug-in
Apple introduced add-on development in Xcode8, which is weak, but can still achieve some functionality.
Create a target
New target select macOS->Xcode Source Editor Extension
XcodeKit
The target created with the name DeleteEmptyLines will have the following files
info.plist
In the file is the configuration for target
XCSourceEditorCommandName here can change the name
SourceEditorExtension.swift
Implemented in theXCSourceEditorExtension
Both are optionalextensionDidFinishLaunching
The plug-in executes at startup timecommandDefinitions
This place will be coveredinfo.plist
The setting of the
SourceEditorCommand.swift
Implemented in theXCSourceEditorCommand
perform
Once the naming of the plug-in is triggered, this method, the parameter, is triggeredinvocation:XCSourceEditorCommandInvocation
Contains the contents of the text cachebuff
buff.selections
That’s the range of selected text,buff.lines
It’s each line of text, and we can change it to change the content of the text
Implement the first plug-in – remove blank lines in the selected code
Add the following code
extension XCSourceEditorCommandInvocation {
var selections: [XCSourceTextRange] {
return buffer.selections as! [XCSourceTextRange]}func deleteEmptyLines(a) {
selections.forEach { (selection) in
let start = selection.startLine
let end = selection.endLine
letemptyIndexs = (start... end) .filter({ (index) -> Bool in
(buffer.lines[index] as! String).match(regular: "^\\s*$")
})
buffer.lines.removeObjects(at: IndexSet(emptyIndexs))
}
}
}
extension String {
func match(regular: String) -> Bool {
returnrange(of: regular, options: .regularExpression) ! =nil}}extension XCSourceTextRange {
var startLine: Int {
return start.line
}
var endLine: Int {
return end.line - (end.column == 0 ? 1 : 0)}}Copy the code
Modify the Perform method in SourceEditorCommand
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) {
defer { completionHandler(nil) }
invocation.deleteEmptyLines()
}
Copy the code
test
Select the target to test
- Select a piece of code
- On the menu bar, choose Editor-> DeleteEmptyLines -> Source Editor Command
If not, look like this: Editor — > Extension bundle display name -> Command name
Json to Model interface
During the development of iOS, I made an interface to deal with the fact that the data returned in the background used the underscore naming method, while the APP used the camel name method.
interface
Select main. storyboard from the project and drag two TextViews and a button into the View Controller
Adjust the style of the control and add layout constraints. Write it in the style you like -, –
Bind these controls to ViewController code
code
Just write the conversion code in the convert method. The longer code is in the GitHub link at the end of this article.
There is a need to pay attention to detail, macOS in input quotes will automatically be converted to Json cannot parse format, so you need to set up the NSTextView isAutomaticQuoteSubstitutionEnabled to false
Running effect
Put Json on the left and click Convert.
Source code and software download address
GitHub
Download
The copyright of this article belongs to Zaihui RESEARCH and development team, welcome to reprint, reprint please reserve source. @Bermose