|
| 1 | +use lsp_server::{Connection, Message, RequestId, Response}; |
| 2 | +use lsp_textdocument::TextDocuments; |
| 3 | +use lsp_types::{ |
| 4 | + request::{Formatting, RangeFormatting, Request}, |
| 5 | + DocumentFormattingParams, OneOf, Position, Range, ServerCapabilities, |
| 6 | + TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::{config::ConfigResolver, opt}; |
| 10 | + |
| 11 | +pub fn run(opt: opt::Opt) -> anyhow::Result<()> { |
| 12 | + // Load the configuration |
| 13 | + let opt_for_config_resolver = opt.clone(); |
| 14 | + let mut config_resolver = ConfigResolver::new(&opt_for_config_resolver)?; |
| 15 | + |
| 16 | + let (connection, io_threads) = Connection::stdio(); |
| 17 | + |
| 18 | + let capabilities = ServerCapabilities { |
| 19 | + document_formatting_provider: Some(OneOf::Left(true)), |
| 20 | + text_document_sync: Some(TextDocumentSyncCapability::Kind( |
| 21 | + TextDocumentSyncKind::INCREMENTAL, |
| 22 | + )), |
| 23 | + ..Default::default() |
| 24 | + }; |
| 25 | + |
| 26 | + connection.initialize(serde_json::to_value(capabilities)?)?; |
| 27 | + |
| 28 | + let mut documents = TextDocuments::new(); |
| 29 | + |
| 30 | + for msg in &connection.receiver { |
| 31 | + match msg { |
| 32 | + Message::Request(req) => { |
| 33 | + if connection.handle_shutdown(&req)? { |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + match req.method.as_str() { |
| 38 | + Formatting::METHOD => { |
| 39 | + let result = |
| 40 | + match serde_json::from_value::<DocumentFormattingParams>(req.params) { |
| 41 | + Ok(params) => { |
| 42 | + let res = handle_formatting( |
| 43 | + req.id.clone(), |
| 44 | + params, |
| 45 | + &mut config_resolver, |
| 46 | + &documents, |
| 47 | + ); |
| 48 | + |
| 49 | + res.unwrap_or(Response::new_ok(req.id, serde_json::Value::Null)) |
| 50 | + } |
| 51 | + Err(err) => Response::new_err(req.id, 1, err.to_string()), |
| 52 | + }; |
| 53 | + |
| 54 | + connection.sender.send(Message::Response(result))?; |
| 55 | + } |
| 56 | + RangeFormatting::METHOD => { |
| 57 | + // TODO: Would be cool to support this in the future |
| 58 | + } |
| 59 | + _ => {} |
| 60 | + } |
| 61 | + } |
| 62 | + Message::Response(_) => {} |
| 63 | + Message::Notification(notification) => { |
| 64 | + documents.listen(notification.method.as_str(), ¬ification.params); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + io_threads.join()?; |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +fn handle_formatting( |
| 75 | + id: RequestId, |
| 76 | + params: DocumentFormattingParams, |
| 77 | + config_resolver: &mut ConfigResolver, |
| 78 | + documents: &TextDocuments, |
| 79 | +) -> Option<Response> { |
| 80 | + let uri = params.text_document.uri; |
| 81 | + let path = uri.path().as_str(); |
| 82 | + |
| 83 | + let src = documents.get_document_content(&uri, None)?; |
| 84 | + |
| 85 | + let config = config_resolver |
| 86 | + .load_configuration(path.as_ref()) |
| 87 | + .unwrap_or_default(); |
| 88 | + |
| 89 | + let new_text = |
| 90 | + stylua_lib::format_code(src, config, None, stylua_lib::OutputVerification::None).ok()?; |
| 91 | + |
| 92 | + if new_text == src { |
| 93 | + return None; |
| 94 | + } |
| 95 | + |
| 96 | + // TODO: We can be smarter about this in the future, and update only the parts that changed |
| 97 | + let edit = TextEdit { |
| 98 | + range: Range { |
| 99 | + start: Position::new(0, 0), |
| 100 | + end: Position::new(u32::MAX, 0), |
| 101 | + }, |
| 102 | + new_text, |
| 103 | + }; |
| 104 | + |
| 105 | + let edits: Vec<TextEdit> = vec![edit]; |
| 106 | + |
| 107 | + Some(Response::new_ok(id, edits)) |
| 108 | +} |
0 commit comments