moved main state to VSCode_Resources;
authorwenzelm
Thu Dec 29 21:54:04 2016 +0100 (2016-12-29)
changeset 64703a115391494ed
parent 64702 d95b9117cb5b
child 64704 08c2d80428ff
moved main state to VSCode_Resources;
misc tuning;
src/Tools/VSCode/src/document_model.scala
src/Tools/VSCode/src/server.scala
src/Tools/VSCode/src/vscode_resources.scala
     1.1 --- a/src/Tools/VSCode/src/document_model.scala	Thu Dec 29 17:25:32 2016 +0100
     1.2 +++ b/src/Tools/VSCode/src/document_model.scala	Thu Dec 29 21:54:04 2016 +0100
     1.3 @@ -13,7 +13,7 @@
     1.4  
     1.5  
     1.6  case class Document_Model(
     1.7 -  session: Session, doc: Line.Document, node_name: Document.Node.Name,
     1.8 +  session: Session, node_name: Document.Node.Name, doc: Line.Document,
     1.9    changed: Boolean = true,
    1.10    published_diagnostics: List[Text.Info[Command.Results]] = Nil)
    1.11  {
     2.1 --- a/src/Tools/VSCode/src/server.scala	Thu Dec 29 17:25:32 2016 +0100
     2.2 +++ b/src/Tools/VSCode/src/server.scala	Thu Dec 29 21:54:04 2016 +0100
     2.3 @@ -82,15 +82,6 @@
     2.4          throw(exn)
     2.5      }
     2.6    })
     2.7 -
     2.8 -
     2.9 -  /* server state */
    2.10 -
    2.11 -  sealed case class State(
    2.12 -    session: Option[Session] = None,
    2.13 -    models: Map[String, Document_Model] = Map.empty,
    2.14 -    pending_input: Set[Document.Node.Name] = Set.empty,
    2.15 -    pending_output: Set[Document.Node.Name] = Set.empty)
    2.16  }
    2.17  
    2.18  class Server(
    2.19 @@ -101,16 +92,15 @@
    2.20    session_dirs: List[Path] = Nil,
    2.21    modes: List[String] = Nil)
    2.22  {
    2.23 -  /* server state */
    2.24 +  /* server session */
    2.25  
    2.26 -  private val state = Synchronized(Server.State())
    2.27 -
    2.28 -  def session: Session = state.value.session getOrElse error("Session inactive")
    2.29 +  private val session_ = Synchronized(None: Option[Session])
    2.30 +  def session: Session = session_.value getOrElse error("Server inactive")
    2.31    def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
    2.32  
    2.33    def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] =
    2.34      for {
    2.35 -      model <- state.value.models.get(node_pos.name)
    2.36 +      model <- resources.get_model(node_pos.name)
    2.37        rendering = model.rendering(options)
    2.38        offset <- model.doc.offset(node_pos.pos)
    2.39      } yield (rendering, offset)
    2.40 @@ -119,32 +109,13 @@
    2.41    /* input from client */
    2.42  
    2.43    private val delay_input =
    2.44 -    Standard_Thread.delay_last(options.seconds("vscode_input_delay")) {
    2.45 -      state.change(st =>
    2.46 -        {
    2.47 -          val changed =
    2.48 -            (for {
    2.49 -              node_name <- st.pending_input.iterator
    2.50 -              model <- st.models.get(node_name.node)
    2.51 -              if model.changed } yield model).toList
    2.52 -          session.update(Document.Blobs.empty,
    2.53 -            for { model <- changed; edit <- model.node_edits } yield edit)
    2.54 -          st.copy(
    2.55 -            models = (st.models /: changed) { case (ms, m) => ms + (m.uri -> m.unchanged) },
    2.56 -            pending_input = Set.empty)
    2.57 -        })
    2.58 -    }
    2.59 +    Standard_Thread.delay_last(options.seconds("vscode_input_delay"))
    2.60 +    { resources.flush_input(session) }
    2.61  
    2.62 -  def update_document(uri: String, text: String)
    2.63 +  private def update_document(uri: String, text: String)
    2.64    {
    2.65 -    state.change(st =>
    2.66 -      {
    2.67 -        val node_name = resources.node_name(uri)
    2.68 -        val model = Document_Model(session, Line.Document(text, text_length), node_name)
    2.69 -        st.copy(
    2.70 -          models = st.models + (uri -> model),
    2.71 -          pending_input = st.pending_input + node_name)
    2.72 -      })
    2.73 +    val model = Document_Model(session, resources.node_name(uri), Line.Document(text, text_length))
    2.74 +    resources.update_model(model)
    2.75      delay_input.invoke()
    2.76    }
    2.77  
    2.78 @@ -153,33 +124,17 @@
    2.79  
    2.80    private val commands_changed =
    2.81      Session.Consumer[Session.Commands_Changed](getClass.getName) {
    2.82 -      case changed =>
    2.83 -        state.change(st => st.copy(pending_output = st.pending_output ++ changed.nodes))
    2.84 +      case changed if changed.nodes.nonEmpty =>
    2.85 +        resources.update_output(changed.nodes)
    2.86          delay_output.invoke()
    2.87 +      case _ =>
    2.88      }
    2.89  
    2.90    private val delay_output: Standard_Thread.Delay =
    2.91 -    Standard_Thread.delay_last(options.seconds("vscode_output_delay")) {
    2.92 +    Standard_Thread.delay_last(options.seconds("vscode_output_delay"))
    2.93 +    {
    2.94        if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
    2.95 -      else {
    2.96 -        state.change(st =>
    2.97 -          {
    2.98 -            val changed_iterator =
    2.99 -              for {
   2.100 -                node_name <- st.pending_output.iterator
   2.101 -                model <- st.models.get(node_name.node)
   2.102 -                rendering = model.rendering(options)
   2.103 -                (diagnostics, model1) <- model.publish_diagnostics(rendering)
   2.104 -              } yield {
   2.105 -                channel.diagnostics(model1.uri, rendering.diagnostics_output(diagnostics))
   2.106 -                model1
   2.107 -              }
   2.108 -            st.copy(
   2.109 -              models = (st.models /: changed_iterator) { case (ms, m) => ms + (m.uri -> m) },
   2.110 -              pending_output = Set.empty)
   2.111 -          }
   2.112 -        )
   2.113 -      }
   2.114 +      else resources.flush_output(channel)
   2.115      }
   2.116  
   2.117  
   2.118 @@ -209,7 +164,8 @@
   2.119        try {
   2.120          val content = Build.session_content(options, false, session_dirs, session_name)
   2.121          val resources =
   2.122 -          new VSCode_Resources(content.loaded_theories, content.known_theories, content.syntax)
   2.123 +          new VSCode_Resources(
   2.124 +            options, content.loaded_theories, content.known_theories, content.syntax)
   2.125  
   2.126          Some(new Session(resources) {
   2.127            override def output_delay = options.seconds("editor_output_delay")
   2.128 @@ -242,7 +198,7 @@
   2.129          Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
   2.130            modes = modes, receiver = receiver))
   2.131  
   2.132 -      state.change(_.copy(session = Some(session)))
   2.133 +      session_.change(_ => Some(session))
   2.134      }
   2.135    }
   2.136  
   2.137 @@ -250,31 +206,32 @@
   2.138    {
   2.139      def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
   2.140  
   2.141 -    state.change(st =>
   2.142 -      st.session match {
   2.143 -        case None => reply("Prover inactive"); st
   2.144 -        case Some(session) =>
   2.145 -          var session_phase: Session.Consumer[Session.Phase] = null
   2.146 -          session_phase =
   2.147 -            Session.Consumer(getClass.getName) {
   2.148 -              case Session.Inactive =>
   2.149 -                session.phase_changed -= session_phase
   2.150 -                session.commands_changed -= commands_changed
   2.151 -                session.all_messages -= all_messages
   2.152 -                reply("")
   2.153 -              case _ =>
   2.154 -            }
   2.155 -          session.phase_changed += session_phase
   2.156 -          session.stop()
   2.157 -          delay_input.revoke()
   2.158 -          delay_output.revoke()
   2.159 -          st.copy(session = None)
   2.160 -      })
   2.161 +    session_.change({
   2.162 +      case Some(session) =>
   2.163 +        var session_phase: Session.Consumer[Session.Phase] = null
   2.164 +        session_phase =
   2.165 +          Session.Consumer(getClass.getName) {
   2.166 +            case Session.Inactive =>
   2.167 +              session.phase_changed -= session_phase
   2.168 +              session.commands_changed -= commands_changed
   2.169 +              session.all_messages -= all_messages
   2.170 +              reply("")
   2.171 +            case _ =>
   2.172 +          }
   2.173 +        session.phase_changed += session_phase
   2.174 +        session.stop()
   2.175 +        delay_input.revoke()
   2.176 +        delay_output.revoke()
   2.177 +        None
   2.178 +      case None =>
   2.179 +        reply("Prover inactive")
   2.180 +        None
   2.181 +    })
   2.182    }
   2.183  
   2.184    def exit() {
   2.185      channel.log("\n")
   2.186 -    sys.exit(if (state.value.session.isDefined) 1 else 0)
   2.187 +    sys.exit(if (session_.value.isDefined) 1 else 0)
   2.188    }
   2.189  
   2.190  
     3.1 --- a/src/Tools/VSCode/src/vscode_resources.scala	Thu Dec 29 17:25:32 2016 +0100
     3.2 +++ b/src/Tools/VSCode/src/vscode_resources.scala	Thu Dec 29 21:54:04 2016 +0100
     3.3 @@ -15,6 +15,8 @@
     3.4  
     3.5  object VSCode_Resources
     3.6  {
     3.7 +  /* file URIs */
     3.8 +
     3.9    def is_wellformed(uri: String): Boolean =
    3.10      try { new JFile(new URI(uri)); true }
    3.11      catch { case _: URISyntaxException | _: IllegalArgumentException => false }
    3.12 @@ -22,16 +24,27 @@
    3.13    def canonical_file(uri: String): JFile =
    3.14      new JFile(new URI(uri)).getCanonicalFile
    3.15  
    3.16 -  val empty: VSCode_Resources =
    3.17 -    new VSCode_Resources(Set.empty, Map.empty, Outer_Syntax.empty)
    3.18 +
    3.19 +  /* internal state */
    3.20 +
    3.21 +  sealed case class State(
    3.22 +    models: Map[String, Document_Model] = Map.empty,
    3.23 +    pending_input: Set[Document.Node.Name] = Set.empty,
    3.24 +    pending_output: Set[Document.Node.Name] = Set.empty)
    3.25  }
    3.26  
    3.27  class VSCode_Resources(
    3.28 +    val options: Options,
    3.29      loaded_theories: Set[String],
    3.30      known_theories: Map[String, Document.Node.Name],
    3.31      base_syntax: Outer_Syntax)
    3.32    extends Resources(loaded_theories, known_theories, base_syntax)
    3.33  {
    3.34 +  private val state = Synchronized(VSCode_Resources.State())
    3.35 +
    3.36 +
    3.37 +  /* document node name */
    3.38 +
    3.39    def node_name(uri: String): Document.Node.Name =
    3.40    {
    3.41      val theory = Thy_Header.thy_name(uri).getOrElse("")
    3.42 @@ -40,4 +53,64 @@
    3.43        else VSCode_Resources.canonical_file(uri).getParent
    3.44      Document.Node.Name(uri, master_dir, theory)
    3.45    }
    3.46 +
    3.47 +
    3.48 +  /* document models */
    3.49 +
    3.50 +  def get_model(uri: String): Option[Document_Model] = state.value.models.get(uri)
    3.51 +
    3.52 +  def update_model(model: Document_Model)
    3.53 +  {
    3.54 +    state.change(st =>
    3.55 +      st.copy(
    3.56 +        models = st.models + (model.node_name.node -> model),
    3.57 +        pending_input = st.pending_input + model.node_name))
    3.58 +  }
    3.59 +
    3.60 +
    3.61 +  /* pending input */
    3.62 +
    3.63 +  def flush_input(session: Session)
    3.64 +  {
    3.65 +    state.change(st =>
    3.66 +      {
    3.67 +        val changed =
    3.68 +          (for {
    3.69 +            node_name <- st.pending_input.iterator
    3.70 +            model <- st.models.get(node_name.node)
    3.71 +            if model.changed } yield model).toList
    3.72 +        val edits = for { model <- changed; edit <- model.node_edits } yield edit
    3.73 +        session.update(Document.Blobs.empty, edits)
    3.74 +        st.copy(
    3.75 +          models = (st.models /: changed) { case (ms, m) => ms + (m.uri -> m.unchanged) },
    3.76 +          pending_input = Set.empty)
    3.77 +      })
    3.78 +  }
    3.79 +
    3.80 +
    3.81 +  /* pending output */
    3.82 +
    3.83 +  def update_output(changed_nodes: Set[Document.Node.Name]): Unit =
    3.84 +    state.change(st => st.copy(pending_output = st.pending_output ++ changed_nodes))
    3.85 +
    3.86 +  def flush_output(channel: Channel)
    3.87 +  {
    3.88 +    state.change(st =>
    3.89 +      {
    3.90 +        val changed_iterator =
    3.91 +          for {
    3.92 +            node_name <- st.pending_output.iterator
    3.93 +            model <- st.models.get(node_name.node)
    3.94 +            rendering = model.rendering(options)
    3.95 +            (diagnostics, model1) <- model.publish_diagnostics(rendering)
    3.96 +          } yield {
    3.97 +            channel.diagnostics(model1.uri, rendering.diagnostics_output(diagnostics))
    3.98 +            model1
    3.99 +          }
   3.100 +        st.copy(
   3.101 +          models = (st.models /: changed_iterator) { case (ms, m) => ms + (m.uri -> m) },
   3.102 +          pending_output = Set.empty)
   3.103 +      }
   3.104 +    )
   3.105 +  }
   3.106  }