@@ -101,6 +101,62 @@ describe('inlineCompletionService', function () {
101
101
} )
102
102
} )
103
103
104
+ describe ( 'truncateOverlapWithRightContext' , function ( ) {
105
+ const fileName = 'test.py'
106
+ const language = 'python'
107
+ let rightContext = 'return target\n'
108
+ const doc = `import math\ndef two_sum(nums, target):\n`
109
+ const provider = new CWInlineCompletionItemProvider ( 0 , 0 )
110
+
111
+ it ( 'removes overlap with right context from suggestion' , async function ( ) {
112
+ const mockSuggestion = 'return target\n'
113
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
114
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
115
+ assert . strictEqual ( result , '' )
116
+ } )
117
+
118
+ it ( 'only removes the overlap part from suggestion' , async function ( ) {
119
+ const mockSuggestion = 'print(nums)\nreturn target\n'
120
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
121
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
122
+ assert . strictEqual ( result , 'print(nums)\n' )
123
+ } )
124
+
125
+ it ( 'only removes the last overlap pattern from suggestion' , async function ( ) {
126
+ const mockSuggestion = 'return target\nprint(nums)\nreturn target\n'
127
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
128
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
129
+ assert . strictEqual ( result , 'return target\nprint(nums)\n' )
130
+ } )
131
+
132
+ it ( 'returns empty string if the remaining suggestion only contains white space' , async function ( ) {
133
+ const mockSuggestion = 'return target\n '
134
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
135
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
136
+ assert . strictEqual ( result , '' )
137
+ } )
138
+
139
+ it ( 'returns the original suggestion if no match found' , async function ( ) {
140
+ const mockSuggestion = 'import numpy\n'
141
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
142
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
143
+ assert . strictEqual ( result , 'import numpy\n' )
144
+ } )
145
+ it ( 'ignores the space at the end of recommendation' , async function ( ) {
146
+ const mockSuggestion = 'return target\n\n\n\n\n'
147
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
148
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
149
+ assert . strictEqual ( result , '' )
150
+ } )
151
+ it ( 'ignores the space at the start of right context' , async function ( ) {
152
+ rightContext = '\n\n\n\nreturn target'
153
+ const mockEditor = createMockTextEditor ( `${ doc } ${ rightContext } ` , fileName , language )
154
+ const mockSuggestion = 'return target\n'
155
+ const result = provider . truncateOverlapWithRightContext ( mockEditor . document , mockSuggestion )
156
+ assert . strictEqual ( result , '' )
157
+ } )
158
+ } )
159
+
104
160
describe ( 'on event change' , async function ( ) {
105
161
beforeEach ( function ( ) {
106
162
const fakeReferences = [
0 commit comments