8
8
import Foundation
9
9
import LanguageServerProtocol
10
10
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 ] ) {
21
42
switch edit {
22
43
case . optionA( let textEdit) :
23
44
edits. append ( textEdit)
@@ -30,8 +51,8 @@ func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [T
30
51
)
31
52
}
32
53
}
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 ] ) {
35
56
let endPosition = Position ( ( startPosition. line, startPosition. character + insertText. count) )
36
57
edits. append (
37
58
TextEdit (
@@ -40,22 +61,14 @@ func getCompletionItemEdits(startPosition: Position, item: CompletionItem) -> [T
40
61
)
41
62
)
42
63
}
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) )
46
67
edits. append (
47
68
TextEdit (
48
69
range: LSPRange ( start: startPosition, end: endPosition) ,
49
- newText: item . label
70
+ newText: label
50
71
)
51
72
)
52
73
}
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
61
74
}
0 commit comments