Skip to content

Commit

Permalink
ALS-811 fix includes completion at root level
Browse files Browse the repository at this point in the history
  • Loading branch information
pope1838 committed Jul 5, 2019
1 parent 054718f commit bd98c41
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import amf.client.remote.Content
import amf.client.resource.{ClientResourceLoader, ResourceLoader}
import amf.core.remote.Vendor
import org.mulesoft.als.common.{DirectoryResolver => InternalDirectoryResolver}
import org.scalatest.{AsyncFunSuite, Matchers}
import org.scalatest.{Assertions, AsyncFunSuite, Matchers}

import scala.concurrent.{ExecutionContext, Future}
import scala.scalajs.js
Expand Down Expand Up @@ -84,17 +84,17 @@ class JsSuggestionsTest extends AsyncFunSuite with Matchers {
}

override def accepts(resource: String): Boolean =
resource == "file:///dir/api.raml" || resource == "file://dir/fragment.raml"
resource == "file:///dir/api.raml" || resource == "file://dir/fragment%202.raml"
})
.as[ClientResourceLoader]

val clientResolver = js
.use(new ClientDirectoryResolver {
override def exists(path: String): js.Promise[Boolean] =
Future(Seq("/api.raml", "fragment.raml", "another.raml").contains(path)).toJSPromise
Future(Seq("/api.raml", "fragment%202.raml", "another.raml").contains(path)).toJSPromise

override def readDir(path: String): js.Promise[js.Array[String]] =
Future(Seq("/fragment.raml", "/another.raml"))
Future(Seq("/fragment 2.raml", "/another.raml"))
.map(_.toJSArray)
.toJSPromise

Expand All @@ -113,7 +113,62 @@ class JsSuggestionsTest extends AsyncFunSuite with Matchers {
.map(suggestions => {
val seq = suggestions.toSeq
seq.size should be(2)
seq.head.text should be("fragment.raml")
seq.head.text should be("fragment 2.raml")
seq.last.text should be("another.raml")
})
})
}

test("Custom Directory Resolver Root") {
val api =
"#%RAML 1.0\ntitle: Project 2\ntraits:\n t: !include \ntypes:\n a: string"

val fragment =
"""#%RAML 1.0 Trait
|responses:
| 200:
""".stripMargin.replace("\r\n", "\n")

val fileLoader = js
.use(new ResourceLoader {
override def fetch(resource: String): js.Promise[Content] = {
if (resource == "file:///api.raml")
js.Promise.resolve[Content](new Content(api, resource))
else
js.Promise.resolve[Content](new Content(fragment, resource))
}

override def accepts(resource: String): Boolean =
resource == "file:///api.raml" || resource == "file://fragment%202.raml"
})
.as[ClientResourceLoader]

val clientResolver = js
.use(new ClientDirectoryResolver {
override def exists(path: String): js.Promise[Boolean] =
Future(Seq("/api.raml", "fragment%202.raml", "another.raml").contains(path)).toJSPromise

override def readDir(path: String): js.Promise[js.Array[String]] =
Future(Seq("/fragment 2.raml", "/another.raml"))
.map(_.toJSArray)
.toJSPromise

override def isDirectory(path: String): js.Promise[Boolean] =
Future(!path.endsWith(".raml")).toJSPromise
})
.as[ClientDirectoryResolver]

JsSuggestions
.init()
.toFuture
.flatMap(_ => {
JsSuggestions
.suggest(Vendor.RAML.name, "file:///api.raml", 51, js.Array(fileLoader), clientResolver)
.toFuture
.map(suggestions => {
val seq = suggestions.toSeq
seq.size should be(2)
seq.head.text should be("fragment 2.raml")
seq.last.text should be("another.raml")
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import scala.concurrent.Future
object PathCompletion {

private def resolvePath(platform: Platform, basePath: String, path: String): String =
platform.resolvePath(basePath + path.stripPrefix("/"))
if (path.nonEmpty) platform.resolvePath(basePath + path.stripPrefix("/"))
else basePath

def complete(defaultPath: String,
nonEncodedAdditionalPath: String,
Expand Down Expand Up @@ -55,7 +56,7 @@ object PathCompletion {
filesFuture.flatMap(shortFilePaths => {
val fullFilePaths: Seq[String] =
shortFilePaths
.map(platform.encodeURI(_))
.map(platform.encodeURI)
.map(resolvePath(platform, finalDirectoryPath, _))

val filter = resolvePath(platform, directoryPath, additionalPath)
Expand All @@ -67,8 +68,9 @@ object PathCompletion {
directoryResolver
.isDirectory(fullFilePath)
.map({
case true if !result.endsWith("/") => platform.decodeURI(result) + "/"
case _ => platform.decodeURI(result)
case true if !result.endsWith("/") =>
platform.decodeURI(result) + "/"
case _ => platform.decodeURI(result)
})
})
Future.sequence(futures)
Expand Down

0 comments on commit bd98c41

Please sign in to comment.