Skip to content

Commit 33e80c4

Browse files
committed
somewhat refractory
1 parent 816db62 commit 33e80c4

4 files changed

Lines changed: 121 additions & 129 deletions

File tree

SourceEditorExtension/VoiceCodeSourceEditorCommand.swift

Lines changed: 119 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
3535
return true
3636
}
3737

38-
func checkLineNumber(lineNumber: Int, nLinesInBuffer: Int) -> Int {
39-
var checkedLineNumber = -1 //TODO: probably better to use an optional
40-
38+
enum errorTypes : Error{
39+
case invalidLineNumber
40+
}
41+
42+
func clampLineNumber(lineNumber: Int, nLinesInBuffer: Int) -> Int {
4143
if(lineNumber < 0) {
42-
checkedLineNumber = 0
44+
return 0
4345
}
4446
else if(lineNumber >= nLinesInBuffer) {
45-
checkedLineNumber = nLinesInBuffer - 1
47+
return nLinesInBuffer - 1
4648
}
4749
else {
48-
checkedLineNumber = lineNumber
50+
return lineNumber
4951
}
50-
51-
return checkedLineNumber
5252
}
5353

5454
func getLineLength(lineNumber: Int, nLinesInBuffer: Int, buffer: XCSourceTextBuffer) -> Int{
55-
let text = buffer.lines[checkLineNumber(lineNumber: lineNumber, nLinesInBuffer: nLinesInBuffer)] as! String
55+
let text = buffer.lines[lineNumber] as! String
5656
return text.characters.count
5757
}
5858

@@ -79,8 +79,8 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
7979
newRange.end.column = endColumn
8080
}
8181

82-
newRange.start.line = checkLineNumber(lineNumber: startLine, nLinesInBuffer: nLinesInBuffer)
83-
newRange.end.line = checkLineNumber(lineNumber: endLine, nLinesInBuffer: nLinesInBuffer)
82+
newRange.start.line = startLine
83+
newRange.end.line = endLine
8484

8585
return newRange
8686
}
@@ -93,122 +93,123 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
9393

9494
let service = connection.remoteObjectProxyWithErrorHandler(handler) as! VoiceCodeXPCServiceProtocol
9595

