src/Tools/VSCode/src/server.scala
changeset 64710 72ca4e5f976e
parent 64709 5e6566ab78bf
child 64717 d2b50eb3d9ab
equal deleted inserted replaced
64709:5e6566ab78bf 64710:72ca4e5f976e
     9 package isabelle.vscode
     9 package isabelle.vscode
    10 
    10 
    11 
    11 
    12 import isabelle._
    12 import isabelle._
    13 
    13 
    14 import java.io.{PrintStream, OutputStream}
    14 import java.io.{PrintStream, OutputStream, File => JFile}
    15 
    15 
    16 import scala.annotation.tailrec
    16 import scala.annotation.tailrec
    17 
    17 
    18 
    18 
    19 object Server
    19 object Server
   104       rendering = model.rendering()
   104       rendering = model.rendering()
   105       offset <- model.doc.offset(node_pos.pos)
   105       offset <- model.doc.offset(node_pos.pos)
   106     } yield (rendering, offset)
   106     } yield (rendering, offset)
   107 
   107 
   108 
   108 
       
   109   /* document content */
       
   110 
       
   111   private def update_document(uri: String, text: String)
       
   112   {
       
   113     resources.update_model(session, uri, text)
       
   114     delay_input.invoke()
       
   115   }
       
   116 
       
   117   private def close_document(uri: String)
       
   118   {
       
   119     resources.close_model(uri) match {
       
   120       case Some(model) =>
       
   121         model.register(watcher)
       
   122         sync_external(Set(model.file))
       
   123       case None =>
       
   124     }
       
   125   }
       
   126 
       
   127   private def sync_external(changed: Set[JFile]): Unit =
       
   128     if (resources.sync_external(changed)) delay_input.invoke()
       
   129 
       
   130   private val watcher = File_Watcher(sync_external(_))
       
   131 
       
   132 
   109   /* input from client */
   133   /* input from client */
   110 
   134 
   111   private val delay_input =
   135   private val delay_input =
   112     Standard_Thread.delay_last(options.seconds("vscode_input_delay"))
   136     Standard_Thread.delay_last(options.seconds("vscode_input_delay"))
   113     { resources.flush_input(session) }
   137     { resources.flush_input(session) }
   114 
       
   115   private def update_document(uri: String, text: String)
       
   116   {
       
   117     resources.update_model(session, uri, text)
       
   118     delay_input.invoke()
       
   119   }
       
   120 
   138 
   121 
   139 
   122   /* output to client */
   140   /* output to client */
   123 
   141 
   124   private val commands_changed =
   142   private val commands_changed =
   133     Standard_Thread.delay_last(options.seconds("vscode_output_delay"))
   151     Standard_Thread.delay_last(options.seconds("vscode_output_delay"))
   134     {
   152     {
   135       if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
   153       if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
   136       else resources.flush_output(channel)
   154       else resources.flush_output(channel)
   137     }
   155     }
       
   156 
       
   157 
       
   158   /* file watcher */
   138 
   159 
   139 
   160 
   140   /* syslog */
   161   /* syslog */
   141 
   162 
   142   private val all_messages =
   163   private val all_messages =
   219           }
   240           }
   220         session.phase_changed += session_phase
   241         session.phase_changed += session_phase
   221         session.stop()
   242         session.stop()
   222         delay_input.revoke()
   243         delay_input.revoke()
   223         delay_output.revoke()
   244         delay_output.revoke()
       
   245         watcher.shutdown()
   224         None
   246         None
   225       case None =>
   247       case None =>
   226         reply("Prover inactive")
   248         reply("Prover inactive")
   227         None
   249         None
   228     })
   250     })
   278           case Protocol.Exit(()) => exit()
   300           case Protocol.Exit(()) => exit()
   279           case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
   301           case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
   280             update_document(uri, text)
   302             update_document(uri, text)
   281           case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
   303           case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
   282             update_document(uri, text)
   304             update_document(uri, text)
   283           case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
   305           case Protocol.DidCloseTextDocument(uri) =>
       
   306             close_document(uri)
   284           case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
   307           case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
   285           case Protocol.Hover(id, node_pos) => hover(id, node_pos)
   308           case Protocol.Hover(id, node_pos) => hover(id, node_pos)
   286           case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   309           case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   287           case _ => channel.log("### IGNORED")
   310           case _ => channel.log("### IGNORED")
   288         }
   311         }