package org.jetbrains.bsp

import java.io.File
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture

import org.eclipse.lsp4j.jsonrpc.ResponseErrorException

object BspUtil {

  implicit class ResponseErrorExceptionOps(err: ResponseErrorException) {
    def toBspError: BspError = {
      BspErrorMessage(s"bsp error: ${err.getMessage} (${err.getResponseError.getCode})")
    }
  }

  implicit class StringOps(str: String) {
    def toURI: URI = new URI(str)
  }

  implicit class URIOps(uri: URI) {
    def toFile: File = new File(uri)
  }

  implicit class CompletableFutureOps[T](cf: CompletableFuture[T]) {
    def catchBspErrors :CompletableFuture[Either[BspError,T]] = cf.handle { (result, error) =>
      if (error != null) error match {
        case responseError: ResponseErrorException =>
          Left(responseError.toBspError)
        case other: Throwable => throw other
      } else Right(result)
    }
  }

}

package org.jetbrains.bsp

import java.io.File
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture

import org.eclipse.lsp4j.jsonrpc.ResponseErrorException

object BspUtil {

  implicit class ResponseErrorExceptionOps(err: ResponseErrorException) {
    def toBspError: BspError = {
      BspErrorMessage(s"bsp error: ${err.getMessage} (${err.getResponseError.getCode})")
    }
  }

  implicit class StringOps(str: String) {
    def toURI: URI = new URI(str)
  }

  implicit class URIOps(uri: URI) {
    def toFile: File = Paths.get(uri).toFile
  }

  implicit class CompletableFutureOps[T](cf: CompletableFuture[T]) {
    def catchBspErrors :CompletableFuture[Either[BspError,T]] = cf.handle { (result, error) =>
      if (error != null) error match {
        case responseError: ResponseErrorException =>
          Left(responseError.toBspError)
        case other: Throwable => throw other
      } else Right(result)
    }
  }

}

Use target.baseDirectory instead of target.id.uri to get base directory for a target-> module mapping. Use common base of source roots as a fallback if baseDirectory is not set.
Use target.baseDirectory instead of target.id.uri to get base directory for a target-> module mapping. Use common base of source roots as a fallback if baseDirectory is not set.

SCL-14851 fixed
