Created
November 3, 2019 10:56
-
-
Save olafurpg/0d21079baf609a3941207949f7078389 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit d1c32af12806e4874a0403183bb27457f365ea1f | |
Author: Olafur Pall Geirsson <[email protected]> | |
Date: Sun Nov 3 10:56:02 2019 +0000 | |
Add support for `*.sc` files | |
diff --git a/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala b/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala | |
index 3b1caa3..1063986 100644 | |
--- a/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala | |
+++ b/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala | |
@@ -145,15 +145,7 @@ class Compilers( | |
token: CancelToken | |
): Future[CompletionList] = | |
withPC(params, None) { (pc, pos) => | |
- pc.complete( | |
- CompilerOffsetParams( | |
- pos.input.syntax, | |
- pos.input.text, | |
- pos.start, | |
- token | |
- ) | |
- ) | |
- .asScala | |
+ pc.complete(CompilerOffsetParams.fromPos(pos, token)).asScala | |
}.getOrElse(Future.successful(new CompletionList())) | |
def hover( | |
@@ -162,14 +154,7 @@ class Compilers( | |
interactiveSemanticdbs: InteractiveSemanticdbs | |
): Future[Option[Hover]] = | |
withPC(params, Some(interactiveSemanticdbs)) { (pc, pos) => | |
- pc.hover( | |
- CompilerOffsetParams( | |
- pos.input.syntax, | |
- pos.input.text, | |
- pos.start, | |
- token | |
- ) | |
- ) | |
+ pc.hover(CompilerOffsetParams.fromPos(pos, token)) | |
.asScala | |
.map(_.asScala) | |
}.getOrElse { | |
@@ -180,14 +165,7 @@ class Compilers( | |
token: CancelToken | |
): Future[DefinitionResult] = | |
withPC(params, None) { (pc, pos) => | |
- pc.definition( | |
- CompilerOffsetParams( | |
- pos.input.syntax, | |
- pos.input.text, | |
- pos.start, | |
- token | |
- ) | |
- ) | |
+ pc.definition(CompilerOffsetParams.fromPos(pos, token)) | |
.asScala | |
.map { c => | |
DefinitionResult( | |
@@ -204,15 +182,7 @@ class Compilers( | |
interactiveSemanticdbs: InteractiveSemanticdbs | |
): Future[SignatureHelp] = | |
withPC(params, Some(interactiveSemanticdbs)) { (pc, pos) => | |
- pc.signatureHelp( | |
- CompilerOffsetParams( | |
- pos.input.syntax, | |
- pos.input.text, | |
- pos.start, | |
- token | |
- ) | |
- ) | |
- .asScala | |
+ pc.signatureHelp(CompilerOffsetParams.fromPos(pos, token)).asScala | |
}.getOrElse(Future.successful(new SignatureHelp())) | |
def loadCompiler( | |
@@ -224,7 +194,7 @@ class Compilers( | |
.orElse(interactiveSemanticdbs.flatMap(_.getBuildTarget(path))) | |
target match { | |
case None => | |
- if (path.toLanguage.isScala) Some(ramboCompiler) | |
+ if (path.isScalaOrScalaScript) Some(ramboCompiler) | |
else None | |
case Some(value) => loadCompiler(value) | |
} | |
diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala | |
index 4c450d1..0d96e54 100644 | |
--- a/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala | |
+++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala | |
@@ -45,10 +45,6 @@ import scala.meta.tokenizers.TokenizeException | |
import scala.util.control.NonFatal | |
import scala.util.Success | |
import com.google.gson.JsonPrimitive | |
-import scala.meta.internal.decorations.DecorationRangesTypeDidChange | |
-import scala.meta.internal.decorations.DecorationOptions | |
-import scala.meta.internal.decorations.ThemableDecorationAttachmentRenderOptions | |
-import scala.meta.internal.decorations.ThemableDecorationInstanceRenderOptions | |
class MetalsLanguageServer( | |
ec: ExecutionContextExecutorService, | |
@@ -602,28 +598,6 @@ class MetalsLanguageServer( | |
@JsonNotification("textDocument/didOpen") | |
def didOpen(params: DidOpenTextDocumentParams): CompletableFuture[Unit] = { | |
val path = params.getTextDocument.getUri.toAbsolutePath | |
- if (path.extension == "sc") { | |
- pprint.log(path) | |
- languageClient.metalsDecorationRangesDidChange( | |
- DecorationRangesTypeDidChange( | |
- params.getTextDocument().getUri(), | |
- Array( | |
- DecorationOptions( | |
- range = new l.Range( | |
- new l.Position(1, 10), | |
- new l.Position(1, 10) | |
- ), | |
- renderOptions = ThemableDecorationInstanceRenderOptions( | |
- after = ThemableDecorationAttachmentRenderOptions( | |
- contentText = " // decoration", | |
- opacity = java.lang.Double.valueOf(0.7) | |
- ) | |
- ) | |
- ) | |
- ) | |
- ) | |
- ) | |
- } | |
openedFiles.add(path) | |
openTextDocument.set(path) | |
diff --git a/mtags/src/main/scala/scala/meta/internal/metals/CompilerOffsetParams.scala b/mtags/src/main/scala/scala/meta/internal/metals/CompilerOffsetParams.scala | |
index 5d5695d..ae98a00 100644 | |
--- a/mtags/src/main/scala/scala/meta/internal/metals/CompilerOffsetParams.scala | |
+++ b/mtags/src/main/scala/scala/meta/internal/metals/CompilerOffsetParams.scala | |
@@ -2,6 +2,7 @@ package scala.meta.internal.metals | |
import scala.meta.pc.CancelToken | |
import scala.meta.pc.OffsetParams | |
+import scala.meta.inputs.Position | |
case class CompilerOffsetParams( | |
filename: String, | |
@@ -9,3 +10,22 @@ case class CompilerOffsetParams( | |
offset: Int, | |
token: CancelToken = EmptyCancelToken | |
) extends OffsetParams | |
+ | |
+object CompilerOffsetParams { | |
+ def fromPos(pos: Position, token: CancelToken): CompilerOffsetParams = { | |
+ val isScript = pos.input.syntax.endsWith(".sc") | |
+ val scriptHeader = "object Script {\n" | |
+ val text = | |
+ if (isScript) scriptHeader + pos.input.text + "\n}" | |
+ else pos.input.text | |
+ val start = | |
+ if (isScript) scriptHeader.length + pos.start | |
+ else pos.start | |
+ CompilerOffsetParams( | |
+ pos.input.syntax, | |
+ text, | |
+ start, | |
+ token | |
+ ) | |
+ } | |
+} | |
diff --git a/mtags/src/main/scala/scala/meta/internal/mtags/MtagsEnrichments.scala b/mtags/src/main/scala/scala/meta/internal/mtags/MtagsEnrichments.scala | |
index c748c40..2e59950 100644 | |
--- a/mtags/src/main/scala/scala/meta/internal/mtags/MtagsEnrichments.scala | |
+++ b/mtags/src/main/scala/scala/meta/internal/mtags/MtagsEnrichments.scala | |
@@ -98,6 +98,12 @@ trait MtagsEnrichments { | |
case _ => false | |
} | |
} | |
+ def isScalaOrScalaScript: Boolean = { | |
+ extension match { | |
+ case "scala" | "sc" => true | |
+ case _ => false | |
+ } | |
+ } | |
def isScala: Boolean = { | |
toLanguage == Language.SCALA | |
} | |
diff --git a/mtags/src/main/scala/scala/meta/internal/pc/CompletionProvider.scala b/mtags/src/main/scala/scala/meta/internal/pc/CompletionProvider.scala | |
index 746c146..89a38a2 100644 | |
--- a/mtags/src/main/scala/scala/meta/internal/pc/CompletionProvider.scala | |
+++ b/mtags/src/main/scala/scala/meta/internal/pc/CompletionProvider.scala | |
@@ -394,25 +394,23 @@ class CompletionProvider( | |
} | |
val latestParentTrees = getLastVisitedParentTrees(pos) | |
- val completion = | |
- completionPosition( | |
- pos, | |
- params.text(), | |
- editRange, | |
- completions, | |
- latestParentTrees | |
- ) | |
+ val completion = completionPosition( | |
+ pos, | |
+ params.text(), | |
+ editRange, | |
+ completions, | |
+ latestParentTrees | |
+ ) | |
val query = completions.name.toString | |
- val items = | |
- filterInteresting( | |
- matchingResults, | |
- kind, | |
- query, | |
- pos, | |
- completion, | |
- editRange, | |
- latestParentTrees | |
- ) | |
+ val items = filterInteresting( | |
+ matchingResults, | |
+ kind, | |
+ query, | |
+ pos, | |
+ completion, | |
+ editRange, | |
+ latestParentTrees | |
+ ) | |
params.checkCanceled() | |
(items, completion, editRange, query) | |
} catch { | |
diff --git a/tests/unit/src/test/scala/tests/worksheets/WorksheetLspSuite.scala b/tests/unit/src/test/scala/tests/worksheets/WorksheetLspSuite.scala | |
new file mode 100644 | |
index 0000000..d7253e3 | |
--- /dev/null | |
+++ b/tests/unit/src/test/scala/tests/worksheets/WorksheetLspSuite.scala | |
@@ -0,0 +1,29 @@ | |
+package tests.worksheets | |
+import tests.BaseLspSuite | |
+ | |
+object WorksheetLspSuite extends BaseLspSuite("worksheet") { | |
+ testAsync("basic") { | |
+ for { | |
+ _ <- server.initialize( | |
+ """ | |
+ |/metals.json | |
+ |{"a": {}} | |
+ |/a/src/main/scala/foo/Lib.scala | |
+ |package foo | |
+ |object Lib { | |
+ | def increment(i: Int): Int =i + 1 | |
+ |} | |
+ |/a/src/main/scala/Main.sc | |
+ |println(identity(42)) | |
+ |println(foo.Lib.increment(42)) | |
+ |""".stripMargin | |
+ ) | |
+ _ <- server.didOpen("a/src/main/scala/foo/Lib.scala") | |
+ _ <- server.didOpen("a/src/main/scala/Main.sc") | |
+ completion <- server.completion("a/src/main/scala/Main.sc", "identity@@") | |
+ _ = assertNoDiff(completion, "identity[A](x: A): A") | |
+ completion <- server.completion("a/src/main/scala/Main.sc", "increment@@") | |
+ _ = assertNoDiff(completion, "increment(i: Int): Int") | |
+ } yield () | |
+ } | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment