/
TestScalaIndenter.scala
332 lines (277 loc) · 8.09 KB
/
TestScalaIndenter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package scala.tools.eclipse.ui
import org.junit.Assert._
import org.junit.runner.RunWith
import org.junit.internal.runners.JUnit4ClassRunner
import org.junit.Test
import org.junit.Before
import org.eclipse.jface.text.IDocument
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.DocumentCommand
import org.eclipse.jface.text.source.ISourceViewer
import org.eclipse.jface.preference.IPreferenceStore
import org.eclipse.jface.preference.PreferenceStore
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants
import org.eclipse.jdt.internal.ui.JavaPlugin
import org.eclipse.jdt.core.IJavaProject
import org.eclipse.jdt.core.JavaCore
import org.eclipse.jdt.internal.core.JavaProject
import org.eclipse.jdt.internal.core.JavaModelManager
import org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner
import org.eclipse.jdt.internal.core.JavaCorePreferenceInitializer
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.ui.internal.editors.text.EditorsPlugin
import java.util.Hashtable
import org.eclipse.jface.text.TextUtilities
@RunWith(classOf[JUnit4ClassRunner])
class TestScalaIndenter {
/**
* Needed because the only constructor for DocumentCommand is protected
*/
class InstantiableDocumentCommands extends DocumentCommand {
}
class MockPreferenceProvider extends PreferenceProvider {
def updateCache : Unit = {
// Nothing to do - we rely on someone setting up this cache
}
}
/**
* This class overrides several functions in ScalaAutoIndentStrategy to reduce
* the overhead of things that need to be initialised for testing
*/
class TestScalaAutoIndentStrategy(
partitioning : String,
project : IJavaProject,
viewer : ISourceViewer,
preferenceProvider : MockPreferenceProvider
) extends ScalaAutoIndentStrategy(partitioning, project, viewer, preferenceProvider) {
override def computeSmartMode : Boolean = {
return true
}
}
val CARET = "_|_"
@Before
def initialiseClass() {
// Initialisation fluff
if (JavaPlugin.getDefault() == null) {
new JavaPlugin()
}
if (EditorsPlugin.getDefault() == null) {
new EditorsPlugin()
}
if (JavaCore.getJavaCore() == null) {
new JavaCore()
}
}
def runTest(textSoFar : String, insert : String, expectedResultWithCaret : String) {
val document = new Document(textSoFar.replace(CARET, ""))
// Create the command with all needed information
val command = new InstantiableDocumentCommands()
command.text = insert
command.length = 0
command.offset = textSoFar.indexOf(CARET)
command.doit = true
command.shiftsCaret = true
command.caretOffset = -1
// Preferences for indentation
val preferenceProvider = new MockPreferenceProvider
// For succinctness we alias DefaultCodeFormatterConstants to Dcfc
import org.eclipse.jdt.core.formatter.{ DefaultCodeFormatterConstants => Dcfc }
preferenceProvider.put(PreferenceConstants.EDITOR_CLOSE_BRACES, "true")
preferenceProvider.put(PreferenceConstants.EDITOR_SMART_TAB, "true")
preferenceProvider.put(Dcfc.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK, "true")
preferenceProvider.put(Dcfc.FORMATTER_BRACE_POSITION_FOR_BLOCK, Dcfc.END_OF_LINE)
preferenceProvider.put(Dcfc.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_TYPE_HEADER, "true")
preferenceProvider.put(Dcfc.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, Dcfc.END_OF_LINE)
preferenceProvider.put(Dcfc.FORMATTER_TAB_CHAR, "space")
preferenceProvider.put(ScalaIndenter.TAB_SIZE, "2")
preferenceProvider.put(ScalaIndenter.INDENT_SIZE, "2")
val project = new JavaProject()
val indentStrategy = new TestScalaAutoIndentStrategy(null, project, null, preferenceProvider)
indentStrategy.customizeDocumentCommand(document, command)
// Allow for not moving the offset
val newOffset =
if (command.caretOffset == -1) {
(textSoFar.substring(0, textSoFar.indexOf(CARET)).replace(CARET, "") + command.text).length
} else {
command.caretOffset
}
val result =
textSoFar.substring(0, textSoFar.indexOf(CARET)).replace(CARET, "") +
command.text +
textSoFar.substring(textSoFar.indexOf(CARET)).replace(CARET, "")
// Replace system specific new lines in the result with a generic '\n'
val lineDelimiter = TextUtilities.getDefaultLineDelimiter(document)
val resultWithCleanNewLines = result.replaceAll(lineDelimiter, "\n")
val expectedResult = expectedResultWithCaret.replace(CARET, "")
assertEquals(expectedResult.replace(CARET, ""), resultWithCleanNewLines)
val expectedOffset = expectedResultWithCaret.lastIndexOf(CARET) - CARET.length
assertEquals(expectedOffset, newOffset)
}
/**
* Test:
* class x {<->
*
* Becomes
* class x {
* <->
* }
*/
@Test
def testClassIndent() {
val textSoFar =
"class x {" + CARET
val expectedResult =
textSoFar + "\n" +
" " + CARET + "\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* trait x {<->
*
* Becomes
* trait x {
* <->
* }
*/
@Test
def testTraitIndent() {
val textSoFar =
"trait x {" + CARET
val expectedResult =
textSoFar + "\n" +
" " + CARET + "\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* class x {
* def y = {<->
* }
*
* Becomes
* class x {
* def y = {
* <->
* }
* }
*/
@Test
def testDefIndent() {
val textSoFar =
"class x {\n" +
" def y = {" + CARET + "\n" +
"}"
val expectedResult =
"class x {\n" +
" def y = {" + CARET + "\n" +
" " + CARET + "\n" +
" }\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* class x {
* def y : Int = {<->
* }
*
* Becomes
* class x {
* def y : Int = {
* <->
* }
* }
*/
@Test
def defWithType() {
val textSoFar =
"class x {\n" +
" def y : Int = {" + CARET + "\n" +
"}"
val expectedResult =
"class x {\n" +
" def y : Int = {" + CARET + "\n" +
" " + CARET + "\n" +
" }\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* class x {
* val xs = List[x]<->
* }
*
* Becomes
* class x {
* val xs = List[x]
* <->
* }
*/
@Test
def testGenericsIndent() {
val textSoFar =
"class x {\n" +
" val xs = List[x]" + CARET + "\n" +
"}"
val expectedResult =
"class x {\n" +
" val xs = List[x]" + CARET + "\n" +
" " + CARET + "\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* class x {
* val xs = List[<->
* }
*
* Becomes
* class x {
* val xs = List[
* <->
* }
*/
@Test
def genericsIndentOverMultipleLines() {
val textSoFar =
"class x {\n" +
" val xs = List[" + CARET + "\n" +
"}"
val expectedResult =
"class x {\n" +
" val xs = List[" + CARET + "\n" +
" " + CARET + "\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
/**
* Test:
* class x {
* y()<->
* }
*
* Becomes
* class x {
* y()
* <->
* }
*/
@Test
def afterFunctionCall() {
val textSoFar =
"class x {\n" +
" y()" + CARET + "\n" +
"}"
val expectedResult =
"class x {\n" +
" y()" + CARET + "\n" +
" " + CARET + "\n" +
"}"
runTest(textSoFar, "\n", expectedResult)
}
}