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 =
19
- lst
20
- .map(elem => '"' + elem.toString.replace('\\ ' , '/' ) + '"' )
21
- .mkString(" [ " , " , " , " ]" )
22
- val dottyIdeJson : String =
23
- s """ [ {
24
- | "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 =
30
+ lst
31
+ .map(elem => '"' + elem.toString.replace('\\ ' , '/' ) + '"' )
32
+ .mkString(" [ " , " , " , " ]" )
33
+
34
+ def classDirectory (workspace : Workspace ): Path = {
35
+ val path = testFolder.resolve(workspace.name).resolve(" out" )
36
+ Files .createDirectories(path)
37
+ path.toAbsolutePath
38
+ }
39
+
40
+ val dependencyClasspath =
41
+ BuildInfo .ideTestsDependencyClasspath.map(_.getAbsolutePath) ++
42
+ workspace.dependsOn.map(w => classDirectory(w).toString)
43
+
44
+ val sourceDirectory : Path = {
45
+ val path = TestFile .sourceDir.resolve(workspace.name).toAbsolutePath
46
+ Files .createDirectories(path)
47
+ path
48
+ }
49
+
50
+ s """ {
51
+ | "id" : " ${workspace.name}",
25
52
| "compilerVersion" : " ${BuildInfo .ideTestsCompilerVersion}",
26
53
| "compilerArguments" : ${showSeq(BuildInfo .ideTestsCompilerArguments)},
27
- | "sourceDirectories" : ${showSeq(BuildInfo .ideTestsSourceDirectories )},
28
- | "dependencyClasspath" : ${showSeq(BuildInfo .ideTestsDependencyClasspath )},
29
- | "classDirectory" : " ${BuildInfo .ideTestsClassDirectory .toString.replace('\\ ' ,'/' )}"
54
+ | "sourceDirectories" : ${showSeq(sourceDirectory :: Nil )},
55
+ | "dependencyClasspath" : ${showSeq(dependencyClasspath )},
56
+ | "classDirectory" : " ${classDirectory(workspace) .toString.replace('\\ ' ,'/' )}"
30
57
|}
31
- |] """ .stripMargin
58
+ | """ .stripMargin
59
+ }
60
+
61
+ Files .createDirectories(testFolder)
32
62
val configFile = testFolder.resolve(DottyLanguageServer .IDE_CONFIG_FILE )
33
- testFolder.toFile.mkdirs()
34
- testFolder.resolve(" src" ).toFile.mkdirs()
35
- testFolder.resolve(" out" ).toFile.mkdirs()
63
+ val configuration = workspaces.map(workspaceSetup).mkString(" [" , " ," , " ]" )
36
64
37
65
new PrintWriter (configFile.toString) {
38
- write(dottyIdeJson )
66
+ write(configuration )
39
67
close()
40
68
}
41
69
@@ -52,8 +80,8 @@ class TestServer(testFolder: Path) {
52
80
* @param fileName file path in the source directory
53
81
* @return the file opened
54
82
*/
55
- def openCode (code : String , fileName : String ): TestFile = {
56
- val testFile = new TestFile (fileName)
83
+ def openCode (code : String , workspace : Workspace , fileName : String ): TestFile = {
84
+ val testFile = new TestFile (workspace.name + sep + fileName)
57
85
val dotdp = new DidOpenTextDocumentParams ()
58
86
val tdi = new TextDocumentItem ()
59
87
tdi.setUri(testFile.uri)
0 commit comments