Skip to content

Commit 3478de1

Browse files
committed
oh dear a lot of stuff I forgot to commit incrementally
including removing unnecessary break statements implementing object:duplicate
1 parent ee36c89 commit 3478de1

1 file changed

Lines changed: 135 additions & 112 deletions

File tree

SourceEditorExtension/VoiceCodeSourceEditorCommand.swift

Lines changed: 135 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
6363
}
6464

6565
func makeRange(startLine: Int, endLine: Int, nLinesInBuffer: Int, startColumn: Int = 0, endColumn: Int = 0, numberOfColumnsInLine: Int = 0) -> XCSourceTextRange {
66+
//MARK: TODO: what am I doing with nLinesInBuffer?
6667
let newRange = XCSourceTextRange()
6768

6869
if(startColumn < 0) {
6970
newRange.start.column = 0
7071
}
7172
else if(startColumn > numberOfColumnsInLine) {
72-
newRange.start.column = numberOfColumnsInLine
73+
newRange.start.column = numberOfColumnsInLine - 1
7374
}
7475
else {
7576
newRange.start.column = startColumn
@@ -79,7 +80,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
7980
newRange.end.column = 0
8081
}
8182
else if(endColumn > numberOfColumnsInLine) {
82-
newRange.end.column = numberOfColumnsInLine
83+
newRange.end.column = numberOfColumnsInLine - 1
8384
}
8485
else {
8586
newRange.end.column = endColumn
@@ -157,126 +158,148 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
157158
let line = clampLineNumber(lineNumber: json["line"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
158159

159160
switch(json["id"].stringValue) {
160-
case "no message":
161-
NSLog("Could not connect to VoiceCode websocket")
162-
break
163-
case "initial-state":
164-
NSLog("Initial state - not handled")
165-
break
166-
167-
// MARK: -
168-
// MARK: OS overrides
169-
case "os:get-selected-text":
170-
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
171-
172-
let message: JSON = [
173-
"id": "setSelectedText",
174-
"text": getSelectedText(selectionRange: currentSelectionRange, buffer: buffer)
175-
]
176-
177-
service.sendMessage(message: message.rawString()!)
178-
break
179-
// MARK: -
180-
// MARK: Editor overrides
181-
case "editor:move-to-line-number":
182-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer)
183-
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
184-
break
185-
case "editor:move-to-line-number-and-way-right":
186-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
187-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: lineLength - 1, endColumn: lineLength - 1, numberOfColumnsInLine: lineLength)
188-
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
189-
break
190-
case "editor:move-to-line-number-and-way-left":
191-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
192-
193-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: 0, numberOfColumnsInLine: lineLength)
194-
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
195-
break
196-
case "editor:insert-under-line-number":
197-
buffer.lines.insert("", at: line)
198-
break
199-
case "editor:select-line-number":
200-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
201-
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
202-
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
203-
break
204-
case "editor:expand-selection-to-scope":
205-
//MARK: TODO: editor:expand-selection-to-scope
206-
break
207-
case "editor:click-expand-selection-to-scope":
208-
//MARK: TODO: editor:click-selection-to-scope
209-
break
210-
case "editor:select-line-number-range":
211-
let lastLine = clampLineNumber(lineNumber: json["lastline"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
212-
let lineLength = getLineLength(lineNumber: lastLine, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
161+
case "no message":
162+
NSLog("Could not connect to VoiceCode websocket")
163+
164+
case "initial-state":
165+
NSLog("Initial state - not handled")
166+
167+
168+
// MARK: -
169+
// MARK: OS overrides
170+
case "os:get-selected-text":
171+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
172+
173+
let message: JSON = [
174+
"id": "setSelectedText",
175+
"text": getSelectedText(selectionRange: currentSelectionRange, buffer: buffer)
176+
]
177+
178+
service.sendMessage(message: message.rawString()!)
179+
180+
// MARK: -
181+
// MARK: DELETE overrides
182+
// case "delete:delete-lines":
183+
//MARK: TODO: case "delete:delete-lines":
213184

214-
buffer.selections[0] = makeRange(startLine: line, endLine: lastLine, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
215-
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
216-
break
217-
case "editor:extend-selection-to-line-number":
218-
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
219-
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
220-
buffer.selections[0] = makeRange(startLine: currentSelectionRange.start.line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
185+
// MARK: -
186+
// MARK: OBJECT overrides
187+
case "object:duplicate":
188+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
189+
let numberOfLinesSelected = (currentSelectionRange.end.line - currentSelectionRange.start.line) + 1;
190+
let insertionPoint = currentSelectionRange.end.line;
191+
192+
var linesToInsert : [String] = []
193+
for readLine in 0..<numberOfLinesSelected {
194+
let currentLine = buffer.lines[currentSelectionRange.start.line + readLine] as! String
195+
linesToInsert.append(currentLine)
196+
}
197+
198+
let indexes = IndexSet((currentSelectionRange.end.line + 1)...(currentSelectionRange.end.line+numberOfLinesSelected))
199+
buffer.lines.insert(linesToInsert, at:indexes)
200+
buffer.selections[0] = makeRange(startLine: insertionPoint + numberOfLinesSelected, endLine: insertionPoint + numberOfLinesSelected, nLinesInBuffer: nLinesInBuffer)
221201

222-
break
223-
case "editor:insert-from-line-number":
224-
let range = buffer.selections[0] as! XCSourceTextRange
225-
if isInsertionPoint(range: range) {
226-
var currentLine = buffer.lines[range.start.line] as! String
227-
var textToInsert = buffer.lines[line] as! String //MARK: TODO: if line is out of range nothing should happen here
228-
let insertIndex = currentLine.index(currentLine.startIndex, offsetBy: range.start.column)
229-
currentLine.insert(contentsOf: textToInsert.characters, at: insertIndex)
230-
currentLine.remove(at: currentLine.index(currentLine.endIndex, offsetBy: -1))
231-
buffer.lines.replaceObject(at: range.start.line, with: currentLine)
232-
}
233-
break
234-
// case "editor:toggle-comments":
235-
// break
236-
// case "editor:insert-code-template":
237-
// break
238-
// case "editor:complete-code-template":
239-
// break
240202
// MARK: -
241-
// MARK: Selection overrides
242-
case "selection:previous-occurrence":
243-
//MARK: TODO: selection:previous-occurrence
203+
// MARK: EDITOR overrides
204+
case "editor:move-to-line-number":
205+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer)
206+
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
207+
208+
case "editor:move-to-line-number-and-way-right":
209+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
210+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: lineLength - 1, endColumn: lineLength - 1, numberOfColumnsInLine: lineLength)
211+
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
212+
213+
case "editor:move-to-line-number-and-way-left":
214+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
215+
216+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: 0, numberOfColumnsInLine: lineLength)
217+
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
218+
219+
case "editor:insert-under-line-number":
220+
buffer.lines.insert("", at: line)
244221

245-
break
246-
case "selection:next-occurrence":
247-
//MARK: TODO: selection:next-occurrence
222+
case "editor:select-line-number":
223+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
224+
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
225+
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
248226

249-
break
250-
case "selection:extend-to-next-occurrence":
251-
//MARK: TODO: selection:extend-to-next-occurrence
227+
// case "editor:expand-selection-to-scope":
228+
//MARK: TODO: editor:expand-selection-to-scope
252229

253-
break
254-
case "selection:extend-to-previous-occurrence":
255-
//MARK: TODO: selection:extend-to-previous-occurrence
230+
// case "editor:click-expand-selection-to-scope":
231+
//MARK: TODO: editor:click-selection-to-scope
256232

257-
break
258-
// case "selection:previous-selection-occurrence":
259-
// break
260-
// case "selection:next-selection-occurrence":
261-
// break
262-
// case "selection:range-upward":
263-
// break
264-
// case "selection:range-downward":
265-
// break
266-
case "selection:range-on-current-line":
267-
//MARK: TODO: selection:range-on-current-line
233+
case "editor:select-line-number-range":
234+
let lastLine = clampLineNumber(lineNumber: json["lastline"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
235+
let lineLength = getLineLength(lineNumber: lastLine, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
236+
237+
buffer.selections[0] = makeRange(startLine: line, endLine: lastLine, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
238+
service.sendMessage(message: jumpToSelectionMessage.rawString()!)
268239

269-
break
270-
case "selection:previous-word-by-surrounding-characters":
271-
//MARK: TODO: selection:previous-word-by-surrounding-characters
240+
case "editor:extend-selection-to-line-number":
241+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
242+
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
243+
buffer.selections[0] = makeRange(startLine: currentSelectionRange.start.line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
244+
272245

273-
break
274-
case "selection:next-word-by-surrounding-characters":
275-
//MARK: TODO: selection:next-word-by-surrounding-characters
246+
case "editor:insert-from-line-number":
247+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
248+
if isInsertionPoint(range: currentSelectionRange) {
249+
var currentLine = buffer.lines[currentSelectionRange.start.line] as! String
250+
var textToInsert = buffer.lines[line] as! String //MARK: TODO: if line is out of range nothing should happen here
251+
let insertIndex = currentLine.index(currentLine.startIndex, offsetBy: currentSelectionRange.start.column)
252+
currentLine.insert(contentsOf: textToInsert.characters, at: insertIndex)
253+
currentLine.remove(at: currentLine.index(currentLine.endIndex, offsetBy: -1))
254+
buffer.lines.replaceObject(at: currentSelectionRange.start.line, with: currentLine)
255+
}
256+
257+
// case "editor:toggle-comments":
258+
//
259+
// case "editor:insert-code-template":
260+
//
261+
// case "editor:complete-code-template":
262+
//
263+
// MARK: -
264+
// MARK: Selection overrides
265+
// case "selection:previous-occurrence":
266+
//MARK: TODO: selection:previous-occurrence
267+
268+
269+
// case "selection:next-occurrence":
270+
//MARK: TODO: selection:next-occurrence
271+
272+
273+
// case "selection:extend-to-next-occurrence":
274+
//MARK: TODO: selection:extend-to-next-occurrence
275+
276+
277+
// case "selection:extend-to-previous-occurrence":
278+
//MARK: TODO: selection:extend-to-previous-occurrence
276279

277-
break
278-
default:
279-
NSLog("not handled")
280+
281+
// case "selection:previous-selection-occurrence":
282+
//
283+
// case "selection:next-selection-occurrence":
284+
//
285+
// case "selection:range-upward":
286+
//
287+
// case "selection:range-downward":
288+
//
289+
// case "selection:range-on-current-line":
290+
//MARK: TODO: selection:range-on-current-line
291+
292+
293+
// case "selection:previous-word-by-surrounding-characters":
294+
//MARK: TODO: selection:previous-word-by-surrounding-characters
295+
296+
297+
// case "selection:next-word-by-surrounding-characters":
298+
//MARK: TODO: selection:next-word-by-surrounding-characters
299+
300+
301+
default:
302+
NSLog("not handled")
280303
}
281304

282305
}

0 commit comments

Comments
 (0)