96-
service.get_latest_command() { (command) in
97-
let buffer = invocation.buffer
96+
service.getLatestCommand() { (command) in
9897
//NSLog(buffer.contentUTI)
9998
NSLog(command)
10099

101-
let dataFromString = command.data(using: .utf8)
102-
let json = try? JSON(data: dataFromString!)
103-
let nLinesInBuffer = buffer.lines.count
104-
105-
switch(json!["id"].stringValue) {
106-
107-
case "initial-state":
108-
NSLog("initial state - not handled")
109-
break
110-
// MARK: -
111-
// MARK: Editor overrides
112-
case "editor:move-to-line-number":
113-
let line = json!["line"].intValue - 1
114-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer)
115-
break
116-
case "editor:move-to-line-number-and-way-right":
117-
let line = json!["line"].intValue - 1
118-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
100+
do {
101+
let json = try JSON(data: command.data(using: .utf8)!)
119102

120-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: lineLength - 1, endColumn: lineLength - 1, numberOfColumnsInLine: lineLength)
121-
// TODO: tell xcode 2 navigate to selection in case selection was offscreen
122-
break
123-
case "editor:move-to-line-number-and-way-left":
124-
let line = json!["line"].intValue - 1
125-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
126-
127-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: 0, numberOfColumnsInLine: lineLength)
128-
// TODO: tell xcode 2 navigate to selection in case selection was offscreen
129-
break
130-
case "editor:insert-under-line-number":
131-
let line = checkLineNumber(lineNumber: json!["line"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
132-
//TODO: probably better to use an optional
133-
if line > -1 {
134-
buffer.lines.insert("", at: line)
135-
}
136-
break
137-
case "editor:select-line-number":
138-
let line = json!["line"].intValue - 1
139-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
140-
141-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
142-
143-
// TODO: tell xcode 2 navigate to selection in case selection was offscreen
144-
break
145-
// case "editor:expand-selection-to-scope":
146-
// break
147-
// case "editor:click-expand-selection-to-scope":
148-
// break
149-
case "editor:select-line-number-range":
150-
let firstLine = json!["first"].intValue - 1
151-
let lastLine = json!["last"].intValue - 1
152-
let lineLength = getLineLength(lineNumber: lastLine, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
153-
154-
buffer.selections[0] = makeRange(startLine: firstLine, endLine: lastLine, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
155-
156-
// TODO: tell xcode 2 navigate to selection in case selection was offscreen
157-
break
158-
case "editor:extend-selection-to-line-number":
159-
break
160-
case "editor:insert-from-line-number":
161-
//TODO: this is not quite working
162-
let line = json!["line"].intValue - 1
163-
let checkedLine = checkLineNumber(lineNumber: line, nLinesInBuffer: nLinesInBuffer)
164-
//TODO: probably better to use an optional
165-
let range = buffer.selections[0] as! XCSourceTextRange
166-
if checkedLine > -1 && isInsertionPoint(range: range) {
167-
var currentLine = buffer.lines[range.start.line] as! String
168-
let textToInsert = buffer.lines[checkedLine] as! String
169-
let insertIndex = currentLine.index(currentLine.startIndex, offsetBy: range.start.column)
170-
currentLine.insert(contentsOf: textToInsert.characters, at: insertIndex)
171-
buffer.lines.replaceObject(at: range.start.line, with: currentLine)
172-
}
173-
break
174-
// case "editor:toggle-comments":
175-
// break
176-
// case "editor:insert-code-template":
177-
// break
178-
// case "editor:complete-code-template":
179-
// break
180-
181-
// MARK: -
182-
// MARK: Selection overrides
183-
// case "selection:previous-occurrence":
184-
// break
185-
// case "selection:next-occurrence":
186-
// break
187-
// case "selection:extend-to-next-occurrence":
188-
// break
189-
// case "selection:extend-to-previous-occurrence":
190-
// break
191-
// case "selection:previous-selection-occurrence":
192-
// break
193-
// case "selection:next-selection-occurrence":
194-
// break
195-
// case "selection:range-upward":
196-
// break
197-
// case "selection:range-downward":
198-
// break
199-
// case "selection:range-on-current-line":
200-
// break
201-
// case "selection:previous-word-by-surrounding-characters":
202-
// break
203-
// case "selection:next-word-by-surrounding-characters":
204-
// break
205-
default:
206-
NSLog("not handled")
103+
try parseJSON(buffer: invocation.buffer, json: json)
104+
}
105+
catch {
106+
NSLog("error parsing json")
207107
}
208108

209109
connection.invalidate()
210110
completionHandler(nil)
211111
}
212112
}
213113

114+
func parseJSON(buffer: XCSourceTextBuffer, json : JSON) throws {
115+
let nLinesInBuffer = buffer.lines.count
116+
let line = clampLineNumber(lineNumber: json["line"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
117+
118+
switch(json["id"].stringValue) {
119+
120+
case "initial-state":
121+
NSLog("initial state - not handled")
122+
break
123+
124+
// MARK: -
125+
// MARK: Editor overrides
126+
case "editor:move-to-line-number":
127+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer)
128+
break
129+
case "editor:move-to-line-number-and-way-right":
130+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
131+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: lineLength - 1, endColumn: lineLength - 1, numberOfColumnsInLine: lineLength)
132+
// TODO: tell xcode to navigate to selection in case selection was offscreen
133+
break
134+
case "editor:move-to-line-number-and-way-left":
135+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
136+
137+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: 0, numberOfColumnsInLine: lineLength)
138+
// TODO: tell xcode to navigate to selection in case selection was offscreen
139+
break
140+
case "editor:insert-under-line-number":
141+
buffer.lines.insert("", at: line)
142+
break
143+
case "editor:select-line-number":
144+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
145+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
146+
147+
// TODO: tell xcode to navigate to selection in case selection was offscreen
148+
break
149+
// case "editor:expand-selection-to-scope":
150+
// break
151+
// case "editor:click-expand-selection-to-scope":
152+
// break
153+
case "editor:select-line-number-range":
154+
let lastLine = clampLineNumber(lineNumber: json["lastline"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
155+
let lineLength = getLineLength(lineNumber: lastLine, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
156+
157+
buffer.selections[0] = makeRange(startLine: line, endLine: lastLine, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
158+
159+
// TODO: tell xcode to navigate to selection in case selection was offscreen
160+
break
161+
case "editor:extend-selection-to-line-number":
162+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
163+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
164+
buffer.selections[0] = makeRange(startLine: currentSelectionRange.start.line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
165+
166+
break
167+
case "editor:insert-from-line-number":
168+
//TODO: this is not quite working
169+
let range = buffer.selections[0] as! XCSourceTextRange
170+
if isInsertionPoint(range: range) {
171+
var currentLine = buffer.lines[range.start.line] as! String
172+
let textToInsert = buffer.lines[line] as! String //TODO: if line is out of range nothing should happen here
173+
let insertIndex = currentLine.index(currentLine.startIndex, offsetBy: range.start.column)
174+
currentLine.insert(contentsOf: textToInsert.characters, at: insertIndex)
175+
buffer.lines.replaceObject(at: range.start.line, with: currentLine)
176+
}
177+
break
178+
// case "editor:toggle-comments":
179+
// break
180+
// case "editor:insert-code-template":
181+
// break
182+
// case "editor:complete-code-template":
183+
// break
184+
185+
// MARK: -
186+
// MARK: Selection overrides
187+
// case "selection:previous-occurrence":
188+
// break
189+
// case "selection:next-occurrence":
190+
// break
191+
// case "selection:extend-to-next-occurrence":
192+
// break
193+
// case "selection:extend-to-previous-occurrence":
194+
// break
195+
// case "selection:previous-selection-occurrence":
196+
// break
197+
// case "selection:next-selection-occurrence":
198+
// break
199+
// case "selection:range-upward":
200+
// break
201+
// case "selection:range-downward":
202+
// break
203+
// case "selection:range-on-current-line":
204+
// break
205+
// case "selection:previous-word-by-surrounding-characters":
206+
// break
207+
// case "selection:next-word-by-surrounding-characters":
208+
// break
209+
default:
210+
NSLog("not handled")
211+
}
212+
213+
}
214+
214215
}

SourceEditorExtension/VoiceCodeSourceEditorExtension.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,5 @@ import XcodeKit
1212
class SourceEditorExtension: NSObject, XCSourceEditorExtension {
1313

1414
func extensionDidFinishLaunching() {
15-
//print("Extension launched...")
1615
}
17-
18-
/*
19-
var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
20-
// If your extension needs to return a collection of command definitions that differs from those in its Info.plist, implement this optional property getter.
21-
return []
22-
}
23-
*/
24-
2516
}

XPCService/VoiceCodeXPCService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Starscream
1515
socket.connect()
1616
}
1717

18-
func get_latest_command(withReply: (String) -> ()) {
18+
func getLatestCommand(withReply: (String) -> ()) {
1919

2020
while(!socket.isConnected) {
2121
// NSLog("connecting")

XPCService/VoiceCodeXPCServiceProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
import Foundation
33

44
@objc protocol VoiceCodeXPCServiceProtocol {
5-
func get_latest_command(withReply: (String)->())
5+
func getLatestCommand(withReply: (String)->())
66
}

0 commit comments

Comments
 (0)