Skip to content

Commit

Permalink
LSP: add a "Show preview" code lense
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Jul 22, 2021
1 parent 0766ebb commit a095d83
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions tools/lsp/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ use structopt::StructOpt;
use lsp_server::{Connection, Message, Request, RequestId, Response};
use lsp_types::notification::{DidChangeTextDocument, DidOpenTextDocument, Notification};
use lsp_types::request::{
CodeActionRequest, DocumentColor, DocumentSymbolRequest, ExecuteCommand, GotoDefinition,
CodeActionRequest, CodeLensRequest, ColorPresentationRequest, Completion, DocumentColor,
DocumentSymbolRequest, ExecuteCommand, GotoDefinition, HoverRequest,
};
use lsp_types::request::{ColorPresentationRequest, Completion, HoverRequest};
use lsp_types::{
CodeActionOrCommand, CodeActionProviderCapability, Color, ColorInformation, ColorPresentation,
Command, CompletionOptions, DidChangeTextDocumentParams, DidOpenTextDocumentParams,
DocumentSymbolResponse, ExecuteCommandOptions, Hover, InitializeParams, Location, OneOf,
Position, PublishDiagnosticsParams, Range, ServerCapabilities, SymbolInformation,
TextDocumentSyncCapability, Url, WorkDoneProgressOptions,
CodeActionOrCommand, CodeActionProviderCapability, CodeLens, CodeLensOptions, Color,
ColorInformation, ColorPresentation, Command, CompletionOptions, DidChangeTextDocumentParams,
DidOpenTextDocumentParams, DocumentSymbolResponse, ExecuteCommandOptions, Hover,
InitializeParams, Location, OneOf, Position, PublishDiagnosticsParams, Range,
ServerCapabilities, SymbolInformation, TextDocumentSyncCapability, Url,
WorkDoneProgressOptions,
};
use sixtyfps_compilerlib::diagnostics::BuildDiagnostics;
use sixtyfps_compilerlib::langtype::Type;
Expand Down Expand Up @@ -161,6 +162,7 @@ fn run_lsp_server() -> Result<(), Error> {
}),
document_symbol_provider: Some(OneOf::Left(true)),
color_provider: Some(true.into()),
code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }),
..ServerCapabilities::default()
};
let server_capabilities = serde_json::to_value(&capabilities).unwrap();
Expand Down Expand Up @@ -291,6 +293,9 @@ fn handle_request(
} else if let Some((id, params)) = cast::<DocumentSymbolRequest>(&mut req) {
let result = get_document_symbols(document_cache, &params.text_document);
connection.sender.send(Message::Response(Response::new_ok(id, result)))?;
} else if let Some((id, params)) = cast::<CodeLensRequest>(&mut req) {
let result = get_code_lenses(document_cache, &params.text_document);
connection.sender.send(Message::Response(Response::new_ok(id, result)))?;
};
Ok(())
}
Expand Down Expand Up @@ -598,3 +603,38 @@ fn get_document_symbols(

// TODO: add the structs
}

fn get_code_lenses(
document_cache: &mut DocumentCache,
text_document: &lsp_types::TextDocumentIdentifier,
) -> Option<Vec<CodeLens>> {
let uri = &text_document.uri;
let filepath = uri.to_file_path().ok()?;
let doc = document_cache.documents.get_document(&filepath)?;

let inner_components = doc.inner_components.clone();
let mut make_range = |node: &SyntaxNode| {
let r = node.text_range();
Some(Range::new(
document_cache.byte_offset_to_position(r.start().into(), &uri)?,
document_cache.byte_offset_to_position(r.end().into(), &uri)?,
))
};

let r = inner_components
.iter()
.filter(|c| !c.is_global())
.filter_map(|c| {
Some(CodeLens {
range: make_range(c.root_element.borrow().node.as_ref()?)?,
command: Some(Command::new(
"▶ Show preview".into(),
SHOW_PREVIEW_COMMAND.into(),
Some(vec![filepath.to_str()?.into(), c.id.as_str().into()]),
)),
data: None,
})
})
.collect::<Vec<_>>();
Some(r)
}

0 comments on commit a095d83

Please sign in to comment.