1
1
package dotty .tools .languageserver .util .server
2
2
3
3
import java .io .PrintWriter
4
+ import java .io .File .{separator => sep }
4
5
import java .net .URI
5
- import java .nio .file .Path
6
+ import java .nio .file .{ Files , Path }
6
7
import java .util
7
8
8
9
import dotty .tools .languageserver .DottyLanguageServer
10
+ import dotty .tools .languageserver .util .Code .Workspace
9
11
import org .eclipse .lsp4j .{ DidOpenTextDocumentParams , InitializeParams , InitializeResult , TextDocumentItem }
10
12
11
- class TestServer (testFolder : Path ) {
13
+ class TestServer (testFolder : Path , workspaces : List [ Workspace ] ) {
12
14
13
15
val server = new DottyLanguageServer
14
16
init()
15
17
16
18
private [this ] def init (): InitializeResult = {
17
- // Fill the configuration with values populated by sbt
18
- def showSeq [T ](lst : Seq [T ]): String = lst.map(elem => '"' + elem.toString + '"' ).mkString(" [ " , " , " , " ]" )
19
- val dottyIdeJson : String =
20
- s """ [ {
21
- | "id" : "dotty-ide-test",
19
+ /**
20
+ * Set up given workspace, return JSON config.
21
+ *
22
+ * This creates the necessary directories to hold the classes and sources. Some values
23
+ * are passed via sbt-buildinfo, such as the classpath containing the scala and dotty libaries.
24
+ *
25
+ * @param workspace The workspace to configure.
26
+ * @return A JSON object representing the configuration for this workspace.
27
+ */
28
+ def workspaceSetup (workspace : Workspace ): String = {
29
+ def showSeq [T ](lst : Seq [T ]): String = lst.map(elem => '"' + elem.toString + '"' ).mkString(" [ " , " , " , " ]" )
30
+
31
+ def classDirectory (workspace : Workspace ): Path = {
32
+ val path = testFolder.resolve(workspace.name).resolve(" out" )
33
+ Files .createDirectories(path)
34
+ path.toAbsolutePath
35
+ }
36
+
37
+ val dependencyClasspath =
38
+ BuildInfo .ideTestsDependencyClasspath.map(_.getAbsolutePath) ++
39
+ workspace.dependsOn.map(w => classDirectory(w).toString)
40
+
41
+ val sourceDirectory : Path = {
42
+ val path = TestFile .sourceDir.resolve(workspace.name).toAbsolutePath
43
+ Files .createDirectories(path)
44
+ path
45
+ }
46
+
47
+ s """ {
48
+ | "id" : " ${workspace.name}",
22
49
| "compilerVersion" : " ${BuildInfo .ideTestsCompilerVersion}",
23
50
| "compilerArguments" : ${showSeq(BuildInfo .ideTestsCompilerArguments)},
24
- | "sourceDirectories" : ${showSeq(BuildInfo .ideTestsSourceDirectories )},
25
- | "dependencyClasspath" : ${showSeq(BuildInfo .ideTestsDependencyClasspath )},
26
- | "classDirectory" : " ${BuildInfo .ideTestsClassDirectory }"
51
+ | "sourceDirectories" : ${showSeq(sourceDirectory :: Nil )},
52
+ | "dependencyClasspath" : ${showSeq(dependencyClasspath )},
53
+ | "classDirectory" : " ${classDirectory(workspace) }"
27
54
|}
28
- |] """ .stripMargin
55
+ | """ .stripMargin
56
+ }
57
+
29
58
val configFile = testFolder.resolve(DottyLanguageServer .IDE_CONFIG_FILE )
30
- testFolder.toFile.mkdirs()
31
- testFolder.resolve(" src" ).toFile.mkdirs()
32
- testFolder.resolve(" out" ).toFile.mkdirs()
59
+ val configuration = workspaces.map(workspaceSetup).mkString(" [" , " ," , " ]" )
33
60
34
61
new PrintWriter (configFile.toString) {
35
- write(dottyIdeJson )
62
+ write(configuration )
36
63
close()
37
64
}
38
65
@@ -49,8 +76,8 @@ class TestServer(testFolder: Path) {
49
76
* @param fileName file path in the source directory
50
77
* @return the file opened
51
78
*/
52
- def openCode (code : String , fileName : String ): TestFile = {
53
- val testFile = new TestFile (fileName)
79
+ def openCode (code : String , workspace : Workspace , fileName : String ): TestFile = {
80
+ val testFile = new TestFile (workspace.name + sep + fileName)
54
81
val dotdp = new DidOpenTextDocumentParams ()
55
82
val tdi = new TextDocumentItem ()
56
83
tdi.setUri(testFile.uri)
0 commit comments