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

  1. downloadLatest version
  2. After decompression, you can move the application to the application for next use!
  3. Open the app!
  4. In the security and privacy Settings should still be open
  5. 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.
  6. Select extensions in Settings and check these features.
  7. 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.plistIn the file is the configuration for target

XCSourceEditorCommandName here can change the name

  • SourceEditorExtension.swiftImplemented in theXCSourceEditorExtensionBoth are optional
    • extensionDidFinishLaunchingThe plug-in executes at startup time
    • commandDefinitionsThis place will be coveredinfo.plistThe setting of the
  • SourceEditorCommand.swiftImplemented in theXCSourceEditorCommand
    • performOnce the naming of the plug-in is triggered, this method, the parameter, is triggeredinvocation:XCSourceEditorCommandInvocationContains the contents of the text cachebuff
    • buff.selectionsThat’s the range of selected text,buff.linesIt’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

  1. Select a piece of code
  2. 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