src/Tools/VSCode/src/language_server.scala
changeset 81067 1b1d72c45ff4
parent 81062 b2df8bf17071
child 81069 54bda6e0e848
equal deleted inserted replaced
81066:18c98588388d 81067:1b1d72c45ff4
   414           val model = rendering.model
   414           val model = rendering.model
   415           rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range)
   415           rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range)
   416             .map(r => LSP.DocumentHighlight.text(model.content.doc.range(r)))
   416             .map(r => LSP.DocumentHighlight.text(model.content.doc.range(r)))
   417         }) getOrElse Nil
   417         }) getOrElse Nil
   418     channel.write(LSP.DocumentHighlights.reply(id, result))
   418     channel.write(LSP.DocumentHighlights.reply(id, result))
       
   419   }
       
   420 
       
   421 
       
   422   /* code actions */
       
   423 
       
   424   def code_action_request(id: LSP.Id, file: JFile, range: Line.Range): Unit = {
       
   425     def extract_sendbacks(body: XML.Body): List[(String, Properties.T)] = {
       
   426       body match {
       
   427         case (XML.Elem(Markup(Markup.SENDBACK, p), b) :: rest) => (XML.content(b), p) :: extract_sendbacks(rest)
       
   428         case (XML.Elem(m, b) :: rest) => extract_sendbacks(b ++ rest)
       
   429         case (e :: rest) => extract_sendbacks(rest)
       
   430         case Nil => Nil
       
   431       }
       
   432     }
       
   433 
       
   434     for {
       
   435       model <- resources.get_model(file)
       
   436       version <- model.version
       
   437       snapshot = resources.snapshot(model)
       
   438       doc = model.content.doc
       
   439       text_range <- doc.text_range(range)
       
   440       text_range2 = Text.Range(text_range.start - 1, text_range.stop + 1)
       
   441     } {
       
   442       val edits = snapshot
       
   443         .select(
       
   444           text_range2,
       
   445           Markup.Elements.full,
       
   446           command_states => _ => Some(command_states.flatMap(_.results.iterator.map(_._2).toList))
       
   447         )
       
   448         .flatMap(info => extract_sendbacks(info.info).flatMap {
       
   449           (s, p) =>
       
   450             for {
       
   451               id <- Position.Id.unapply(p)
       
   452               (node, command) <- snapshot.find_command(id)
       
   453               start <- node.command_start(command)
       
   454               range = command.core_range + start
       
   455               current_text <- doc.get_text(range)
       
   456               line_range = doc.range(range)
       
   457               padding = p.contains(Markup.PADDING_COMMAND)
       
   458             } yield {
       
   459               val edit_text =
       
   460                 resources.output_edit(if (padding) current_text + "\n" + s else current_text + s)
       
   461               val edit = LSP.TextEdit(line_range, edit_text)
       
   462               (s, edit)
       
   463             }
       
   464         })
       
   465         .distinct
       
   466 
       
   467       val actions = edits.map((name, edit) => {
       
   468         val text_edits = List(LSP.TextDocumentEdit(file, Some(version), List(edit)))
       
   469 
       
   470         LSP.CodeAction(name, text_edits)
       
   471       })
       
   472       val reply = LSP.CodeActionRequest.reply(id, actions)
       
   473 
       
   474       channel.write(reply)
       
   475     }
   419   }
   476   }
   420 
   477 
   421 
   478 
   422   /* symbols */
   479   /* symbols */
   423 
   480 
   452           case LSP.Exclude_Word_Permanently(()) => update_dictionary(false, true)
   509           case LSP.Exclude_Word_Permanently(()) => update_dictionary(false, true)
   453           case LSP.Reset_Words(()) => reset_dictionary()
   510           case LSP.Reset_Words(()) => reset_dictionary()
   454           case LSP.Hover(id, node_pos) => hover(id, node_pos)
   511           case LSP.Hover(id, node_pos) => hover(id, node_pos)
   455           case LSP.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   512           case LSP.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   456           case LSP.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos)
   513           case LSP.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos)
       
   514           case LSP.CodeActionRequest(id, file, range) => code_action_request(id, file, range)
   457           case LSP.Decoration_Request(file) => decoration_request(file)
   515           case LSP.Decoration_Request(file) => decoration_request(file)
   458           case LSP.Caret_Update(caret) => update_caret(caret)
   516           case LSP.Caret_Update(caret) => update_caret(caret)
   459           case LSP.Output_Set_Margin(margin) => dynamic_output.set_margin(margin)
   517           case LSP.Output_Set_Margin(margin) => dynamic_output.set_margin(margin)
   460           case LSP.State_Init(id) => State_Panel.init(id, server)
   518           case LSP.State_Init(id) => State_Panel.init(id, server)
   461           case LSP.State_Exit(state_id) => State_Panel.exit(state_id)
   519           case LSP.State_Exit(state_id) => State_Panel.exit(state_id)