Skip to content

Commit

Permalink
Merge pull request #15 from yieldmo/save-cursor-position
Browse files Browse the repository at this point in the history
Save cursor position
  • Loading branch information
Elber Carneiro committed Nov 28, 2016
2 parents 65e4fe6 + 55d68c7 commit 58461e8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Uncrustifier/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.2</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
41 changes: 38 additions & 3 deletions Uncrustify/SourceEditorCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
}

func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void) {
// save the currect selection
let previousSelection: XCSourceTextRange? = (invocation.buffer.selections.firstObject as? XCSourceTextRange)?.copy() as? XCSourceTextRange

let errorPipe = Pipe()
let outputPipe = Pipe()

Expand Down Expand Up @@ -68,9 +71,41 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {

}

// fixes crash if there is no selection when completion handler is called
invocation.buffer.selections.add(XCSourceTextRange(start: XCSourceTextPosition(line: 0, column: 0), end: XCSourceTextPosition(line: 0, column: 0)))

// adjust selection to fit within the formatted buffer
invocation.buffer.selections.removeAllObjects()
if var selection = previousSelection{

func adjustedSelection(_ s: XCSourceTextRange) -> XCSourceTextRange{
let lineLimit = invocation.buffer.lines.count - 1

if (s.start.line > lineLimit){
s.start.line = lineLimit
}

if (s.end.line > lineLimit){
s.end.line = lineLimit
}

let columnLimitStart = (invocation.buffer.lines[s.start.line] as! NSString).length - 1
if (s.start.column > columnLimitStart){
s.start.column = columnLimitStart
}

let columnLimitEnd = (invocation.buffer.lines[s.end.line] as! NSString).length - 1
if (s.end.column > columnLimitEnd){
s.end.column = columnLimitEnd
}

return s
}

invocation.buffer.selections.add(adjustedSelection(selection))
}
else{
// fixes crash if there is no selection when completion handler is called
invocation.buffer.selections.add(XCSourceTextRange(start: XCSourceTextPosition(line: 0, column: 0), end: XCSourceTextPosition(line: 0, column: 0)))
}

completionHandler(nil)
}
}

0 comments on commit 58461e8

Please sign in to comment.