Skip to content

Commit

Permalink
Merge pull request #98 from scalameta/monaco-editor-0.13.1
Browse files Browse the repository at this point in the history
Update to Monaco Editor version 0.13.1
  • Loading branch information
olafurpg authored Sep 24, 2018
2 parents 0850f51 + 0fe3a1f commit 122f657
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 508 deletions.
4 changes: 2 additions & 2 deletions bin/update-monaco-facade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fi
sed \
-e 's/): [^ ]* is [^; ]*;/): boolean;/' \
-e 's/: void | /: /' \
-e 's/onDidChange?: IEvent<this>/onDidChange?: IEvent<CodeLensProvider>/' \
-e '/^declare module monaco.languages.\(typescript\|html\|css\|json\) {$/,/^}$/d' \
-e '/^declare namespace monaco.languages.\(typescript\|html\|css\|json\) {$/,/^}$/d' \
-e 's/ const enum / enum /' \
< "$NPM_MONACO_D_TS" \
> "$MONACO_D_TS"

Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ inThisBuild(
"[email protected]",
url("https://github.com/jonas")
)
),
)
)
)

Expand Down Expand Up @@ -150,7 +150,7 @@ lazy val js = project
"webpack-merge" -> "4.1.1"
),
npmDependencies in Compile ++= Seq(
"monaco-editor" -> "0.10.1",
"monaco-editor" -> "0.13.1",
"roboto-fontface" -> "0.7.0",
"js-sha512" -> "0.4.0"
)
Expand Down
6 changes: 5 additions & 1 deletion metadoc-js/src/main/scala/metadoc/MetadocApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,9 @@ object MetadocApp {
promise.future
}

val ScalaLanguageExtensionPoint = new ILanguageExtensionPoint("scala")
val ScalaLanguageExtensionPoint = {
val language = jsObject[ILanguageExtensionPoint]
language.id = "scala"
language
}
}
7 changes: 2 additions & 5 deletions metadoc-js/src/main/scala/metadoc/MetadocEditorService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MetadocEditorService(index: MetadocSemanticdbIndex)
options.scrollBeyondLastLine = false

val overrides = jsObject[IEditorOverrideServices]
overrides.textModelService = MetadocTextModelService
overrides.editorService = this
overrides("textModelService") = MetadocTextModelService
overrides("editorService") = this

