Skip to content

Commit 8bd8d69

Browse files
committed
getSelectedText implementation means that edit menu doesn't get triggered for prex etc.
1 parent 565eb83 commit 8bd8d69

3 files changed

Lines changed: 85 additions & 30 deletions

File tree

SourceEditorExtension/VoiceCodeSourceEditorCommand.swift

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,41 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
8585
return newRange
8686
}
8787

88+
func getSelectedText(currentSelectionRange: XCSourceTextRange) -> String {
89+
var selectedText = ""
90+
91+
if(isInsertionPoint(range: currentSelectionRange)) {
92+
return selectedText
93+
}
94+
95+
//all on same line
96+
if(currentSelectionRange.start.line == currentSelectionRange.end.line) {
97+
let lineStr = buffer.lines[currentSelectionRange.start.line] as! String
98+
let startIndex = lineStr.index(lineStr.startIndex, offsetBy: currentSelectionRange.start.column)
99+
let endIndex = lineStr.index(lineStr.startIndex, offsetBy: currentSelectionRange.end.column)
100+
let range = startIndex..<endIndex
101+
selectedText.append(lineStr.substring(with:range))
102+
}
103+
else {
104+
for index in currentSelectionRange.start.line...currentSelectionRange.end.line {
105+
let lineStr = buffer.lines[index] as! String
106+
if index == currentSelectionRange.start.line {
107+
let startIndex = lineStr.index(lineStr.startIndex, offsetBy: currentSelectionRange.start.column)
108+
selectedText.append(lineStr.substring(from: startIndex))
109+
}
110+
else if index == currentSelectionRange.end.line{
111+
let endIndex = lineStr.index(lineStr.startIndex, offsetBy: currentSelectionRange.end.column)
112+
selectedText.append(lineStr.substring(to: endIndex))
113+
}
114+
else{
115+
selectedText.append(lineStr)
116+
}
117+
}
118+
}
119+
120+
return selectedText
121+
}
122+
88123
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void) -> Void
89124
{
90125
let handler: (Error) -> () = { error in
@@ -100,7 +135,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
100135
do {
101136
let json = try JSON(data: command.data(using: .utf8)!)
102137

103-
try parseJSON(buffer: invocation.buffer, json: json)
138+
try parseJSON(buffer: invocation.buffer, json: json, service: service)
104139
}
105140
catch {
106141
NSLog("error parsing json")
@@ -111,7 +146,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
111146
}
112147
}
113148

114-
func parseJSON(buffer: XCSourceTextBuffer, json : JSON) throws {
149+
func parseJSON(buffer: XCSourceTextBuffer, json : JSON, service: VoiceCodeXPCServiceProtocol) throws {
115150
let nLinesInBuffer = buffer.lines.count
116151
let line = clampLineNumber(lineNumber: json["line"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
117152

@@ -124,29 +159,41 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
124159
break
125160

126161
// MARK: -
162+
// MARK: OS overrides
163+
case "os:get-selected-text":
164+
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
165+
166+
let message: JSON = [
167+
"id": "setSelectedText",
168+
"text": getSelectedText(currentSelectionRange)
169+
]
170+
171+
service.sendMessage(message: message.rawString()!)
172+
break
173+
// MARK: -
127174
// MARK: Editor overrides
128175
case "editor:move-to-line-number":
129176
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer)
177+
service.sendMessage(message: "{\"id\": \"jumpToSelection\"}")
130178
break
131179
case "editor:move-to-line-number-and-way-right":
132180
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
133181
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: lineLength - 1, endColumn: lineLength - 1, numberOfColumnsInLine: lineLength)
134-
// TODO: tell xcode to navigate to selection in case selection was offscreen
182+
service.sendMessage(message: "{\"id\": \"jumpToSelection\"}")
135183
break
136184
case "editor:move-to-line-number-and-way-left":
137185
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
138186

139187
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: 0, numberOfColumnsInLine: lineLength)
140-
// TODO: tell xcode to navigate to selection in case selection was offscreen
188+
service.sendMessage(message: "{\"id\": \"jumpToSelection\"}")
141189
break
142190
case "editor:insert-under-line-number":
143191
buffer.lines.insert("", at: line)
144192
break
145193
case "editor:select-line-number":
146194
let lineLength = getLineLength(lineNumber: line, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
147195
buffer.selections[0] = makeRange(startLine: line, endLine: line, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
148-
149-
// TODO: tell xcode to navigate to selection in case selection was offscreen
196+
service.sendMessage(message: "{\"id\": \"jumpToSelection\"}")
150197
break
151198
// case "editor:expand-selection-to-scope":
152199
// break
@@ -157,8 +204,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
157204
let lineLength = getLineLength(lineNumber: lastLine, nLinesInBuffer: nLinesInBuffer, buffer: buffer)
158205

159206
buffer.selections[0] = makeRange(startLine: line, endLine: lastLine, nLinesInBuffer: nLinesInBuffer, startColumn: 0, endColumn: lineLength, numberOfColumnsInLine: lineLength)
160-
161-
// TODO: tell xcode to navigate to selection in case selection was offscreen
207+
service.sendMessage(message: "{\"id\": \"jumpToSelection\"}")
162208
break
163209
case "editor:extend-selection-to-line-number":
164210
let currentSelectionRange = buffer.selections[0] as! XCSourceTextRange
@@ -186,28 +232,32 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
186232

187233
// MARK: -
188234
// MARK: Selection overrides
189-
// case "selection:previous-occurrence":
190-
// break
191-
// case "selection:next-occurrence":
192-
// break
193-
// case "selection:extend-to-next-occurrence":
194-
// break
195-
// case "selection:extend-to-previous-occurrence":
196-
// break
197-
// case "selection:previous-selection-occurrence":
198-
// break
199-
// case "selection:next-selection-occurrence":
200-
// break
201-
// case "selection:range-upward":
202-
// break
203-
// case "selection:range-downward":
204-
// break
205-
// case "selection:range-on-current-line":
206-
// break
207-
// case "selection:previous-word-by-surrounding-characters":
208-
// break
209-
// case "selection:next-word-by-surrounding-characters":
210-
// break
235+
case "selection:previous-occurrence":
236+
let range = buffer.selections[0] as! XCSourceTextRange
237+
let selectedText = getSelectedText(currentSelectionRange: range)
238+
239+
240+
break
241+
case "selection:next-occurrence":
242+
break
243+
case "selection:extend-to-next-occurrence":
244+
break
245+
case "selection:extend-to-previous-occurrence":
246+
break
247+
case "selection:previous-selection-occurrence":
248+
break
249+
case "selection:next-selection-occurrence":
250+
break
251+
case "selection:range-upward":
252+
break
253+
case "selection:range-downward":
254+
break
255+
case "selection:range-on-current-line":
256+
break
257+
case "selection:previous-word-by-surrounding-characters":
258+
break
259+
case "selection:next-word-by-surrounding-characters":
260+
break
211261
default:
212262
NSLog("not handled")
213263
}

XPCService/VoiceCodeXPCService.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import Starscream
1818
socket.connect()
1919
}
2020

21+
func sendMessage(message: String) {
22+
socket.write(string: message)
23+
}
24+
2125
func getLatestCommand(withReply: (String) -> ()) {
2226

2327
//we need to handle this better here - it will block until the connection is made, if it can't make the connection

XPCService/VoiceCodeXPCServiceProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import Foundation
33

44
@objc protocol VoiceCodeXPCServiceProtocol {
55
func getLatestCommand(withReply: (String)->())
6+
func sendMessage(message: String)
67
}

0 commit comments

Comments
 (0)