Skip to content

Commit 93e6138

Browse files
committed
Add LSPCompletionItemsUtil Namespace
1 parent 665839b commit 93e6138

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

CodeEdit/Features/LSP/LSPService.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ final class LSPService: ObservableObject {
140140
// Handle the case where completions is an array of CompletionItem
141141
print("\n*******\nCompletion Items:\n*******\n")
142142
for item in completionItems {
143-
let textEdits = getCompletionItemEdits(startPosition: textPosition, item: item)
143+
let textEdits = LSPCompletionItemsUtil.getCompletionItemEdits(
144+
startPosition: textPosition,
145+
item: item
146+
)
144147
for edit in textEdits {
145148
print(edit)
146149
}
@@ -150,7 +153,10 @@ final class LSPService: ObservableObject {
150153
// Handle the case where completions is a CompletionList
151154
print("\n*******\nCompletion Items:\n*******\n")
152155
for item in completionList.items {
153-
let textEdits = getCompletionItemEdits(startPosition: textPosition, item: item)
156+
let textEdits = LSPCompletionItemsUtil.getCompletionItemEdits(
157+
startPosition: textPosition,
158+
item: item
159+
)
154160
for edit in textEdits {
155161
print(edit)
156162
}

CodeEdit/Features/LSP/LSPUtil.swift

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,37 @@
88
import Foundation
99
import LanguageServerProtocol
1010

11-
/// Helper function to get the edits from a completion item
12-
/// - Parameters:
13-
/// - startPosition: The position where the completion was requested
14-
/// - item: The completion item
15-
/// - Returns: An array of TextEdit objects
16-
func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [TextEdit] {
17-
var edits: [TextEdit] = []
18-
19-
// If a TextEdit or InsertReplaceEdit value was provided
20-
if let edit = item.textEdit {
11+
enum LSPCompletionItemsUtil {
12+
13+
/// Helper function to get the edits from a completion item
14+
/// - Parameters:
15+
/// - startPosition: The position where the completion was requested
16+
/// - item: The completion item
17+
/// - Returns: An array of TextEdit objects
18+
static func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [TextEdit] {
19+
var edits: [TextEdit] = []
20+
21+
// If a TextEdit or InsertReplaceEdit value was provided
22+
if let edit = item.textEdit {
23+
editOrReplaceItem(edit: edit, &edits)
24+
} else if let insertText = item.insertText {
25+
// If the `insertText` value was provided
26+
insertTextItem(startPosition: startPosition, insertText: insertText, &edits)
27+
} else if !item.label.isEmpty {
28+
// Fallback to the label
29+
labelItem(startPosition: startPosition, label: item.label, &edits)
30+
}
31+
32+
// If additional edits were provided
33+
// An example would be to also include an 'import' statement at the top of the file
34+
if let additionalEdits = item.additionalTextEdits {
35+
edits.append(contentsOf: additionalEdits)
36+
}
37+
38+
return edits
39+
}
40+
41+
private static func editOrReplaceItem(edit: TwoTypeOption<TextEdit, InsertReplaceEdit>, _ edits: inout [TextEdit]) {
2142
switch edit {
2243
case .optionA(let textEdit):
2344
edits.append(textEdit)
@@ -30,8 +51,8 @@ func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [T
3051
)
3152
}
3253
}
33-
// If the `insertText` value was provided
34-
else if let insertText = item.insertText {
54+
55+
private static func insertTextItem(startPosition: Position, insertText: String, _ edits: inout [TextEdit]) {
3556
let endPosition = Position((startPosition.line, startPosition.character + insertText.count))
3657
edits.append(
3758
TextEdit(
@@ -40,22 +61,14 @@ func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [T
4061
)
4162
)
4263
}
43-
// Fallback to the label
44-
else if item.label != "" {
45-
let endPosition = Position((startPosition.line, startPosition.character + item.label.count))
64+
65+
private static func labelItem(startPosition: Position, label: String, _ edits: inout [TextEdit]) {
66+
let endPosition = Position((startPosition.line, startPosition.character + label.count))
4667
edits.append(
4768
TextEdit(
4869
range: LSPRange(start: startPosition, end: endPosition),
49-
newText: item.label
70+
newText: label
5071
)
5172
)
5273
}
53-
54-
// If additional edits were provided
55-
// An example would be to also include an 'import' statement at the top of the file
56-
if let additionalEdits = item.additionalTextEdits {
57-
edits.append(contentsOf: additionalEdits)
58-
}
59-
60-
return edits
6174
}

0 commit comments

Comments
 (0)