val editor = monaco.editor.Editor.create(app, options, overrides)
editor.asInstanceOf[js.Dynamic].getControl = { () =>
Expand All @@ -39,9 +39,6 @@ class MetadocEditorService(index: MetadocSemanticdbIndex)
def resize(): Unit =
editor.layout()

def getModelUri(): Uri =
editor.getModel().uri

def open(input: IResourceInput): Future[IStandaloneCodeEditor] = {
val selection = input.options.selection
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.concurrent.Future
import monaco.Promise
import monaco.Uri
import monaco.editor.Editor
import monaco.editor.IModel
import monaco.editor.ITextModel
import monaco.services.IReference
import monaco.services.ITextEditorModel
import monaco.services.ITextModelService
Expand All @@ -20,9 +20,9 @@ object MetadocTextModelService extends ITextModelService {
modelDocument(createUri(filename)).map(_.model)

// TODO(olafur): Move this state out for easier testing.
private val modelDocumentCache = mutable.Map.empty[IModel, s.TextDocument]
private val modelDocumentCache = mutable.Map.empty[ITextModel, s.TextDocument]

private def document(model: IModel) =
private def document(model: ITextModel) =
MetadocMonacoDocument(
modelDocumentCache(model),
new ImmortalReference(ITextEditorModel(model))
Expand Down
28 changes: 7 additions & 21 deletions metadoc-js/src/main/scala/metadoc/ScalaDefinitionProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,24 @@ package metadoc
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.scalajs.js
import monaco.editor.IReadOnlyModel
import monaco.editor.ITextModel
import monaco.languages.DefinitionProvider
import monaco.languages.Languages.Definition
import monaco.languages.Location
import monaco.CancellationToken
import monaco.Position

class ScalaDefinitionProvider(index: MetadocSemanticdbIndex)
extends DefinitionProvider {
override def provideDefinition(
model: IReadOnlyModel,
model: ITextModel,
position: Position,
token: CancellationToken
) = {
val offset = model.getOffsetAt(position).toInt
def empty = Future.successful(js.Array[Location]())
for {
symbol <- index.fetchSymbol(offset)
locations <- {
symbol
.map(_.definition)
.fold(empty) {
case Some(defn) =>
for {
model <- MetadocTextModelService.modelReference(defn.filename)
} yield {
val location =
model.`object`.textEditorModel.resolveLocation(defn)
js.Array[Location](location)
}
case None => empty
}
}
} yield locations
for (symbol <- index.fetchSymbol(offset)) yield {
val locationOpt = symbol.flatMap(_.definition).map(resolveLocation)
js.Array[Location](locationOpt.toSeq: _*): Definition
}
}.toMonacoThenable
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package metadoc
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import monaco.CancellationToken
import monaco.editor.IReadOnlyModel
import monaco.editor.ITextModel
import monaco.languages.DocumentSymbolProvider
import monaco.languages.SymbolInformation
import monaco.languages.SymbolKind
Expand All @@ -30,21 +30,21 @@ class ScalaDocumentSymbolProvider(index: MetadocSemanticdbIndex)
}

override def provideDocumentSymbols(
model: IReadOnlyModel,
model: ITextModel,
token: CancellationToken
) = {
for {
Some(doc) <- index.semanticdb(model.uri.path)
} yield {
val symbols = getDocumentSymbols(doc).map {
case DocumentSymbol(denotation, kind, definition) =>
new SymbolInformation(
name = denotation.displayName,
// TODO: pretty print `.tpe`: https://github.com/scalameta/scalameta/issues/1479
containerName = denotation.symbol,
kind = kind,
location = model.resolveLocation(definition)
)
val symbol = jsObject[SymbolInformation]
symbol.name = denotation.displayName
// TODO: print signature instead of `denotation.symbol`: https://github.com/scalameta/metadoc/issues/99
symbol.containerName = denotation.symbol
symbol.kind = kind
symbol.location = resolveLocation(definition)
symbol
}
js.Array[SymbolInformation](symbols: _*)
}
Expand Down
30 changes: 8 additions & 22 deletions metadoc-js/src/main/scala/metadoc/ScalaReferenceProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package metadoc
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.scalajs.js
import monaco.editor.IReadOnlyModel
import monaco.editor.ITextModel
import monaco.languages.Location
import monaco.languages.ReferenceContext
import monaco.languages.ReferenceProvider
Expand All @@ -14,35 +14,21 @@ import MetadocEnrichments._
class ScalaReferenceProvider(index: MetadocSemanticdbIndex)
extends ReferenceProvider {
override def provideReferences(
model: IReadOnlyModel,
model: ITextModel,
position: Position,
context: ReferenceContext,
token: CancellationToken
) = {
val offset = model.getOffsetAt(position).toInt
for {
sym <- index.fetchSymbol(offset)
locations <- Future.sequence {
val references = sym.map(_.references).getOrElse(Map.empty)
references.map {
case (filename, ranges) =>
// Create the model for each reference. A reference can come from
// another file, and we need that file's model in order to get
// correct range selection.
MetadocTextModelService
.modelDocument(createUri(filename))
.map {
case MetadocMonacoDocument(_, model) =>
ranges.ranges.map { r =>
model.`object`.textEditorModel.resolveLocation(
r.toPosition(filename)
)
}
}
}
}
} yield {
js.Array[Location](locations.flatten.toSeq: _*)
val references = sym.map(_.references).getOrElse(Map.empty)
val locations = references.flatMap {
case (filename, ranges) =>
ranges.ranges.map(_.toPosition(filename)).map(resolveLocation)
}
js.Array[Location](locations.toSeq: _*)
}
}.toMonacoThenable
}
26 changes: 11 additions & 15 deletions metadoc-js/src/main/scala/metadoc/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import scala.scalajs.js.JSConverters._
import metadoc.{schema => d}
import monaco.Promise
import monaco.Range
import monaco.Thenable
import monaco.Monaco.Thenable
import monaco.Uri
import monaco.editor.IReadOnlyModel
import monaco.languages.Location
import monaco.services.IResourceInput
import monaco.services.ITextEditorOptions
Expand Down Expand Up @@ -48,18 +47,15 @@ package object metadoc {
future.toJSPromise.asInstanceOf[Thenable[T]]
}

implicit class XtensionIReadOnlyModel(val self: IReadOnlyModel)
extends AnyVal {
def resolveLocation(pos: d.Position): Location = {
val range = new Range(
pos.startLine + 1,
pos.startCharacter + 1,
pos.endLine + 1,
pos.endCharacter + 1
)
val uri = createUri(pos.filename)
val location = new Location(uri, range)
location
}
def resolveLocation(pos: d.Position): Location = {
val location = jsObject[Location]
location.uri = createUri(pos.filename)
location.range = new Range(
pos.startLine + 1,
pos.startCharacter + 1,
pos.endLine + 1,
pos.endCharacter + 1
)
location
}
}
Loading

0 comments on commit 122f657

Please sign in to comment.