Skip to content

Commit

Permalink
Managing Dependency injections with module management
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalathe committed Aug 18, 2023
1 parent 4083f6f commit fa36eb0
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/io/smartdatalake/Main.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.smartdatalake

import io.smartdatalake.languageserver.SmartDataLakeLanguageServer
import io.smartdatalake.modules.AppModule
import org.eclipse.lsp4j.jsonrpc.Launcher
import org.eclipse.lsp4j.launch.LSPLauncher
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.lsp4j.services.{LanguageClient, LanguageClientAware, LanguageServer}

import java.io.{InputStream, PrintStream}
import java.util.logging.{Level, LogManager, Logger}

/**
* @author scalathe
*/
object Main {
object Main extends AppModule {

def main(args : Array[String]): Unit = {

Expand All @@ -24,7 +24,7 @@ object Main {
}

private def startServer(in: InputStream, out: PrintStream) = {
val helloLanguageServer = new SmartDataLakeLanguageServer
val helloLanguageServer: LanguageServer & LanguageClientAware = languageServer
val launcher: Launcher[LanguageClient] = LSPLauncher.createServerLauncher(helloLanguageServer, in, out)
val client: LanguageClient = launcher.getRemoteProxy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.eclipse.lsp4j.{CompletionItem, CompletionItemKind}

import scala.util.{Failure, Success, Try}

class SDLBCompletionEngineImpl extends SDLBCompletionEngine {
class SDLBCompletionEngineImpl(private val schemaReader: SchemaReader) extends SDLBCompletionEngine {

val schemaReader: SchemaReader = new SchemaReaderImpl("sdl-schema/sdl-schema-2.5.0.json") //TODO should be retrieved from a service keeping its state, object for example
//val schemaReader: SchemaReader = new SchemaReaderImpl("sdl-schema/sdl-schema-2.5.0.json") //TODO should be retrieved from a service keeping its state, object for example

override def generateCompletionItems(context: SDLBContext): List[CompletionItem] = context.parentPath match
case path if path.startsWith("actions") && path.count(_ == '.') == 1 => generatePropertiesOfAction(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import org.eclipse.lsp4j.{Hover, MarkupContent, MarkupKind}

import scala.util.{Failure, Success, Try}

class SDLBHoverEngineImpl extends SDLBHoverEngine:

val schemaReader: SchemaReader = new SchemaReaderImpl("sdl-schema/sdl-schema-2.5.0.json") //TODO should be retrieved from a service keeping its state, object for example

class SDLBHoverEngineImpl(private val schemaReader: SchemaReader) extends SDLBHoverEngine:

override def generateHoveringInformation(context: SDLBContext): Hover =
val markupContent = new MarkupContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import org.eclipse.lsp4j.*

import java.util.concurrent.CompletableFuture

class SmartDataLakeLanguageServer extends LanguageServer with LanguageClientAware {
class SmartDataLakeLanguageServer(private val textDocumentService: TextDocumentService, private val workspaceService: WorkspaceService) extends LanguageServer with LanguageClientAware {

private val textDocumentService = new SmartDataLakeTextDocumentService
private val workspaceService = new SmartDataLakeWorkspaceService
private var client: Option[LanguageClient] = None
private var errorCode = 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.smartdatalake.languageserver
import io.smartdatalake.completion.{SDLBCompletionEngine, SDLBCompletionEngineImpl}
import io.smartdatalake.context.SDLBContext
import io.smartdatalake.hover.{SDLBHoverEngine, SDLBHoverEngineImpl}
import io.smartdatalake.schema.SchemaReader
import org.eclipse.lsp4j.jsonrpc.messages
import org.eclipse.lsp4j.services.TextDocumentService
import org.eclipse.lsp4j.{CodeAction, CodeActionParams, CodeLens, CodeLensParams, Command, CompletionItem, CompletionItemKind, CompletionList, CompletionParams, DefinitionParams, DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentFormattingParams, DocumentHighlight, DocumentHighlightParams, DocumentOnTypeFormattingParams, DocumentRangeFormattingParams, DocumentSymbol, DocumentSymbolParams, Hover, HoverParams, InsertReplaceEdit, Location, LocationLink, MarkupContent, MarkupKind, Position, Range, ReferenceParams, RenameParams, SignatureHelp, SignatureHelpParams, SymbolInformation, TextDocumentPositionParams, TextEdit, WorkspaceEdit}
Expand All @@ -12,18 +13,16 @@ import java.util.concurrent.CompletableFuture
import scala.io.Source
import scala.util.Using

class SmartDataLakeTextDocumentService extends TextDocumentService {
class SmartDataLakeTextDocumentService(private val completionEngine: SDLBCompletionEngine, private val hoverEngine: SDLBHoverEngine) extends TextDocumentService {

private var context: SDLBContext = SDLBContext.EMPTY_CONTEXT
private val completionEngine: SDLBCompletionEngine = new SDLBCompletionEngineImpl
private val hoverEngine: SDLBHoverEngine = new SDLBHoverEngineImpl //TODO DI!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

override def completion(params: CompletionParams): CompletableFuture[messages.Either[util.List[CompletionItem], CompletionList]] = {

CompletableFuture.supplyAsync(() => {
context = context.withCaretPosition(params.getPosition.getLine+1, params.getPosition.getCharacter)
val completionItems = new util.ArrayList[CompletionItem]()
val suggestions: List[CompletionItem] = new SDLBCompletionEngineImpl().generateCompletionItems(context)
val suggestions: List[CompletionItem] = completionEngine.generateCompletionItems(context)
suggestions.foreach(e => completionItems.add(e))

messages.Either.forLeft(completionItems).asInstanceOf[messages.Either[util.List[CompletionItem], CompletionList]]
Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/io/smartdatalake/modules/AppModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.smartdatalake.modules

import io.smartdatalake.completion.{SDLBCompletionEngine, SDLBCompletionEngineImpl}
import io.smartdatalake.hover.{SDLBHoverEngine, SDLBHoverEngineImpl}
import io.smartdatalake.languageserver.{SmartDataLakeLanguageServer, SmartDataLakeTextDocumentService, SmartDataLakeWorkspaceService}
import io.smartdatalake.schema.{SchemaReader, SchemaReaderImpl}
import org.eclipse.lsp4j.services.{LanguageClientAware, LanguageServer, TextDocumentService, WorkspaceService}

trait AppModule {
lazy val schemaReader: SchemaReader = new SchemaReaderImpl("sdl-schema/sdl-schema-2.5.0.json")
lazy val completionEngine: SDLBCompletionEngine = new SDLBCompletionEngineImpl(schemaReader)
lazy val hoverEngine: SDLBHoverEngine = new SDLBHoverEngineImpl(schemaReader)
lazy val textDocumentService: TextDocumentService = new SmartDataLakeTextDocumentService(completionEngine, hoverEngine)
lazy val workspaceService: WorkspaceService = new SmartDataLakeWorkspaceService
lazy val languageServer: LanguageServer & LanguageClientAware = new SmartDataLakeLanguageServer(textDocumentService, workspaceService)

}

3 changes: 2 additions & 1 deletion src/test/scala/io/smartdatalake/UnitSpec.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.smartdatalake

import io.smartdatalake.modules.TestModule
import org.scalatest.*
import org.scalatest.flatspec.*
import org.scalatest.matchers.*

import scala.io.Source
import scala.util.Using

abstract class UnitSpec extends AnyFlatSpec with should.Matchers with OptionValues with Inside with Inspectors:
abstract class UnitSpec extends AnyFlatSpec with should.Matchers with OptionValues with Inside with Inspectors with TestModule:
def loadFile(filePath: String): String =
Using.resource(getClass.getClassLoader.getResourceAsStream(filePath)) { inputStream =>
Source.fromInputStream(inputStream).getLines().mkString("\n").trim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import scala.io.Source
import scala.util.Using

class SDLBCompletionEngineSpec extends UnitSpec {

val completionEngine = new SDLBCompletionEngineImpl


"SDLB Completion engine" should "retrieve all the properties of copyAction" in {
val context = SDLBContext.fromText(loadFile("fixture/hocon/with-multi-lines-flattened-example.conf"))
.withCaretPosition(16, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import scala.io.Source
import scala.util.Using

class SchemaReaderSpec extends UnitSpec {

val schemaReader = new SchemaReaderImpl("fixture/sdl-schema/sdl-schema-2.5.0.json")


"Schema Reader" should "retrieve all the properties of copyAction" in {
val actual = schemaReader.retrieveActionProperties("CopyAction").toList
val expected = List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ import scala.io.Source
import scala.util.Using

class SDLBHoverEngineSpec extends UnitSpec {

val hoveringEngine = new SDLBHoverEngineImpl


"SDLB Completion engine" should "retrieve all the properties of copyAction" in {
val context = SDLBContext.fromText(loadFile("fixture/hocon/with-multi-lines-flattened-example.conf"))
.withCaretPosition(6, 4)
val expected =
"""Configuration of a custom Spark-DataFrame transformation between many inputs and many outputs (n:m).
|Define a transform function which receives a map of input DataObjectIds with DataFrames and a map of options and has
|to return a map of output DataObjectIds with DataFrames, see also trait[[CustomDfsTransformer]] .""".stripMargin
//hoveringEngine.generateHoveringInformation(context).getContents.getRight.getValue shouldBe expected
hoverEngine.generateHoveringInformation(context).getContents.getRight.getValue shouldBe expected
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import org.eclipse.lsp4j.InitializeResult
import java.util.concurrent.CompletableFuture

class SmartDataLakeLanguageServerSpec extends UnitSpec {

def sdlLanguageServer = new SmartDataLakeLanguageServer


"SDL Language Server" should "have autocompletion as a capability" in {
val capabilities: CompletableFuture[InitializeResult] = sdlLanguageServer.initialize(null)
val capabilities: CompletableFuture[InitializeResult] = languageServer.initialize(null)
capabilities.get().getCapabilities.getCompletionProvider shouldNot be (null)
}

"SDL Language Server" should "have hovering as a capability" in {
val capabilities: CompletableFuture[InitializeResult] = sdlLanguageServer.initialize(null)
val capabilities: CompletableFuture[InitializeResult] = languageServer.initialize(null)
capabilities.get().getCapabilities.getHoverProvider.getLeft shouldBe true
}

it should "exit without errors if shutdown before" in {
val server = sdlLanguageServer
val server = languageServer
server.shutdown()
server.getErrorCode should be (0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import io.smartdatalake.languageserver.SmartDataLakeTextDocumentService
import org.eclipse.lsp4j.{CompletionParams, DidOpenTextDocumentParams, HoverParams, Position, TextDocumentItem}

class SmartDataLakeTextDocumentServiceSpec extends UnitSpec {

val textDocumentService = new SmartDataLakeTextDocumentService


def params: CompletionParams =
val p = new CompletionParams()
// Careful, Position of LSP4J is 0-based
Expand Down
11 changes: 11 additions & 0 deletions src/test/scala/io/smartdatalake/modules/TestModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.smartdatalake.modules
import io.smartdatalake.completion.{SDLBCompletionEngine, SDLBCompletionEngineImpl}
import io.smartdatalake.languageserver.SmartDataLakeLanguageServer
import io.smartdatalake.schema.{SchemaReader, SchemaReaderImpl}

trait TestModule extends AppModule {
override lazy val schemaReader: SchemaReader = new SchemaReaderImpl("fixture/sdl-schema/sdl-schema-2.5.0.json")
override lazy val completionEngine: SDLBCompletionEngineImpl = new SDLBCompletionEngineImpl(schemaReader)
override lazy val languageServer: SmartDataLakeLanguageServer = new SmartDataLakeLanguageServer(textDocumentService, workspaceService)

}

0 comments on commit fa36eb0

Please sign in to comment.