@@ -6,37 +6,41 @@ import {MOCK_COMPLETION_POS} from './mock';
6
6
describe ( 'CompletionFormatter' , ( ) => {
7
7
describe ( 'create' , ( ) => {
8
8
it ( 'should create a new instance of CompletionFormatter' , ( ) => {
9
- const formatter = CompletionFormatter . create (
9
+ const formatter = new CompletionFormatter (
10
10
'const greeting = "Hello, World!";' ,
11
11
MOCK_COMPLETION_POS . column ,
12
+ '' ,
12
13
) ;
13
14
expect ( formatter ) . toBeInstanceOf ( CompletionFormatter ) ;
14
15
} ) ;
15
16
} ) ;
16
17
17
18
describe ( 'removeInvalidLineBreaks' , ( ) => {
18
19
it ( 'should remove trailing line breaks' , ( ) => {
19
- const formatter = CompletionFormatter . create (
20
+ const formatter = new CompletionFormatter (
20
21
'function sum(a, b) {\n return a + b;\n}\n\n' ,
21
22
MOCK_COMPLETION_POS . column ,
23
+ '' ,
22
24
) ;
23
25
const result = formatter . removeInvalidLineBreaks ( ) . build ( ) ;
24
26
expect ( result ) . toBe ( 'function sum(a, b) {\n return a + b;\n}' ) ;
25
27
} ) ;
26
28
27
29
it ( 'should not remove line breaks in the middle of the text' , ( ) => {
28
- const formatter = CompletionFormatter . create (
30
+ const formatter = new CompletionFormatter (
29
31
'const x = 5;\nconst y = 10;\nconst sum = x + y;' ,
30
32
MOCK_COMPLETION_POS . column ,
33
+ '' ,
31
34
) ;
32
35
const result = formatter . removeInvalidLineBreaks ( ) . build ( ) ;
33
36
expect ( result ) . toBe ( 'const x = 5;\nconst y = 10;\nconst sum = x + y;' ) ;
34
37
} ) ;
35
38
36
39
it ( 'should handle empty string' , ( ) => {
37
- const formatter = CompletionFormatter . create (
40
+ const formatter = new CompletionFormatter (
38
41
'' ,
39
42
MOCK_COMPLETION_POS . column ,
43
+ '' ,
40
44
) ;
41
45
const result = formatter . removeInvalidLineBreaks ( ) . build ( ) ;
42
46
expect ( result ) . toBe ( '' ) ;
@@ -45,18 +49,20 @@ describe('CompletionFormatter', () => {
45
49
46
50
describe ( 'removeMarkdownCodeSyntax' , ( ) => {
47
51
it ( 'should remove markdown code block syntax' , ( ) => {
48
- const formatter = CompletionFormatter . create (
52
+ const formatter = new CompletionFormatter (
49
53
'```\nconst array = [1, 2, 3];\narray.map(x => x * 2);\n```' ,
50
54
MOCK_COMPLETION_POS . column ,
55
+ '' ,
51
56
) ;
52
57
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
53
58
expect ( result ) . toBe ( 'const array = [1, 2, 3];\narray.map(x => x * 2);' ) ;
54
59
} ) ;
55
60
56
61
it ( 'should remove multiple markdown code blocks' , ( ) => {
57
- const formatter = CompletionFormatter . create (
62
+ const formatter = new CompletionFormatter (
58
63
'```\nfunction greet(name) {\n return `Hello, ${name}!`;\n}\n```\nSome text\n```\nconst result = greet("Alice");\nconsole.log(result);\n```' ,
59
64
MOCK_COMPLETION_POS . column ,
65
+ '' ,
60
66
) ;
61
67
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
62
68
expect ( result ) . toBe (
@@ -65,9 +71,10 @@ describe('CompletionFormatter', () => {
65
71
} ) ;
66
72
67
73
it ( 'should handle code blocks with language specifiers' , ( ) => {
68
- const formatter = CompletionFormatter . create (
74
+ const formatter = new CompletionFormatter (
69
75
'```javascript\nclass Person {\n constructor(name) {\n this.name = name;\n }\n}\n```' ,
70
76
MOCK_COMPLETION_POS . column ,
77
+ '' ,
71
78
) ;
72
79
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
73
80
expect ( result ) . toBe (
@@ -76,27 +83,30 @@ describe('CompletionFormatter', () => {
76
83
} ) ;
77
84
78
85
it ( 'should not modify text without code blocks' , ( ) => {
79
- const formatter = CompletionFormatter . create (
86
+ const formatter = new CompletionFormatter (
80
87
'const PI = 3.14159;' ,
81
88
MOCK_COMPLETION_POS . column ,
89
+ '' ,
82
90
) ;
83
91
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
84
92
expect ( result ) . toBe ( 'const PI = 3.14159;' ) ;
85
93
} ) ;
86
94
87
95
it ( 'should handle empty string' , ( ) => {
88
- const formatter = CompletionFormatter . create (
96
+ const formatter = new CompletionFormatter (
89
97
'' ,
90
98
MOCK_COMPLETION_POS . column ,
99
+ '' ,
91
100
) ;
92
101
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
93
102
expect ( result ) . toBe ( '' ) ;
94
103
} ) ;
95
104
96
105
it ( 'should handle incomplete code blocks' , ( ) => {
97
- const formatter = CompletionFormatter . create (
106
+ const formatter = new CompletionFormatter (
98
107
'```\nconst incomplete = true;' ,
99
108
MOCK_COMPLETION_POS . column ,
109
+ '' ,
100
110
) ;
101
111
const result = formatter . removeMarkdownCodeSyntax ( ) . build ( ) ;
102
112
expect ( result ) . toBe ( '```\nconst incomplete = true;' ) ;
@@ -105,9 +115,10 @@ describe('CompletionFormatter', () => {
105
115
106
116
describe ( 'removeExcessiveNewlines' , ( ) => {
107
117
it ( 'should replace three or more consecutive newlines with two newlines' , ( ) => {
108
- const formatter = CompletionFormatter . create (
118
+ const formatter = new CompletionFormatter (
109
119
'import React from "react";\n\n\n\nconst App = () => {\n return <div>Hello React</div>;\n};' ,
110
120
MOCK_COMPLETION_POS . column ,
121
+ '' ,
111
122
) ;
112
123
const result = formatter . removeExcessiveNewlines ( ) . build ( ) ;
113
124
expect ( result ) . toBe (
@@ -116,18 +127,20 @@ describe('CompletionFormatter', () => {
116
127
} ) ;
117
128
118
129
it ( 'should not modify text with two or fewer consecutive newlines' , ( ) => {
119
- const formatter = CompletionFormatter . create (
130
+ const formatter = new CompletionFormatter (
120
131
'const x = 10;\n\nconst y = 20;' ,
121
132
MOCK_COMPLETION_POS . column ,
133
+ '' ,
122
134
) ;
123
135
const result = formatter . removeExcessiveNewlines ( ) . build ( ) ;
124
136
expect ( result ) . toBe ( 'const x = 10;\n\nconst y = 20;' ) ;
125
137
} ) ;
126
138
127
139
it ( 'should handle multiple occurrences of excessive newlines' , ( ) => {
128
- const formatter = CompletionFormatter . create (
140
+ const formatter = new CompletionFormatter (
129
141
'function add(a, b) {\n return a + b;\n}\n\n\nfunction subtract(a, b) {\n return a - b;\n}\n\n\n\nconst result = add(5, 3);' ,
130
142
MOCK_COMPLETION_POS . column ,
143
+ '' ,
131
144
) ;
132
145
const result = formatter . removeExcessiveNewlines ( ) . build ( ) ;
133
146
expect ( result ) . toBe (
@@ -136,18 +149,20 @@ describe('CompletionFormatter', () => {
136
149
} ) ;
137
150
138
151
it ( 'should handle empty string' , ( ) => {
139
- const formatter = CompletionFormatter . create (
152
+ const formatter = new CompletionFormatter (
140
153
'' ,
141
154
MOCK_COMPLETION_POS . column ,
155
+ '' ,
142
156
) ;
143
157
const result = formatter . removeExcessiveNewlines ( ) . build ( ) ;
144
158
expect ( result ) . toBe ( '' ) ;
145
159
} ) ;
146
160
147
161
it ( 'should handle string with only newlines' , ( ) => {
148
- const formatter = CompletionFormatter . create (
162
+ const formatter = new CompletionFormatter (
149
163
'\n\n\n\n' ,
150
164
MOCK_COMPLETION_POS . column ,
165
+ '' ,
151
166
) ;
152
167
const result = formatter . removeExcessiveNewlines ( ) . build ( ) ;
153
168
expect ( result ) . toBe ( '\n\n' ) ;
@@ -156,43 +171,56 @@ describe('CompletionFormatter', () => {
156
171
157
172
describe ( 'indentByColumn' , ( ) => {
158
173
it ( 'should indent subsequent lines by the current column position' , ( ) => {
159
- const formatter = CompletionFormatter . create (
174
+ const formatter = new CompletionFormatter (
160
175
'const numbers = [\n1,\n2,\n3\n];' ,
161
176
4 ,
177
+ '' ,
162
178
) ;
163
179
const result = formatter . indentByColumn ( ) . build ( ) ;
164
180
expect ( result ) . toBe ( 'const numbers = [\n 1,\n 2,\n 3\n ];' ) ;
165
181
} ) ;
166
182
167
183
it ( 'should not modify single line text' , ( ) => {
168
- const formatter = CompletionFormatter . create ( 'const x = 42;' , 3 ) ;
184
+ const formatter = new CompletionFormatter ( 'const x = 42;' , 3 , '' ) ;
169
185
const result = formatter . indentByColumn ( ) . build ( ) ;
170
186
expect ( result ) . toBe ( 'const x = 42;' ) ;
171
187
} ) ;
172
188
173
189
it ( 'should handle empty string' , ( ) => {
174
- const formatter = CompletionFormatter . create ( '' , 5 ) ;
190
+ const formatter = new CompletionFormatter ( '' , 5 , '' ) ;
175
191
const result = formatter . indentByColumn ( ) . build ( ) ;
176
192
expect ( result ) . toBe ( '' ) ;
177
193
} ) ;
178
194
179
195
it ( 'should handle text with existing indentation' , ( ) => {
180
- const formatter = CompletionFormatter . create (
196
+ const formatter = new CompletionFormatter (
181
197
'function test() {\n const x = 1;\n return x;\n}' ,
182
198
6 ,
199
+ '' ,
183
200
) ;
184
201
const result = formatter . indentByColumn ( ) . build ( ) ;
185
202
expect ( result ) . toBe (
186
203
'function test() {\n const x = 1;\n return x;\n }' ,
187
204
) ;
188
205
} ) ;
206
+
207
+ it ( 'should not indent when there is text before cursor in same line' , ( ) => {
208
+ const formatter = new CompletionFormatter (
209
+ '= [\n1,\n2\n];' ,
210
+ 8 ,
211
+ 'const x ' ,
212
+ ) ;
213
+ const result = formatter . indentByColumn ( ) . build ( ) ;
214
+ expect ( result ) . toBe ( '= [\n1,\n2\n];' ) ;
215
+ } ) ;
189
216
} ) ;
190
217
191
218
describe ( 'build' , ( ) => {
192
219
it ( 'should return the formatted completion' , ( ) => {
193
- const formatter = CompletionFormatter . create (
220
+ const formatter = new CompletionFormatter (
194
221
' const square = (x) => x * x; ' ,
195
222
MOCK_COMPLETION_POS . column ,
223
+ '' ,
196
224
) ;
197
225
const result = formatter . build ( ) ;
198
226
expect ( result ) . toBe ( ' const square = (x) => x * x; ' ) ;
@@ -201,9 +229,10 @@ describe('CompletionFormatter', () => {
201
229
202
230
describe ( 'chaining methods' , ( ) => {
203
231
it ( 'should allow chaining of multiple formatting methods' , ( ) => {
204
- const formatter = CompletionFormatter . create (
232
+ const formatter = new CompletionFormatter (
205
233
'```\nconst fruits = ["apple", "banana", "orange"];\nconst upperFruits = fruits.map(fruit => fruit.toUpperCase());\n```\n\n\n\nconsole.log(upperFruits);' ,
206
234
MOCK_COMPLETION_POS . column ,
235
+ '' ,
207
236
) ;
208
237
const result = formatter
209
238
. removeMarkdownCodeSyntax ( )
@@ -216,9 +245,10 @@ describe('CompletionFormatter', () => {
216
245
} ) ;
217
246
218
247
it ( 'should handle empty string with all formatting methods' , ( ) => {
219
- const formatter = CompletionFormatter . create (
248
+ const formatter = new CompletionFormatter (
220
249
'' ,
221
250
MOCK_COMPLETION_POS . column ,
251
+ '' ,
222
252
) ;
223
253
const result = formatter
224
254
. removeMarkdownCodeSyntax ( )
0 commit comments